mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
feat: support calling get-user API by only email, phone or userId without owner (#2398)
This commit is contained in:
@ -160,35 +160,47 @@ func (c *ApiController) GetUser() {
|
|||||||
id = util.GetId(userFromUserId.Owner, userFromUserId.Name)
|
id = util.GetId(userFromUserId.Owner, userFromUserId.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if owner == "" {
|
var user *object.User
|
||||||
owner = util.GetOwnerFromId(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
organization, err := object.GetOrganization(util.GetId("admin", owner))
|
if id == "" && owner == "" {
|
||||||
if err != nil {
|
switch {
|
||||||
c.ResponseError(err.Error())
|
case email != "":
|
||||||
return
|
user, err = object.GetUserByEmailOnly(email)
|
||||||
}
|
case phone != "":
|
||||||
|
user, err = object.GetUserByPhoneOnly(phone)
|
||||||
|
case userId != "":
|
||||||
|
user, err = object.GetUserByUserIdOnly(userId)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if owner == "" {
|
||||||
|
owner = util.GetOwnerFromId(id)
|
||||||
|
}
|
||||||
|
|
||||||
if !organization.IsProfilePublic {
|
organization, err := object.GetOrganization(util.GetId("admin", owner))
|
||||||
requestUserId := c.GetSessionUsername()
|
if err != nil {
|
||||||
hasPermission, err := object.CheckUserPermission(requestUserId, id, false, c.GetAcceptLanguage())
|
|
||||||
if !hasPermission {
|
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var user *object.User
|
if !organization.IsProfilePublic {
|
||||||
switch {
|
requestUserId := c.GetSessionUsername()
|
||||||
case email != "":
|
hasPermission, err := object.CheckUserPermission(requestUserId, id, false, c.GetAcceptLanguage())
|
||||||
user, err = object.GetUserByEmail(owner, email)
|
if !hasPermission {
|
||||||
case phone != "":
|
c.ResponseError(err.Error())
|
||||||
user, err = object.GetUserByPhone(owner, phone)
|
return
|
||||||
case userId != "":
|
}
|
||||||
user = userFromUserId
|
}
|
||||||
default:
|
|
||||||
user, err = object.GetUser(id)
|
switch {
|
||||||
|
case email != "":
|
||||||
|
user, err = object.GetUserByEmail(owner, email)
|
||||||
|
case phone != "":
|
||||||
|
user, err = object.GetUserByPhone(owner, phone)
|
||||||
|
case userId != "":
|
||||||
|
user = userFromUserId
|
||||||
|
default:
|
||||||
|
user, err = object.GetUser(id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -372,6 +372,24 @@ func GetUserByEmail(owner string, email string) (*User, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserByEmailOnly(email string) (*User, error) {
|
||||||
|
if email == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{Email: email}
|
||||||
|
existed, err := ormer.Engine.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if existed {
|
||||||
|
return &user, nil
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserByPhone(owner string, phone string) (*User, error) {
|
func GetUserByPhone(owner string, phone string) (*User, error) {
|
||||||
if owner == "" || phone == "" {
|
if owner == "" || phone == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -390,6 +408,24 @@ func GetUserByPhone(owner string, phone string) (*User, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserByPhoneOnly(phone string) (*User, error) {
|
||||||
|
if phone == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{Phone: phone}
|
||||||
|
existed, err := ormer.Engine.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if existed {
|
||||||
|
return &user, nil
|
||||||
|
} else {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetUserByUserId(owner string, userId string) (*User, error) {
|
func GetUserByUserId(owner string, userId string) (*User, error) {
|
||||||
if owner == "" || userId == "" {
|
if owner == "" || userId == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
Reference in New Issue
Block a user