Skip to content

Commit 2a6d3ba

Browse files
adelowotechknowlogick
authored andcommitted
Allow admin toggle forcing a password change for newly created users (#4563)
1 parent f98040a commit 2a6d3ba

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

modules/auth/admin.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
// AdminCreateUserForm form for admin to create user
1414
type AdminCreateUserForm struct {
15-
LoginType string `binding:"Required"`
16-
LoginName string
17-
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
18-
Email string `binding:"Required;Email;MaxSize(254)"`
19-
Password string `binding:"MaxSize(255)"`
20-
SendNotify bool
15+
LoginType string `binding:"Required"`
16+
LoginName string
17+
UserName string `binding:"Required;AlphaDashDot;MaxSize(35)"`
18+
Email string `binding:"Required;Email;MaxSize(254)"`
19+
Password string `binding:"MaxSize(255)"`
20+
SendNotify bool
21+
MustChangePassword bool
2122
}
2223

2324
// Validate validates form fields

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ sign_up_now = Need an account? Register now.
206206
sign_up_successful = Account was successfully created.
207207
confirmation_mail_sent_prompt = A new confirmation email has been sent to <b>%s</b>. Please check your inbox within the next %s to complete the registration process.
208208
must_change_password = Update your password
209+
allow_password_change = Require user to change password (recommended)
209210
reset_password_mail_sent_prompt = A confirmation email has been sent to <b>%s</b>. Please check your inbox within the next %s to complete the password reset process.
210211
active_your_account = Activate Your Account
211212
account_activated = Account has been activated

routers/admin/users.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
8282
Passwd: form.Password,
8383
IsActive: true,
8484
LoginType: models.LoginPlain,
85-
MustChangePassword: true,
85+
MustChangePassword: form.MustChangePassword,
8686
}
8787

8888
if len(form.LoginType) > 0 {

routers/admin/users_test.go

+44-6
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ func TestNewUserPost_MustChangePassword(t *testing.T) {
2929
email := "gitea@gitea.io"
3030

3131
form := auth.AdminCreateUserForm{
32-
LoginType: "local",
33-
LoginName: "local",
34-
UserName: username,
35-
Email: email,
36-
Password: "xxxxxxxx",
37-
SendNotify: false,
32+
LoginType: "local",
33+
LoginName: "local",
34+
UserName: username,
35+
Email: email,
36+
Password: "xxxxxxxx",
37+
SendNotify: false,
38+
MustChangePassword: true,
3839
}
3940

4041
NewUserPost(ctx, form)
@@ -48,3 +49,40 @@ func TestNewUserPost_MustChangePassword(t *testing.T) {
4849
assert.Equal(t, email, u.Email)
4950
assert.True(t, u.MustChangePassword)
5051
}
52+
53+
func TestNewUserPost_MustChangePasswordFalse(t *testing.T) {
54+
55+
models.PrepareTestEnv(t)
56+
ctx := test.MockContext(t, "admin/users/new")
57+
58+
u := models.AssertExistsAndLoadBean(t, &models.User{
59+
IsAdmin: true,
60+
ID: 2,
61+
}).(*models.User)
62+
63+
ctx.User = u
64+
65+
username := "gitea"
66+
email := "gitea@gitea.io"
67+
68+
form := auth.AdminCreateUserForm{
69+
LoginType: "local",
70+
LoginName: "local",
71+
UserName: username,
72+
Email: email,
73+
Password: "xxxxxxxx",
74+
SendNotify: false,
75+
MustChangePassword: false,
76+
}
77+
78+
NewUserPost(ctx, form)
79+
80+
assert.NotEmpty(t, ctx.Flash.SuccessMsg)
81+
82+
u, err := models.GetUserByName(username)
83+
84+
assert.NoError(t, err)
85+
assert.Equal(t, username, u.Name)
86+
assert.Equal(t, email, u.Email)
87+
assert.False(t, u.MustChangePassword)
88+
}

templates/admin/user/new.tmpl

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<input id="password" name="password" type="password" value="{{.password}}" {{if eq .login_type "0-0"}}required{{end}}>
4343
</div>
4444

45+
<div class="inline field">
46+
<div class="ui checkbox">
47+
<label><strong>{{.i18n.Tr "auth.allow_password_change" }}</strong></label>
48+
<input name="must_change_password" type="checkbox" checked>
49+
</div>
50+
</div>
51+
4552
<!-- Send register notify e-mail -->
4653
{{if .CanSendEmail}}
4754
<div class="inline field">

0 commit comments

Comments
 (0)