Move organization related structs into sub package (#18518)
* Move organization related structs into sub package * Fix test * Fix lint * Move more functions into sub packages * Fix bug * Fix test * Update models/organization/team_repo.go Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Apply suggestions from code review Co-authored-by: KN4CK3R <admin@oldschoolhack.me> * Fix fmt * Follow suggestion from @Gusted * Fix test * Fix test * Fix bug * Use ctx but db.DefaultContext on routers * Fix bug * Fix bug * fix bug * Update models/organization/team_user.go * Fix bug Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
d4c789dfc1
commit
b06b9a056c
94 changed files with 3107 additions and 2995 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue