Skip to content

feat: Able to disable non-admin to create new organization #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,14 @@ func runWeb(ctx *cli.Context) error {

// ***** START: Organization *****
m.Group("/org", func() {
m.Get("/create", org.Create)
m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
m.Group("", func() {
m.Get("/create", org.Create)
m.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
}, func(ctx *context.Context) {
if !ctx.User.CanCreateOrganization() {
ctx.NotFound()
}
})

m.Group("/:org", func() {
m.Get("/dashboard", user.Dashboard)
Expand Down
2 changes: 2 additions & 0 deletions conf/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ ISSUE_INDEXER_PATH = indexers/issues.bleve
UPDATE_BUFFER_LEN = 20

[admin]
; Disable regular (non-admin) users to create organizations
DISABLE_REGULAR_ORG_CREATION = false

[security]
; Whether the installer is disabled
Expand Down
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (u *User) CanCreateRepo() bool {

// CanCreateOrganization returns true if user can create organisation.
func (u *User) CanCreateOrganization() bool {
return u.IsAdmin || u.AllowCreateOrganization
return u.IsAdmin || (u.AllowCreateOrganization && !setting.Admin.DisableRegularOrgCreation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's u.AllowCreateOrganization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen shot 2017-02-14 at 4 35 46 pm

last item.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Admin can disable permission of a user. so hide this item if DISABLE_REGULAR_ORG_CREATION as true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I think maybe you could let this option disabled and default value is setting.Admin.DisableRegularOrgCreation when setting.Admin.DisableRegularOrgCreation is false and change https://github.com/go-gitea/gitea/blob/master/models/user.go#L655 to the UI form's value?

Copy link
Member Author

@appleboy appleboy Feb 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value as true makes sense for me. Admin needs to update all user setting if you set DisableRegularOrgCreation as false again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But even that, it should be the value on the UI?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. That's update user.

}

// CanEditGitHook returns true if user can edit Git hooks.
Expand Down
21 changes: 21 additions & 0 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package models
import (
"testing"

"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)

Expand All @@ -17,3 +19,22 @@ func TestGetUserEmailsByNames(t *testing.T) {
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user9"}))
assert.Equal(t, []string{"[email protected]", "[email protected]"}, GetUserEmailsByNames([]string{"user8", "user5"}))
}

func TestCanCreateOrganization(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())

admin := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
assert.True(t, admin.CanCreateOrganization())

user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
assert.True(t, user.CanCreateOrganization())
// Disable user create organization permission.
user.AllowCreateOrganization = false
assert.False(t, user.CanCreateOrganization())

setting.Admin.DisableRegularOrgCreation = true
user.AllowCreateOrganization = true
assert.True(t, admin.CanCreateOrganization())
assert.False(t, user.CanCreateOrganization())

}
7 changes: 7 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ var (
FileExtensions: strings.Split(".md,.markdown,.mdown,.mkd", ","),
}

// Admin settings
Admin struct {
DisableRegularOrgCreation bool
}

// Picture settings
AvatarUploadPath string
GravatarSource string
Expand Down Expand Up @@ -855,6 +860,8 @@ please consider changing to GITEA_CUSTOM`)
log.Fatal(4, "Failed to map UI settings: %v", err)
} else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
log.Fatal(4, "Failed to map Markdown settings: %v", err)
} else if err = Cfg.Section("admin").MapTo(&Admin); err != nil {
log.Fatal(4, "Fail to map Admin settings: %v", err)
} else if err = Cfg.Section("cron").MapTo(&Cron); err != nil {
log.Fatal(4, "Failed to map Cron settings: %v", err)
} else if err = Cfg.Section("git").MapTo(&Git); err != nil {
Expand Down
1 change: 1 addition & 0 deletions routers/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func EditUser(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminUsers"] = true
ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation

prepareUserInfo(ctx)
if ctx.Written() {
Expand Down
2 changes: 2 additions & 0 deletions templates/admin/user/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@
<input name="allow_import_local" type="checkbox" {{if .User.CanImportLocal}}checked{{end}}>
</div>
</div>
{{if not .DisableRegularOrgCreation}}
<div class="inline field">
<div class="ui checkbox">
<label><strong>{{.i18n.Tr "admin.users.allow_create_organization"}}</strong></label>
<input name="allow_create_organization" type="checkbox" {{if .User.CanCreateOrganization}}checked{{end}}>
</div>
</div>
{{end}}

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

Expand Down