Skip to content

Commit e2eac75

Browse files
authored
Fix bug when the linked account was disactived and list the linked accounts (#29263)
The bug has been fixed on v1.22 but not backport to v1.21. This original PR have many refactors so I don't think it's necessary to backport all of them. Fix #28667
1 parent 5b8b22b commit e2eac75

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

models/auth/oauth2.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,14 @@ func (err ErrOAuthApplicationNotFound) Unwrap() error {
626626
return util.ErrNotExist
627627
}
628628

629-
// GetActiveOAuth2ProviderSources returns all actived LoginOAuth2 sources
630-
func GetActiveOAuth2ProviderSources() ([]*Source, error) {
629+
// GetOAuth2ProviderSources returns all actived LoginOAuth2 sources
630+
func GetOAuth2ProviderSources(onlyActive bool) ([]*Source, error) {
631631
sources := make([]*Source, 0, 1)
632-
if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, OAuth2).Find(&sources); err != nil {
632+
sess := db.GetEngine(db.DefaultContext)
633+
if onlyActive {
634+
sess = sess.Where("is_active = ?", true)
635+
}
636+
if err := sess.Where("type = ?", OAuth2).Find(&sources); err != nil {
633637
return nil, err
634638
}
635639
return sources, nil

routers/web/auth/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func SignIn(ctx *context.Context) {
146146
return
147147
}
148148

149-
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
149+
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
150150
if err != nil {
151151
ctx.ServerError("UserSignIn", err)
152152
return
@@ -170,7 +170,7 @@ func SignIn(ctx *context.Context) {
170170
func SignInPost(ctx *context.Context) {
171171
ctx.Data["Title"] = ctx.Tr("sign_in")
172172

173-
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
173+
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
174174
if err != nil {
175175
ctx.ServerError("UserSignIn", err)
176176
return
@@ -392,7 +392,7 @@ func SignUp(ctx *context.Context) {
392392

393393
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
394394

395-
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
395+
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
396396
if err != nil {
397397
ctx.ServerError("UserSignUp", err)
398398
return
@@ -422,7 +422,7 @@ func SignUpPost(ctx *context.Context) {
422422

423423
ctx.Data["SignUpLink"] = setting.AppSubURL + "/user/sign_up"
424424

425-
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
425+
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
426426
if err != nil {
427427
ctx.ServerError("UserSignUp", err)
428428
return

routers/web/user/setting/security/security.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func loadSecurityData(ctx *context.Context) {
105105
}
106106
ctx.Data["AccountLinks"] = sources
107107

108-
orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
108+
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(false)
109109
if err != nil {
110110
ctx.ServerError("GetActiveOAuth2Providers", err)
111111
return

services/auth/source/oauth2/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func ResetOAuth2() error {
6262

6363
// initOAuth2Sources is used to load and register all active OAuth2 providers
6464
func initOAuth2Sources() error {
65-
authSources, _ := auth.GetActiveOAuth2ProviderSources()
65+
authSources, _ := auth.GetOAuth2ProviderSources(true)
6666
for _, source := range authSources {
6767
oauth2Source, ok := source.Cfg.(*Source)
6868
if !ok {

services/auth/source/oauth2/providers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ func GetOAuth2Providers() []Provider {
9494
return providers
9595
}
9696

97-
// GetActiveOAuth2Providers returns the map of configured active OAuth2 providers
97+
// GetOAuth2ProvidersMap returns the map of configured active OAuth2 providers
9898
// key is used as technical name (like in the callbackURL)
9999
// values to display
100-
func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) {
100+
func GetOAuth2ProvidersMap(onlyActive bool) ([]string, map[string]Provider, error) {
101101
// Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
102-
103-
authSources, err := auth.GetActiveOAuth2ProviderSources()
102+
authSources, err := auth.GetOAuth2ProviderSources(onlyActive)
104103
if err != nil {
105104
return nil, nil, err
106105
}

0 commit comments

Comments
 (0)