feat: query user by userId (#879)

* feat: add `getUserByUserId` func

* Update user.go

Co-authored-by: Yang Luo <hsluoyz@qq.com>
This commit is contained in:
q1anx1 2022-07-14 21:46:13 +08:00 committed by GitHub
parent 10449e89ab
commit f0b0891ac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -86,6 +86,7 @@ func (c *ApiController) GetUsers() {
func (c *ApiController) GetUser() {
id := c.Input().Get("id")
email := c.Input().Get("email")
userId := c.Input().Get("userId")
owner := c.Input().Get("owner")
if owner == "" {
@ -103,10 +104,12 @@ func (c *ApiController) GetUser() {
}
var user *object.User
if email == "" {
user = object.GetUser(id)
} else {
if email != "" {
user = object.GetUserByEmail(owner, email)
} else if userId != "" {
user = object.GetUserByUserId(owner, userId)
} else {
user = object.GetUser(id)
}
c.Data["json"] = object.GetMaskedUser(user)

View File

@ -270,6 +270,24 @@ func GetUserByEmail(owner string, email string) *User {
}
}
func GetUserByUserId(owner string, userId string) *User {
if owner == "" || userId == "" {
return nil
}
user := User{Owner: owner, Id: userId}
existed, err := adapter.Engine.Get(&user)
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
func GetUser(id string) *User {
owner, name := util.GetOwnerAndNameFromId(id)
return getUser(owner, name)
@ -329,9 +347,11 @@ func UpdateUser(id string, user *User, columns []string, isGlobalAdmin bool) boo
}
if len(columns) == 0 {
columns = []string{"owner", "display_name", "avatar",
columns = []string{
"owner", "display_name", "avatar",
"location", "address", "region", "language", "affiliation", "title", "homepage", "bio", "score", "tag", "signup_application",
"is_admin", "is_global_admin", "is_forbidden", "is_deleted", "hash", "is_default_avatar", "properties", "webauthnCredentials"}
"is_admin", "is_global_admin", "is_forbidden", "is_deleted", "hash", "is_default_avatar", "properties", "webauthnCredentials",
}
}
if isGlobalAdmin {
columns = append(columns, "name", "email", "phone")
@ -398,10 +418,10 @@ func AddUsers(users []*User) bool {
return false
}
//organization := GetOrganizationByUser(users[0])
// organization := GetOrganizationByUser(users[0])
for _, user := range users {
// this function is only used for syncer or batch upload, so no need to encrypt the password
//user.UpdateUserPassword(organization)
// user.UpdateUserPassword(organization)
user.UpdateUserHash()
user.PreHash = user.Hash