mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-08 00:50:28 +08:00
Improve IDP username handling.
This commit is contained in:
@ -135,13 +135,7 @@ func (c *ApiController) Login() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if form.Method == "signup" {
|
if form.Method == "signup" {
|
||||||
userId := ""
|
userId := object.GetUserIdByField(application, provider.Type, userInfo.Username)
|
||||||
if provider.Type == "github" {
|
|
||||||
userId = object.GetUserIdByField(application, "github", userInfo.Username)
|
|
||||||
} else if provider.Type == "google" {
|
|
||||||
userId = object.GetUserIdByField(application, "google", userInfo.Email)
|
|
||||||
}
|
|
||||||
|
|
||||||
if userId != "" {
|
if userId != "" {
|
||||||
//if object.IsForbidden(userId) {
|
//if object.IsForbidden(userId) {
|
||||||
// c.forbiddenAccountResp(userId)
|
// c.forbiddenAccountResp(userId)
|
||||||
@ -163,11 +157,7 @@ func (c *ApiController) Login() {
|
|||||||
if userId := object.GetUserIdByField(application, "email", userInfo.Email); userId != "" {
|
if userId := object.GetUserIdByField(application, "email", userInfo.Email); userId != "" {
|
||||||
resp = c.HandleLoggedIn(userId, &form)
|
resp = c.HandleLoggedIn(userId, &form)
|
||||||
|
|
||||||
if provider.Type == "github" {
|
object.LinkUserAccount(userId, provider.Type, userInfo.Username)
|
||||||
_ = object.LinkUserAccount(userId, "github", userInfo.Username)
|
|
||||||
} else if provider.Type == "google" {
|
|
||||||
_ = object.LinkUserAccount(userId, "google", userInfo.Email)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//resp = &Response{Status: "ok", Msg: "", Data: res}
|
//resp = &Response{Status: "ok", Msg: "", Data: res}
|
||||||
@ -180,16 +170,11 @@ func (c *ApiController) Login() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
linkRes := false
|
isLinked := object.LinkUserAccount(userId, provider.Type, userInfo.Username)
|
||||||
if provider.Type == "github" {
|
if isLinked {
|
||||||
linkRes = object.LinkUserAccount(userId, "github", userInfo.Username)
|
resp = &Response{Status: "ok", Msg: "", Data: isLinked}
|
||||||
} else if provider.Type == "google" {
|
|
||||||
linkRes = object.LinkUserAccount(userId, "google", userInfo.Email)
|
|
||||||
}
|
|
||||||
if linkRes {
|
|
||||||
resp = &Response{Status: "ok", Msg: "", Data: linkRes}
|
|
||||||
} else {
|
} else {
|
||||||
resp = &Response{Status: "error", Msg: "link account failed", Data: linkRes}
|
resp = &Response{Status: "error", Msg: "link account failed", Data: isLinked}
|
||||||
}
|
}
|
||||||
//if len(object.GetMemberAvatar(userId)) == 0 {
|
//if len(object.GetMemberAvatar(userId)) == 0 {
|
||||||
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, userId)
|
// avatar := UploadAvatarToOSS(tempUserAccount.AvatarUrl, userId)
|
||||||
|
@ -32,8 +32,8 @@ type GoogleIdProvider struct {
|
|||||||
RedirectUrl string
|
RedirectUrl string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGoogleIdProvider(clientId string, clientSecret string, redirectUrl string) *GithubIdProvider {
|
func NewGoogleIdProvider(clientId string, clientSecret string, redirectUrl string) *GoogleIdProvider {
|
||||||
idp := &GithubIdProvider{
|
idp := &GoogleIdProvider{
|
||||||
ClientId: clientId,
|
ClientId: clientId,
|
||||||
ClientSecret: clientSecret,
|
ClientSecret: clientSecret,
|
||||||
RedirectUrl: redirectUrl,
|
RedirectUrl: redirectUrl,
|
||||||
@ -91,6 +91,7 @@ func (idp *GoogleIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error)
|
|||||||
return userInfo, errors.New("google email is empty")
|
return userInfo, errors.New("google email is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userInfo.Username = userResponse.Email
|
||||||
userInfo.Email = userResponse.Email
|
userInfo.Email = userResponse.Email
|
||||||
userInfo.AvatarUrl = userResponse.Picture
|
userInfo.AvatarUrl = userResponse.Picture
|
||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
|
@ -113,7 +113,7 @@ func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
|||||||
return nil, errors.New("openId is empty")
|
return nil, errors.New("openId is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token, idp.ClientId, openId)
|
getUserInfoUrl := fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s", token.AccessToken, idp.ClientId, openId)
|
||||||
getUserInfoResponse, err := idp.Client.Get(getUserInfoUrl)
|
getUserInfoResponse, err := idp.Client.Get(getUserInfoUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -128,7 +128,7 @@ func (idp *QqIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
|||||||
defer getUserInfoResponse.Body.Close()
|
defer getUserInfoResponse.Body.Close()
|
||||||
userInfoContent, err := ioutil.ReadAll(getUserInfoResponse.Body)
|
userInfoContent, err := ioutil.ReadAll(getUserInfoResponse.Body)
|
||||||
var userResponse response
|
var userResponse response
|
||||||
err = json.Unmarshal(userInfoContent, &userInfo)
|
err = json.Unmarshal(userInfoContent, &userResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ type User struct {
|
|||||||
|
|
||||||
Github string `xorm:"varchar(100)" json:"github"`
|
Github string `xorm:"varchar(100)" json:"github"`
|
||||||
Google string `xorm:"varchar(100)" json:"google"`
|
Google string `xorm:"varchar(100)" json:"google"`
|
||||||
|
Qq string `xorm:"varchar(100)" json:"qq"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGlobalUsers() []*User {
|
func GetGlobalUsers() []*User {
|
||||||
|
Reference in New Issue
Block a user