Skip to content

Commit 7670c1c

Browse files
authored
Fixed several activation bugs (#15473)
* Removed unneeded form tag. * Fixed typo. * Fixed NPE. * Use better error page. * Splitted GET and POST.
1 parent ee3fb92 commit 7670c1c

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

routers/routes/web.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ func RegisterRoutes(m *web.Route) {
472472

473473
m.Group("/user", func() {
474474
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
475-
m.Any("/activate", user.Activate, reqSignIn)
475+
m.Get("/activate", user.Activate, reqSignIn)
476+
m.Post("/activate", user.ActivatePost, reqSignIn)
476477
m.Any("/activate_email", user.ActivateEmail)
477478
m.Get("/avatar/{username}/{size}", user.Avatar)
478479
m.Get("/email2user", user.Email2User)

routers/user/auth.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{
12401240
}
12411241
}
12421242

1243-
// TODO: probably we should respect 'remeber' user's choice...
1243+
// TODO: probably we should respect 'remember' user's choice...
12441244
linkAccount(ctx, user, *gothUser, true)
12451245
return // user is already created here, all redirects are handled
12461246
} else if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingLogin {
@@ -1327,12 +1327,11 @@ func handleUserCreated(ctx *context.Context, u *models.User, gothUser *goth.User
13271327
// Activate render activate user page
13281328
func Activate(ctx *context.Context) {
13291329
code := ctx.Query("code")
1330-
password := ctx.Query("password")
13311330

13321331
if len(code) == 0 {
13331332
ctx.Data["IsActivatePage"] = true
1334-
if ctx.User.IsActive {
1335-
ctx.Error(http.StatusNotFound)
1333+
if ctx.User == nil || ctx.User.IsActive {
1334+
ctx.NotFound("invalid user", nil)
13361335
return
13371336
}
13381337
// Resend confirmation email.
@@ -1364,6 +1363,34 @@ func Activate(ctx *context.Context) {
13641363

13651364
// if account is local account, verify password
13661365
if user.LoginSource == 0 {
1366+
ctx.Data["Code"] = code
1367+
ctx.Data["NeedsPassword"] = true
1368+
ctx.HTML(http.StatusOK, TplActivate)
1369+
return
1370+
}
1371+
1372+
handleAccountActivation(ctx, user)
1373+
}
1374+
1375+
// ActivatePost handles account activation with password check
1376+
func ActivatePost(ctx *context.Context) {
1377+
code := ctx.Query("code")
1378+
if len(code) == 0 {
1379+
ctx.Redirect(setting.AppSubURL + "/user/activate")
1380+
return
1381+
}
1382+
1383+
user := models.VerifyUserActiveCode(code)
1384+
// if code is wrong
1385+
if user == nil {
1386+
ctx.Data["IsActivateFailed"] = true
1387+
ctx.HTML(http.StatusOK, TplActivate)
1388+
return
1389+
}
1390+
1391+
// if account is local account, verify password
1392+
if user.LoginSource == 0 {
1393+
password := ctx.Query("password")
13671394
if len(password) == 0 {
13681395
ctx.Data["Code"] = code
13691396
ctx.Data["NeedsPassword"] = true
@@ -1377,6 +1404,10 @@ func Activate(ctx *context.Context) {
13771404
}
13781405
}
13791406

1407+
handleAccountActivation(ctx, user)
1408+
}
1409+
1410+
func handleAccountActivation(ctx *context.Context, user *models.User) {
13801411
user.IsActive = true
13811412
var err error
13821413
if user.Rands, err = models.GetUserSalt(); err != nil {
@@ -1385,7 +1416,7 @@ func Activate(ctx *context.Context) {
13851416
}
13861417
if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
13871418
if models.IsErrUserNotExist(err) {
1388-
ctx.Error(http.StatusNotFound)
1419+
ctx.NotFound("UpdateUserCols", err)
13891420
} else {
13901421
ctx.ServerError("UpdateUser", err)
13911422
}

templates/user/auth/activate.tmpl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
{{end}}
2020
{{else}}
2121
{{if .NeedsPassword}}
22-
<form class="ui form" action="{{AppSubUrl}}/user/activate" method="post">
23-
<div class="required inline field">
24-
<label for="password">{{.i18n.Tr "password"}}</label>
25-
<input id="password" name="password" type="password" autocomplete="off" required>
26-
</div>
27-
<div class="inline field">
28-
<label></label>
29-
<button class="ui green button">{{.i18n.Tr "install.confirm_password"}}</button>
30-
</div>
31-
<input id="code" name="code" type="hidden" value="{{.Code}}">
32-
</form>
22+
<div class="required inline field">
23+
<label for="password">{{.i18n.Tr "password"}}</label>
24+
<input id="password" name="password" type="password" autocomplete="off" required>
25+
</div>
26+
<div class="inline field">
27+
<label></label>
28+
<button class="ui green button">{{.i18n.Tr "install.confirm_password"}}</button>
29+
</div>
30+
<input id="code" name="code" type="hidden" value="{{.Code}}">
3331
{{else if .IsSendRegisterMail}}
3432
<p>{{.i18n.Tr "auth.confirmation_mail_sent_prompt" (.Email|Escape) .ActiveCodeLives | Str2html}}</p>
3533
{{else if .IsActivateFailed}}

0 commit comments

Comments
 (0)