diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-24 17:49:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-24 17:49:20 +0800 |
commit | a666829a37be6f9fd98f9e7dd1767c420f7f3b32 (patch) | |
tree | 9ab1434b759a8a2cb275a83149903a823851e309 /models/org.go | |
parent | 4e7ca946da2a2642a62f114825129bf5d7ed9196 (diff) | |
download | gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.tar.gz gitea-a666829a37be6f9fd98f9e7dd1767c420f7f3b32.zip |
Move user related model into models/user (#17781)
* Move user related model into models/user
* Fix lint for windows
* Fix windows lint
* Fix windows lint
* Move some tests in models
* Merge
Diffstat (limited to 'models/org.go')
-rw-r--r-- | models/org.go | 76 |
1 files changed, 43 insertions, 33 deletions
diff --git a/models/org.go b/models/org.go index ea73d03d42..63e1b99af5 100644 --- a/models/org.go +++ b/models/org.go @@ -21,10 +21,10 @@ import ( ) // Organization represents an organization -type Organization User +type Organization user_model.User // OrgFromUser converts user to organization -func OrgFromUser(user *User) *Organization { +func OrgFromUser(user *user_model.User) *Organization { return (*Organization)(user) } @@ -188,8 +188,8 @@ func (org *Organization) RemoveOrgRepo(repoID int64) error { } // AsUser returns the org as user object -func (org *Organization) AsUser() *User { - return (*User)(org) +func (org *Organization) AsUser() *user_model.User { + return (*user_model.User)(org) } // DisplayName returns full name if it's not empty, @@ -204,34 +204,34 @@ func (org *Organization) CustomAvatarRelativePath() string { } // CreateOrganization creates record of a new organization. -func CreateOrganization(org *Organization, owner *User) (err error) { +func CreateOrganization(org *Organization, owner *user_model.User) (err error) { if !owner.CanCreateOrganization() { return ErrUserNotAllowedCreateOrg{} } - if err = IsUsableUsername(org.Name); err != nil { + if err = user_model.IsUsableUsername(org.Name); err != nil { return err } - isExist, err := IsUserExist(0, org.Name) + isExist, err := user_model.IsUserExist(0, org.Name) if err != nil { return err } else if isExist { - return ErrUserAlreadyExist{org.Name} + return user_model.ErrUserAlreadyExist{Name: org.Name} } org.LowerName = strings.ToLower(org.Name) - if org.Rands, err = GetUserSalt(); err != nil { + if org.Rands, err = user_model.GetUserSalt(); err != nil { return err } - if org.Salt, err = GetUserSalt(); err != nil { + if org.Salt, err = user_model.GetUserSalt(); err != nil { return err } org.UseCustomAvatar = true org.MaxRepoCreation = -1 org.NumTeams = 1 org.NumMembers = 1 - org.Type = UserTypeOrganization + org.Type = user_model.UserTypeOrganization ctx, committer, err := db.TxContext() if err != nil { @@ -246,7 +246,7 @@ func CreateOrganization(org *Organization, owner *User) (err error) { if err = db.Insert(ctx, org); err != nil { return fmt.Errorf("insert organization: %v", err) } - if err = generateRandomAvatar(db.GetEngine(ctx), org.AsUser()); err != nil { + if err = user_model.GenerateRandomAvatarCtx(ctx, org.AsUser()); err != nil { return fmt.Errorf("generate random avatar: %v", err) } @@ -304,7 +304,7 @@ func GetOrgByName(name string) (*Organization, error) { } u := &Organization{ LowerName: strings.ToLower(name), - Type: UserTypeOrganization, + Type: user_model.UserTypeOrganization, } has, err := db.GetEngine(db.DefaultContext).Get(u) if err != nil { @@ -325,7 +325,7 @@ func CountOrganizations() int64 { // DeleteOrganization deletes models associated to an organization. func DeleteOrganization(ctx context.Context, org *Organization) error { - if org.Type != UserTypeOrganization { + if org.Type != user_model.UserTypeOrganization { return fmt.Errorf("%s is a user not an organization", org.Name) } @@ -340,7 +340,7 @@ func DeleteOrganization(ctx context.Context, org *Organization) error { return fmt.Errorf("deleteBeans: %v", err) } - if _, err := e.ID(org.ID).Delete(new(User)); err != nil { + if _, err := e.ID(org.ID).Delete(new(user_model.User)); err != nil { return fmt.Errorf("Delete: %v", err) } @@ -433,12 +433,12 @@ func (org *Organization) GetOrgUserMaxAuthorizeLevel(uid int64) (AccessMode, err } // GetUsersWhoCanCreateOrgRepo returns users which are able to create repo in organization -func GetUsersWhoCanCreateOrgRepo(orgID int64) ([]*User, error) { +func GetUsersWhoCanCreateOrgRepo(orgID int64) ([]*user_model.User, error) { return getUsersWhoCanCreateOrgRepo(db.GetEngine(db.DefaultContext), orgID) } -func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*User, error) { - users := make([]*User, 0, 10) +func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*user_model.User, error) { + users := make([]*user_model.User, 0, 10) return users, e. Join("INNER", "`team_user`", "`team_user`.uid=`user`.id"). Join("INNER", "`team`", "`team`.id=`team_user`.team_id"). @@ -450,8 +450,8 @@ func getUsersWhoCanCreateOrgRepo(e db.Engine, orgID int64) ([]*User, error) { type MinimalOrg = Organization // GetUserOrgsList returns one user's all orgs list -func GetUserOrgsList(user *User) ([]*MinimalOrg, error) { - schema, err := db.TableInfo(new(User)) +func GetUserOrgsList(user *user_model.User) ([]*MinimalOrg, error) { + schema, err := db.TableInfo(new(user_model.User)) if err != nil { return nil, err } @@ -507,6 +507,12 @@ func GetUserOrgsList(user *User) ([]*MinimalOrg, error) { return orgs, nil } +// SearchOrganizationsOptions options to filter organizations +type SearchOrganizationsOptions struct { + db.ListOptions + All bool +} + // FindOrgOptions finds orgs options type FindOrgOptions struct { db.ListOptions @@ -549,7 +555,7 @@ func FindOrgs(opts FindOrgOptions) ([]*Organization, error) { func CountOrgs(opts FindOrgOptions) (int64, error) { return db.GetEngine(db.DefaultContext). Where(opts.toConds()). - Count(new(User)) + Count(new(user_model.User)) } func getOwnedOrgsByUserID(sess db.Engine, userID int64) ([]*Organization, error) { @@ -564,11 +570,11 @@ func getOwnedOrgsByUserID(sess db.Engine, userID int64) ([]*Organization, error) } // HasOrgOrUserVisible tells if the given user can see the given org or user -func HasOrgOrUserVisible(org, user *User) bool { +func HasOrgOrUserVisible(org, user *user_model.User) bool { return hasOrgOrUserVisible(db.GetEngine(db.DefaultContext), org, user) } -func hasOrgOrUserVisible(e db.Engine, orgOrUser, user *User) bool { +func hasOrgOrUserVisible(e db.Engine, orgOrUser, user *user_model.User) bool { // Not SignedUser if user == nil { return orgOrUser.Visibility == structs.VisibleTypePublic @@ -585,7 +591,7 @@ func hasOrgOrUserVisible(e db.Engine, orgOrUser, user *User) bool { } // HasOrgsVisible tells if the given user can see at least one of the orgs provided -func HasOrgsVisible(orgs []*Organization, user *User) bool { +func HasOrgsVisible(orgs []*Organization, user *user_model.User) bool { if len(orgs) == 0 { return false } @@ -718,7 +724,11 @@ func GetOrgByIDCtx(ctx context.Context, id int64) (*Organization, error) { if err != nil { return nil, err } else if !has { - return nil, ErrUserNotExist{id, "", 0} + return nil, user_model.ErrUserNotExist{ + UID: id, + Name: "", + KeyID: 0, + } } return u, nil } @@ -896,17 +906,17 @@ type AccessibleReposEnvironment interface { Repos(page, pageSize int) ([]*Repository, error) MirrorRepos() ([]*Repository, error) AddKeyword(keyword string) - SetSort(SearchOrderBy) + SetSort(db.SearchOrderBy) } type accessibleReposEnv struct { org *Organization - user *User + user *user_model.User team *Team teamIDs []int64 e db.Engine keyword string - orderBy SearchOrderBy + orderBy db.SearchOrderBy } // AccessibleReposEnv builds an AccessibleReposEnvironment for the repositories in `org` @@ -916,10 +926,10 @@ func (org *Organization) AccessibleReposEnv(userID int64) (AccessibleReposEnviro } func (org *Organization) accessibleReposEnv(e db.Engine, userID int64) (AccessibleReposEnvironment, error) { - var user *User + var user *user_model.User if userID > 0 { - u, err := getUserByID(e, userID) + u, err := user_model.GetUserByIDEngine(e, userID) if err != nil { return nil, err } @@ -935,7 +945,7 @@ func (org *Organization) accessibleReposEnv(e db.Engine, userID int64) (Accessib user: user, teamIDs: teamIDs, e: e, - orderBy: SearchOrderByRecentUpdated, + orderBy: db.SearchOrderByRecentUpdated, }, nil } @@ -946,7 +956,7 @@ func (org *Organization) AccessibleTeamReposEnv(team *Team) AccessibleReposEnvir org: org, team: team, e: db.GetEngine(db.DefaultContext), - orderBy: SearchOrderByRecentUpdated, + orderBy: db.SearchOrderByRecentUpdated, } } @@ -1049,6 +1059,6 @@ func (env *accessibleReposEnv) AddKeyword(keyword string) { env.keyword = keyword } -func (env *accessibleReposEnv) SetSort(orderBy SearchOrderBy) { +func (env *accessibleReposEnv) SetSort(orderBy db.SearchOrderBy) { env.orderBy = orderBy } |