diff options
Diffstat (limited to 'models/user/user.go')
-rw-r--r-- | models/user/user.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/models/user/user.go b/models/user/user.go index a3094a13ce..0e51cf955c 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1211,3 +1211,59 @@ func GetAdminUser() (*User, error) { return &admin, nil } + +// IsUserVisibleToViewer check if viewer is able to see user profile +func IsUserVisibleToViewer(u, viewer *User) bool { + return isUserVisibleToViewer(db.GetEngine(db.DefaultContext), u, viewer) +} + +func isUserVisibleToViewer(e db.Engine, u, viewer *User) bool { + if viewer != nil && viewer.IsAdmin { + return true + } + + switch u.Visibility { + case structs.VisibleTypePublic: + return true + case structs.VisibleTypeLimited: + if viewer == nil || viewer.IsRestricted { + return false + } + return true + case structs.VisibleTypePrivate: + if viewer == nil || viewer.IsRestricted { + return false + } + + // If they follow - they see each over + follower := IsFollowing(u.ID, viewer.ID) + if follower { + return true + } + + // Now we need to check if they in some organization together + count, err := e.Table("team_user"). + Where( + builder.And( + builder.Eq{"uid": viewer.ID}, + builder.Or( + builder.Eq{"org_id": u.ID}, + builder.In("org_id", + builder.Select("org_id"). + From("team_user", "t2"). + Where(builder.Eq{"uid": u.ID}))))). + Count() + if err != nil { + return false + } + + if count < 0 { + // No common organization + return false + } + + // they are in an organization together + return true + } + return false +} |