Skip to content

Commit e72d001

Browse files
GiteaBotCrimsonEdgeHopewxiaoguang
authored
Fix Account linking page (#33325) (#33327)
Backport #33325 by CrimsonEdgeHope Fix password form missing whilst linking account even with `ENABLE_PASSWORD_SIGNIN_FORM = true`. Remove redundant empty box in account linking sign up page when `LinkAccountMode` is true. Co-authored-by: CrimsonEdgeHope <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 8d9ea68 commit e72d001

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

routers/web/auth/linkaccount.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var tplLinkAccount base.TplName = "user/auth/link_account"
2929

3030
// LinkAccount shows the page where the user can decide to login or create a new account
3131
func LinkAccount(ctx *context.Context) {
32+
// FIXME: these common template variables should be prepared in one common function, but not just copy-paste again and again.
3233
ctx.Data["DisablePassword"] = !setting.Service.RequireExternalRegistrationPassword || setting.Service.AllowOnlyExternalRegistration
3334
ctx.Data["Title"] = ctx.Tr("link_account")
3435
ctx.Data["LinkAccountMode"] = true
@@ -43,13 +44,19 @@ func LinkAccount(ctx *context.Context) {
4344
ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
4445
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
4546
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
47+
ctx.Data["EnablePasswordSignInForm"] = setting.Service.EnablePasswordSignInForm
4648
ctx.Data["ShowRegistrationButton"] = false
4749

4850
// use this to set the right link into the signIn and signUp templates in the link_account template
4951
ctx.Data["SignInLink"] = setting.AppSubURL + "/user/link_account_signin"
5052
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/link_account_signup"
5153

5254
gothUser, ok := ctx.Session.Get("linkAccountGothUser").(goth.User)
55+
56+
// If you'd like to quickly debug the "link account" page layout, just uncomment the blow line
57+
// Don't worry, when the below line exists, the lint won't pass: ineffectual assignment to gothUser (ineffassign)
58+
// gothUser, ok = goth.User{Email: "invalid-email", Name: "."}, true // intentionally use invalid data to avoid pass the registration check
59+
5360
if !ok {
5461
// no account in session, so just redirect to the login page, then the user could restart the process
5562
ctx.Redirect(setting.AppSubURL + "/user/login")
@@ -135,6 +142,8 @@ func LinkAccountPostSignIn(ctx *context.Context) {
135142
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
136143
ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
137144
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
145+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
146+
ctx.Data["EnablePasswordSignInForm"] = setting.Service.EnablePasswordSignInForm
138147
ctx.Data["ShowRegistrationButton"] = false
139148

140149
// use this to set the right link into the signIn and signUp templates in the link_account template
@@ -223,6 +232,8 @@ func LinkAccountPostRegister(ctx *context.Context) {
223232
ctx.Data["McaptchaURL"] = setting.Service.McaptchaURL
224233
ctx.Data["CfTurnstileSitekey"] = setting.Service.CfTurnstileSitekey
225234
ctx.Data["DisableRegistration"] = setting.Service.DisableRegistration
235+
ctx.Data["AllowOnlyInternalRegistration"] = setting.Service.AllowOnlyInternalRegistration
236+
ctx.Data["EnablePasswordSignInForm"] = setting.Service.EnablePasswordSignInForm
226237
ctx.Data["ShowRegistrationButton"] = false
227238

228239
// use this to set the right link into the signIn and signUp templates in the link_account template

templates/user/auth/link_account.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
</div>
1717
</overflow-menu>
1818
<div class="ui middle very relaxed page grid">
19-
<div class="column">
19+
<div class="column tw-my-5">
20+
{{/* these styles are quite tricky but it needs to be the same as the signin page */}}
2021
<div class="ui tab {{if not .user_exists}}active{{end}}" data-tab="auth-link-signup-tab">
22+
<div class="tw-flex tw-flex-col tw-gap-4 tw-max-w-2xl tw-m-auto">
2123
{{if .AutoRegistrationFailedPrompt}}<div class="ui message">{{.AutoRegistrationFailedPrompt}}</div>{{end}}
2224
{{template "user/auth/signup_inner" .}}
25+
</div>
2326
</div>
2427
<div class="ui tab {{if .user_exists}}active{{end}}" data-tab="auth-link-signin-tab">
28+
<div class="tw-flex tw-flex-col tw-gap-4 tw-max-w-2xl tw-m-auto">
2529
{{template "user/auth/signin_inner" .}}
30+
</div>
2631
</div>
2732
</div>
2833
</div>

templates/user/auth/signin.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{{template "base/head" .}}
22
<div role="main" aria-label="{{.Title}}" class="page-content user signin{{if .LinkAccountMode}} icon{{end}}">
33
<div class="ui middle very relaxed page grid">
4+
{{/* these styles are quite tricky and should also apply to the signup and link_account pages */}}
45
<div class="column tw-flex tw-flex-col tw-gap-4 tw-max-w-2xl tw-m-auto">
56
{{template "user/auth/signin_inner" .}}
67
</div>

templates/user/auth/signup_inner.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@
5959
</div>
6060

6161
<div class="ui container fluid">
62+
{{if not .LinkAccountMode}}
6263
<div class="ui attached segment header top tw-flex tw-flex-col tw-items-center">
63-
{{if not .LinkAccountMode}}
6464
<div class="field">
6565
<span>{{ctx.Locale.Tr "auth.already_have_account"}}</span>
6666
<a href="{{AppSubUrl}}/user/login">{{ctx.Locale.Tr "auth.sign_in_now"}}</a>
6767
</div>
68-
{{end}}
6968
</div>
69+
{{end}}
7070
</div>

tests/integration/signin_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import (
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/test"
1616
"code.gitea.io/gitea/modules/translation"
17+
"code.gitea.io/gitea/modules/web"
18+
"code.gitea.io/gitea/services/context"
1719
"code.gitea.io/gitea/tests"
1820

21+
"github.com/markbates/goth"
1922
"github.com/stretchr/testify/assert"
2023
)
2124

@@ -96,6 +99,11 @@ func TestSigninWithRememberMe(t *testing.T) {
9699
func TestEnablePasswordSignInForm(t *testing.T) {
97100
defer tests.PrepareTestEnv(t)()
98101

102+
mockLinkAccount := func(ctx *context.Context) {
103+
gothUser := goth.User{Email: "invalid-email", Name: "."}
104+
_ = ctx.Session.Set("linkAccountGothUser", gothUser)
105+
}
106+
99107
t.Run("EnablePasswordSignInForm=false", func(t *testing.T) {
100108
defer tests.PrintCurrentTest(t)()
101109
defer test.MockVariableValue(&setting.Service.EnablePasswordSignInForm, false)()
@@ -106,6 +114,12 @@ func TestEnablePasswordSignInForm(t *testing.T) {
106114

107115
req = NewRequest(t, "POST", "/user/login")
108116
MakeRequest(t, req, http.StatusForbidden)
117+
118+
req = NewRequest(t, "GET", "/user/link_account")
119+
defer web.RouteMockReset()
120+
web.RouteMock(web.MockAfterMiddlewares, mockLinkAccount)
121+
resp = MakeRequest(t, req, http.StatusOK)
122+
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/link_account_signin']", false)
109123
})
110124

111125
t.Run("EnablePasswordSignInForm=true", func(t *testing.T) {
@@ -118,5 +132,11 @@ func TestEnablePasswordSignInForm(t *testing.T) {
118132

119133
req = NewRequest(t, "POST", "/user/login")
120134
MakeRequest(t, req, http.StatusOK)
135+
136+
req = NewRequest(t, "GET", "/user/link_account")
137+
defer web.RouteMockReset()
138+
web.RouteMock(web.MockAfterMiddlewares, mockLinkAccount)
139+
resp = MakeRequest(t, req, http.StatusOK)
140+
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/link_account_signin']", true)
121141
})
122142
}

0 commit comments

Comments
 (0)