42 lines
1.1 KiB
42 lines
1.1 KiB
3 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package auth
|
||
|
|
||
|
import (
|
||
3 years ago
|
"code.gitea.io/gitea/models/auth"
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
// DeleteSource deletes a AuthSource record in DB.
|
||
|
func DeleteSource(source *auth.Source) error {
|
||
3 years ago
|
count, err := db.GetEngine(db.DefaultContext).Count(&user_model.User{LoginSource: source.ID})
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
} else if count > 0 {
|
||
3 years ago
|
return auth.ErrSourceInUse{
|
||
3 years ago
|
ID: source.ID,
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
count, err = db.GetEngine(db.DefaultContext).Count(&user_model.ExternalLoginUser{LoginSourceID: source.ID})
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
} else if count > 0 {
|
||
3 years ago
|
return auth.ErrSourceInUse{
|
||
3 years ago
|
ID: source.ID,
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
if registerableSource, ok := source.Cfg.(auth.RegisterableSource); ok {
|
||
3 years ago
|
if err := registerableSource.UnregisterSource(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
_, err = db.GetEngine(db.DefaultContext).ID(source.ID).Delete(new(auth.Source))
|
||
3 years ago
|
return err
|
||
|
}
|