Skip to content

Commit 795b75e

Browse files
committed
Parameter DISABLE_LOCAL_USER_MANAGEMENT added
Added parameter DISABLE_LOCAL_USER_MANAGEMENT (false by default) in app.ini [service] section; when true disables local modifications of username, fullname and e-mail fields in user Settings. Author-Change-Id: IB#1105051
1 parent 95ff559 commit 795b75e

File tree

7 files changed

+32
-6
lines changed

7 files changed

+32
-6
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ EMAIL_DOMAIN_WHITELIST=
569569
DISABLE_REGISTRATION = false
570570
; Allow registration only using third-party services, it works only when DISABLE_REGISTRATION is false
571571
ALLOW_ONLY_EXTERNAL_REGISTRATION = false
572+
; Disable local user management (i.e. when user data and password comes from LDAP and should not be changed locally in gitea).
573+
DISABLE_LOCAL_USER_MANAGEMENT = false
572574
; User must sign in to view anything.
573575
REQUIRE_SIGNIN_VIEW = false
574576
; Mail notification

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ set name for unique queues. Individual queues will default to
409409
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".
410410
- `DEFAULT_ORG_MEMBER_VISIBLE`: **false** True will make the membership of the users visible when added to the organisation.
411411
- `ALLOW_ONLY_EXTERNAL_REGISTRATION`: **false** Set to true to force registration only using third-party services.
412+
- `DISABLE_LOCAL_USER_MANAGEMENT`: **false** Set to true to disable local user management in gitea (i.e. when users are managed in LDAP).
412413
- `NO_REPLY_ADDRESS`: **DOMAIN** Default value for the domain part of the user's email address in the git log if he has set KeepEmailPrivate to true.
413414
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
414415

models/user.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,24 @@ func updateUserCols(e Engine, u *User, cols ...string) error {
11481148

11491149
// UpdateUserSetting updates user's settings.
11501150
func UpdateUserSetting(u *User) error {
1151+
1152+
// Don't allow username, fullname nor email changes if local user management is disabled.
1153+
if setting.Service.DisableLocalUserManagement {
1154+
if currUser, err := GetUserByID(u.ID); err == nil {
1155+
if currUser.Name != u.Name {
1156+
return fmt.Errorf("cannot change %s username; local user management disabled", u.Name)
1157+
}
1158+
if currUser.FullName != u.FullName {
1159+
return fmt.Errorf("cannot change %s full name; local user management disabled", u.Name)
1160+
}
1161+
if currUser.Email != u.Email {
1162+
return fmt.Errorf("cannot change %s e-mail; local user management disabled", u.Name)
1163+
}
1164+
} else {
1165+
return err
1166+
}
1167+
}
1168+
11511169
if !u.IsOrganization() {
11521170
if err := checkDupEmail(x, u); err != nil {
11531171
return err

modules/auth/user_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type InstallForm struct {
5151
EnableOpenIDSignUp bool
5252
DisableRegistration bool
5353
AllowOnlyExternalRegistration bool
54+
DisableLocalUserManagement bool
5455
EnableCaptcha bool
5556
RequireSignInView bool
5657
DefaultKeepEmailPrivate bool

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Service struct {
2020
EmailDomainWhitelist []string
2121
DisableRegistration bool
2222
AllowOnlyExternalRegistration bool
23+
DisableLocalUserManagement bool
2324
ShowRegistrationButton bool
2425
ShowMilestonesDashboardPage bool
2526
RequireSignInView bool
@@ -61,6 +62,7 @@ func newService() {
6162
Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
6263
Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
6364
Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
65+
Service.DisableLocalUserManagement = sec.Key("DISABLE_LOCAL_USER_MANAGEMENT").MustBool()
6466
Service.EmailDomainWhitelist = sec.Key("EMAIL_DOMAIN_WHITELIST").Strings(",")
6567
Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!(Service.DisableRegistration || Service.AllowOnlyExternalRegistration))
6668
Service.ShowMilestonesDashboardPage = sec.Key("SHOW_MILESTONES_DASHBOARD_PAGE").MustBool(true)

routers/user/setting/profile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ const (
3434
func Profile(ctx *context.Context) {
3535
ctx.Data["Title"] = ctx.Tr("settings")
3636
ctx.Data["PageIsSettingsProfile"] = true
37+
ctx.Data["DisableLocalUserManagement"] = setting.Service.DisableLocalUserManagement
3738

3839
ctx.HTML(200, tplSettingsProfile)
3940
}
4041

4142
func handleUsernameChange(ctx *context.Context, newName string) {
4243
// Non-local users are not allowed to change their username.
43-
if len(newName) == 0 || !ctx.User.IsLocal() {
44+
if len(newName) == 0 || !ctx.User.IsLocal() || setting.Service.DisableLocalUserManagement {
4445
return
4546
}
4647

@@ -80,6 +81,7 @@ func handleUsernameChange(ctx *context.Context, newName string) {
8081
func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
8182
ctx.Data["Title"] = ctx.Tr("settings")
8283
ctx.Data["PageIsSettingsProfile"] = true
84+
ctx.Data["DisableLocalUserManagement"] = setting.Service.DisableLocalUserManagement
8385

8486
if ctx.HasError() {
8587
ctx.HTML(200, tplSettingsProfile)

templates/user/settings/profile.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
<p>{{.i18n.Tr "settings.profile_desc"}}</p>
1111
<form class="ui form" action="{{.Link}}" method="post">
1212
{{.CsrfTokenHtml}}
13-
<div class="required field {{if .Err_Name}}error{{end}}">
13+
<div class="{{if not .DisableLocalUserManagement}}required{{end}} field {{if .Err_Name}}error{{end}}">
1414
<label for="username">{{.i18n.Tr "username"}}<span class="text red hide" id="name-change-prompt"> {{.i18n.Tr "settings.change_username_prompt"}}</span></label>
15-
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if not .SignedUser.IsLocal}}disabled{{end}}>
15+
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus {{if not .DisableLocalUserManagement}}required{{end}} {{if or (not .SignedUser.IsLocal) (.DisableLocalUserManagement) }}disabled{{end}}>
1616
{{if not .SignedUser.IsLocal}}
1717
<p class="help text blue">{{$.i18n.Tr "settings.password_username_disabled"}}</p>
1818
{{end}}
1919
</div>
2020
<div class="field {{if .Err_FullName}}error{{end}}">
2121
<label for="full_name">{{.i18n.Tr "settings.full_name"}}</label>
22-
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}">
22+
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}" {{if .DisableLocalUserManagement}}readonly{{end}}>
2323
</div>
24-
<div class="required field {{if .Err_Email}}error{{end}}">
24+
<div class="{{if not .DisableLocalUserManagement}}required{{end}} field {{if .Err_Email}}error{{end}}">
2525
<label for="email">{{.i18n.Tr "email"}}</label>
26-
<input id="email" name="email" value="{{.SignedUser.Email}}">
26+
<input id="email" name="email" value="{{.SignedUser.Email}}" {{if .DisableLocalUserManagement}}readonly{{end}}>
2727
</div>
2828
<div class="inline field">
2929
<div class="ui checkbox" id="keep-email-private">

0 commit comments

Comments
 (0)