Skip to content

Commit 79770b5

Browse files
committed
Improve the team test
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 89255e9 commit 79770b5

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

models/user.go

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,10 @@ func (u *User) IsPasswordSet() bool {
434434

435435
// IsVisibleToUser check if viewer is able to see user profile
436436
func (u *User) IsVisibleToUser(viewer *User) bool {
437+
return u.isVisibleToUser(x, viewer)
438+
}
437439

440+
func (u *User) isVisibleToUser(e Engine, viewer *User) bool {
438441
if viewer != nil && viewer.IsAdmin {
439442
return true
440443
}
@@ -458,53 +461,28 @@ func (u *User) IsVisibleToUser(viewer *User) bool {
458461
return true
459462
}
460463

461-
// Now we need to check if they in some team together
462-
463-
var orgIds1 []int64
464-
var orgIds2 []int64
465-
466-
// First user all teams organization
467-
if err := x.Table("team_user").
468-
Cols("team_user.org_id").
469-
Where("team_user.uid = ?", u.ID).
470-
Find(&orgIds1); err != nil {
471-
return false
472-
}
473-
if len(orgIds1) == 0 {
474-
// No teams 1
475-
return false
476-
}
477-
478-
// Second user all teams organization
479-
if err := x.Table("team_user").
480-
Cols("team_user.org_id").
481-
Where("team_user.uid = ?", viewer.ID).
482-
Find(&orgIds2); err != nil {
483-
return false
484-
}
485-
if len(orgIds2) == 0 {
486-
// No teams 2
487-
return false
488-
}
489-
490-
// Intersect they organizations
491-
var cond builder.Cond = builder.
492-
In("id", orgIds1).
493-
And(builder.In("id", orgIds2)).
494-
And(builder.Eq{"type": UserTypeOrganization})
495-
count, err := x.Table("user").
496-
Cols("id").
497-
Where(cond).
498-
Count(1)
464+
// Now we need to check if they in some organization together
465+
count, err := x.Table("team_user").
466+
Where(
467+
builder.And(
468+
builder.Eq{"uid": viewer.ID},
469+
builder.Or(
470+
builder.Eq{"org_id": u.ID},
471+
builder.In("org_id",
472+
builder.Select("org_id").
473+
From("team_user", "t2").
474+
Where(builder.Eq{"uid": u.ID}))))).
475+
Count(new(TeamUser))
499476
if err != nil {
500477
return false
501478
}
502-
if count == 0 {
503-
// they teams from different orgs?
479+
480+
if count < 0 {
481+
// No common organization
504482
return false
505483
}
506484

507-
// they in some organizations together
485+
// they are in an organization together
508486
return true
509487
}
510488
return false

0 commit comments

Comments
 (0)