Skip to content

Commit

Permalink
Adding button to link accounts from user settings (go-gitea#19792)
Browse files Browse the repository at this point in the history
* Adding button to link accounts from user settings

* Only display button to link user accounts when at least one OAuth2 provider is active
  • Loading branch information
Mai-Lapyst authored and AbdulrhmnGhanem committed Aug 23, 2022
1 parent 9485a9d commit 5783962
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
12 changes: 11 additions & 1 deletion routers/web/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,17 @@ func SignInOAuthCallback(ctx *context.Context) {
}

if u == nil {
if !setting.Service.AllowOnlyInternalRegistration && setting.OAuth2Client.EnableAutoRegistration {
if ctx.Doer != nil {
// attach user to already logged in user
err = externalaccount.LinkAccountToUser(ctx.Doer, gothUser)
if err != nil {
ctx.ServerError("UserLinkAccount", err)
return
}

ctx.Redirect(setting.AppSubURL + "/user/settings/security")
return
} else if !setting.Service.AllowOnlyInternalRegistration && setting.OAuth2Client.EnableAutoRegistration {
// create new user with details from oauth2 provider
var missingFields []string
if gothUser.UserID == "" {
Expand Down
9 changes: 9 additions & 0 deletions routers/web/user/setting/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/auth/source/oauth2"
)

const (
Expand Down Expand Up @@ -109,6 +110,14 @@ func loadSecurityData(ctx *context.Context) {
}
ctx.Data["AccountLinks"] = sources

orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
if err != nil {
ctx.ServerError("GetActiveOAuth2Providers", err)
return
}
ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names
ctx.Data["OAuth2Providers"] = oauth2Providers

openid, err := user_model.GetUserOpenIDs(ctx.Doer.ID)
if err != nil {
ctx.ServerError("GetUserOpenIDs", err)
Expand Down
8 changes: 4 additions & 4 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,6 @@ func RegisterRoutes(m *web.Route) {
}, openIDSignInEnabled)
m.Get("/sign_up", auth.SignUp)
m.Post("/sign_up", bindIgnErr(forms.RegisterForm{}), auth.SignUpPost)
m.Group("/oauth2", func() {
m.Get("/{provider}", auth.SignInOAuth)
m.Get("/{provider}/callback", auth.SignInOAuthCallback)
})
m.Get("/link_account", linkAccountEnabled, auth.LinkAccount)
m.Post("/link_account_signin", linkAccountEnabled, bindIgnErr(forms.SignInForm{}), auth.LinkAccountPostSignIn)
m.Post("/link_account_signup", linkAccountEnabled, bindIgnErr(forms.RegisterForm{}), auth.LinkAccountPostRegister)
Expand Down Expand Up @@ -458,6 +454,10 @@ func RegisterRoutes(m *web.Route) {
m.Get("/task/{task}", reqSignIn, user.TaskStatus)
m.Get("/stopwatches", reqSignIn, user.GetStopwatches)
m.Get("/search", ignExploreSignIn, user.Search)
m.Group("/oauth2", func() {
m.Get("/{provider}", auth.SignInOAuth)
m.Get("/{provider}/callback", auth.SignInOAuthCallback)
})
})
// ***** END: User *****

Expand Down
18 changes: 18 additions & 0 deletions templates/user/settings/security/accountlinks.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<h4 class="ui top attached header">
{{.i18n.Tr "settings.manage_account_links"}}
{{if .OrderedOAuth2Names}}
<div class="ui right">
<div class="ui dropdown">
<div class="ui primary tiny button">Link Account</div>
<div class="menu">
{{range $key := .OrderedOAuth2Names}}
{{$provider := index $.OAuth2Providers $key}}
<a class="item" href="{{AppSubUrl}}/user/oauth2/{{$key}}">
<img
alt="{{$provider.DisplayName}}"
src="{{AppSubUrl}}{{$provider.Image}}"
width="20" height="20">{{$provider.DisplayName}}
</a>
{{end}}
</div>
</div>
</div>
{{end}}
</h4>
<div class="ui attached segment">
<div class="ui key list">
Expand Down

0 comments on commit 5783962

Please sign in to comment.