diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-19 19:41:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 19:41:40 +0800 |
commit | 7a0347315995b25bcb2dca4786504fb699b5f004 (patch) | |
tree | 803dfd39286216fd0521ad16539ffd9fc5f87fc0 /routers/web | |
parent | a09b40de8d1dae7107437cfba42cee201fcd6d42 (diff) | |
download | gitea-7a0347315995b25bcb2dca4786504fb699b5f004.tar.gz gitea-7a0347315995b25bcb2dca4786504fb699b5f004.zip |
Use a standalone struct name for Organization (#17632)
* Use a standalone struct name for Organization
* recover unnecessary change
* make the code readable
* Fix template failure
* Fix template failure
* Move HasMemberWithUserID to org
* Fix test
* Remove unnecessary user type check
* Fix test
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'routers/web')
-rw-r--r-- | routers/web/org/home.go | 4 | ||||
-rw-r--r-- | routers/web/org/org.go | 4 | ||||
-rw-r--r-- | routers/web/org/setting.go | 14 | ||||
-rw-r--r-- | routers/web/org/teams.go | 6 | ||||
-rw-r--r-- | routers/web/repo/issue.go | 13 | ||||
-rw-r--r-- | routers/web/repo/issue_label.go | 2 | ||||
-rw-r--r-- | routers/web/repo/pull.go | 2 | ||||
-rw-r--r-- | routers/web/repo/repo.go | 2 | ||||
-rw-r--r-- | routers/web/repo/setting.go | 4 | ||||
-rw-r--r-- | routers/web/repo/setting_protected_branch.go | 2 | ||||
-rw-r--r-- | routers/web/repo/tag.go | 2 | ||||
-rw-r--r-- | routers/web/user/home.go | 12 | ||||
-rw-r--r-- | routers/web/user/oauth.go | 6 |
13 files changed, 39 insertions, 34 deletions
diff --git a/routers/web/org/home.go b/routers/web/org/home.go index 89bd12a18f..0752fa12cb 100644 --- a/routers/web/org/home.go +++ b/routers/web/org/home.go @@ -30,7 +30,7 @@ func Home(ctx *context.Context) { org := ctx.Org.Organization - if !models.HasOrgOrUserVisible(org, ctx.User) { + if !models.HasOrgOrUserVisible(org.AsUser(), ctx.User) { ctx.NotFound("HasOrgOrUserVisible", nil) return } @@ -140,7 +140,7 @@ func Home(ctx *context.Context) { ctx.Data["Total"] = count ctx.Data["MembersTotal"] = membersCount ctx.Data["Members"] = members - ctx.Data["Teams"] = org.Teams + ctx.Data["Teams"] = ctx.Org.Teams ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull diff --git a/routers/web/org/org.go b/routers/web/org/org.go index beba3daca4..6722f8d104 100644 --- a/routers/web/org/org.go +++ b/routers/web/org/org.go @@ -49,7 +49,7 @@ func CreatePost(ctx *context.Context) { return } - org := &models.User{ + org := &models.Organization{ Name: form.OrgName, IsActive: true, Type: models.UserTypeOrganization, @@ -75,5 +75,5 @@ func CreatePost(ctx *context.Context) { } log.Trace("Organization created: %s", org.Name) - ctx.Redirect(org.DashboardLink()) + ctx.Redirect(org.AsUser().DashboardLink()) } diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go index f27cb40838..f05dbd8bc1 100644 --- a/routers/web/org/setting.go +++ b/routers/web/org/setting.go @@ -68,7 +68,7 @@ func SettingsPost(ctx *context.Context) { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form) return - } else if err = models.ChangeUserName(org, form.Name); err != nil { + } else if err = models.ChangeUserName(org.AsUser(), form.Name); err != nil { if err == models.ErrUserNameIllegal { ctx.Data["OrgName"] = true ctx.RenderWithErr(ctx.Tr("form.illegal_username"), tplSettingsOptions, &form) @@ -100,18 +100,20 @@ func SettingsPost(ctx *context.Context) { visibilityChanged := form.Visibility != org.Visibility org.Visibility = form.Visibility - if err := models.UpdateUser(org); err != nil { + if err := models.UpdateUser(org.AsUser()); err != nil { ctx.ServerError("UpdateUser", err) return } // update forks visibility if visibilityChanged { - if err := org.GetRepositories(db.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil { + repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ + Actor: org.AsUser(), Private: true, ListOptions: db.ListOptions{Page: 1, PageSize: org.NumRepos}}) + if err != nil { ctx.ServerError("GetRepositories", err) return } - for _, repo := range org.Repos { + for _, repo := range repos { repo.OwnerName = org.Name if err := models.UpdateRepository(repo, true); err != nil { ctx.ServerError("UpdateRepository", err) @@ -134,7 +136,7 @@ func SettingsPost(ctx *context.Context) { func SettingsAvatar(ctx *context.Context) { form := web.GetForm(ctx).(*forms.AvatarForm) form.Source = forms.AvatarLocal - if err := userSetting.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil { + if err := userSetting.UpdateAvatarSetting(ctx, form, ctx.Org.Organization.AsUser()); err != nil { ctx.Flash.Error(err.Error()) } else { ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success")) @@ -145,7 +147,7 @@ func SettingsAvatar(ctx *context.Context) { // SettingsDeleteAvatar response for delete avatar on settings page func SettingsDeleteAvatar(ctx *context.Context) { - if err := ctx.Org.Organization.DeleteAvatar(); err != nil { + if err := ctx.Org.Organization.AsUser().DeleteAvatar(); err != nil { ctx.Flash.Error(err.Error()) } diff --git a/routers/web/org/teams.go b/routers/web/org/teams.go index 28ffac4dd3..ac6a42904a 100644 --- a/routers/web/org/teams.go +++ b/routers/web/org/teams.go @@ -38,13 +38,13 @@ func Teams(ctx *context.Context) { ctx.Data["Title"] = org.FullName ctx.Data["PageIsOrgTeams"] = true - for _, t := range org.Teams { + for _, t := range ctx.Org.Teams { if err := t.GetMembers(&models.SearchMembersOptions{}); err != nil { ctx.ServerError("GetMembers", err) return } } - ctx.Data["Teams"] = org.Teams + ctx.Data["Teams"] = ctx.Org.Teams ctx.HTML(http.StatusOK, tplTeams) } @@ -158,7 +158,7 @@ func TeamsAction(ctx *context.Context) { case "team": ctx.Redirect(ctx.Org.OrgLink + "/teams/" + url.PathEscape(ctx.Org.Team.LowerName)) case "home": - ctx.Redirect(ctx.Org.Organization.HomeLink()) + ctx.Redirect(ctx.Org.Organization.AsUser().HomeLink()) default: ctx.Redirect(ctx.Org.OrgLink + "/teams") } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 95363258e9..129287b96a 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -2644,13 +2644,15 @@ func handleTeamMentions(ctx *context.Context) { return } - isAdmin := false + var isAdmin bool var err error + var teams []*models.Team + var org = models.OrgFromUser(ctx.Repo.Owner) // Admin has super access. if ctx.User.IsAdmin { isAdmin = true } else { - isAdmin, err = ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) + isAdmin, err = org.IsOwnedBy(ctx.User.ID) if err != nil { ctx.ServerError("IsOwnedBy", err) return @@ -2658,19 +2660,20 @@ func handleTeamMentions(ctx *context.Context) { } if isAdmin { - if err := ctx.Repo.Owner.LoadTeams(); err != nil { + teams, err = org.LoadTeams() + if err != nil { ctx.ServerError("LoadTeams", err) return } } else { - ctx.Repo.Owner.Teams, err = ctx.Repo.Owner.GetUserTeams(ctx.User.ID) + teams, err = org.GetUserTeams(ctx.User.ID) if err != nil { ctx.ServerError("GetUserTeams", err) return } } - ctx.Data["MentionableTeams"] = ctx.Repo.Owner.Teams + ctx.Data["MentionableTeams"] = teams ctx.Data["MentionableTeamsOrg"] = ctx.Repo.Owner.Name ctx.Data["MentionableTeamsOrgAvatar"] = ctx.Repo.Owner.AvatarLink() } diff --git a/routers/web/repo/issue_label.go b/routers/web/repo/issue_label.go index b97f57175b..c6fa8c4c8c 100644 --- a/routers/web/repo/issue_label.go +++ b/routers/web/repo/issue_label.go @@ -88,7 +88,7 @@ func RetrieveLabels(ctx *context.Context) { ctx.ServerError("org.IsOwnedBy", err) return } - ctx.Org.OrgLink = org.OrganisationLink() + ctx.Org.OrgLink = org.AsUser().OrganisationLink() ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner ctx.Data["OrganizationLink"] = ctx.Org.OrgLink } diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 4337278214..d5aa480d1f 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -217,7 +217,7 @@ func ForkPost(ctx *context.Context) { // Check ownership of organization. if ctxUser.IsOrganization() { - isOwner, err := ctxUser.IsOwnedBy(ctx.User.ID) + isOwner, err := models.OrgFromUser(ctxUser).IsOwnedBy(ctx.User.ID) if err != nil { ctx.ServerError("IsOwnedBy", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 5e15415d6a..2d6189824d 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -94,7 +94,7 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User { return nil } if !ctx.User.IsAdmin { - canCreate, err := org.CanCreateOrgRepo(ctx.User.ID) + canCreate, err := models.OrgFromUser(org).CanCreateOrgRepo(ctx.User.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 641052316c..9b5f635988 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -588,7 +588,7 @@ func SettingsPost(ctx *context.Context) { } if newOwner.Type == models.UserTypeOrganization { - if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !newOwner.HasMemberWithUserID(ctx.User.ID) { + if !ctx.User.IsAdmin && newOwner.Visibility == structs.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.User.ID) { // The user shouldn't know about this organization ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil) return @@ -879,7 +879,7 @@ func AddTeamPost(ctx *context.Context) { return } - team, err := ctx.Repo.Owner.GetTeam(name) + team, err := models.OrgFromUser(ctx.Repo.Owner).GetTeam(name) if err != nil { if models.IsErrTeamNotExist(err) { ctx.Flash.Error(ctx.Tr("form.team_not_exist")) diff --git a/routers/web/repo/setting_protected_branch.go b/routers/web/repo/setting_protected_branch.go index 32105b1d43..b13dfae683 100644 --- a/routers/web/repo/setting_protected_branch.go +++ b/routers/web/repo/setting_protected_branch.go @@ -156,7 +156,7 @@ func SettingsProtectedBranch(c *context.Context) { } if c.Repo.Owner.IsOrganization() { - teams, err := c.Repo.Owner.TeamsWithAccessToRepo(c.Repo.Repository.ID, models.AccessModeRead) + teams, err := models.OrgFromUser(c.Repo.Owner).TeamsWithAccessToRepo(c.Repo.Repository.ID, models.AccessModeRead) if err != nil { c.ServerError("Repo.Owner.TeamsWithAccessToRepo", err) return diff --git a/routers/web/repo/tag.go b/routers/web/repo/tag.go index b4d268759c..4383b6214a 100644 --- a/routers/web/repo/tag.go +++ b/routers/web/repo/tag.go @@ -149,7 +149,7 @@ func setTagsContext(ctx *context.Context) error { ctx.Data["Users"] = users if ctx.Repo.Owner.IsOrganization() { - teams, err := ctx.Repo.Owner.TeamsWithAccessToRepo(ctx.Repo.Repository.ID, models.AccessModeRead) + teams, err := models.OrgFromUser(ctx.Repo.Owner).TeamsWithAccessToRepo(ctx.Repo.Repository.ID, models.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 b9f5d044fa..c0ecd0c2a0 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -47,8 +47,8 @@ func getDashboardContextUser(ctx *context.Context) *models.User { ctxUser := ctx.User orgName := ctx.Params(":org") if len(orgName) > 0 { - ctxUser = ctx.Org.Organization - ctx.Data["Teams"] = ctx.Org.Organization.Teams + ctxUser = ctx.Org.Organization.AsUser() + ctx.Data["Teams"] = ctx.Org.Teams } ctx.Data["ContextUser"] = ctxUser @@ -97,9 +97,9 @@ func Dashboard(ctx *context.Context) { if ctxUser.IsOrganization() { var env models.AccessibleReposEnvironment if ctx.Org.Team != nil { - env = ctxUser.AccessibleTeamReposEnv(ctx.Org.Team) + env = models.OrgFromUser(ctxUser).AccessibleTeamReposEnv(ctx.Org.Team) } else { - env, err = ctxUser.AccessibleReposEnv(ctx.User.ID) + env, err = models.OrgFromUser(ctxUser).AccessibleReposEnv(ctx.User.ID) if err != nil { ctx.ServerError("AccessibleReposEnv", err) return @@ -756,9 +756,9 @@ func getActiveTeamOrOrgRepoIds(ctxUser *models.User, team *models.Team, unitType var env models.AccessibleReposEnvironment if team != nil { - env = ctxUser.AccessibleTeamReposEnv(team) + env = models.OrgFromUser(ctxUser).AccessibleTeamReposEnv(team) } else { - env, err = ctxUser.AccessibleReposEnv(ctxUser.ID) + env, err = models.OrgFromUser(ctxUser).AccessibleReposEnv(ctxUser.ID) if err != nil { return nil, fmt.Errorf("AccessibleReposEnv: %v", err) } diff --git a/routers/web/user/oauth.go b/routers/web/user/oauth.go index 3210d033d3..7b1854c521 100644 --- a/routers/web/user/oauth.go +++ b/routers/web/user/oauth.go @@ -283,11 +283,11 @@ func getOAuthGroupsForUser(user *models.User) ([]string, error) { var groups []string for _, org := range orgs { groups = append(groups, org.Name) - - if err := org.LoadTeams(); err != nil { + teams, err := org.LoadTeams() + if err != nil { return nil, fmt.Errorf("LoadTeams: %v", err) } - for _, team := range org.Teams { + for _, team := range teams { if team.IsMember(user.ID) { groups = append(groups, org.Name+":"+team.LowerName) } |