Skip to content

Commit 36c0c95

Browse files
KN4CK3RSysoev, Vladimir
authored and
Sysoev, Vladimir
committed
Simplify visibility checks (go-gitea#20406)
Was looking into the visibility checks because I need them for something different and noticed the checks are more complicated than they have to be. The rule is just: user/org is visible if - The doer is a member of the org, regardless of the org visibility - The doer is not restricted and the user/org is public or limited
1 parent 14460a7 commit 36c0c95

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

models/user/search.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,18 @@ func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session {
5959
}
6060

6161
if opts.Actor != nil {
62-
exprCond := builder.Expr("org_user.org_id = `user`.id")
63-
6462
// If Admin - they see all users!
6563
if !opts.Actor.IsAdmin {
66-
// Force visibility for privacy
67-
var accessCond builder.Cond
64+
// Users can see an organization they are a member of
65+
accessCond := builder.In("id", builder.Select("org_id").From("org_user").Where(builder.Eq{"uid": opts.Actor.ID}))
6866
if !opts.Actor.IsRestricted {
69-
accessCond = builder.Or(
70-
builder.In("id", builder.Select("org_id").From("org_user").LeftJoin("`user`", exprCond).Where(builder.And(builder.Eq{"uid": opts.Actor.ID}, builder.Eq{"visibility": structs.VisibleTypePrivate}))),
71-
builder.In("visibility", structs.VisibleTypePublic, structs.VisibleTypeLimited))
72-
} else {
73-
// restricted users only see orgs they are a member of
74-
accessCond = builder.In("id", builder.Select("org_id").From("org_user").LeftJoin("`user`", exprCond).Where(builder.And(builder.Eq{"uid": opts.Actor.ID})))
67+
// Not-Restricted users can see public and limited users/organizations
68+
accessCond = accessCond.Or(builder.In("visibility", structs.VisibleTypePublic, structs.VisibleTypeLimited))
7569
}
7670
// Don't forget about self
7771
accessCond = accessCond.Or(builder.Eq{"id": opts.Actor.ID})
7872
cond = cond.And(accessCond)
7973
}
80-
8174
} else {
8275
// Force visibility for privacy
8376
// Not logged in - only public users

0 commit comments

Comments
 (0)