diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-11-03 09:41:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-03 01:41:00 +0000 |
commit | 1bf5527eac6b947010c8faf408f6747de2a2384f (patch) | |
tree | a78319354a20c1ca68d6ab78268ae9811a7f854c /routers/web/user | |
parent | 80715ae5c7ca41a8ee2ad7b6459c587cc63195bf (diff) | |
download | gitea-1bf5527eac6b947010c8faf408f6747de2a2384f.tar.gz gitea-1bf5527eac6b947010c8faf408f6747de2a2384f.zip |
Refactor Find Sources and fix bug when view a user who belongs to an unactive auth source (#27798)
The steps to reproduce it.
First, create a new oauth2 source.
Then, a user login with this oauth2 source.
Disable the oauth2 source.
Visit users -> settings -> security, 500 will be displayed.
This is because this page only load active Oauth2 sources but not all
Oauth2 sources.
Diffstat (limited to 'routers/web/user')
-rw-r--r-- | routers/web/user/setting/security/security.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/routers/web/user/setting/security/security.go b/routers/web/user/setting/security/security.go index 58c637e2b3..e64901ae72 100644 --- a/routers/web/user/setting/security/security.go +++ b/routers/web/user/setting/security/security.go @@ -6,12 +6,14 @@ package security import ( "net/http" + "sort" auth_model "code.gitea.io/gitea/models/auth" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/auth/source/oauth2" ) @@ -105,11 +107,31 @@ func loadSecurityData(ctx *context.Context) { } ctx.Data["AccountLinks"] = sources - orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers(ctx) + authSources, err := auth_model.FindSources(ctx, auth_model.FindSourcesOptions{ + IsActive: util.OptionalBoolNone, + LoginType: auth_model.OAuth2, + }) if err != nil { - ctx.ServerError("GetActiveOAuth2Providers", err) + ctx.ServerError("FindSources", err) return } + + var orderedOAuth2Names []string + oauth2Providers := make(map[string]oauth2.Provider) + for _, source := range authSources { + provider, err := oauth2.CreateProviderFromSource(source) + if err != nil { + ctx.ServerError("CreateProviderFromSource", err) + return + } + oauth2Providers[source.Name] = provider + if source.IsActive { + orderedOAuth2Names = append(orderedOAuth2Names, source.Name) + } + } + + sort.Strings(orderedOAuth2Names) + ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names ctx.Data["OAuth2Providers"] = oauth2Providers |