From 6dc2f401c9d4d07ca672d4f3a304799e70364f1b Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Wed, 30 Jan 2019 18:04:14 +0100 Subject: [PATCH] Don't discard the value of DISABLE_REGULAR_ORG_CREATION (#5886) * Consider the configuration value of DISABLE_REGULAR_ORG_CREATION when creating a user --- models/user.go | 1 + models/user_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/models/user.go b/models/user.go index 4ab78ec04..5eb9db1e5 100644 --- a/models/user.go +++ b/models/user.go @@ -814,6 +814,7 @@ func CreateUser(u *User) (err error) { u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization u.MaxRepoCreation = -1 u.Theme = setting.UI.DefaultTheme + u.AllowCreateOrganization = !setting.Admin.DisableRegularOrgCreation if _, err = sess.Insert(u); err != nil { return err diff --git a/models/user_test.go b/models/user_test.go index 9d011f32a..35efc3959 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -213,3 +213,47 @@ func TestDisplayName(t *testing.T) { assert.NotEqual(t, len(strings.TrimSpace(displayName)), 0) } } + +func TestCreateUser(t *testing.T) { + user := &User{ + Name: "GiteaBot", + Email: "GiteaBot@gitea.io", + Passwd: ";p['////..-++']", + IsAdmin: false, + Theme: setting.UI.DefaultTheme, + MustChangePassword: false, + } + + assert.NoError(t, CreateUser(user)) + + assert.NoError(t, DeleteUser(user)) +} + +func TestCreateUser_Issue5882(t *testing.T) { + + // Init settings + _ = setting.Admin + + passwd := ".//.;1;;//.,-=_" + + tt := []struct { + user *User + disableOrgCreation bool + }{ + {&User{Name: "GiteaBot", Email: "GiteaBot@gitea.io", Passwd: passwd, MustChangePassword: false}, false}, + {&User{Name: "GiteaBot2", Email: "GiteaBot2@gitea.io", Passwd: passwd, MustChangePassword: false}, true}, + } + + for _, v := range tt { + setting.Admin.DisableRegularOrgCreation = v.disableOrgCreation + + assert.NoError(t, CreateUser(v.user)) + + u, err := GetUserByEmail(v.user.Email) + assert.NoError(t, err) + + assert.Equal(t, !u.AllowCreateOrganization, v.disableOrgCreation) + + assert.NoError(t, DeleteUser(v.user)) + } +}