Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when the linked account was disactived and list the linked accounts #29263

Merged
merged 4 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,10 +626,14 @@ func (err ErrOAuthApplicationNotFound) Unwrap() error {
return util.ErrNotExist
}

// GetActiveOAuth2ProviderSources returns all actived LoginOAuth2 sources
func GetActiveOAuth2ProviderSources() ([]*Source, error) {
// GetOAuth2ProviderSources returns all actived LoginOAuth2 sources
func GetOAuth2ProviderSources(onlyActive bool) ([]*Source, error) {
sources := make([]*Source, 0, 1)
if err := db.GetEngine(db.DefaultContext).Where("is_active = ? and type = ?", true, OAuth2).Find(&sources); err != nil {
sess := db.GetEngine(db.DefaultContext)
if onlyActive {
sess = sess.Where("is_active = ?", true)
}
if err := sess.Where("type = ?", OAuth2).Find(&sources); err != nil {
return nil, err
}
return sources, nil
Expand Down
8 changes: 4 additions & 4 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func SignIn(ctx *context.Context) {
return
}

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

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

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

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

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

orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(true)
if err != nil {
ctx.ServerError("UserSignUp", err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/setting/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func loadSecurityData(ctx *context.Context) {
}
ctx.Data["AccountLinks"] = sources

orderedOAuth2Names, oauth2Providers, err := oauth2.GetActiveOAuth2Providers()
orderedOAuth2Names, oauth2Providers, err := oauth2.GetOAuth2ProvidersMap(false)
if err != nil {
ctx.ServerError("GetActiveOAuth2Providers", err)
return
Expand Down
2 changes: 1 addition & 1 deletion services/auth/source/oauth2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func ResetOAuth2() error {

// initOAuth2Sources is used to load and register all active OAuth2 providers
func initOAuth2Sources() error {
authSources, _ := auth.GetActiveOAuth2ProviderSources()
authSources, _ := auth.GetOAuth2ProviderSources(true)
for _, source := range authSources {
oauth2Source, ok := source.Cfg.(*Source)
if !ok {
Expand Down
7 changes: 3 additions & 4 deletions services/auth/source/oauth2/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,12 @@ func GetOAuth2Providers() []Provider {
return providers
}

// GetActiveOAuth2Providers returns the map of configured active OAuth2 providers
// GetOAuth2ProvidersMap returns the map of configured active OAuth2 providers
// key is used as technical name (like in the callbackURL)
// values to display
func GetActiveOAuth2Providers() ([]string, map[string]Provider, error) {
func GetOAuth2ProvidersMap(onlyActive bool) ([]string, map[string]Provider, error) {
// Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type

authSources, err := auth.GetActiveOAuth2ProviderSources()
authSources, err := auth.GetOAuth2ProviderSources(onlyActive)
if err != nil {
return nil, nil, err
}
Expand Down
Loading