Skip to content

Commit d67b278

Browse files
appleboylunny
authored andcommitted
feat: Able to disable non-admin to create new organization (#927)
1 parent 23aba52 commit d67b278

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

cmd/web.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,14 @@ func runWeb(ctx *cli.Context) error {
345345

346346
// ***** START: Organization *****
347347
m.Group("/org", func() {
348-
m.Get("/create", org.Create)
349-
m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
348+
m.Group("", func() {
349+
m.Get("/create", org.Create)
350+
m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
351+
}, func(ctx *context.Context) {
352+
if !ctx.User.CanCreateOrganization() {
353+
ctx.NotFound()
354+
}
355+
})
350356

351357
m.Group("/:org", func() {
352358
m.Get("/dashboard", user.Dashboard)

conf/app.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ ISSUE_INDEXER_PATH = indexers/issues.bleve
163163
UPDATE_BUFFER_LEN = 20
164164

165165
[admin]
166+
; Disable regular (non-admin) users to create organizations
167+
DISABLE_REGULAR_ORG_CREATION = false
166168

167169
[security]
168170
; Whether the installer is disabled

models/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (u *User) CanCreateRepo() bool {
223223

224224
// CanCreateOrganization returns true if user can create organisation.
225225
func (u *User) CanCreateOrganization() bool {
226-
return u.IsAdmin || u.AllowCreateOrganization
226+
return u.IsAdmin || (u.AllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation)
227227
}
228228

229229
// CanEditGitHook returns true if user can edit Git hooks.

models/user_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package models
77
import (
88
"testing"
99

10+
"code.gitea.io/gitea/modules/setting"
11+
1012
"github.com/stretchr/testify/assert"
1113
)
1214

@@ -17,3 +19,22 @@ func TestGetUserEmailsByNames(t *testing.T) {
1719
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user9"}))
1820
assert.Equal(t, []string{"[email protected]", "[email protected]"}, GetUserEmailsByNames([]string{"user8", "user5"}))
1921
}
22+
23+
func TestCanCreateOrganization(t *testing.T) {
24+
assert.NoError(t, PrepareTestDatabase())
25+
26+
admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
27+
assert.True(t, admin.CanCreateOrganization())
28+
29+
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
30+
assert.True(t, user.CanCreateOrganization())
31+
// Disable user create organization permission.
32+
user.AllowCreateOrganization = false
33+
assert.False(t, user.CanCreateOrganization())
34+
35+
setting.Admin.DisableRegularOrgCreation = true
36+
user.AllowCreateOrganization = true
37+
assert.True(t, admin.CanCreateOrganization())
38+
assert.False(t, user.CanCreateOrganization())
39+
40+
}

modules/setting/setting.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ var (
257257
FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","),
258258
}
259259

260+
// Admin settings
261+
Admin struct {
262+
DisableRegularOrgCreation bool
263+
}
264+
260265
// Picture settings
261266
AvatarUploadPath string
262267
GravatarSource string
@@ -855,6 +860,8 @@ please consider changing to GITEA_CUSTOM`)
855860
log.Fatal(4, "Failed to map UI settings: %v", err)
856861
} else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
857862
log.Fatal(4, "Failed to map Markdown settings: %v", err)
863+
} else if err = Cfg.Section("admin").MapTo(&Admin); err != nil {
864+
log.Fatal(4, "Fail to map Admin settings: %v", err)
858865
} else if err = Cfg.Section("cron").MapTo(&Cron); err != nil {
859866
log.Fatal(4, "Failed to map Cron settings: %v", err)
860867
} else if err = Cfg.Section("git").MapTo(&Git); err != nil {

routers/admin/users.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func EditUser(ctx *context.Context) {
158158
ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
159159
ctx.Data["PageIsAdmin"] = true
160160
ctx.Data["PageIsAdminUsers"] = true
161+
ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
161162

162163
prepareUserInfo(ctx)
163164
if ctx.Written() {

templates/admin/user/edit.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@
9797
<input name="allow_import_local" type="checkbox" {{if .User.CanImportLocal}}checked{{end}}>
9898
</div>
9999
</div>
100+
{{if not .DisableRegularOrgCreation}}
100101
<div class="inline field">
101102
<div class="ui checkbox">
102103
<label><strong>{{.i18n.Tr "admin.users.allow_create_organization"}}</strong></label>
103104
<input name="allow_create_organization" type="checkbox" {{if .User.CanCreateOrganization}}checked{{end}}>
104105
</div>
105106
</div>
107+
{{end}}
106108

107109
<div class="ui divider"></div>
108110

0 commit comments

Comments
 (0)