From 660310cd6d727ea3e99e148ae02f383206ff4e52 Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Fri, 1 Nov 2019 21:38:22 +0100 Subject: [PATCH 1/6] Add possibility to global disable repo units. --- custom/conf/app.ini.sample | 3 ++ models/repo.go | 42 +++++++++++++++++++++++ models/unit.go | 50 +++++++++++++++++++++++++++- modules/setting/repository.go | 2 ++ options/locale/locale_en-US.ini | 1 + routers/api/v1/repo/repo.go | 10 +++--- routers/repo/setting.go | 10 +++--- routers/routes/routes.go | 5 +++ routers/user/home.go | 12 +++++++ templates/base/head_navbar.tmpl | 4 +++ templates/repo/settings/options.tmpl | 28 ++++++++++++++++ 11 files changed, 156 insertions(+), 11 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 33cd0506ed46..eecdf6839f60 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -39,6 +39,9 @@ ACCESS_CONTROL_ALLOW_ORIGIN = USE_COMPAT_SSH_URI = false ; Close issues as long as a commit on any branch marks it as fixed DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false +; Comma-separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki +; Note: Setting this will not disable units on existing repositories, use some script if you want to do that. +DISABLED_REPO_UNITS = [repository.editor] ; List of file extensions for which lines should be wrapped in the CodeMirror editor diff --git a/models/repo.go b/models/repo.go index 7945cb309d34..0a82b50646e0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -124,6 +124,7 @@ func loadRepoConfig() { // NewRepoContext creates a new repository context func NewRepoContext() { loadRepoConfig() + loadUnitConfig() RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp")) } @@ -764,6 +765,47 @@ func (repo *Repository) CanUserDelete(user *User) (bool, error) { return false, nil } +// allowEnableUnit check if unit can't be enabled due to being global disabled +func (repo *Repository) allowEnableUnit(ut UnitType, isAdmin bool) bool { + if isAdmin { + // Site admin can activate even if global disabled + return true + } + if repo.UnitEnabled(ut) { + // If unit already active, it can be kept active + return true + } + if ut.UnitGlobalDisabled() { + return false + } + return true +} + +// AllowEnableInternalWiki returns true if the enabling internal wiki can be allowed (not global disabled) +func (repo *Repository) AllowEnableInternalWiki(isAdmin bool) bool { + return repo.allowEnableUnit(UnitTypeWiki, isAdmin) +} + +// AllowEnableExternalWiki returns true if the enabling external wiki can be allowed (not global disabled) +func (repo *Repository) AllowEnableExternalWiki(isAdmin bool) bool { + return repo.allowEnableUnit(UnitTypeExternalWiki, isAdmin) +} + +// AllowEnableInternalIssues returns true if the enabling internal issues can be allowed (not global disabled) +func (repo *Repository) AllowEnableInternalIssues(isAdmin bool) bool { + return repo.allowEnableUnit(UnitTypeIssues, isAdmin) +} + +// AllowEnableExternalTracker returns true if the enabling internal wiki can be allowed (not global disabled) +func (repo *Repository) AllowEnableExternalTracker(isAdmin bool) bool { + return repo.allowEnableUnit(UnitTypeExternalTracker, isAdmin) +} + +// AllowEnablePulls returns true if the enabling internal wiki can be allowed (not global disabled and possible for repo) +func (repo *Repository) AllowEnablePulls(isAdmin bool) bool { + return repo.CanEnablePulls() && repo.allowEnableUnit(UnitTypePullRequests, isAdmin) +} + // CanEnablePulls returns true if repository meets the requirements of accepting pulls. func (repo *Repository) CanEnablePulls() bool { return !repo.IsMirror && !repo.IsEmpty diff --git a/models/unit.go b/models/unit.go index 9f5c8d3cbbf6..b5358219e154 100644 --- a/models/unit.go +++ b/models/unit.go @@ -9,6 +9,7 @@ import ( "strings" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" ) // UnitType is Unit's Type @@ -83,8 +84,55 @@ var ( UnitTypeCode, UnitTypeReleases, } + + // DisabledRepoUnits contains the units that have been globally disabled + DisabledRepoUnits = []UnitType{} ) +func loadUnitConfig() { + DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...) + // Check that must units are not disabled + for i, disabledU := range DisabledRepoUnits { + if !disabledU.CanDisable() { + log.Warn("Not allowed to global disable unit %s", disabledU.String()) + DisabledRepoUnits = append(DisabledRepoUnits[:i], DisabledRepoUnits[i+1:]...) + } + } + // Remove disabled units from default units + for _, disabledU := range DisabledRepoUnits { + for i, defaultU := range DefaultRepoUnits { + if defaultU == disabledU { + DefaultRepoUnits = append(DefaultRepoUnits[:i], DefaultRepoUnits[i+1:]...) + } + } + } +} + +// IsUnitGlobalDisabled checks if unit type is global disabled +func IsUnitGlobalDisabled(u UnitType) bool { + for _, ud := range DisabledRepoUnits { + if u == ud { + return true + } + } + return false +} + +// UnitGlobalDisabled checks if unit type is global disabled +func (u *UnitType) UnitGlobalDisabled() bool { + return IsUnitGlobalDisabled(*u) +} + +// CanDisable returns if this unit type could be disabled. +func (u *UnitType) CanDisable() bool { + for _, mu := range MustRepoUnits { + if *u == mu { + return false + } + } + return true +} + // Unit is a section of one repository type Unit struct { Type UnitType @@ -96,7 +144,7 @@ type Unit struct { // CanDisable returns if this unit could be disabled. func (u *Unit) CanDisable() bool { - return true + return u.Type.CanDisable() } // IsLessThan compares order of two units diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 3e183b6c98ff..c90944aebd08 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -35,6 +35,7 @@ var ( AccessControlAllowOrigin string UseCompatSSHURI bool DefaultCloseIssuesViaCommitsInAnyBranch bool + DisabledRepoUnits []string // Repository editor settings Editor struct { @@ -89,6 +90,7 @@ var ( AccessControlAllowOrigin: "", UseCompatSSHURI: false, DefaultCloseIssuesViaCommitsInAnyBranch: false, + DisabledRepoUnits: []string{}, // Repository editor settings Editor: struct { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 4d1af69db53f..8007e410e047 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -613,6 +613,7 @@ stargazers = Stargazers forks = Forks pick_reaction = Pick your reaction reactions_more = and %d more +unit_disabled = The site administrator has disabled this repository section. archive.title = This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests. archive.issue.nocomment = This repo is archived. You cannot comment on issues. diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 422bb0ac3a2e..c15245d80c98 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -673,7 +673,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { units = append(units, *unit) } } else if *opts.HasIssues { - if opts.ExternalTracker != nil { + if opts.ExternalTracker != nil && repo.AllowEnableExternalTracker(ctx.User.IsAdmin) { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) { @@ -696,7 +696,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle, }, }) - } else { + } else if repo.AllowEnableInternalIssues(ctx.User.IsAdmin) { // Default to built-in tracker var config *models.IssuesConfig @@ -733,7 +733,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { units = append(units, *unit) } } else if *opts.HasWiki { - if opts.ExternalWiki != nil { + if opts.ExternalWiki != nil && repo.AllowEnableExternalWiki(ctx.User.IsAdmin) { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) { @@ -749,7 +749,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL, }, }) - } else { + } else if repo.AllowEnableInternalWiki(ctx.User.IsAdmin) { config := &models.UnitConfig{} units = append(units, models.RepoUnit{ RepoID: repo.ID, @@ -764,7 +764,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil { units = append(units, *unit) } - } else if *opts.HasPullRequests { + } else if *opts.HasPullRequests && repo.AllowEnablePulls(ctx.User.IsAdmin) { // We do allow setting individual PR settings through the API, so // we get the config settings and then set them // if those settings were provided in the opts. diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 663394fe3db6..67cc165928f3 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -227,7 +227,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { } if form.EnableWiki { - if form.EnableExternalWiki { + if form.EnableExternalWiki && repo.AllowEnableExternalWiki(ctx.User.IsAdmin) { if !validation.IsValidExternalURL(form.ExternalWikiURL) { ctx.Flash.Error(ctx.Tr("repo.settings.external_wiki_url_error")) ctx.Redirect(repo.Link() + "/settings") @@ -241,7 +241,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { ExternalWikiURL: form.ExternalWikiURL, }, }) - } else { + } else if repo.AllowEnableInternalWiki(ctx.User.IsAdmin) { units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypeWiki, @@ -251,7 +251,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { } if form.EnableIssues { - if form.EnableExternalTracker { + if form.EnableExternalTracker && repo.AllowEnableExternalTracker(ctx.User.IsAdmin) { if !validation.IsValidExternalURL(form.ExternalTrackerURL) { ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error")) ctx.Redirect(repo.Link() + "/settings") @@ -271,7 +271,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { ExternalTrackerStyle: form.TrackerIssueStyle, }, }) - } else { + } else if repo.AllowEnableInternalIssues(ctx.User.IsAdmin) { units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypeIssues, @@ -284,7 +284,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { } } - if form.EnablePulls { + if form.EnablePulls && repo.AllowEnablePulls(ctx.User.IsAdmin) { units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypePullRequests, diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 13a5bb27084d..ffad13cca519 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -255,6 +255,11 @@ func RegisterRoutes(m *macaron.Macaron) { } m.Use(user.GetNotificationCount) + m.Use(func(ctx *context.Context) { + ctx.Data["UnitWikiGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypeWiki) + ctx.Data["UnitIssuesGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypeIssues) + ctx.Data["UnitPullsGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypePullRequests) + }) // FIXME: not all routes need go through same middlewares. // Especially some AJAX requests, we can reduce middleware number to improve performance. diff --git a/routers/user/home.go b/routers/user/home.go index 40b3bc3fc1b8..f371a819fa1c 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -153,9 +153,21 @@ func Dashboard(ctx *context.Context) { func Issues(ctx *context.Context) { isPullList := ctx.Params(":type") == "pulls" if isPullList { + if models.IsUnitGlobalDisabled(models.UnitTypePullRequests) { + log.Debug("Pull request overview page not available due to unit global disabled.") + ctx.Status(404) + return + } + ctx.Data["Title"] = ctx.Tr("pull_requests") ctx.Data["PageIsPulls"] = true } else { + if models.IsUnitGlobalDisabled(models.UnitTypeIssues) { + log.Debug("Issues overview page not available due go unit global disabled.") + ctx.Status(404) + return + } + ctx.Data["Title"] = ctx.Tr("issues") ctx.Data["PageIsIssues"] = true } diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index fdba57d5bf6f..1db3cc5dae40 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -10,8 +10,12 @@ {{if .IsSigned}} {{.i18n.Tr "dashboard"}} + {{if not .UnitIssuesGlobalDisabled}} {{.i18n.Tr "issues"}} + {{end}} + {{if not .UnitPullsGlobalDisabled}} {{.i18n.Tr "pull_requests"}} + {{end}} {{.i18n.Tr "explore"}} {{else if .IsLandingPageHome}} {{.i18n.Tr "home"}} diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index a93efb8d2584..b1ff65e03fe9 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -133,20 +133,32 @@ {{$isWikiEnabled := or (.Repository.UnitEnabled $.UnitTypeWiki) (.Repository.UnitEnabled $.UnitTypeExternalWiki)}}
+ {{if or (.Repository.AllowEnableInternalWiki .IsAdmin) (.Repository.AllowEnableExternalWiki .IsAdmin)}}
+ {{else}} +
+ {{end}}
+ {{if .Repository.AllowEnableInternalWiki .IsAdmin}}
+ {{else}} +
+ {{end}}
+ {{if .Repository.AllowEnableExternalWiki .IsAdmin}}
+ {{else}} +
+ {{end}}
@@ -163,14 +175,22 @@ {{$isIssuesEnabled := or (.Repository.UnitEnabled $.UnitTypeIssues) (.Repository.UnitEnabled $.UnitTypeExternalTracker)}}
+ {{if or (.Repository.AllowEnableInternalIssues .IsAdmin) (.Repository.AllowEnableExternalTracker .IsAdmin)}}
+ {{else}} +
+ {{end}}
+ {{if .Repository.AllowEnableInternalIssues .IsAdmin}}
+ {{else}} +
+ {{end}}
@@ -198,7 +218,11 @@
+ {{if .Repository.AllowEnableExternalTracker .IsAdmin}}
+ {{else}} +
+ {{end}}
@@ -240,7 +264,11 @@ {{$prUnit := .Repository.MustGetUnit $.UnitTypePullRequests}}
+ {{if .Repository.AllowEnablePulls .IsAdmin}}
+ {{else}} +
+ {{end}}
From 3c0f89338c2fc3defea33f794b240a0d04bf2948 Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Sat, 4 Jan 2020 20:53:27 +0100 Subject: [PATCH 2/6] Add Default Repo Unit app.ini setting. --- custom/conf/app.ini.sample | 8 ++++++-- models/unit.go | 36 ++++++++++++++++++++++++++++++++++- modules/setting/repository.go | 2 ++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/custom/conf/app.ini.sample b/custom/conf/app.ini.sample index 2d96728149b1..261636327393 100644 --- a/custom/conf/app.ini.sample +++ b/custom/conf/app.ini.sample @@ -42,9 +42,13 @@ DEFAULT_CLOSE_ISSUES_VIA_COMMITS_IN_ANY_BRANCH = false ; Allow users to push local repositories to Gitea and have them automatically created for a user or an org ENABLE_PUSH_CREATE_USER = false ENABLE_PUSH_CREATE_ORG = false -; Comma-separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki -; Note: Setting this will not disable units on existing repositories, use some script if you want to do that. +; Comma separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki DISABLED_REPO_UNITS = +; Comma separated list of default repo units. Allowed values: repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki. +; Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility. +; External wiki and issue tracker can't be enabled by default as it requires additional settings. +; Disabled repo units will not be added to new repositories regardless if it is in the default list. +DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki [repository.editor] ; List of file extensions for which lines should be wrapped in the CodeMirror editor diff --git a/models/unit.go b/models/unit.go index b5358219e154..e232e9155c59 100644 --- a/models/unit.go +++ b/models/unit.go @@ -79,6 +79,12 @@ var ( UnitTypeWiki, } + // NotAllowedDefaultRepoUnits contains units that can't be default + NotAllowedDefaultRepoUnits = []UnitType{ + UnitTypeExternalWiki, + UnitTypeExternalTracker, + } + // MustRepoUnits contains the units could not be disabled currently MustRepoUnits = []UnitType{ UnitTypeCode, @@ -90,6 +96,24 @@ var ( ) func loadUnitConfig() { + setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...) + // Default repo units set if setting is not empty + if len(setDefaultRepoUnits) > 0 { + // MustRepoUnits required as default + DefaultRepoUnits = make([]UnitType, len(MustRepoUnits)) + copy(DefaultRepoUnits, MustRepoUnits) + for _, defaultU := range setDefaultRepoUnits { + if !defaultU.CanBeDefault() { + log.Warn("Not allowed as default unit: %s", defaultU.String()) + continue + } + // MustRepoUnits already added + if defaultU.CanDisable() { + DefaultRepoUnits = append(DefaultRepoUnits, defaultU) + } + } + } + DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...) // Check that must units are not disabled for i, disabledU := range DisabledRepoUnits { @@ -123,7 +147,7 @@ func (u *UnitType) UnitGlobalDisabled() bool { return IsUnitGlobalDisabled(*u) } -// CanDisable returns if this unit type could be disabled. +// CanDisable checks if this unit type can be disabled. func (u *UnitType) CanDisable() bool { for _, mu := range MustRepoUnits { if *u == mu { @@ -133,6 +157,16 @@ func (u *UnitType) CanDisable() bool { return true } +// CanBeDefault checks if the unit type can be a default repo unit +func (u *UnitType) CanBeDefault() bool { + for _, nadU := range NotAllowedDefaultRepoUnits { + if *u == nadU { + return false + } + } + return true +} + // Unit is a section of one repository type Unit struct { Type UnitType diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 3bb03c7db1a0..807b29b2d886 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -38,6 +38,7 @@ var ( EnablePushCreateUser bool EnablePushCreateOrg bool DisabledRepoUnits []string + DefaultRepoUnits []string // Repository editor settings Editor struct { @@ -100,6 +101,7 @@ var ( EnablePushCreateUser: false, EnablePushCreateOrg: false, DisabledRepoUnits: []string{}, + DefaultRepoUnits: []string{}, // Repository editor settings Editor: struct { From 70b39b917ed87249703be7e1d844a82a30d6dfbf Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Thu, 9 Jan 2020 07:19:10 +0100 Subject: [PATCH 3/6] Hide units --- models/unit.go | 16 +++++++++++ templates/repo/settings/options.tmpl | 42 ++++++++++++++-------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/models/unit.go b/models/unit.go index e232e9155c59..a37f4cb4cbca 100644 --- a/models/unit.go +++ b/models/unit.go @@ -167,6 +167,22 @@ func (u *UnitType) CanBeDefault() bool { return true } +// GetContradictingUnits return units that can't be active at same time as this unit +func (u *UnitType) GetContradictingUnits() []UnitType { + switch *u { + case UnitTypeIssues: + return []UnitType{UnitTypeExternalTracker} + case UnitTypeExternalTracker: + return []UnitType{UnitTypeIssues} + case UnitTypeWiki: + return []UnitType{UnitTypeExternalWiki} + case UnitTypeExternalWiki: + return []UnitType{UnitTypeWiki} + default: + return nil + } +} + // Unit is a section of one repository type Unit struct { Type UnitType diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index 5d92de972fe4..f805ff44481d 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -144,10 +144,10 @@ {{$isWikiEnabled := or (.Repository.UnitEnabled $.UnitTypeWiki) (.Repository.UnitEnabled $.UnitTypeExternalWiki)}}
- {{if or (.Repository.AllowEnableInternalWiki .IsAdmin) (.Repository.AllowEnableExternalWiki .IsAdmin)}} -
+ {{if and ($.UnitTypeWiki.UnitGlobalDisabled) ($.UnitTypeExternalWiki.UnitGlobalDisabled)}} +
{{else}} -
+
{{end}} @@ -155,20 +155,20 @@
- {{if .Repository.AllowEnableInternalWiki .IsAdmin}} -
- {{else}} + {{if $.UnitTypeWiki.UnitGlobalDisabled}}
+ {{else}} +
{{end}}
- {{if .Repository.AllowEnableExternalWiki .IsAdmin}} -
- {{else}} + {{if $.UnitTypeWiki.UnitGlobalDisabled}}
+ {{else}} +
{{end}} @@ -186,10 +186,10 @@ {{$isIssuesEnabled := or (.Repository.UnitEnabled $.UnitTypeIssues) (.Repository.UnitEnabled $.UnitTypeExternalTracker)}}
- {{if or (.Repository.AllowEnableInternalIssues .IsAdmin) (.Repository.AllowEnableExternalTracker .IsAdmin)}} -
- {{else}} + {{if or ($.UnitTypeIssues.UnitGlobalDisabled) ($.UnitTypeExternalTracker.UnitGlobalDisabled)}}
+ {{else}} +
{{end}} @@ -197,10 +197,10 @@
- {{if .Repository.AllowEnableInternalIssues .IsAdmin}} -
- {{else}} + {{if $.UnitTypeIssues.UnitGlobalDisabled}}
+ {{else}} +
{{end}} @@ -229,10 +229,10 @@
- {{if .Repository.AllowEnableExternalTracker .IsAdmin}} -
- {{else}} + {{if $.UnitTypeExternalWiki.UnitGlobalDisabled}}
+ {{else}} +
{{end}} @@ -275,10 +275,10 @@ {{$prUnit := .Repository.MustGetUnit $.UnitTypePullRequests}}
- {{if .Repository.AllowEnablePulls .IsAdmin}} -
- {{else}} + {{if $.UnitTypePullRequests.UnitGlobalDisabled}}
+ {{else}} +
{{end}} From ddd01f6a21b6db0a3319f94977be0f073e29a44a Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Sun, 12 Jan 2020 22:16:51 +0100 Subject: [PATCH 4/6] Hide disabled repo units --- models/repo.go | 51 ++-------- models/repo_unit.go | 13 ++- models/unit.go | 9 +- routers/api/v1/repo/repo.go | 140 +++++++++++++-------------- routers/repo/setting.go | 125 +++++++++++++----------- routers/routes/routes.go | 6 +- routers/user/home.go | 4 +- templates/repo/settings/options.tmpl | 14 +-- 8 files changed, 168 insertions(+), 194 deletions(-) diff --git a/models/repo.go b/models/repo.go index 06e0774511f2..20b440065871 100644 --- a/models/repo.go +++ b/models/repo.go @@ -406,6 +406,7 @@ func (repo *Repository) getUnits(e Engine) (err error) { } repo.Units, err = getUnitsByRepoID(e, repo.ID) + log.Trace("repo.Units: %-+v", repo.Units) return err } @@ -814,47 +815,6 @@ func (repo *Repository) CanUserDelete(user *User) (bool, error) { return false, nil } -// allowEnableUnit check if unit can't be enabled due to being global disabled -func (repo *Repository) allowEnableUnit(ut UnitType, isAdmin bool) bool { - if isAdmin { - // Site admin can activate even if global disabled - return true - } - if repo.UnitEnabled(ut) { - // If unit already active, it can be kept active - return true - } - if ut.UnitGlobalDisabled() { - return false - } - return true -} - -// AllowEnableInternalWiki returns true if the enabling internal wiki can be allowed (not global disabled) -func (repo *Repository) AllowEnableInternalWiki(isAdmin bool) bool { - return repo.allowEnableUnit(UnitTypeWiki, isAdmin) -} - -// AllowEnableExternalWiki returns true if the enabling external wiki can be allowed (not global disabled) -func (repo *Repository) AllowEnableExternalWiki(isAdmin bool) bool { - return repo.allowEnableUnit(UnitTypeExternalWiki, isAdmin) -} - -// AllowEnableInternalIssues returns true if the enabling internal issues can be allowed (not global disabled) -func (repo *Repository) AllowEnableInternalIssues(isAdmin bool) bool { - return repo.allowEnableUnit(UnitTypeIssues, isAdmin) -} - -// AllowEnableExternalTracker returns true if the enabling internal wiki can be allowed (not global disabled) -func (repo *Repository) AllowEnableExternalTracker(isAdmin bool) bool { - return repo.allowEnableUnit(UnitTypeExternalTracker, isAdmin) -} - -// AllowEnablePulls returns true if the enabling internal wiki can be allowed (not global disabled and possible for repo) -func (repo *Repository) AllowEnablePulls(isAdmin bool) bool { - return repo.CanEnablePulls() && repo.allowEnableUnit(UnitTypePullRequests, isAdmin) -} - // CanEnablePulls returns true if repository meets the requirements of accepting pulls. func (repo *Repository) CanEnablePulls() bool { return !repo.IsMirror && !repo.IsEmpty @@ -1777,14 +1737,19 @@ func UpdateRepositoryUpdatedTime(repoID int64, updateTime time.Time) error { } // UpdateRepositoryUnits updates a repository's units -func UpdateRepositoryUnits(repo *Repository, units []RepoUnit) (err error) { +func UpdateRepositoryUnits(repo *Repository, units []RepoUnit, deleteUnitTypes []UnitType) (err error) { sess := x.NewSession() defer sess.Close() if err = sess.Begin(); err != nil { return err } - if _, err = sess.Where("repo_id = ?", repo.ID).Delete(new(RepoUnit)); err != nil { + // Delete existing settings of units before adding again + for _, u := range units { + deleteUnitTypes = append(deleteUnitTypes, u.Type) + } + + if _, err = sess.Where("repo_id = ?", repo.ID).In("type", deleteUnitTypes).Delete(new(RepoUnit)); err != nil { return err } diff --git a/models/repo_unit.go b/models/repo_unit.go index a6162a65e516..ec680c395e09 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -170,5 +170,16 @@ func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig { } func getUnitsByRepoID(e Engine, repoID int64) (units []*RepoUnit, err error) { - return units, e.Where("repo_id = ?", repoID).Find(&units) + var tmpUnits []*RepoUnit + if err := e.Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil { + return nil, err + } + + for _, u := range tmpUnits { + if !u.Type.UnitGlobalDisabled() { + units = append(units, u) + } + } + + return units, nil } diff --git a/models/unit.go b/models/unit.go index a37f4cb4cbca..0dcd8fa48dca 100644 --- a/models/unit.go +++ b/models/unit.go @@ -132,8 +132,8 @@ func loadUnitConfig() { } } -// IsUnitGlobalDisabled checks if unit type is global disabled -func IsUnitGlobalDisabled(u UnitType) bool { +// UnitGlobalDisabled checks if unit type is global disabled +func (u UnitType) UnitGlobalDisabled() bool { for _, ud := range DisabledRepoUnits { if u == ud { return true @@ -142,11 +142,6 @@ func IsUnitGlobalDisabled(u UnitType) bool { return false } -// UnitGlobalDisabled checks if unit type is global disabled -func (u *UnitType) UnitGlobalDisabled() bool { - return IsUnitGlobalDisabled(*u) -} - // CanDisable checks if this unit type can be disabled. func (u *UnitType) CanDisable() bool { for _, mu := range MustRepoUnits { diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 345944b13c88..cca284df0985 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -726,25 +726,10 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { repo := ctx.Repo.Repository var units []models.RepoUnit + var deleteUnitTypes []models.UnitType - for _, tp := range models.MustRepoUnits { - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: tp, - Config: new(models.UnitConfig), - }) - } - - if opts.HasIssues == nil { - // If HasIssues setting not touched, rewrite existing repo unit - if unit, err := repo.GetUnit(models.UnitTypeIssues); err == nil { - units = append(units, *unit) - } else if unit, err := repo.GetUnit(models.UnitTypeExternalTracker); err == nil { - units = append(units, *unit) - } - } else if *opts.HasIssues { - if opts.ExternalTracker != nil && repo.AllowEnableExternalTracker(ctx.User.IsAdmin) { - + if opts.HasIssues != nil { + if *opts.HasIssues && opts.ExternalTracker != nil && !models.UnitTypeExternalTracker.UnitGlobalDisabled() { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) { err := fmt.Errorf("External tracker URL not valid") @@ -766,7 +751,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle, }, }) - } else if repo.AllowEnableInternalIssues(ctx.User.IsAdmin) { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues) + } else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() { // Default to built-in tracker var config *models.IssuesConfig @@ -792,19 +778,19 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { Type: models.UnitTypeIssues, Config: config, }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker) + } else if !*opts.HasIssues { + if !models.UnitTypeExternalTracker.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker) + } + if !models.UnitTypeIssues.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues) + } } } - if opts.HasWiki == nil { - // If HasWiki setting not touched, rewrite existing repo unit - if unit, err := repo.GetUnit(models.UnitTypeWiki); err == nil { - units = append(units, *unit) - } else if unit, err := repo.GetUnit(models.UnitTypeExternalWiki); err == nil { - units = append(units, *unit) - } - } else if *opts.HasWiki { - if opts.ExternalWiki != nil && repo.AllowEnableExternalWiki(ctx.User.IsAdmin) { - + if opts.HasWiki != nil { + if *opts.HasWiki && opts.ExternalWiki != nil && !models.UnitTypeExternalWiki.UnitGlobalDisabled() { // Check that values are valid if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) { err := fmt.Errorf("External wiki URL not valid") @@ -819,64 +805,72 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL, }, }) - } else if repo.AllowEnableInternalWiki(ctx.User.IsAdmin) { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki) + } else if *opts.HasWiki && opts.ExternalWiki == nil && !models.UnitTypeWiki.UnitGlobalDisabled() { config := &models.UnitConfig{} units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypeWiki, Config: config, }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki) + } else if !*opts.HasWiki { + if !models.UnitTypeExternalWiki.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki) + } + if !models.UnitTypeWiki.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki) + } } } - if opts.HasPullRequests == nil { - // If HasPullRequest setting not touched, rewrite existing repo unit - if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil { - units = append(units, *unit) - } - } else if *opts.HasPullRequests && repo.AllowEnablePulls(ctx.User.IsAdmin) { - // We do allow setting individual PR settings through the API, so - // we get the config settings and then set them - // if those settings were provided in the opts. - unit, err := repo.GetUnit(models.UnitTypePullRequests) - var config *models.PullRequestsConfig - if err != nil { - // Unit type doesn't exist so we make a new config file with default values - config = &models.PullRequestsConfig{ - IgnoreWhitespaceConflicts: false, - AllowMerge: true, - AllowRebase: true, - AllowRebaseMerge: true, - AllowSquash: true, + if opts.HasPullRequests != nil { + if *opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() { + // We do allow setting individual PR settings through the API, so + // we get the config settings and then set them + // if those settings were provided in the opts. + unit, err := repo.GetUnit(models.UnitTypePullRequests) + var config *models.PullRequestsConfig + if err != nil { + // Unit type doesn't exist so we make a new config file with default values + config = &models.PullRequestsConfig{ + IgnoreWhitespaceConflicts: false, + AllowMerge: true, + AllowRebase: true, + AllowRebaseMerge: true, + AllowSquash: true, + } + } else { + config = unit.PullRequestsConfig() } - } else { - config = unit.PullRequestsConfig() - } - if opts.IgnoreWhitespaceConflicts != nil { - config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts - } - if opts.AllowMerge != nil { - config.AllowMerge = *opts.AllowMerge - } - if opts.AllowRebase != nil { - config.AllowRebase = *opts.AllowRebase - } - if opts.AllowRebaseMerge != nil { - config.AllowRebaseMerge = *opts.AllowRebaseMerge - } - if opts.AllowSquash != nil { - config.AllowSquash = *opts.AllowSquash - } + if opts.IgnoreWhitespaceConflicts != nil { + config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts + } + if opts.AllowMerge != nil { + config.AllowMerge = *opts.AllowMerge + } + if opts.AllowRebase != nil { + config.AllowRebase = *opts.AllowRebase + } + if opts.AllowRebaseMerge != nil { + config.AllowRebaseMerge = *opts.AllowRebaseMerge + } + if opts.AllowSquash != nil { + config.AllowSquash = *opts.AllowSquash + } - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: models.UnitTypePullRequests, - Config: config, - }) + units = append(units, models.RepoUnit{ + RepoID: repo.ID, + Type: models.UnitTypePullRequests, + Config: config, + }) + } else if !*opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests) + } } - if err := models.UpdateRepositoryUnits(repo, units); err != nil { + if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err) return err } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 3f014c18ea0a..57520416cdca 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -205,78 +205,85 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { case "advanced": var units []models.RepoUnit + var deleteUnitTypes []models.UnitType // This section doesn't require repo_name/RepoName to be set in the form, don't show it // as an error on the UI for this action ctx.Data["Err_RepoName"] = nil - for _, tp := range models.MustRepoUnits { + if form.EnableWiki && form.EnableExternalWiki && !models.UnitTypeExternalWiki.UnitGlobalDisabled() { + if !validation.IsValidExternalURL(form.ExternalWikiURL) { + ctx.Flash.Error(ctx.Tr("repo.settings.external_wiki_url_error")) + ctx.Redirect(repo.Link() + "/settings") + return + } + + units = append(units, models.RepoUnit{ + RepoID: repo.ID, + Type: models.UnitTypeExternalWiki, + Config: &models.ExternalWikiConfig{ + ExternalWikiURL: form.ExternalWikiURL, + }, + }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki) + } else if form.EnableWiki && !form.EnableExternalWiki && !models.UnitTypeWiki.UnitGlobalDisabled() { units = append(units, models.RepoUnit{ RepoID: repo.ID, - Type: tp, + Type: models.UnitTypeWiki, Config: new(models.UnitConfig), }) - } - - if form.EnableWiki { - if form.EnableExternalWiki && repo.AllowEnableExternalWiki(ctx.User.IsAdmin) { - if !validation.IsValidExternalURL(form.ExternalWikiURL) { - ctx.Flash.Error(ctx.Tr("repo.settings.external_wiki_url_error")) - ctx.Redirect(repo.Link() + "/settings") - return - } - - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: models.UnitTypeExternalWiki, - Config: &models.ExternalWikiConfig{ - ExternalWikiURL: form.ExternalWikiURL, - }, - }) - } else if repo.AllowEnableInternalWiki(ctx.User.IsAdmin) { - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: models.UnitTypeWiki, - Config: new(models.UnitConfig), - }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki) + } else { + if !models.UnitTypeExternalWiki.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki) + } + if !models.UnitTypeWiki.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki) } } - if form.EnableIssues { - if form.EnableExternalTracker && repo.AllowEnableExternalTracker(ctx.User.IsAdmin) { - if !validation.IsValidExternalURL(form.ExternalTrackerURL) { - ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error")) - ctx.Redirect(repo.Link() + "/settings") - return - } - if len(form.TrackerURLFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(form.TrackerURLFormat) { - ctx.Flash.Error(ctx.Tr("repo.settings.tracker_url_format_error")) - ctx.Redirect(repo.Link() + "/settings") - return - } - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: models.UnitTypeExternalTracker, - Config: &models.ExternalTrackerConfig{ - ExternalTrackerURL: form.ExternalTrackerURL, - ExternalTrackerFormat: form.TrackerURLFormat, - ExternalTrackerStyle: form.TrackerIssueStyle, - }, - }) - } else if repo.AllowEnableInternalIssues(ctx.User.IsAdmin) { - units = append(units, models.RepoUnit{ - RepoID: repo.ID, - Type: models.UnitTypeIssues, - Config: &models.IssuesConfig{ - EnableTimetracker: form.EnableTimetracker, - AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime, - EnableDependencies: form.EnableIssueDependencies, - }, - }) + if form.EnableIssues && form.EnableExternalTracker && !models.UnitTypeExternalTracker.UnitGlobalDisabled() { + if !validation.IsValidExternalURL(form.ExternalTrackerURL) { + ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error")) + ctx.Redirect(repo.Link() + "/settings") + return + } + if len(form.TrackerURLFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(form.TrackerURLFormat) { + ctx.Flash.Error(ctx.Tr("repo.settings.tracker_url_format_error")) + ctx.Redirect(repo.Link() + "/settings") + return + } + units = append(units, models.RepoUnit{ + RepoID: repo.ID, + Type: models.UnitTypeExternalTracker, + Config: &models.ExternalTrackerConfig{ + ExternalTrackerURL: form.ExternalTrackerURL, + ExternalTrackerFormat: form.TrackerURLFormat, + ExternalTrackerStyle: form.TrackerIssueStyle, + }, + }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues) + } else if form.EnableIssues && !form.EnableExternalTracker && !models.UnitTypeIssues.UnitGlobalDisabled() { + units = append(units, models.RepoUnit{ + RepoID: repo.ID, + Type: models.UnitTypeIssues, + Config: &models.IssuesConfig{ + EnableTimetracker: form.EnableTimetracker, + AllowOnlyContributorsToTrackTime: form.AllowOnlyContributorsToTrackTime, + EnableDependencies: form.EnableIssueDependencies, + }, + }) + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker) + } else { + if !models.UnitTypeExternalTracker.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker) + } + if !models.UnitTypeIssues.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues) } } - if form.EnablePulls && repo.AllowEnablePulls(ctx.User.IsAdmin) { + if form.EnablePulls && !models.UnitTypePullRequests.UnitGlobalDisabled() { units = append(units, models.RepoUnit{ RepoID: repo.ID, Type: models.UnitTypePullRequests, @@ -288,9 +295,11 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { AllowSquash: form.PullsAllowSquash, }, }) + } else if !models.UnitTypePullRequests.UnitGlobalDisabled() { + deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests) } - if err := models.UpdateRepositoryUnits(repo, units); err != nil { + if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil { ctx.ServerError("UpdateRepositoryUnits", err) return } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index a794d1d2a83c..0b6664f00d10 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -263,9 +263,9 @@ func RegisterRoutes(m *macaron.Macaron) { m.Use(user.GetNotificationCount) m.Use(func(ctx *context.Context) { - ctx.Data["UnitWikiGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypeWiki) - ctx.Data["UnitIssuesGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypeIssues) - ctx.Data["UnitPullsGlobalDisabled"] = models.IsUnitGlobalDisabled(models.UnitTypePullRequests) + ctx.Data["UnitWikiGlobalDisabled"] = models.UnitTypeWiki.UnitGlobalDisabled() + ctx.Data["UnitIssuesGlobalDisabled"] = models.UnitTypeIssues.UnitGlobalDisabled() + ctx.Data["UnitPullsGlobalDisabled"] = models.UnitTypePullRequests.UnitGlobalDisabled() }) // FIXME: not all routes need go through same middlewares. diff --git a/routers/user/home.go b/routers/user/home.go index 2a0b10779c6e..f816ae5c4bf5 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -346,7 +346,7 @@ var issueReposQueryPattern = regexp.MustCompile(`^\[\d+(,\d+)*,?\]$`) func Issues(ctx *context.Context) { isPullList := ctx.Params(":type") == "pulls" if isPullList { - if models.IsUnitGlobalDisabled(models.UnitTypePullRequests) { + if models.UnitTypePullRequests.UnitGlobalDisabled() { log.Debug("Pull request overview page not available due to unit global disabled.") ctx.Status(404) return @@ -355,7 +355,7 @@ func Issues(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("pull_requests") ctx.Data["PageIsPulls"] = true } else { - if models.IsUnitGlobalDisabled(models.UnitTypeIssues) { + if models.UnitTypeIssues.UnitGlobalDisabled() { log.Debug("Issues overview page not available due go unit global disabled.") ctx.Status(404) return diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index f805ff44481d..c674fcf7f962 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -144,7 +144,7 @@ {{$isWikiEnabled := or (.Repository.UnitEnabled $.UnitTypeWiki) (.Repository.UnitEnabled $.UnitTypeExternalWiki)}}
- {{if and ($.UnitTypeWiki.UnitGlobalDisabled) ($.UnitTypeExternalWiki.UnitGlobalDisabled)}} + {{if and (.UnitTypeWiki.UnitGlobalDisabled) (.UnitTypeExternalWiki.UnitGlobalDisabled)}}
{{else}}
@@ -155,7 +155,7 @@
- {{if $.UnitTypeWiki.UnitGlobalDisabled}} + {{if .UnitTypeWiki.UnitGlobalDisabled}}
{{else}}
@@ -165,7 +165,7 @@
- {{if $.UnitTypeWiki.UnitGlobalDisabled}} + {{if .UnitTypeExternalWiki.UnitGlobalDisabled}}
{{else}}
@@ -186,7 +186,7 @@ {{$isIssuesEnabled := or (.Repository.UnitEnabled $.UnitTypeIssues) (.Repository.UnitEnabled $.UnitTypeExternalTracker)}}
- {{if or ($.UnitTypeIssues.UnitGlobalDisabled) ($.UnitTypeExternalTracker.UnitGlobalDisabled)}} + {{if and (.UnitTypeIssues.UnitGlobalDisabled) (.UnitTypeExternalTracker.UnitGlobalDisabled)}}
{{else}}
@@ -197,7 +197,7 @@
- {{if $.UnitTypeIssues.UnitGlobalDisabled}} + {{if .UnitTypeIssues.UnitGlobalDisabled}}
{{else}}
@@ -229,7 +229,7 @@
- {{if $.UnitTypeExternalWiki.UnitGlobalDisabled}} + {{if .UnitTypeExternalTracker.UnitGlobalDisabled}}
{{else}}
@@ -275,7 +275,7 @@ {{$prUnit := .Repository.MustGetUnit $.UnitTypePullRequests}}
- {{if $.UnitTypePullRequests.UnitGlobalDisabled}} + {{if .UnitTypePullRequests.UnitGlobalDisabled}}
{{else}}
From 157393c20f09714b67fbfbb5f09592e5bd2f4514 Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Sun, 12 Jan 2020 23:05:54 +0100 Subject: [PATCH 5/6] Minor fixes --- models/unit.go | 16 ---------------- models/user.go | 3 +++ routers/user/home.go | 10 ++++++++-- templates/base/head_navbar.tmpl | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/models/unit.go b/models/unit.go index 0dcd8fa48dca..bd2e6b13a627 100644 --- a/models/unit.go +++ b/models/unit.go @@ -162,22 +162,6 @@ func (u *UnitType) CanBeDefault() bool { return true } -// GetContradictingUnits return units that can't be active at same time as this unit -func (u *UnitType) GetContradictingUnits() []UnitType { - switch *u { - case UnitTypeIssues: - return []UnitType{UnitTypeExternalTracker} - case UnitTypeExternalTracker: - return []UnitType{UnitTypeIssues} - case UnitTypeWiki: - return []UnitType{UnitTypeExternalWiki} - case UnitTypeExternalWiki: - return []UnitType{UnitTypeWiki} - default: - return nil - } -} - // Unit is a section of one repository type Unit struct { Type UnitType diff --git a/models/user.go b/models/user.go index a8f2c6fd2233..8170afdf643c 100644 --- a/models/user.go +++ b/models/user.go @@ -621,6 +621,7 @@ func (u *User) GetRepositories(page, pageSize int) (err error) { } // GetRepositoryIDs returns repositories IDs where user owned and has unittypes +// Caller shall check that units is not globally disabled func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) { var ids []int64 @@ -635,6 +636,7 @@ func (u *User) GetRepositoryIDs(units ...UnitType) ([]int64, error) { } // GetOrgRepositoryIDs returns repositories IDs where user's team owned and has unittypes +// Caller shall check that units is not globally disabled func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) { var ids []int64 @@ -654,6 +656,7 @@ func (u *User) GetOrgRepositoryIDs(units ...UnitType) ([]int64, error) { } // GetAccessRepoIDs returns all repositories IDs where user's or user is a team member organizations +// Caller shall check that units is not globally disabled func (u *User) GetAccessRepoIDs(units ...UnitType) ([]int64, error) { ids, err := u.GetRepositoryIDs(units...) if err != nil { diff --git a/routers/user/home.go b/routers/user/home.go index f816ae5c4bf5..9aadce4e804f 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -157,6 +157,12 @@ func Dashboard(ctx *context.Context) { // Milestones render the user milestones page func Milestones(ctx *context.Context) { + if models.UnitTypeIssues.UnitGlobalDisabled() && models.UnitTypePullRequests.UnitGlobalDisabled() { + log.Debug("Milestones overview page not available as both issues and pull requests are globally disabled") + ctx.Status(404) + return + } + ctx.Data["Title"] = ctx.Tr("milestones") ctx.Data["PageIsMilestonesDashboard"] = true @@ -347,7 +353,7 @@ func Issues(ctx *context.Context) { isPullList := ctx.Params(":type") == "pulls" if isPullList { if models.UnitTypePullRequests.UnitGlobalDisabled() { - log.Debug("Pull request overview page not available due to unit global disabled.") + log.Debug("Pull request overview page not available as it is globally disabled.") ctx.Status(404) return } @@ -356,7 +362,7 @@ func Issues(ctx *context.Context) { ctx.Data["PageIsPulls"] = true } else { if models.UnitTypeIssues.UnitGlobalDisabled() { - log.Debug("Issues overview page not available due go unit global disabled.") + log.Debug("Issues overview page not available as it is globally disabled.") ctx.Status(404) return } diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index 0e6f521f98ab..5f1b9405d97a 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -16,7 +16,7 @@ {{if not .UnitPullsGlobalDisabled}} {{.i18n.Tr "pull_requests"}} {{end}} - {{if not .UnitIssuesGlobalDisabled}} + {{if not (and .UnitIssuesGlobalDisabled .UnitPullsGlobalDisabled)}} {{if .ShowMilestonesDashboardPage}}{{.i18n.Tr "milestones"}}{{end}} {{end}} {{.i18n.Tr "explore"}} From 9fdd5871599fa2dca1befdc937cab9f87051532c Mon Sep 17 00:00:00 2001 From: David Svantesson Date: Mon, 13 Jan 2020 21:22:20 +0100 Subject: [PATCH 6/6] Indicate disabled units in team settings. --- options/locale/locale_en-US.ini | 1 + templates/org/team/new.tmpl | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 66e4efa377a0..3ea8ec01784f 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1590,6 +1590,7 @@ team_desc_helper = Describe the purpose or role of the team. team_access_desc = Repository access team_permission_desc = Permission team_unit_desc = Allow Access to Repository Sections +team_unit_disabled = (Disabled) form.name_reserved = The organization name '%s' is reserved. form.name_pattern_not_allowed = The pattern '%s' is not allowed in an organization name. diff --git a/templates/org/team/new.tmpl b/templates/org/team/new.tmpl index c38fa4d94019..228f86824a06 100644 --- a/templates/org/team/new.tmpl +++ b/templates/org/team/new.tmpl @@ -81,10 +81,14 @@
{{range $t, $unit := $.Units}} + {{if $unit.Type.UnitGlobalDisabled}} +
+ {{else}}
+ {{end}}
- + {{$.i18n.Tr $unit.DescKey}}