Skip to content

Commit 15886ce

Browse files
6543KN4CK3R
andauthored
Fixed several activation bugs (#15473) (#15685)
* Removed unneeded form tag. * Fixed typo. * Fixed NPE. * Use better error page. * Splitted GET and POST. Co-authored-by: KN4CK3R <[email protected]>
1 parent a725d31 commit 15886ce

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

routers/routes/web.go

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

471471
m.Group("/user", func() {
472472
// r.Get("/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
473-
m.Any("/activate", user.Activate, reqSignIn)
473+
m.Get("/activate", user.Activate, reqSignIn)
474+
m.Post("/activate", user.ActivatePost, reqSignIn)
474475
m.Any("/activate_email", user.ActivateEmail)
475476
m.Get("/avatar/{username}/{size}", user.Avatar)
476477
m.Get("/email2user", user.Email2User)

routers/user/auth.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,12 +1233,11 @@ func SignUpPost(ctx *context.Context) {
12331233
// Activate render activate user page
12341234
func Activate(ctx *context.Context) {
12351235
code := ctx.Query("code")
1236-
password := ctx.Query("password")
12371236

12381237
if len(code) == 0 {
12391238
ctx.Data["IsActivatePage"] = true
1240-
if ctx.User.IsActive {
1241-
ctx.Error(404)
1239+
if ctx.User == nil || ctx.User.IsActive {
1240+
ctx.NotFound("invalid user", nil)
12421241
return
12431242
}
12441243
// Resend confirmation email.
@@ -1270,6 +1269,34 @@ func Activate(ctx *context.Context) {
12701269

12711270
// if account is local account, verify password
12721271
if user.LoginSource == 0 {
1272+
ctx.Data["Code"] = code
1273+
ctx.Data["NeedsPassword"] = true
1274+
ctx.HTML(http.StatusOK, TplActivate)
1275+
return
1276+
}
1277+
1278+
handleAccountActivation(ctx, user)
1279+
}
1280+
1281+
// ActivatePost handles account activation with password check
1282+
func ActivatePost(ctx *context.Context) {
1283+
code := ctx.Query("code")
1284+
if len(code) == 0 {
1285+
ctx.Redirect(setting.AppSubURL + "/user/activate")
1286+
return
1287+
}
1288+
1289+
user := models.VerifyUserActiveCode(code)
1290+
// if code is wrong
1291+
if user == nil {
1292+
ctx.Data["IsActivateFailed"] = true
1293+
ctx.HTML(http.StatusOK, TplActivate)
1294+
return
1295+
}
1296+
1297+
// if account is local account, verify password
1298+
if user.LoginSource == 0 {
1299+
password := ctx.Query("password")
12731300
if len(password) == 0 {
12741301
ctx.Data["Code"] = code
12751302
ctx.Data["NeedsPassword"] = true
@@ -1283,6 +1310,10 @@ func Activate(ctx *context.Context) {
12831310
}
12841311
}
12851312

1313+
handleAccountActivation(ctx, user)
1314+
}
1315+
1316+
func handleAccountActivation(ctx *context.Context, user *models.User) {
12861317
user.IsActive = true
12871318
var err error
12881319
if user.Rands, err = models.GetUserSalt(); err != nil {
@@ -1291,7 +1322,7 @@ func Activate(ctx *context.Context) {
12911322
}
12921323
if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil {
12931324
if models.IsErrUserNotExist(err) {
1294-
ctx.Error(404)
1325+
ctx.NotFound("UpdateUserCols", err)
12951326
} else {
12961327
ctx.ServerError("UpdateUser", err)
12971328
}

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)