diff options
author | Rémy Boulanouar <rboulanouar@gmail.com> | 2019-02-18 17:00:27 +0100 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-02-18 18:00:27 +0200 |
commit | 64ce159a6eacc81962d07a8f5ef7f69c17365363 (patch) | |
tree | d6b94d035de14df8b1a773d97ab35937cce1d00a /models/user.go | |
parent | ae3a913122d3430ccf14da22f09daf6f636a00f8 (diff) | |
download | gitea-64ce159a6eacc81962d07a8f5ef7f69c17365363.tar.gz gitea-64ce159a6eacc81962d07a8f5ef7f69c17365363.zip |
Allow to set organization visibility (public, internal, private) (#1763)
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/models/user.go b/models/user.go index e50385d411..3fb1c3b59e 100644 --- a/models/user.go +++ b/models/user.go @@ -25,22 +25,23 @@ import ( "time" "unicode/utf8" - "github.com/Unknwon/com" - "github.com/go-xorm/builder" - "github.com/go-xorm/xorm" - "github.com/nfnt/resize" - "golang.org/x/crypto/pbkdf2" - "golang.org/x/crypto/ssh" - "code.gitea.io/git" - api "code.gitea.io/sdk/gitea" - "code.gitea.io/gitea/modules/avatar" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/generate" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + api "code.gitea.io/sdk/gitea" + + "github.com/Unknwon/com" + "github.com/go-xorm/builder" + "github.com/go-xorm/core" + "github.com/go-xorm/xorm" + "github.com/nfnt/resize" + "golang.org/x/crypto/pbkdf2" + "golang.org/x/crypto/ssh" ) // UserType defines the user type @@ -136,8 +137,9 @@ type User struct { Description string NumTeams int NumMembers int - Teams []*Team `xorm:"-"` - Members []*User `xorm:"-"` + Teams []*Team `xorm:"-"` + Members []*User `xorm:"-"` + Visibility structs.VisibleType `xorm:"NOT NULL DEFAULT 0"` // Preferences DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"` @@ -526,6 +528,16 @@ func (u *User) IsUserOrgOwner(orgID int64) bool { return isOwner } +// IsUserPartOfOrg returns true if user with userID is part of the u organisation. +func (u *User) IsUserPartOfOrg(userID int64) bool { + isMember, err := IsOrganizationMember(u.ID, userID) + if err != nil { + log.Error(4, "IsOrganizationMember: %v", err) + return false + } + return isMember +} + // IsPublicMember returns true if user public his/her membership in given organization. func (u *User) IsPublicMember(orgID int64) bool { isMember, err := IsPublicMembership(orgID, u.ID) @@ -1341,13 +1353,18 @@ type SearchUserOptions struct { UID int64 OrderBy SearchOrderBy Page int - PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum + Private bool // Include private orgs in search + OwnerID int64 // id of user for visibility calculation + PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum IsActive util.OptionalBool SearchByEmail bool // Search by email as well as username/full name } func (opts *SearchUserOptions) toConds() builder.Cond { - var cond builder.Cond = builder.Eq{"type": opts.Type} + + var cond = builder.NewCond() + cond = cond.And(builder.Eq{"type": opts.Type}) + if len(opts.Keyword) > 0 { lowerKeyword := strings.ToLower(opts.Keyword) keywordCond := builder.Or( @@ -1361,6 +1378,27 @@ func (opts *SearchUserOptions) toConds() builder.Cond { cond = cond.And(keywordCond) } + if !opts.Private { + // user not logged in and so they won't be allowed to see non-public orgs + cond = cond.And(builder.In("visibility", structs.VisibleTypePublic)) + } + + if opts.OwnerID > 0 { + var exprCond builder.Cond + if DbCfg.Type == core.MYSQL { + exprCond = builder.Expr("org_user.org_id = user.id") + } else if DbCfg.Type == core.MSSQL { + exprCond = builder.Expr("org_user.org_id = [user].id") + } else { + exprCond = builder.Expr("org_user.org_id = \"user\".id") + } + var accessCond = builder.NewCond() + accessCond = builder.Or( + builder.In("id", builder.Select("org_id").From("org_user").LeftJoin("`user`", exprCond).Where(builder.And(builder.Eq{"uid": opts.OwnerID}, builder.Eq{"visibility": structs.VisibleTypePrivate}))), + builder.In("visibility", structs.VisibleTypePublic, structs.VisibleTypeLimited)) + cond = cond.And(accessCond) + } + if opts.UID > 0 { cond = cond.And(builder.Eq{"id": opts.UID}) } |