Skip to content

Commit 325add7

Browse files
authored
Add option for administrator to reset user 2FA (#14243)
* Frontend * Backend * only show 2FA-Reset option if posible
1 parent 15a475b commit 325add7

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

modules/auth/admin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type AdminEditUserForm struct {
4242
AllowImportLocal bool
4343
AllowCreateOrganization bool
4444
ProhibitLogin bool
45+
Reset2FA bool `form:"reset_2fa"`
4546
}
4647

4748
// Validate validates form fields

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,7 @@ users.delete_account = Delete User Account
21162116
users.still_own_repo = This user still owns one or more repositories. Delete or transfer these repositories first.
21172117
users.still_has_org = This user is a member of an organization. Remove the user from any organizations first.
21182118
users.deletion_success = The user account has been deleted.
2119+
users.reset_2fa = Reset 2FA
21192120

21202121
emails.email_manage_panel = User Email Management
21212122
emails.primary = Primary

routers/admin/users.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ func prepareUserInfo(ctx *context.Context) *models.User {
183183
}
184184
ctx.Data["Sources"] = sources
185185

186+
ctx.Data["TwoFactorEnabled"] = true
187+
_, err = models.GetTwoFactorByUID(u.ID)
188+
if err != nil {
189+
if !models.IsErrTwoFactorNotEnrolled(err) {
190+
ctx.InternalServerError(err)
191+
return nil
192+
}
193+
ctx.Data["TwoFactorEnabled"] = false
194+
}
195+
186196
return u
187197
}
188198

@@ -259,6 +269,19 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
259269
u.HashPassword(form.Password)
260270
}
261271

272+
if form.Reset2FA {
273+
tf, err := models.GetTwoFactorByUID(u.ID)
274+
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
275+
ctx.InternalServerError(err)
276+
return
277+
}
278+
279+
if err = models.DeleteTwoFactorByID(tf.ID, u.ID); err != nil {
280+
ctx.InternalServerError(err)
281+
return
282+
}
283+
}
284+
262285
u.LoginName = form.LoginName
263286
u.FullName = form.FullName
264287
u.Email = form.Email

templates/admin/user/edit.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@
110110
</div>
111111
{{end}}
112112

113+
{{if .TwoFactorEnabled}}
114+
<div class="ui divider"></div>
115+
<div class="inline field">
116+
<div class="ui checkbox">
117+
<label><strong>{{.i18n.Tr "admin.users.reset_2fa"}}</strong></label>
118+
<input name="reset_2fa" type="checkbox">
119+
</div>
120+
</div>
121+
{{end}}
122+
113123
<div class="ui divider"></div>
114124

115125
<div class="field">

0 commit comments

Comments
 (0)