Sync info from 3rd-party if possible.

This commit is contained in:
Yang Luo 2021-04-27 19:35:40 +08:00
parent 6002395d12
commit c317d601a5
2 changed files with 17 additions and 1 deletions

View File

@ -196,6 +196,18 @@ func (c *ApiController) Login() {
}
user := object.GetUser(userId)
// sync info from 3rd-party if possible
if user.DisplayName == "" && userInfo.Username != "" {
object.SetUserField(user, "displayName", userInfo.Username)
}
if user.Avatar == "" && userInfo.AvatarUrl != "" {
object.SetUserField(user, "avatar", userInfo.AvatarUrl)
}
if user.Email == "" && userInfo.Email != "" {
object.SetUserField(user, "email", userInfo.Email)
}
isLinked := object.LinkUserAccount(user, provider.Type, userInfo.Username)
if isLinked {
resp = &Response{Status: "ok", Msg: "", Data: isLinked}

View File

@ -142,7 +142,7 @@ func GetUserByField(organizationName string, field string, value string) *User {
}
}
func LinkUserAccount(user *User, field string, value string) bool {
func SetUserField(user *User, field string, value string) bool {
affected, err := adapter.engine.Table(user).ID(core.PK{user.Owner, user.Name}).Update(map[string]interface{}{field: value})
if err != nil {
panic(err)
@ -151,6 +151,10 @@ func LinkUserAccount(user *User, field string, value string) bool {
return affected != 0
}
func LinkUserAccount(user *User, field string, value string) bool {
return SetUserField(user, field, value)
}
func GetUserField(user *User, field string) string {
// https://socketloop.com/tutorials/golang-how-to-get-struct-field-and-value-by-name
u := reflect.ValueOf(user)