Skip to content

Commit 1928918

Browse files
CrimsonEdgeHopewxiaoguang
CrimsonEdgeHope
andauthored
Fix Account linking page (#33325)
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: wxiaoguang <[email protected]>
1 parent b7614e2 commit 1928918

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 templates.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
@@ -15,8 +15,11 @@ import (
1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/test"
1717
"code.gitea.io/gitea/modules/translation"
18+
"code.gitea.io/gitea/modules/web"
19+
"code.gitea.io/gitea/services/context"
1820
"code.gitea.io/gitea/tests"
1921

22+
"github.com/markbates/goth"
2023
"github.com/stretchr/testify/assert"
2124
"github.com/stretchr/testify/require"
2225
)
@@ -98,6 +101,11 @@ func TestSigninWithRememberMe(t *testing.T) {
98101
func TestEnablePasswordSignInForm(t *testing.T) {
99102
defer tests.PrepareTestEnv(t)()
100103

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

109117
req = NewRequest(t, "POST", "/user/login")
110118
MakeRequest(t, req, http.StatusForbidden)
119+
120+
req = NewRequest(t, "GET", "/user/link_account")
121+
defer web.RouteMockReset()
122+
web.RouteMock(web.MockAfterMiddlewares, mockLinkAccount)
123+
resp = MakeRequest(t, req, http.StatusOK)
124+
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/link_account_signin']", false)
111125
})
112126

113127
t.Run("EnablePasswordSignInForm=true", func(t *testing.T) {
@@ -120,5 +134,11 @@ func TestEnablePasswordSignInForm(t *testing.T) {
120134

121135
req = NewRequest(t, "POST", "/user/login")
122136
MakeRequest(t, req, http.StatusOK)
137+
138+
req = NewRequest(t, "GET", "/user/link_account")
139+
defer web.RouteMockReset()
140+
web.RouteMock(web.MockAfterMiddlewares, mockLinkAccount)
141+
resp = MakeRequest(t, req, http.StatusOK)
142+
NewHTMLParser(t, resp.Body).AssertElement(t, "form[action='/user/link_account_signin']", true)
123143
})
124144
}

0 commit comments

Comments
 (0)