@@ -432,6 +432,62 @@ func (u *User) IsPasswordSet() bool {
432
432
return len (u .Passwd ) != 0
433
433
}
434
434
435
+ // IsVisibleToUser check if viewer is able to see user profile
436
+ func (u * User ) IsVisibleToUser (viewer * User ) bool {
437
+ return u .isVisibleToUser (x , viewer )
438
+ }
439
+
440
+ func (u * User ) isVisibleToUser (e Engine , viewer * User ) bool {
441
+ if viewer != nil && viewer .IsAdmin {
442
+ return true
443
+ }
444
+
445
+ switch u .Visibility {
446
+ case structs .VisibleTypePublic :
447
+ return true
448
+ case structs .VisibleTypeLimited :
449
+ if viewer == nil || viewer .IsRestricted {
450
+ return false
451
+ }
452
+ return true
453
+ case structs .VisibleTypePrivate :
454
+ if viewer == nil || viewer .IsRestricted {
455
+ return false
456
+ }
457
+
458
+ // If they follow - they see each over
459
+ follower := IsFollowing (u .ID , viewer .ID )
460
+ if follower {
461
+ return true
462
+ }
463
+
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 ))
476
+ if err != nil {
477
+ return false
478
+ }
479
+
480
+ if count < 0 {
481
+ // No common organization
482
+ return false
483
+ }
484
+
485
+ // they are in an organization together
486
+ return true
487
+ }
488
+ return false
489
+ }
490
+
435
491
// IsOrganization returns true if user is actually a organization.
436
492
func (u * User ) IsOrganization () bool {
437
493
return u .Type == UserTypeOrganization
@@ -796,8 +852,13 @@ func IsUsableUsername(name string) error {
796
852
return isUsableName (reservedUsernames , reservedUserPatterns , name )
797
853
}
798
854
855
+ // CreateUserOverwriteOptions are an optional options who overwrite system defaults on user creation
856
+ type CreateUserOverwriteOptions struct {
857
+ Visibility structs.VisibleType
858
+ }
859
+
799
860
// CreateUser creates record of a new user.
800
- func CreateUser (u * User ) (err error ) {
861
+ func CreateUser (u * User , overwriteDefault ... * CreateUserOverwriteOptions ) (err error ) {
801
862
if err = IsUsableUsername (u .Name ); err != nil {
802
863
return err
803
864
}
@@ -831,8 +892,6 @@ func CreateUser(u *User) (err error) {
831
892
return ErrEmailAlreadyUsed {u .Email }
832
893
}
833
894
834
- u .KeepEmailPrivate = setting .Service .DefaultKeepEmailPrivate
835
-
836
895
u .LowerName = strings .ToLower (u .Name )
837
896
u .AvatarEmail = u .Email
838
897
if u .Rands , err = GetUserSalt (); err != nil {
@@ -841,10 +900,18 @@ func CreateUser(u *User) (err error) {
841
900
if err = u .SetPassword (u .Passwd ); err != nil {
842
901
return err
843
902
}
903
+
904
+ // set system defaults
905
+ u .KeepEmailPrivate = setting .Service .DefaultKeepEmailPrivate
906
+ u .Visibility = setting .Service .DefaultUserVisibilityMode
844
907
u .AllowCreateOrganization = setting .Service .DefaultAllowCreateOrganization && ! setting .Admin .DisableRegularOrgCreation
845
908
u .EmailNotificationsPreference = setting .Admin .DefaultEmailNotification
846
909
u .MaxRepoCreation = - 1
847
910
u .Theme = setting .UI .DefaultTheme
911
+ // overwrite defaults if set
912
+ if len (overwriteDefault ) != 0 && overwriteDefault [0 ] != nil {
913
+ u .Visibility = overwriteDefault [0 ].Visibility
914
+ }
848
915
849
916
if _ , err = sess .Insert (u ); err != nil {
850
917
return err
@@ -1527,10 +1594,9 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
1527
1594
cond = cond .And (keywordCond )
1528
1595
}
1529
1596
1597
+ // If visibility filtered
1530
1598
if len (opts .Visible ) > 0 {
1531
1599
cond = cond .And (builder .In ("visibility" , opts .Visible ))
1532
- } else {
1533
- cond = cond .And (builder .In ("visibility" , structs .VisibleTypePublic ))
1534
1600
}
1535
1601
1536
1602
if opts .Actor != nil {
@@ -1543,16 +1609,27 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
1543
1609
exprCond = builder .Expr ("org_user.org_id = \" user\" .id" )
1544
1610
}
1545
1611
1546
- var accessCond builder.Cond
1547
- if ! opts .Actor .IsRestricted {
1548
- accessCond = builder .Or (
1549
- 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 }))),
1550
- builder .In ("visibility" , structs .VisibleTypePublic , structs .VisibleTypeLimited ))
1551
- } else {
1552
- // restricted users only see orgs they are a member of
1553
- accessCond = builder .In ("id" , builder .Select ("org_id" ).From ("org_user" ).LeftJoin ("`user`" , exprCond ).Where (builder .And (builder.Eq {"uid" : opts .Actor .ID })))
1612
+ // If Admin - they see all users!
1613
+ if ! opts .Actor .IsAdmin {
1614
+ // Force visiblity for privacy
1615
+ var accessCond builder.Cond
1616
+ if ! opts .Actor .IsRestricted {
1617
+ accessCond = builder .Or (
1618
+ 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 }))),
1619
+ builder .In ("visibility" , structs .VisibleTypePublic , structs .VisibleTypeLimited ))
1620
+ } else {
1621
+ // restricted users only see orgs they are a member of
1622
+ accessCond = builder .In ("id" , builder .Select ("org_id" ).From ("org_user" ).LeftJoin ("`user`" , exprCond ).Where (builder .And (builder.Eq {"uid" : opts .Actor .ID })))
1623
+ }
1624
+ // Don't forget about self
1625
+ accessCond = accessCond .Or (builder.Eq {"id" : opts .Actor .ID })
1626
+ cond = cond .And (accessCond )
1554
1627
}
1555
- cond = cond .And (accessCond )
1628
+
1629
+ } else {
1630
+ // Force visiblity for privacy
1631
+ // Not logged in - only public users
1632
+ cond = cond .And (builder .In ("visibility" , structs .VisibleTypePublic ))
1556
1633
}
1557
1634
1558
1635
if opts .UID > 0 {
0 commit comments