diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-03-29 14:29:02 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 14:29:02 +0800 |
commit | b06b9a056c0af751e576978f6ef3c914ee959b9c (patch) | |
tree | aa0d11413038baa5d47af65fd435665c698fe456 /services | |
parent | d4c789dfc1c341413b77a2f21fe7339982102bed (diff) | |
download | gitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.tar.gz gitea-b06b9a056c0af751e576978f6ef3c914ee959b9c.zip |
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>
Diffstat (limited to 'services')
-rw-r--r-- | services/auth/source/ldap/source_authenticate.go | 10 | ||||
-rw-r--r-- | services/auth/source/ldap/source_group_sync.go | 27 | ||||
-rw-r--r-- | services/auth/source/ldap/source_sync.go | 6 | ||||
-rw-r--r-- | services/issue/assignee.go | 15 | ||||
-rw-r--r-- | services/mailer/mail_repo.go | 5 | ||||
-rw-r--r-- | services/org/org.go | 5 | ||||
-rw-r--r-- | services/org/org_test.go | 15 | ||||
-rw-r--r-- | services/repository/repository.go | 3 | ||||
-rw-r--r-- | services/repository/transfer.go | 9 | ||||
-rw-r--r-- | services/repository/transfer_test.go | 5 | ||||
-rw-r--r-- | services/user/user.go | 3 | ||||
-rw-r--r-- | services/user/user_test.go | 7 |
12 files changed, 59 insertions, 51 deletions
diff --git a/services/auth/source/ldap/source_authenticate.go b/services/auth/source/ldap/source_authenticate.go index 2c1bcc29cc..ddd70627ed 100644 --- a/services/auth/source/ldap/source_authenticate.go +++ b/services/auth/source/ldap/source_authenticate.go @@ -8,10 +8,10 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/services/mailer" user_service "code.gitea.io/gitea/services/user" @@ -65,8 +65,8 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str if user != nil { if source.GroupsEnabled && (source.GroupTeamMap != "" || source.GroupTeamMapRemoval) { - orgCache := make(map[string]*models.Organization) - teamCache := make(map[string]*models.Team) + orgCache := make(map[string]*organization.Organization) + teamCache := make(map[string]*organization.Team) source.SyncLdapGroupsToTeams(user, sr.LdapTeamAdd, sr.LdapTeamRemove, orgCache, teamCache) } if isAttributeSSHPublicKeySet && asymkey_model.SynchronizePublicKeys(user, source.authSource, sr.SSHPublicKey) { @@ -111,8 +111,8 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str _ = user_service.UploadAvatar(user, sr.Avatar) } if source.GroupsEnabled && (source.GroupTeamMap != "" || source.GroupTeamMapRemoval) { - orgCache := make(map[string]*models.Organization) - teamCache := make(map[string]*models.Team) + orgCache := make(map[string]*organization.Organization) + teamCache := make(map[string]*organization.Team) source.SyncLdapGroupsToTeams(user, sr.LdapTeamAdd, sr.LdapTeamRemove, orgCache, teamCache) } diff --git a/services/auth/source/ldap/source_group_sync.go b/services/auth/source/ldap/source_group_sync.go index 7c62af705e..e797e015b2 100644 --- a/services/auth/source/ldap/source_group_sync.go +++ b/services/auth/source/ldap/source_group_sync.go @@ -6,12 +6,14 @@ package ldap import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" ) // SyncLdapGroupsToTeams maps LDAP groups to organization and team memberships -func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, ldapTeamRemove map[string][]string, orgCache map[string]*models.Organization, teamCache map[string]*models.Team) { +func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, ldapTeamRemove map[string][]string, orgCache map[string]*organization.Organization, teamCache map[string]*organization.Team) { var err error if source.GroupsEnabled && source.GroupTeamMapRemoval { // when the user is not a member of configs LDAP group, remove mapped organizations/teams memberships @@ -20,7 +22,7 @@ func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, for orgName, teamNames := range ldapTeamAdd { org, ok := orgCache[orgName] if !ok { - org, err = models.GetOrgByName(orgName) + org, err = organization.GetOrgByName(orgName) if err != nil { // organization must be created before LDAP group sync log.Warn("LDAP group sync: Could not find organisation %s: %v", orgName, err) @@ -28,14 +30,7 @@ func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, } orgCache[orgName] = org } - if isMember, err := models.IsOrganizationMember(org.ID, user.ID); !isMember && err == nil { - log.Trace("LDAP group sync: adding user [%s] to organization [%s]", user.Name, org.Name) - err = org.AddMember(user.ID) - if err != nil { - log.Error("LDAP group sync: Could not add user to organization: %v", err) - continue - } - } + for _, teamName := range teamNames { team, ok := teamCache[orgName+teamName] if !ok { @@ -47,12 +42,12 @@ func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, } teamCache[orgName+teamName] = team } - if isMember, err := models.IsTeamMember(org.ID, team.ID, user.ID); !isMember && err == nil { + if isMember, err := organization.IsTeamMember(db.DefaultContext, org.ID, team.ID, user.ID); !isMember && err == nil { log.Trace("LDAP group sync: adding user [%s] to team [%s]", user.Name, org.Name) } else { continue } - err := team.AddMember(user.ID) + err := models.AddTeamMember(team, user.ID) if err != nil { log.Error("LDAP group sync: Could not add user to team: %v", err) } @@ -63,12 +58,12 @@ func (source *Source) SyncLdapGroupsToTeams(user *user_model.User, ldapTeamAdd, // remove membership to organizations/teams if user is not member of corresponding LDAP group // e.g. lets assume user is member of LDAP group "x", but LDAP group team map contains LDAP groups "x" and "y" // then users membership gets removed for all organizations/teams mapped by LDAP group "y" -func removeMappedMemberships(user *user_model.User, ldapTeamRemove map[string][]string, orgCache map[string]*models.Organization, teamCache map[string]*models.Team) { +func removeMappedMemberships(user *user_model.User, ldapTeamRemove map[string][]string, orgCache map[string]*organization.Organization, teamCache map[string]*organization.Team) { var err error for orgName, teamNames := range ldapTeamRemove { org, ok := orgCache[orgName] if !ok { - org, err = models.GetOrgByName(orgName) + org, err = organization.GetOrgByName(orgName) if err != nil { // organization must be created before LDAP group sync log.Warn("LDAP group sync: Could not find organisation %s: %v", orgName, err) @@ -86,12 +81,12 @@ func removeMappedMemberships(user *user_model.User, ldapTeamRemove map[string][] continue } } - if isMember, err := models.IsTeamMember(org.ID, team.ID, user.ID); isMember && err == nil { + if isMember, err := organization.IsTeamMember(db.DefaultContext, org.ID, team.ID, user.ID); isMember && err == nil { log.Trace("LDAP group sync: removing user [%s] from team [%s]", user.Name, org.Name) } else { continue } - err = team.RemoveMember(user.ID) + err = models.RemoveTeamMember(team, user.ID) if err != nil { log.Error("LDAP group sync: Could not remove user from team: %v", err) } diff --git a/services/auth/source/ldap/source_sync.go b/services/auth/source/ldap/source_sync.go index b7ea1f0584..65efed78c1 100644 --- a/services/auth/source/ldap/source_sync.go +++ b/services/auth/source/ldap/source_sync.go @@ -10,9 +10,9 @@ import ( "sort" "strings" - "code.gitea.io/gitea/models" asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" user_service "code.gitea.io/gitea/services/user" @@ -62,8 +62,8 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { }) userPos := 0 - orgCache := make(map[string]*models.Organization) - teamCache := make(map[string]*models.Team) + orgCache := make(map[string]*organization.Organization) + teamCache := make(map[string]*organization.Team) for _, su := range sr { select { diff --git a/services/issue/assignee.go b/services/issue/assignee.go index f09c51293b..86d653dfa6 100644 --- a/services/issue/assignee.go +++ b/services/issue/assignee.go @@ -6,6 +6,8 @@ package issue import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -166,7 +168,7 @@ func IsValidReviewRequest(reviewer, doer *user_model.User, isAdd bool, issue *mo } // IsValidTeamReviewRequest Check permission for ReviewRequest Team -func IsValidTeamReviewRequest(reviewer *models.Team, doer *user_model.User, isAdd bool, issue *models.Issue) error { +func IsValidTeamReviewRequest(reviewer *organization.Team, doer *user_model.User, isAdd bool, issue *models.Issue) error { if doer.IsOrganization() { return models.ErrNotValidReviewRequest{ Reason: "Organization can't be doer to add reviewer", @@ -183,7 +185,7 @@ func IsValidTeamReviewRequest(reviewer *models.Team, doer *user_model.User, isAd if isAdd { if issue.Repo.IsPrivate { - hasTeam := models.HasTeamRepo(reviewer.OrgID, reviewer.ID, issue.RepoID) + hasTeam := organization.HasTeamRepo(db.DefaultContext, reviewer.OrgID, reviewer.ID, issue.RepoID) if !hasTeam { return models.ErrNotValidReviewRequest{ @@ -221,7 +223,7 @@ func IsValidTeamReviewRequest(reviewer *models.Team, doer *user_model.User, isAd } // TeamReviewRequest add or remove a review request from a team for this PR, and make comment for it. -func TeamReviewRequest(issue *models.Issue, doer *user_model.User, reviewer *models.Team, isAdd bool) (comment *models.Comment, err error) { +func TeamReviewRequest(issue *models.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *models.Comment, err error) { if isAdd { comment, err = models.AddTeamReviewRequest(issue, reviewer, doer) } else { @@ -241,11 +243,14 @@ func TeamReviewRequest(issue *models.Issue, doer *user_model.User, reviewer *mod return } - if err = reviewer.GetMembers(&models.SearchMembersOptions{}); err != nil { + members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{ + TeamID: reviewer.ID, + }) + if err != nil { return } - for _, member := range reviewer.Members { + for _, member := range members { if member.ID == comment.Issue.PosterID { continue } diff --git a/services/mailer/mail_repo.go b/services/mailer/mail_repo.go index 0abc666f1a..7f856f2d40 100644 --- a/services/mailer/mail_repo.go +++ b/services/mailer/mail_repo.go @@ -8,7 +8,8 @@ import ( "bytes" "fmt" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" @@ -24,7 +25,7 @@ func SendRepoTransferNotifyMail(doer, newOwner *user_model.User, repo *repo_mode } if newOwner.IsOrganization() { - users, err := models.GetUsersWhoCanCreateOrgRepo(newOwner.ID) + users, err := organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, newOwner.ID) if err != nil { return err } diff --git a/services/org/org.go b/services/org/org.go index 4c0cfe87fc..da7a71fec5 100644 --- a/services/org/org.go +++ b/services/org/org.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/storage" @@ -16,7 +17,7 @@ import ( ) // DeleteOrganization completely and permanently deletes everything of organization. -func DeleteOrganization(org *models.Organization) error { +func DeleteOrganization(org *organization.Organization) error { ctx, commiter, err := db.TxContext() if err != nil { return err @@ -31,7 +32,7 @@ func DeleteOrganization(org *models.Organization) error { return models.ErrUserOwnRepos{UID: org.ID} } - if err := models.DeleteOrganization(ctx, org); err != nil { + if err := organization.DeleteOrganization(ctx, org); err != nil { return fmt.Errorf("DeleteOrganization: %v", err) } diff --git a/services/org/org_test.go b/services/org/org_test.go index aaa2756bb5..b30bfddbb4 100644 --- a/services/org/org_test.go +++ b/services/org/org_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -21,18 +22,18 @@ func TestMain(m *testing.M) { func TestDeleteOrganization(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - org := unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 6}).(*models.Organization) + org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 6}).(*organization.Organization) assert.NoError(t, DeleteOrganization(org)) - unittest.AssertNotExistsBean(t, &models.Organization{ID: 6}) - unittest.AssertNotExistsBean(t, &models.OrgUser{OrgID: 6}) - unittest.AssertNotExistsBean(t, &models.Team{OrgID: 6}) + unittest.AssertNotExistsBean(t, &organization.Organization{ID: 6}) + unittest.AssertNotExistsBean(t, &organization.OrgUser{OrgID: 6}) + unittest.AssertNotExistsBean(t, &organization.Team{OrgID: 6}) - org = unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 3}).(*models.Organization) + org = unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3}).(*organization.Organization) err := DeleteOrganization(org) assert.Error(t, err) assert.True(t, models.IsErrUserOwnRepos(err)) - user := unittest.AssertExistsAndLoadBean(t, &models.Organization{ID: 5}).(*models.Organization) + user := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 5}).(*organization.Organization) assert.Error(t, DeleteOrganization(user)) - unittest.CheckConsistencyFor(t, &user_model.User{}, &models.Team{}) + unittest.CheckConsistencyFor(t, &user_model.User{}, &organization.Team{}) } diff --git a/services/repository/repository.go b/services/repository/repository.go index c3ca867187..5886b96ab8 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -9,6 +9,7 @@ import ( "fmt" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" @@ -50,7 +51,7 @@ func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_mod func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_model.Repository, error) { if !authUser.IsAdmin { if owner.IsOrganization() { - if ok, err := models.CanCreateOrgRepo(owner.ID, authUser.ID); err != nil { + if ok, err := organization.CanCreateOrgRepo(owner.ID, authUser.ID); err != nil { return nil, err } else if !ok { return nil, fmt.Errorf("cannot push-create repository for org") diff --git a/services/repository/transfer.go b/services/repository/transfer.go index 1e8b78dfc6..0abb03a88d 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -21,7 +22,7 @@ import ( var repoWorkingPool = sync.NewExclusivePool() // TransferOwnership transfers all corresponding setting from old user to new one. -func TransferOwnership(doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*models.Team) error { +func TransferOwnership(doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error { if err := repo.GetOwner(db.DefaultContext); err != nil { return err } @@ -46,7 +47,7 @@ func TransferOwnership(doer, newOwner *user_model.User, repo *repo_model.Reposit } for _, team := range teams { - if err := team.AddRepository(newRepo); err != nil { + if err := models.AddRepository(team, newRepo); err != nil { return err } } @@ -81,7 +82,7 @@ func ChangeRepositoryName(doer *user_model.User, repo *repo_model.Repository, ne // StartRepositoryTransfer transfer a repo from one owner to a new one. // it make repository into pending transfer state, if doer can not create repo for new owner. -func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*models.Team) error { +func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.Repository, teams []*organization.Team) error { if err := models.TestRepositoryReadyForTransfer(repo.Status); err != nil { return err } @@ -93,7 +94,7 @@ func StartRepositoryTransfer(doer, newOwner *user_model.User, repo *repo_model.R // If new owner is an org and user can create repos he can transfer directly too if newOwner.IsOrganization() { - allowed, err := models.CanCreateOrgRepo(newOwner.ID, doer.ID) + allowed, err := organization.CanCreateOrgRepo(newOwner.ID, doer.ID) if err != nil { return err } diff --git a/services/repository/transfer_test.go b/services/repository/transfer_test.go index d4fbe95834..1081c76c7e 100644 --- a/services/repository/transfer_test.go +++ b/services/repository/transfer_test.go @@ -9,6 +9,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -53,7 +54,7 @@ func TestTransferOwnership(t *testing.T) { Content: "user3/repo3", }) - unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &models.Team{}) + unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{}) } func TestStartRepositoryTransferSetPermission(t *testing.T) { @@ -74,5 +75,5 @@ func TestStartRepositoryTransferSetPermission(t *testing.T) { assert.NoError(t, err) assert.True(t, hasAccess) - unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &models.Team{}) + unittest.CheckConsistencyFor(t, &repo_model.Repository{}, &user_model.User{}, &organization.Team{}) } diff --git a/services/user/user.go b/services/user/user.go index e31b174c0c..f88c0df93d 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -16,6 +16,7 @@ import ( admin_model "code.gitea.io/gitea/models/admin" asymkey_model "code.gitea.io/gitea/models/asymkey" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/avatar" @@ -50,7 +51,7 @@ func DeleteUser(u *user_model.User) error { } // Check membership of organization. - count, err = models.GetOrganizationCount(ctx, u) + count, err = organization.GetOrganizationCount(ctx, u) if err != nil { return fmt.Errorf("GetOrganizationCount: %v", err) } else if count > 0 { diff --git a/services/user/user_test.go b/services/user/user_test.go index 5b35db790c..7835530394 100644 --- a/services/user/user_test.go +++ b/services/user/user_test.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -36,11 +37,11 @@ func TestDeleteUser(t *testing.T) { return } - orgUsers := make([]*models.OrgUser, 0, 10) - assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&orgUsers, &models.OrgUser{UID: userID})) + orgUsers := make([]*organization.OrgUser, 0, 10) + assert.NoError(t, db.GetEngine(db.DefaultContext).Find(&orgUsers, &organization.OrgUser{UID: userID})) for _, orgUser := range orgUsers { if err := models.RemoveOrgUser(orgUser.OrgID, orgUser.UID); err != nil { - assert.True(t, models.IsErrLastOrgOwner(err)) + assert.True(t, organization.IsErrLastOrgOwner(err)) return } } |