mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Add GetSortedUsers() and GetUserCount() APIs.
This commit is contained in:
parent
1394dce306
commit
0b3742b0b1
@ -73,8 +73,17 @@ func (c *ApiController) GetUsers() {
|
||||
// @router /get-user [get]
|
||||
func (c *ApiController) GetUser() {
|
||||
id := c.Input().Get("id")
|
||||
owner := c.Input().Get("owner")
|
||||
email := c.Input().Get("email")
|
||||
|
||||
c.Data["json"] = object.GetMaskedUser(object.GetUser(id))
|
||||
var user *object.User
|
||||
if email == "" {
|
||||
user = object.GetUser(id)
|
||||
} else {
|
||||
user = object.GetUserByEmail(owner, email)
|
||||
}
|
||||
|
||||
c.Data["json"] = object.GetMaskedUser(user)
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
@ -265,3 +274,42 @@ func (c *ApiController) CheckUserPassword() {
|
||||
c.ResponseError(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// GetSortedUsers
|
||||
// @Title GetSortedUsers
|
||||
// @Description
|
||||
// @Param owner query string true "The owner of users"
|
||||
// @Param sorter query string true "The DB column name to sort by, e.g., created_time"
|
||||
// @Param limit query string true "The count of users to return, e.g., 25"
|
||||
// @Success 200 {array} object.User The Response object
|
||||
// @router /get-sorted-users [get]
|
||||
func (c *ApiController) GetSortedUsers() {
|
||||
owner := c.Input().Get("owner")
|
||||
sorter := c.Input().Get("sorter")
|
||||
limit := util.ParseInt(c.Input().Get("limit"))
|
||||
|
||||
c.Data["json"] = object.GetMaskedUsers(object.GetSortedUsers(owner, sorter, limit))
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetUserCount
|
||||
// @Title GetUserCount
|
||||
// @Description
|
||||
// @Param owner query string true "The owner of users"
|
||||
// @Param isOnline query string true "The filter for query, 1 for online, 0 for offline, empty string for all users"
|
||||
// @Success 200 {int} int The count of filtered users for an organization
|
||||
// @router /get-user-count [get]
|
||||
func (c *ApiController) GetUserCount() {
|
||||
owner := c.Input().Get("owner")
|
||||
isOnline := c.Input().Get("isOnline")
|
||||
|
||||
count := 0
|
||||
if isOnline == "" {
|
||||
count = object.GetUserCount(owner)
|
||||
} else {
|
||||
count = object.GetOnlineUserCount(owner, util.ParseInt(isOnline))
|
||||
}
|
||||
|
||||
c.Data["json"] = count
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
@ -111,6 +111,15 @@ func GetUserCount(owner string) int {
|
||||
return int(count)
|
||||
}
|
||||
|
||||
func GetOnlineUserCount(owner string, isOnline int) int {
|
||||
count, err := adapter.Engine.Where("is_online = ?", isOnline).Count(&User{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return int(count)
|
||||
}
|
||||
|
||||
func GetUsers(owner string) []*User {
|
||||
users := []*User{}
|
||||
err := adapter.Engine.Desc("created_time").Find(&users, &User{Owner: owner})
|
||||
@ -121,6 +130,16 @@ func GetUsers(owner string) []*User {
|
||||
return users
|
||||
}
|
||||
|
||||
func GetSortedUsers(owner string, sorter string, limit int) []*User {
|
||||
users := []*User{}
|
||||
err := adapter.Engine.Desc(sorter).Limit(limit, 0).Find(&users, &User{Owner: owner})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return users
|
||||
}
|
||||
|
||||
func GetPaginationUsers(owner string, offset, limit int) []*User {
|
||||
users := []*User{}
|
||||
err := adapter.Engine.Desc("created_time").Limit(limit, offset).Find(&users, &User{Owner: owner})
|
||||
@ -149,6 +168,24 @@ func getUser(owner string, name string) *User {
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserByEmail(owner string, email string) *User {
|
||||
if owner == "" || email == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
user := User{Owner: owner, Email: email}
|
||||
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)
|
||||
|
@ -55,6 +55,8 @@ func initAPI() {
|
||||
|
||||
beego.Router("/api/get-global-users", &controllers.ApiController{}, "GET:GetGlobalUsers")
|
||||
beego.Router("/api/get-users", &controllers.ApiController{}, "GET:GetUsers")
|
||||
beego.Router("/api/get-sorted-users", &controllers.ApiController{}, "GET:GetSortedUsers")
|
||||
beego.Router("/api/get-user-count", &controllers.ApiController{}, "GET:GetUserCount")
|
||||
beego.Router("/api/get-user", &controllers.ApiController{}, "GET:GetUser")
|
||||
beego.Router("/api/update-user", &controllers.ApiController{}, "POST:UpdateUser")
|
||||
beego.Router("/api/add-user", &controllers.ApiController{}, "POST:AddUser")
|
||||
|
Loading…
x
Reference in New Issue
Block a user