Skip to content

Commit 908136c

Browse files
rjnienaber6543techknowlogick
authored
add configuration option to restrict users by default (#16256)
* add configuration option to restrict users by default * default IsRestricted permission only set on sign up setting this in the model messes with other workflows (e.g. syncing LDAP users) where the IsRestricted permission needs to be explicitly set and not overridden by a config value * fix formatting * Apply suggestions from code review * ensure newly created user is set to restricted * ensure imports are in the correct order Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 251d7f5 commit 908136c

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

custom/conf/app.example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ PATH =
652652
;; Default value for AllowCreateOrganization
653653
;; Every new user will have rights set to create organizations depending on this setting
654654
;DEFAULT_ALLOW_CREATE_ORGANIZATION = true
655+
;; Default value for IsRestricted
656+
;; Every new user will have restricted permissions depending on this setting
657+
;DEFAULT_USER_IS_RESTRICTED = false
655658
;;
656659
;; Either "public", "limited" or "private", default is "public"
657660
;; Limited is for users visible only to signed users

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ relation to port exhaustion.
502502
- `HCAPTCHA_SITEKEY`: **""**: Sign up at https://www.hcaptcha.com/ to get a sitekey for hcaptcha.
503503
- `DEFAULT_KEEP_EMAIL_PRIVATE`: **false**: By default set users to keep their email address private.
504504
- `DEFAULT_ALLOW_CREATE_ORGANIZATION`: **true**: Allow new users to create organizations by default.
505+
- `DEFAULT_USER_IS_RESTRICTED`: **false**: Give new users restricted permissions by default
505506
- `DEFAULT_ENABLE_DEPENDENCIES`: **true**: Enable this to have dependencies enabled by default.
506507
- `ALLOW_CROSS_REPOSITORY_DEPENDENCIES` : **true** Enable this to allow dependencies on issues from any repository where the user is granted access.
507508
- `ENABLE_USER_HEATMAP`: **true**: Enable this to display the heatmap on users profiles.

integrations/signup_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/setting"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/unknwon/i18n"
@@ -33,6 +34,28 @@ func TestSignup(t *testing.T) {
3334
MakeRequest(t, req, http.StatusOK)
3435
}
3536

37+
func TestSignupAsRestricted(t *testing.T) {
38+
defer prepareTestEnv(t)()
39+
40+
setting.Service.EnableCaptcha = false
41+
setting.Service.DefaultUserIsRestricted = true
42+
43+
req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
44+
"user_name": "restrictedUser",
45+
"email": "[email protected]",
46+
"password": "examplePassword!1",
47+
"retype": "examplePassword!1",
48+
})
49+
MakeRequest(t, req, http.StatusFound)
50+
51+
// should be able to view new user's page
52+
req = NewRequest(t, "GET", "/restrictedUser")
53+
MakeRequest(t, req, http.StatusOK)
54+
55+
user2 := models.AssertExistsAndLoadBean(t, &models.User{Name: "restrictedUser"}).(*models.User)
56+
assert.True(t, user2.IsRestricted)
57+
}
58+
3659
func TestSignupEmail(t *testing.T) {
3760
defer prepareTestEnv(t)()
3861

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var Service = struct {
4949
HcaptchaSitekey string
5050
DefaultKeepEmailPrivate bool
5151
DefaultAllowCreateOrganization bool
52+
DefaultUserIsRestricted bool
5253
EnableTimetracking bool
5354
DefaultEnableTimetracking bool
5455
DefaultEnableDependencies bool
@@ -134,6 +135,7 @@ func newService() {
134135
Service.HcaptchaSitekey = sec.Key("HCAPTCHA_SITEKEY").MustString("")
135136
Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
136137
Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
138+
Service.DefaultUserIsRestricted = sec.Key("DEFAULT_USER_IS_RESTRICTED").MustBool(false)
137139
Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true)
138140
if Service.EnableTimetracking {
139141
Service.DefaultEnableTimetracking = sec.Key("DEFAULT_ENABLE_TIMETRACKING").MustBool(true)

routers/web/user/auth.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,10 +1204,11 @@ func SignUpPost(ctx *context.Context) {
12041204
}
12051205

12061206
u := &models.User{
1207-
Name: form.UserName,
1208-
Email: form.Email,
1209-
Passwd: form.Password,
1210-
IsActive: !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm),
1207+
Name: form.UserName,
1208+
Email: form.Email,
1209+
Passwd: form.Password,
1210+
IsActive: !(setting.Service.RegisterEmailConfirm || setting.Service.RegisterManualConfirm),
1211+
IsRestricted: setting.Service.DefaultUserIsRestricted,
12111212
}
12121213

12131214
if !createAndHandleCreatedUser(ctx, tplSignUp, form, u, nil, false) {

0 commit comments

Comments
 (0)