feat: support github login (#2639)

* Support Github Login

* improve according to codefactor

* fix due to last updates

* optimization

Co-authored-by: Noah Hsu <i@nn.ci>
This commit is contained in:
itsHenry
2022-12-27 22:11:22 +08:00
committed by GitHub
parent c00dcc8f39
commit 83fe17c6ec
8 changed files with 125 additions and 1 deletions

View File

@ -145,6 +145,11 @@ func InitialSettings() []model.SettingItem {
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.INDEX},
{Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
// GitHub settings
{Key: conf.GithubClientId, Value: "", Type: conf.TypeString, Group: model.GITHUB, Flag: model.PRIVATE},
{Key: conf.GithubClientSecrets, Value: "", Type: conf.TypeString, Group: model.GITHUB, Flag: model.PRIVATE},
{Key: conf.GithubLoginEnabled, Value: "false", Type: conf.TypeBool, Group: model.GITHUB, Flag: model.PUBLIC},
}
if flags.Dev {
initialSettingItems = append(initialSettingItems, []model.SettingItem{

View File

@ -53,6 +53,11 @@ const (
// single
Token = "token"
IndexProgress = "index_progress"
//Github
GithubClientId = "github_client_id"
GithubClientSecrets = "github_client_secrets"
GithubLoginEnabled = "github_login_enabled"
)
const (

View File

@ -21,6 +21,14 @@ func GetUserByName(username string) (*model.User, error) {
return &user, nil
}
func GetUserByGithubID(githubID int) (*model.User, error) {
user := model.User{GithubID: githubID}
if err := db.Where(user).First(&user).Error; err != nil {
return nil, errors.Wrapf(err, "The Github ID is not associated with a user")
}
return &user, nil
}
func GetUserById(id uint) (*model.User, error) {
var u model.User
if err := db.First(&u, id).Error; err != nil {

View File

@ -8,6 +8,7 @@ const (
GLOBAL
ARIA2
INDEX
GITHUB
)
const (

View File

@ -31,6 +31,7 @@ type User struct {
// 9: webdav write
Permission int32 `json:"permission"`
OtpSecret string `json:"-"`
GithubID int `json:"github_id"`
}
func (u User) IsGuest() bool {