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 /routers | |
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 'routers')
31 files changed, 214 insertions, 182 deletions
diff --git a/routers/api/v1/admin/org.go b/routers/api/v1/admin/org.go index e4850ac494..727f3193cf 100644 --- a/routers/api/v1/admin/org.go +++ b/routers/api/v1/admin/org.go @@ -8,8 +8,8 @@ package admin import ( "net/http" - "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/context" "code.gitea.io/gitea/modules/convert" @@ -52,7 +52,7 @@ func CreateOrg(ctx *context.APIContext) { visibility = api.VisibilityModes[form.Visibility] } - org := &models.Organization{ + org := &organization.Organization{ Name: form.UserName, FullName: form.FullName, Description: form.Description, @@ -63,7 +63,7 @@ func CreateOrg(ctx *context.APIContext) { Visibility: visibility, } - if err := models.CreateOrganization(org, ctx.ContextUser); err != nil { + if err := organization.CreateOrganization(org, ctx.ContextUser); err != nil { if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNameCharsNotAllowed(err) || @@ -115,7 +115,7 @@ func GetAllOrgs(ctx *context.APIContext) { } orgs := make([]*api.Organization, len(users)) for i := range users { - orgs[i] = convert.ToOrganization(models.OrgFromUser(users[i])) + orgs[i] = convert.ToOrganization(organization.OrgFromUser(users[i])) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 3debf58a17..1fed95284b 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -71,6 +71,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -309,7 +310,7 @@ func reqOrgOwnership() func(ctx *context.APIContext) { return } - isOwner, err := models.IsOrganizationOwner(orgID, ctx.Doer.ID) + isOwner, err := organization.IsOrganizationOwner(ctx, orgID, ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err) return @@ -336,7 +337,7 @@ func reqTeamMembership() func(ctx *context.APIContext) { } orgID := ctx.Org.Team.OrgID - isOwner, err := models.IsOrganizationOwner(orgID, ctx.Doer.ID) + isOwner, err := organization.IsOrganizationOwner(ctx, orgID, ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err) return @@ -344,11 +345,11 @@ func reqTeamMembership() func(ctx *context.APIContext) { return } - if isTeamMember, err := models.IsTeamMember(orgID, ctx.Org.Team.ID, ctx.Doer.ID); err != nil { + if isTeamMember, err := organization.IsTeamMember(ctx, orgID, ctx.Org.Team.ID, ctx.Doer.ID); err != nil { ctx.Error(http.StatusInternalServerError, "IsTeamMember", err) return } else if !isTeamMember { - isOrgMember, err := models.IsOrganizationMember(orgID, ctx.Doer.ID) + isOrgMember, err := organization.IsOrganizationMember(ctx, orgID, ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) } else if isOrgMember { @@ -378,7 +379,7 @@ func reqOrgMembership() func(ctx *context.APIContext) { return } - if isMember, err := models.IsOrganizationMember(orgID, ctx.Doer.ID); err != nil { + if isMember, err := organization.IsOrganizationMember(ctx, orgID, ctx.Doer.ID); err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) return } else if !isMember { @@ -427,9 +428,9 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) { var err error if assignOrg { - ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":org")) + ctx.Org.Organization, err = organization.GetOrgByName(ctx.Params(":org")) if err != nil { - if models.IsErrOrgNotExist(err) { + if organization.IsErrOrgNotExist(err) { redirectUserID, err := user_model.LookupUserRedirect(ctx.Params(":org")) if err == nil { context.RedirectToUser(ctx.Context, ctx.Params(":org"), redirectUserID) @@ -447,9 +448,9 @@ func orgAssignment(args ...bool) func(ctx *context.APIContext) { } if assignTeam { - ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid")) + ctx.Org.Team, err = organization.GetTeamByID(ctx.ParamsInt64(":teamid")) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.NotFound() } else { ctx.Error(http.StatusInternalServerError, "GetTeamById", err) diff --git a/routers/api/v1/org/member.go b/routers/api/v1/org/member.go index 9dae15462f..85fe2ded4d 100644 --- a/routers/api/v1/org/member.go +++ b/routers/api/v1/org/member.go @@ -9,6 +9,7 @@ import ( "net/url" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/setting" @@ -19,19 +20,19 @@ import ( // listMembers list an organization's members func listMembers(ctx *context.APIContext, publicOnly bool) { - opts := &models.FindOrgMembersOpts{ + opts := &organization.FindOrgMembersOpts{ OrgID: ctx.Org.Organization.ID, PublicOnly: publicOnly, ListOptions: utils.GetListOptions(ctx), } - count, err := models.CountOrgMembers(opts) + count, err := organization.CountOrgMembers(opts) if err != nil { ctx.InternalServerError(err) return } - members, _, err := models.FindOrgMembers(opts) + members, _, err := organization.FindOrgMembers(opts) if err != nil { ctx.InternalServerError(err) return @@ -190,7 +191,7 @@ func IsPublicMember(ctx *context.APIContext) { if ctx.Written() { return } - is, err := models.IsPublicMembership(ctx.Org.Organization.ID, userToCheck.ID) + is, err := organization.IsPublicMembership(ctx.Org.Organization.ID, userToCheck.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsPublicMembership", err) return @@ -234,7 +235,7 @@ func PublicizeMember(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Cannot publicize another member") return } - err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true) + err := organization.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToPublicize.ID, true) if err != nil { ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err) return @@ -274,7 +275,7 @@ func ConcealMember(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Cannot conceal another member") return } - err := models.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false) + err := organization.ChangeOrgUserStatus(ctx.Org.Organization.ID, userToConceal.ID, false) if err != nil { ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err) return @@ -308,8 +309,8 @@ func DeleteMember(ctx *context.APIContext) { if ctx.Written() { return } - if err := ctx.Org.Organization.RemoveMember(member.ID); err != nil { - ctx.Error(http.StatusInternalServerError, "RemoveMember", err) + if err := models.RemoveOrgUser(ctx.Org.Organization.ID, member.ID); err != nil { + ctx.Error(http.StatusInternalServerError, "RemoveOrgUser", err) } ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/org/org.go b/routers/api/v1/org/org.go index fcc98f4b78..d55a4a4514 100644 --- a/routers/api/v1/org/org.go +++ b/routers/api/v1/org/org.go @@ -8,8 +8,8 @@ package org import ( "net/http" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -25,17 +25,17 @@ func listUserOrgs(ctx *context.APIContext, u *user_model.User) { listOptions := utils.GetListOptions(ctx) showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == u.ID) - opts := models.FindOrgOptions{ + opts := organization.FindOrgOptions{ ListOptions: listOptions, UserID: u.ID, IncludePrivate: showPrivate, } - orgs, err := models.FindOrgs(opts) + orgs, err := organization.FindOrgs(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "FindOrgs", err) return } - maxResults, err := models.CountOrgs(opts) + maxResults, err := organization.CountOrgs(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "CountOrgs", err) return @@ -135,12 +135,12 @@ func GetUserOrgsPermissions(ctx *context.APIContext) { op := api.OrganizationPermissions{} - if !models.HasOrgOrUserVisible(o, ctx.ContextUser) { + if !organization.HasOrgOrUserVisible(ctx, o, ctx.ContextUser) { ctx.NotFound("HasOrgOrUserVisible", nil) return } - org := models.OrgFromUser(o) + org := organization.OrgFromUser(o) authorizeLevel, err := org.GetOrgUserMaxAuthorizeLevel(ctx.ContextUser.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetOrgUserAuthorizeLevel", err) @@ -212,7 +212,7 @@ func GetAll(ctx *context.APIContext) { } orgs := make([]*api.Organization, len(publicOrgs)) for i := range publicOrgs { - orgs[i] = convert.ToOrganization(models.OrgFromUser(publicOrgs[i])) + orgs[i] = convert.ToOrganization(organization.OrgFromUser(publicOrgs[i])) } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) @@ -252,7 +252,7 @@ func Create(ctx *context.APIContext) { visibility = api.VisibilityModes[form.Visibility] } - org := &models.Organization{ + org := &organization.Organization{ Name: form.UserName, FullName: form.FullName, Description: form.Description, @@ -263,7 +263,7 @@ func Create(ctx *context.APIContext) { Visibility: visibility, RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, } - if err := models.CreateOrganization(org, ctx.Doer); err != nil { + if err := organization.CreateOrganization(org, ctx.Doer); err != nil { if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNameCharsNotAllowed(err) || @@ -295,7 +295,7 @@ func Get(ctx *context.APIContext) { // "200": // "$ref": "#/responses/Organization" - if !models.HasOrgOrUserVisible(ctx.Org.Organization.AsUser(), ctx.Doer) { + if !organization.HasOrgOrUserVisible(ctx, ctx.Org.Organization.AsUser(), ctx.Doer) { ctx.NotFound("HasOrgOrUserVisible", nil) return } diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index ca8cda269a..206012530f 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -47,7 +48,7 @@ func ListTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - teams, count, err := models.SearchOrgTeams(&models.SearchOrgTeamOptions{ + teams, count, err := organization.SearchTeam(&organization.SearchTeamOptions{ ListOptions: utils.GetListOptions(ctx), OrgID: ctx.Org.Organization.ID, }) @@ -90,7 +91,7 @@ func ListUserTeams(ctx *context.APIContext) { // "200": // "$ref": "#/responses/TeamList" - teams, count, err := models.GetUserTeams(&models.GetUserTeamOptions{ + teams, count, err := organization.SearchTeam(&organization.SearchTeamOptions{ ListOptions: utils.GetListOptions(ctx), UserID: ctx.Doer.ID, }) @@ -104,7 +105,7 @@ func ListUserTeams(ctx *context.APIContext) { for i := range teams { apiOrg, ok := cache[teams[i].OrgID] if !ok { - org, err := models.GetOrgByID(teams[i].OrgID) + org, err := organization.GetOrgByID(teams[i].OrgID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetUserByID", err) return @@ -150,11 +151,11 @@ func GetTeam(ctx *context.APIContext) { ctx.JSON(http.StatusOK, convert.ToTeam(ctx.Org.Team)) } -func attachTeamUnits(team *models.Team, units []string) { +func attachTeamUnits(team *organization.Team, units []string) { unitTypes := unit_model.FindUnitTypes(units...) - team.Units = make([]*models.TeamUnit, 0, len(units)) + team.Units = make([]*organization.TeamUnit, 0, len(units)) for _, tp := range unitTypes { - team.Units = append(team.Units, &models.TeamUnit{ + team.Units = append(team.Units, &organization.TeamUnit{ OrgID: team.OrgID, Type: tp, AccessMode: team.AccessMode, @@ -170,10 +171,10 @@ func convertUnitsMap(unitsMap map[string]string) map[unit_model.Type]perm.Access return res } -func attachTeamUnitsMap(team *models.Team, unitsMap map[string]string) { - team.Units = make([]*models.TeamUnit, 0, len(unitsMap)) +func attachTeamUnitsMap(team *organization.Team, unitsMap map[string]string) { + team.Units = make([]*organization.TeamUnit, 0, len(unitsMap)) for unitKey, p := range unitsMap { - team.Units = append(team.Units, &models.TeamUnit{ + team.Units = append(team.Units, &organization.TeamUnit{ OrgID: team.OrgID, Type: unit_model.TypeFromKey(unitKey), AccessMode: perm.ParseAccessMode(p), @@ -210,7 +211,7 @@ func CreateTeam(ctx *context.APIContext) { if p < perm.AccessModeAdmin && len(form.UnitsMap) > 0 { p = unit_model.MinUnitAccessMode(convertUnitsMap(form.UnitsMap)) } - team := &models.Team{ + team := &organization.Team{ OrgID: ctx.Org.Organization.ID, Name: form.Name, Description: form.Description, @@ -231,7 +232,7 @@ func CreateTeam(ctx *context.APIContext) { } if err := models.NewTeam(team); err != nil { - if models.IsErrTeamAlreadyExist(err) { + if organization.IsErrTeamAlreadyExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "NewTeam", err) @@ -368,7 +369,7 @@ func GetTeamMembers(ctx *context.APIContext) { // "200": // "$ref": "#/responses/UserList" - isMember, err := models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.Doer.ID) + isMember, err := organization.IsOrganizationMember(ctx, ctx.Org.Team.OrgID, ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) return @@ -377,14 +378,17 @@ func GetTeamMembers(ctx *context.APIContext) { return } - if err := ctx.Org.Team.GetMembers(&models.SearchMembersOptions{ + teamMembers, err := organization.GetTeamMembers(ctx, &organization.SearchMembersOptions{ ListOptions: utils.GetListOptions(ctx), - }); err != nil { + TeamID: ctx.Org.Team.ID, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamMembers", err) return } + members := make([]*api.User, len(ctx.Org.Team.Members)) - for i, member := range ctx.Org.Team.Members { + for i, member := range teamMembers { members[i] = convert.ToUser(member, ctx.Doer) } @@ -422,7 +426,7 @@ func GetTeamMember(ctx *context.APIContext) { return } teamID := ctx.ParamsInt64("teamid") - isTeamMember, err := models.IsUserInTeams(u.ID, []int64{teamID}) + isTeamMember, err := organization.IsUserInTeams(ctx, u.ID, []int64{teamID}) if err != nil { ctx.Error(http.StatusInternalServerError, "IsUserInTeams", err) return @@ -462,7 +466,7 @@ func AddTeamMember(ctx *context.APIContext) { if ctx.Written() { return } - if err := ctx.Org.Team.AddMember(u.ID); err != nil { + if err := models.AddTeamMember(ctx.Org.Team, u.ID); err != nil { ctx.Error(http.StatusInternalServerError, "AddMember", err) return } @@ -499,8 +503,8 @@ func RemoveTeamMember(ctx *context.APIContext) { return } - if err := ctx.Org.Team.RemoveMember(u.ID); err != nil { - ctx.Error(http.StatusInternalServerError, "RemoveMember", err) + if err := models.RemoveTeamMember(ctx.Org.Team, u.ID); err != nil { + ctx.Error(http.StatusInternalServerError, "RemoveTeamMember", err) return } ctx.Status(http.StatusNoContent) @@ -533,13 +537,16 @@ func GetTeamRepos(ctx *context.APIContext) { // "$ref": "#/responses/RepositoryList" team := ctx.Org.Team - if err := team.GetRepositories(&models.SearchOrgTeamOptions{ + teamRepos, err := organization.GetTeamRepositories(ctx, &organization.SearchTeamRepoOptions{ ListOptions: utils.GetListOptions(ctx), - }); err != nil { + TeamID: team.ID, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) + return } repos := make([]*api.Repository, len(team.Repos)) - for i, repo := range team.Repos { + for i, repo := range teamRepos { access, err := models.AccessLevel(ctx.Doer, repo) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTeamRepos", err) @@ -606,7 +613,7 @@ func AddTeamRepository(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } - if err := ctx.Org.Team.AddRepository(repo); err != nil { + if err := models.AddRepository(ctx.Org.Team, repo); err != nil { ctx.Error(http.StatusInternalServerError, "AddRepository", err) return } @@ -656,7 +663,7 @@ func RemoveTeamRepository(ctx *context.APIContext) { ctx.Error(http.StatusForbidden, "", "Must have admin-level access to the repository") return } - if err := ctx.Org.Team.RemoveRepository(repo.ID); err != nil { + if err := models.RemoveRepository(ctx.Org.Team, repo.ID); err != nil { ctx.Error(http.StatusInternalServerError, "RemoveRepository", err) return } @@ -707,14 +714,15 @@ func SearchTeam(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - opts := &models.SearchOrgTeamOptions{ + opts := &organization.SearchTeamOptions{ + UserID: ctx.Doer.ID, Keyword: ctx.FormTrim("q"), OrgID: ctx.Org.Organization.ID, IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"), ListOptions: listOptions, } - teams, maxResults, err := models.SearchOrgTeams(opts) + teams, maxResults, err := organization.SearchTeam(opts) if err != nil { log.Error("SearchTeam failed: %v", err) ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 065bad2708..f22bed813e 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -11,6 +11,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -448,27 +449,27 @@ func CreateBranchProtection(ctx *context.APIContext) { } var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64 if repo.Owner.IsOrganization() { - whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false) + whitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err) return } - mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false) + mergeWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err) return } - approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false) + approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } @@ -692,9 +693,9 @@ func EditBranchProtection(ctx *context.APIContext) { var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64 if repo.Owner.IsOrganization() { if form.PushWhitelistTeams != nil { - whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false) + whitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } @@ -705,9 +706,9 @@ func EditBranchProtection(ctx *context.APIContext) { whitelistTeams = protectBranch.WhitelistTeamIDs } if form.MergeWhitelistTeams != nil { - mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false) + mergeWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } @@ -718,9 +719,9 @@ func EditBranchProtection(ctx *context.APIContext) { mergeWhitelistTeams = protectBranch.MergeWhitelistTeamIDs } if form.ApprovalsWhitelistTeams != nil { - approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false) + approvalsWhitelistTeams, err = organization.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err) return } diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 6cf51b2467..aaff9107cf 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -10,6 +10,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "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" @@ -108,9 +109,9 @@ func CreateFork(ctx *context.APIContext) { if form.Organization == nil { forker = ctx.Doer } else { - org, err := models.GetOrgByName(*form.Organization) + org, err := organization.GetOrgByName(*form.Organization) if err != nil { - if models.IsErrOrgNotExist(err) { + if organization.IsErrOrgNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetOrgByName", err) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index ef8c2d008e..05c9c27144 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" @@ -161,9 +162,9 @@ func SearchIssues(ctx *context.APIContext) { ctx.Error(http.StatusBadRequest, "", "Owner organisation is required for filtering on team") return } - team, err := models.GetTeam(opts.OwnerID, ctx.FormString("team")) + team, err := organization.GetTeam(opts.OwnerID, ctx.FormString("team")) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusBadRequest, "Team not found", err) } else { ctx.Error(http.StatusInternalServerError, "GetUserByName", err) diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index 1461c2a145..1adf5bbe86 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -13,6 +13,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" @@ -90,7 +91,7 @@ func Migrate(ctx *context.APIContext) { if repoOwner.IsOrganization() { // Check ownership of organization. - isOwner, err := models.OrgFromUser(repoOwner).IsOwnedBy(ctx.Doer.ID) + isOwner, err := organization.OrgFromUser(repoOwner).IsOwnedBy(ctx.Doer.ID) if err != nil { ctx.Error(http.StatusInternalServerError, "IsOwnedBy", err) return diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index b4a484f68c..331b78e1d8 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" @@ -722,12 +723,12 @@ func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions if ctx.Repo.Repository.Owner.IsOrganization() && len(opts.TeamReviewers) > 0 { - teamReviewers := make([]*models.Team, 0, len(opts.TeamReviewers)) + teamReviewers := make([]*organization.Team, 0, len(opts.TeamReviewers)) for _, t := range opts.TeamReviewers { - var teamReviewer *models.Team - teamReviewer, err = models.GetTeam(ctx.Repo.Owner.ID, t) + var teamReviewer *organization.Team + teamReviewer, err = organization.GetTeam(ctx.Repo.Owner.ID, t) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.NotFound("TeamNotExist", fmt.Sprintf("Team '%s' not exist", t)) return } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 7e386d2e5b..2d979235a0 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -13,6 +13,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" unit_model "code.gitea.io/gitea/models/unit" @@ -398,7 +399,7 @@ func Generate(ctx *context.APIContext) { } if !ctx.Doer.IsAdmin { - canCreate, err := models.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx.Doer.ID) + canCreate, err := organization.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx.Doer.ID) if err != nil { ctx.ServerError("CanCreateOrgRepo", err) return @@ -484,9 +485,9 @@ func CreateOrgRepo(ctx *context.APIContext) { // "403": // "$ref": "#/responses/forbidden" opt := web.GetForm(ctx).(*api.CreateRepoOption) - org, err := models.GetOrgByName(ctx.Params(":org")) + org, err := organization.GetOrgByName(ctx.Params(":org")) if err != nil { - if models.IsErrOrgNotExist(err) { + if organization.IsErrOrgNotExist(err) { ctx.Error(http.StatusUnprocessableEntity, "", err) } else { ctx.Error(http.StatusInternalServerError, "GetOrgByName", err) @@ -494,7 +495,7 @@ func CreateOrgRepo(ctx *context.APIContext) { return } - if !models.HasOrgOrUserVisible(org.AsUser(), ctx.Doer) { + if !organization.HasOrgOrUserVisible(ctx, org.AsUser(), ctx.Doer) { ctx.NotFound("HasOrgOrUserVisible", nil) return } diff --git a/routers/api/v1/repo/teams.go b/routers/api/v1/repo/teams.go index 024224b5a3..1e3ea326d3 100644 --- a/routers/api/v1/repo/teams.go +++ b/routers/api/v1/repo/teams.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/convert" api "code.gitea.io/gitea/modules/structs" @@ -41,7 +42,7 @@ func ListTeams(ctx *context.APIContext) { return } - teams, err := models.GetRepoTeams(ctx.Repo.Repository) + teams, err := organization.GetRepoTeams(ctx.Repo.Repository) if err != nil { ctx.InternalServerError(err) return @@ -101,7 +102,7 @@ func IsTeam(ctx *context.APIContext) { return } - if team.HasRepository(ctx.Repo.Repository.ID) { + if models.HasRepository(team, ctx.Repo.Repository.ID) { if err := team.GetUnits(); err != nil { ctx.Error(http.StatusInternalServerError, "GetUnits", err) return @@ -196,20 +197,20 @@ func changeRepoTeam(ctx *context.APIContext, add bool) { return } - repoHasTeam := team.HasRepository(ctx.Repo.Repository.ID) + repoHasTeam := models.HasRepository(team, ctx.Repo.Repository.ID) var err error if add { if repoHasTeam { ctx.Error(http.StatusUnprocessableEntity, "alreadyAdded", fmt.Errorf("team '%s' is already added to repo", team.Name)) return } - err = team.AddRepository(ctx.Repo.Repository) + err = models.AddRepository(team, ctx.Repo.Repository) } else { if !repoHasTeam { ctx.Error(http.StatusUnprocessableEntity, "notAdded", fmt.Errorf("team '%s' was not added to repo", team.Name)) return } - err = team.RemoveRepository(ctx.Repo.Repository.ID) + err = models.RemoveRepository(team, ctx.Repo.Repository.ID) } if err != nil { ctx.InternalServerError(err) @@ -219,10 +220,10 @@ func changeRepoTeam(ctx *context.APIContext, add bool) { ctx.Status(http.StatusNoContent) } -func getTeamByParam(ctx *context.APIContext) *models.Team { - team, err := models.GetTeam(ctx.Repo.Owner.ID, ctx.Params(":team")) +func getTeamByParam(ctx *context.APIContext) *organization.Team { + team, err := organization.GetTeam(ctx.Repo.Owner.ID, ctx.Params(":team")) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Error(http.StatusNotFound, "TeamNotExit", err) return nil } diff --git a/routers/api/v1/repo/transfer.go b/routers/api/v1/repo/transfer.go index e149f9c8f0..7578fbd187 100644 --- a/routers/api/v1/repo/transfer.go +++ b/routers/api/v1/repo/transfer.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "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" @@ -67,23 +68,23 @@ func Transfer(ctx *context.APIContext) { } if newOwner.Type == user_model.UserTypeOrganization { - if !ctx.Doer.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.Doer.ID) { + if !ctx.Doer.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !organization.OrgFromUser(newOwner).HasMemberWithUserID(ctx.Doer.ID) { // The user shouldn't know about this organization ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found") return } } - var teams []*models.Team + var teams []*organization.Team if opts.TeamIDs != nil { if !newOwner.IsOrganization() { ctx.Error(http.StatusUnprocessableEntity, "repoTransfer", "Teams can only be added to organization-owned repositories") return } - org := convert.ToOrganization(models.OrgFromUser(newOwner)) + org := convert.ToOrganization(organization.OrgFromUser(newOwner)) for _, tID := range *opts.TeamIDs { - team, err := models.GetTeamByID(tID) + team, err := organization.GetTeamByID(tID) if err != nil { ctx.Error(http.StatusUnprocessableEntity, "team", fmt.Errorf("team %d not found", tID)) return diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 83f97f8f01..cdbc35471b 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -21,7 +21,7 @@ import ( // getStarredRepos returns the repos that the user with the specified userID has // starred func getStarredRepos(user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, error) { - starredRepos, err := models.GetStarredRepos(user.ID, private, listOptions) + starredRepos, err := repo_model.GetStarredRepos(user.ID, private, listOptions) if err != nil { return nil, err } diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index e9d871c5c1..018f75762f 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -98,7 +98,7 @@ func GetInfo(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - if !models.IsUserVisibleToViewer(ctx.ContextUser, ctx.Doer) { + if !user_model.IsUserVisibleToViewer(ctx.ContextUser, ctx.Doer) { // fake ErrUserNotExist error message to not leak information about existence ctx.NotFound("GetUserByName", user_model.ErrUserNotExist{Name: ctx.Params(":username")}) return diff --git a/routers/api/v1/user/watch.go b/routers/api/v1/user/watch.go index 22e76f5ebc..e7c6837cb8 100644 --- a/routers/api/v1/user/watch.go +++ b/routers/api/v1/user/watch.go @@ -19,7 +19,7 @@ import ( // getWatchedRepos returns the repos that the user with the specified userID is watching func getWatchedRepos(user *user_model.User, private bool, listOptions db.ListOptions) ([]*api.Repository, int64, error) { - watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions) + watchedRepos, total, err := repo_model.GetWatchedRepos(user.ID, private, listOptions) if err != nil { return nil, 0, err } diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 1466731b9e..24a0f13b54 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.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/modules/base" "code.gitea.io/gitea/modules/context" @@ -39,7 +40,7 @@ func Home(ctx *context.Context) { org := ctx.Org.Organization - if !models.HasOrgOrUserVisible(org.AsUser(), ctx.Doer) { + if !organization.HasOrgOrUserVisible(ctx, org.AsUser(), ctx.Doer) { ctx.NotFound("HasOrgOrUserVisible", nil) return } @@ -122,7 +123,7 @@ func Home(ctx *context.Context) { return } - opts := &models.FindOrgMembersOpts{ + opts := &organization.FindOrgMembersOpts{ OrgID: org.ID, PublicOnly: true, ListOptions: db.ListOptions{Page: 1, PageSize: 25}, @@ -137,13 +138,13 @@ func Home(ctx *context.Context) { opts.PublicOnly = !isMember && !ctx.Doer.IsAdmin } - members, _, err := models.FindOrgMembers(opts) + members, _, err := organization.FindOrgMembers(opts) if err != nil { ctx.ServerError("FindOrgMembers", err) return } - membersCount, err := models.CountOrgMembers(opts) + membersCount, err := organization.CountOrgMembers(opts) if err != nil { ctx.ServerError("CountOrgMembers", err) return diff --git a/routers/web/org/members.go b/routers/web/org/members.go index 9e65e61404..add8e724bd 100644 --- a/routers/web/org/members.go +++ b/routers/web/org/members.go @@ -9,6 +9,7 @@ import ( "net/http" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -31,7 +32,7 @@ func Members(ctx *context.Context) { page = 1 } - opts := &models.FindOrgMembersOpts{ + opts := &organization.FindOrgMembersOpts{ OrgID: org.ID, PublicOnly: true, } @@ -45,7 +46,7 @@ func Members(ctx *context.Context) { opts.PublicOnly = !isMember && !ctx.Doer.IsAdmin } - total, err := models.CountOrgMembers(opts) + total, err := organization.CountOrgMembers(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "CountOrgMembers") return @@ -54,7 +55,7 @@ func Members(ctx *context.Context) { pager := context.NewPagination(int(total), setting.UI.MembersPagingNum, page, 5) opts.ListOptions.Page = page opts.ListOptions.PageSize = setting.UI.MembersPagingNum - members, membersIsPublic, err := models.FindOrgMembers(opts) + members, membersIsPublic, err := organization.FindOrgMembers(opts) if err != nil { ctx.ServerError("GetMembers", err) return @@ -84,20 +85,20 @@ func MembersAction(ctx *context.Context) { ctx.Error(http.StatusNotFound) return } - err = models.ChangeOrgUserStatus(org.ID, uid, false) + err = organization.ChangeOrgUserStatus(org.ID, uid, false) case "public": if ctx.Doer.ID != uid && !ctx.Org.IsOwner { ctx.Error(http.StatusNotFound) return } - err = models.ChangeOrgUserStatus(org.ID, uid, true) + err = organization.ChangeOrgUserStatus(org.ID, uid, true) case "remove": if !ctx.Org.IsOwner { ctx.Error(http.StatusNotFound) return } - err = org.RemoveMember(uid) - if models.IsErrLastOrgOwner(err) { + err = models.RemoveOrgUser(org.ID, uid) + if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.JSON(http.StatusOK, map[string]interface{}{ "redirect": ctx.Org.OrgLink + "/members", @@ -105,8 +106,8 @@ func MembersAction(ctx *context.Context) { return } case "leave": - err = org.RemoveMember(ctx.Doer.ID) - if models.IsErrLastOrgOwner(err) { + err = models.RemoveOrgUser(org.ID, ctx.Doer.ID) + if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.JSON(http.StatusOK, map[string]interface{}{ "redirect": ctx.Org.OrgLink + "/members", diff --git a/routers/web/org/org.go b/routers/web/org/org.go index b641f768b2..32d8787995 100644 --- a/routers/web/org/org.go +++ b/routers/web/org/org.go @@ -9,8 +9,8 @@ import ( "errors" "net/http" - "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/base" "code.gitea.io/gitea/modules/context" @@ -51,7 +51,7 @@ func CreatePost(ctx *context.Context) { return } - org := &models.Organization{ + org := &organization.Organization{ Name: form.OrgName, IsActive: true, Type: user_model.UserTypeOrganization, @@ -59,7 +59,7 @@ func CreatePost(ctx *context.Context) { RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, } - if err := models.CreateOrganization(org, ctx.Doer); err != nil { + if err := organization.CreateOrganization(org, ctx.Doer); err != nil { ctx.Data["Err_OrgName"] = true switch { case user_model.IsErrUserAlreadyExist(err): @@ -68,7 +68,7 @@ func CreatePost(ctx *context.Context) { ctx.RenderWithErr(ctx.Tr("org.form.name_reserved", err.(db.ErrNameReserved).Name), tplCreateOrg, &form) case db.IsErrNamePatternNotAllowed(err): ctx.RenderWithErr(ctx.Tr("org.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplCreateOrg, &form) - case models.IsErrUserNotAllowedCreateOrg(err): + case organization.IsErrUserNotAllowedCreateOrg(err): ctx.RenderWithErr(ctx.Tr("org.form.create_org_not_allowed"), tplCreateOrg, &form) default: ctx.ServerError("CreateOrganization", err) diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index d13c4e854d..034a8ce978 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -13,6 +13,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -43,7 +44,7 @@ func Teams(ctx *context.Context) { ctx.Data["PageIsOrgTeams"] = true for _, t := range ctx.Org.Teams { - if err := t.GetMembers(&models.SearchMembersOptions{}); err != nil { + if err := t.GetMembersCtx(ctx); err != nil { ctx.ServerError("GetMembers", err) return } @@ -69,11 +70,11 @@ func TeamsAction(ctx *context.Context) { ctx.Error(http.StatusNotFound) return } - err = ctx.Org.Team.AddMember(ctx.Doer.ID) + err = models.AddTeamMember(ctx.Org.Team, ctx.Doer.ID) case "leave": - err = ctx.Org.Team.RemoveMember(ctx.Doer.ID) + err = models.RemoveTeamMember(ctx.Org.Team, ctx.Doer.ID) if err != nil { - if models.IsErrLastOrgOwner(err) { + if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) } else { log.Error("Action(%s): %v", ctx.Params(":action"), err) @@ -94,9 +95,9 @@ func TeamsAction(ctx *context.Context) { ctx.Error(http.StatusNotFound) return } - err = ctx.Org.Team.RemoveMember(uid) + err = models.RemoveTeamMember(ctx.Org.Team, uid) if err != nil { - if models.IsErrLastOrgOwner(err) { + if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) } else { log.Error("Action(%s): %v", ctx.Params(":action"), err) @@ -139,14 +140,14 @@ func TeamsAction(ctx *context.Context) { if ctx.Org.Team.IsMember(u.ID) { ctx.Flash.Error(ctx.Tr("org.teams.add_duplicate_users")) } else { - err = ctx.Org.Team.AddMember(u.ID) + err = models.AddTeamMember(ctx.Org.Team, u.ID) } page = "team" } if err != nil { - if models.IsErrLastOrgOwner(err) { + if organization.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) } else { log.Error("Action(%s): %v", ctx.Params(":action"), err) @@ -191,13 +192,13 @@ func TeamsRepoAction(ctx *context.Context) { ctx.ServerError("GetRepositoryByName", err) return } - err = ctx.Org.Team.AddRepository(repo) + err = models.AddRepository(ctx.Org.Team, repo) case "remove": - err = ctx.Org.Team.RemoveRepository(ctx.FormInt64("repoid")) + err = models.RemoveRepository(ctx.Org.Team, ctx.FormInt64("repoid")) case "addall": - err = ctx.Org.Team.AddAllRepositories() + err = models.AddAllRepositories(ctx.Org.Team) case "removeall": - err = ctx.Org.Team.RemoveAllRepositories() + err = models.RemoveAllRepositories(ctx.Org.Team) } if err != nil { @@ -220,7 +221,7 @@ func NewTeam(ctx *context.Context) { ctx.Data["Title"] = ctx.Org.Organization.FullName ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamsNew"] = true - ctx.Data["Team"] = &models.Team{} + ctx.Data["Team"] = &organization.Team{} ctx.Data["Units"] = unit_model.Units ctx.HTML(http.StatusOK, tplTeamNew) } @@ -251,7 +252,7 @@ func NewTeamPost(ctx *context.Context) { p = unit_model.MinUnitAccessMode(unitPerms) } - t := &models.Team{ + t := &organization.Team{ OrgID: ctx.Org.Organization.ID, Name: form.TeamName, Description: form.Description, @@ -261,9 +262,9 @@ func NewTeamPost(ctx *context.Context) { } if t.AccessMode < perm.AccessModeAdmin { - units := make([]*models.TeamUnit, 0, len(unitPerms)) + units := make([]*organization.TeamUnit, 0, len(unitPerms)) for tp, perm := range unitPerms { - units = append(units, &models.TeamUnit{ + units = append(units, &organization.TeamUnit{ OrgID: ctx.Org.Organization.ID, Type: tp, AccessMode: perm, @@ -291,7 +292,7 @@ func NewTeamPost(ctx *context.Context) { if err := models.NewTeam(t); err != nil { ctx.Data["Err_TeamName"] = true switch { - case models.IsErrTeamAlreadyExist(err): + case organization.IsErrTeamAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form) default: ctx.ServerError("NewTeam", err) @@ -307,7 +308,7 @@ func TeamMembers(ctx *context.Context) { ctx.Data["Title"] = ctx.Org.Team.Name ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamMembers"] = true - if err := ctx.Org.Team.GetMembers(&models.SearchMembersOptions{}); err != nil { + if err := ctx.Org.Team.GetMembersCtx(ctx); err != nil { ctx.ServerError("GetMembers", err) return } @@ -320,7 +321,7 @@ func TeamRepositories(ctx *context.Context) { ctx.Data["Title"] = ctx.Org.Team.Name ctx.Data["PageIsOrgTeams"] = true ctx.Data["PageIsOrgTeamRepos"] = true - if err := ctx.Org.Team.GetRepositories(&models.SearchOrgTeamOptions{}); err != nil { + if err := ctx.Org.Team.GetRepositoriesCtx(ctx); err != nil { ctx.ServerError("GetRepositories", err) return } @@ -374,17 +375,17 @@ func EditTeamPost(ctx *context.Context) { } t.Description = form.Description if t.AccessMode < perm.AccessModeAdmin { - units := make([]models.TeamUnit, 0, len(unitPerms)) + units := make([]organization.TeamUnit, 0, len(unitPerms)) for tp, perm := range unitPerms { - units = append(units, models.TeamUnit{ + units = append(units, organization.TeamUnit{ OrgID: t.OrgID, TeamID: t.ID, Type: tp, AccessMode: perm, }) } - if err := models.UpdateTeamUnits(t, units); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadIssue", err.Error()) + if err := organization.UpdateTeamUnits(t, units); err != nil { + ctx.Error(http.StatusInternalServerError, "UpdateTeamUnits", err.Error()) return } } @@ -403,7 +404,7 @@ func EditTeamPost(ctx *context.Context) { if err := models.UpdateTeam(t, isAuthChanged, isIncludeAllChanged); err != nil { ctx.Data["Err_TeamName"] = true switch { - case models.IsErrTeamAlreadyExist(err): + case organization.IsErrTeamAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("form.team_name_been_taken"), tplTeamNew, &form) default: ctx.ServerError("UpdateTeam", err) diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index aeb1f0a020..ee4738c970 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -19,6 +19,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/unit" user_model "code.gitea.io/gitea/models/user" @@ -471,7 +472,7 @@ func retrieveProjects(ctx *context.Context, repo *repo_model.Repository) { // repoReviewerSelection items to bee shown type repoReviewerSelection struct { IsTeam bool - Team *models.Team + Team *organization.Team User *user_model.User Review *models.Review CanChange bool @@ -504,7 +505,7 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *repo_model.Repository, is pullReviews []*repoReviewerSelection reviewersResult []*repoReviewerSelection teamReviewersResult []*repoReviewerSelection - teamReviewers []*models.Team + teamReviewers []*organization.Team reviewers []*user_model.User ) @@ -586,7 +587,7 @@ func RetrieveRepoReviewers(ctx *context.Context, repo *repo_model.Repository, is item.User = item.Review.Reviewer } else if item.Review.ReviewerTeamID > 0 { if err = item.Review.LoadReviewerTeam(); err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { continue } ctx.ServerError("LoadReviewerTeam", err) @@ -1965,9 +1966,9 @@ func UpdatePullReviewRequest(ctx *context.Context) { return } - team, err := models.GetTeamByID(-reviewID) + team, err := organization.GetTeamByID(-reviewID) if err != nil { - ctx.ServerError("models.GetTeamByID", err) + ctx.ServerError("GetTeamByID", err) return } @@ -2702,8 +2703,8 @@ func handleTeamMentions(ctx *context.Context) { var isAdmin bool var err error - var teams []*models.Team - org := models.OrgFromUser(ctx.Repo.Owner) + var teams []*organization.Team + org := organization.OrgFromUser(ctx.Repo.Owner) // Admin has super access. if ctx.Doer.IsAdmin { isAdmin = true diff --git a/routers/web/repo/issue_label.go b/routers/web/repo/issue_label.go index ec04fa1660..a8866fc072 100644 --- a/routers/web/repo/issue_label.go +++ b/routers/web/repo/issue_label.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/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" @@ -77,7 +78,7 @@ func RetrieveLabels(ctx *context.Context) { } ctx.Data["OrgLabels"] = orgLabels - org, err := models.GetOrgByName(ctx.Repo.Owner.LowerName) + org, err := organization.GetOrgByName(ctx.Repo.Owner.LowerName) if err != nil { ctx.ServerError("GetOrgByName", err) return diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index a4edfd9ee4..af99f1b275 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -18,6 +18,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/unit" user_model "code.gitea.io/gitea/models/user" @@ -111,12 +112,12 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository { ctx.Data["ForkRepo"] = forkRepo - ownedOrgs, err := models.GetOrgsCanCreateRepoByUserID(ctx.Doer.ID) + ownedOrgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx.Doer.ID) if err != nil { ctx.ServerError("GetOrgsCanCreateRepoByUserID", err) return nil } - var orgs []*models.Organization + var orgs []*organization.Organization for _, org := range ownedOrgs { if forkRepo.OwnerID != org.ID && !repo_model.HasForkedRepo(org.ID, forkRepo.ID) { orgs = append(orgs, org) @@ -216,7 +217,7 @@ func ForkPost(ctx *context.Context) { // Check if user is allowed to create repo's on the organization. if ctxUser.IsOrganization() { - isAllowedToFork, err := models.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx.Doer.ID) + isAllowedToFork, err := organization.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx.Doer.ID) if err != nil { ctx.ServerError("CanCreateOrgRepo", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 4e07d47203..8b03133611 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -14,6 +14,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/unit" user_model "code.gitea.io/gitea/models/user" @@ -57,14 +58,14 @@ func MustBeAbleToUpload(ctx *context.Context) { } func checkContextUser(ctx *context.Context, uid int64) *user_model.User { - orgs, err := models.GetOrgsCanCreateRepoByUserID(ctx.Doer.ID) + orgs, err := organization.GetOrgsCanCreateRepoByUserID(ctx.Doer.ID) if err != nil { ctx.ServerError("GetOrgsCanCreateRepoByUserID", err) return nil } if !ctx.Doer.IsAdmin { - orgsAvailable := []*models.Organization{} + orgsAvailable := []*organization.Organization{} for i := 0; i < len(orgs); i++ { if orgs[i].CanCreateRepo() { orgsAvailable = append(orgsAvailable, orgs[i]) @@ -96,7 +97,7 @@ func checkContextUser(ctx *context.Context, uid int64) *user_model.User { return nil } if !ctx.Doer.IsAdmin { - canCreate, err := models.OrgFromUser(org).CanCreateOrgRepo(ctx.Doer.ID) + canCreate, err := organization.OrgFromUser(org).CanCreateOrgRepo(ctx.Doer.ID) if err != nil { ctx.ServerError("CanCreateOrgRepo", err) return nil diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 75db7ca3e2..ef99eee15a 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -17,6 +17,7 @@ import ( "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" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" unit_model "code.gitea.io/gitea/models/unit" @@ -648,7 +649,7 @@ func SettingsPost(ctx *context.Context) { } if newOwner.Type == user_model.UserTypeOrganization { - if !ctx.Doer.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.Doer.ID) { + if !ctx.Doer.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !organization.OrgFromUser(newOwner).HasMemberWithUserID(ctx.Doer.ID) { // The user shouldn't know about this organization ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil) return @@ -835,7 +836,7 @@ func Collaboration(ctx *context.Context) { } ctx.Data["Collaborators"] = users - teams, err := models.GetRepoTeams(ctx.Repo.Repository) + teams, err := organization.GetRepoTeams(ctx.Repo.Repository) if err != nil { ctx.ServerError("GetRepoTeams", err) return @@ -938,9 +939,9 @@ func AddTeamPost(ctx *context.Context) { return } - team, err := models.OrgFromUser(ctx.Repo.Owner).GetTeam(name) + team, err := organization.OrgFromUser(ctx.Repo.Owner).GetTeam(name) if err != nil { - if models.IsErrTeamNotExist(err) { + if organization.IsErrTeamNotExist(err) { ctx.Flash.Error(ctx.Tr("form.team_not_exist")) ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration") } else { @@ -955,13 +956,13 @@ func AddTeamPost(ctx *context.Context) { return } - if models.HasTeamRepo(ctx.Repo.Repository.OwnerID, team.ID, ctx.Repo.Repository.ID) { + if organization.HasTeamRepo(ctx, ctx.Repo.Repository.OwnerID, team.ID, ctx.Repo.Repository.ID) { ctx.Flash.Error(ctx.Tr("repo.settings.add_team_duplicate")) ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration") return } - if err = team.AddRepository(ctx.Repo.Repository); err != nil { + if err = models.AddRepository(team, ctx.Repo.Repository); err != nil { ctx.ServerError("team.AddRepository", err) return } @@ -978,13 +979,13 @@ func DeleteTeam(ctx *context.Context) { return } - team, err := models.GetTeamByID(ctx.FormInt64("id")) + team, err := organization.GetTeamByID(ctx.FormInt64("id")) if err != nil { ctx.ServerError("GetTeamByID", err) return } - if err = team.RemoveRepository(ctx.Repo.Repository.ID); err != nil { + if err = models.RemoveRepository(team, ctx.Repo.Repository.ID); err != nil { ctx.ServerError("team.RemoveRepositorys", err) return } diff --git a/routers/web/repo/setting_protected_branch.go b/routers/web/repo/setting_protected_branch.go index dae618a758..a26a0a620a 100644 --- a/routers/web/repo/setting_protected_branch.go +++ b/routers/web/repo/setting_protected_branch.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" @@ -158,7 +159,7 @@ func SettingsProtectedBranch(c *context.Context) { } if c.Repo.Owner.IsOrganization() { - teams, err := models.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c.Repo.Repository.ID, perm.AccessModeRead) + teams, err := organization.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c.Repo.Repository.ID, perm.AccessModeRead) if err != nil { c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err) return diff --git a/routers/web/repo/settings_test.go b/routers/web/repo/settings_test.go index db1e905869..36d02de273 100644 --- a/routers/web/repo/settings_test.go +++ b/routers/web/repo/settings_test.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" asymkey_model "code.gitea.io/gitea/models/asymkey" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" @@ -231,7 +232,7 @@ func TestAddTeamPost(t *testing.T) { Type: user_model.UserTypeOrganization, } - team := &models.Team{ + team := &organization.Team{ ID: 11, OrgID: 26, } @@ -255,7 +256,7 @@ func TestAddTeamPost(t *testing.T) { AddTeamPost(ctx) - assert.True(t, team.HasRepository(re.ID)) + assert.True(t, models.HasRepository(team, re.ID)) assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status()) assert.Empty(t, ctx.Flash.ErrorMsg) } @@ -271,7 +272,7 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { Type: user_model.UserTypeOrganization, } - team := &models.Team{ + team := &organization.Team{ ID: 11, OrgID: 26, } @@ -295,7 +296,7 @@ func TestAddTeamPost_NotAllowed(t *testing.T) { AddTeamPost(ctx) - assert.False(t, team.HasRepository(re.ID)) + assert.False(t, models.HasRepository(team, re.ID)) assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status()) assert.NotEmpty(t, ctx.Flash.ErrorMsg) } @@ -311,7 +312,7 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { Type: user_model.UserTypeOrganization, } - team := &models.Team{ + team := &organization.Team{ ID: 11, OrgID: 26, } @@ -336,7 +337,7 @@ func TestAddTeamPost_AddTeamTwice(t *testing.T) { AddTeamPost(ctx) AddTeamPost(ctx) - assert.True(t, team.HasRepository(re.ID)) + assert.True(t, models.HasRepository(team, re.ID)) assert.EqualValues(t, http.StatusSeeOther, ctx.Resp.Status()) assert.NotEmpty(t, ctx.Flash.ErrorMsg) } @@ -385,7 +386,7 @@ func TestDeleteTeam(t *testing.T) { Type: user_model.UserTypeOrganization, } - team := &models.Team{ + team := &organization.Team{ ID: 2, OrgID: 3, } @@ -409,5 +410,5 @@ func TestDeleteTeam(t *testing.T) { DeleteTeam(ctx) - assert.False(t, team.HasRepository(re.ID)) + assert.False(t, models.HasRepository(team, re.ID)) } diff --git a/routers/web/repo/tag.go b/routers/web/repo/tag.go index 0114704f82..7da1e36c81 100644 --- a/routers/web/repo/tag.go +++ b/routers/web/repo/tag.go @@ -10,6 +10,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" @@ -150,7 +151,7 @@ func setTagsContext(ctx *context.Context) error { ctx.Data["Users"] = users if ctx.Repo.Owner.IsOrganization() { - teams, err := models.OrgFromUser(ctx.Repo.Owner).TeamsWithAccessToRepo(ctx.Repo.Repository.ID, perm.AccessModeRead) + teams, err := organization.OrgFromUser(ctx.Repo.Owner).TeamsWithAccessToRepo(ctx.Repo.Repository.ID, perm.AccessModeRead) if err != nil { ctx.ServerError("Repo.Owner.TeamsWithAccessToRepo", err) return err diff --git a/routers/web/user/home.go b/routers/web/user/home.go index e0beb0cbee..156cf0fa64 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -17,6 +17,7 @@ import ( "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" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" @@ -74,7 +75,7 @@ func Dashboard(ctx *context.Context) { ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard") ctx.Data["PageIsDashboard"] = true ctx.Data["PageIsNews"] = true - cnt, _ := models.GetOrganizationCount(ctx, ctxUser) + cnt, _ := organization.GetOrganizationCount(ctx, ctxUser) ctx.Data["UserOrgsCount"] = cnt var uid int64 @@ -99,11 +100,11 @@ func Dashboard(ctx *context.Context) { var err error var mirrors []*repo_model.Repository if ctxUser.IsOrganization() { - var env models.AccessibleReposEnvironment + var env organization.AccessibleReposEnvironment if ctx.Org.Team != nil { - env = models.OrgFromUser(ctxUser).AccessibleTeamReposEnv(ctx.Org.Team) + env = organization.OrgFromUser(ctxUser).AccessibleTeamReposEnv(ctx.Org.Team) } else { - env, err = models.OrgFromUser(ctxUser).AccessibleReposEnv(ctx.Doer.ID) + env, err = organization.AccessibleReposEnv(ctx, organization.OrgFromUser(ctxUser), ctx.Doer.ID) if err != nil { ctx.ServerError("AccessibleReposEnv", err) return @@ -404,8 +405,8 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) { // -------------------------------------------------------------------------- // Get repository IDs where User/Org/Team has access. - var team *models.Team - var org *models.Organization + var team *organization.Team + var org *organization.Organization if ctx.Org != nil { org = ctx.Org.Organization team = ctx.Org.Team diff --git a/routers/web/user/profile.go b/routers/web/user/profile.go index db2660af61..99198e8866 100644 --- a/routers/web/user/profile.go +++ b/routers/web/user/profile.go @@ -12,6 +12,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/context" @@ -40,7 +41,7 @@ func Profile(ctx *context.Context) { } // check view permissions - if !models.IsUserVisibleToViewer(ctx.ContextUser, ctx.Doer) { + if !user_model.IsUserVisibleToViewer(ctx.ContextUser, ctx.Doer) { ctx.NotFound("user", fmt.Errorf(ctx.ContextUser.Name)) return } @@ -91,7 +92,7 @@ func Profile(ctx *context.Context) { showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID) - orgs, err := models.FindOrgs(models.FindOrgOptions{ + orgs, err := organization.FindOrgs(organization.FindOrgOptions{ UserID: ctx.ContextUser.ID, IncludePrivate: showPrivate, }) @@ -101,7 +102,7 @@ func Profile(ctx *context.Context) { } ctx.Data["Orgs"] = orgs - ctx.Data["HasOrgsVisible"] = models.HasOrgsVisible(orgs, ctx.Doer) + ctx.Data["HasOrgsVisible"] = organization.HasOrgsVisible(orgs, ctx.Doer) tab := ctx.FormString("tab") ctx.Data["TabName"] = tab diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 09871ef763..7a875e38cb 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -17,6 +17,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/base" @@ -218,7 +219,7 @@ func Organization(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsOrganization"] = true - opts := models.FindOrgOptions{ + opts := organization.FindOrgOptions{ ListOptions: db.ListOptions{ PageSize: setting.UI.Admin.UserPagingNum, Page: ctx.FormInt("page"), @@ -231,12 +232,12 @@ func Organization(ctx *context.Context) { opts.Page = 1 } - orgs, err := models.FindOrgs(opts) + orgs, err := organization.FindOrgs(opts) if err != nil { ctx.ServerError("FindOrgs", err) return } - total, err := models.CountOrgs(opts) + total, err := organization.CountOrgs(opts) if err != nil { ctx.ServerError("CountOrgs", err) return |