API: get list of users with additional auth info (#17305)
* batch disable users * batch revoke users tokens * split batch disable user and revoke token * API: get users with auth info and isExternal flag * fix tests for batch disable users * Users: refactor /api/users/search endpoint * Users: use alias for "user" table * Chore: add BatchDisableUsers() to the bus * Users: order user list by id explicitly * Users: return AuthModule from /api/users/:id endpoint * Users: do not return unused fields * Users: fix SearchUsers method after last changes * User: return auth module as array for future purposes * User: tests for SearchUsers() * User: return only latest auth module in SearchUsers() * User: fix JOIN, get only most recent auth module
This commit is contained in:
@@ -435,7 +435,15 @@ func SearchUsers(query *models.SearchUsersQuery) error {
|
||||
|
||||
whereConditions := make([]string, 0)
|
||||
whereParams := make([]interface{}, 0)
|
||||
sess := x.Table("user")
|
||||
sess := x.Table("user").Alias("u")
|
||||
|
||||
// Join with only most recent auth module
|
||||
joinCondition := `(
|
||||
SELECT id from user_auth
|
||||
WHERE user_auth.user_id = u.id
|
||||
ORDER BY user_auth.created DESC `
|
||||
joinCondition = "user_auth.id=" + joinCondition + dialect.Limit(1) + ")"
|
||||
sess.Join("LEFT", "user_auth", joinCondition)
|
||||
|
||||
if query.OrgId > 0 {
|
||||
whereConditions = append(whereConditions, "org_id = ?")
|
||||
@@ -450,7 +458,7 @@ func SearchUsers(query *models.SearchUsersQuery) error {
|
||||
if query.AuthModule != "" {
|
||||
whereConditions = append(
|
||||
whereConditions,
|
||||
`id IN (SELECT user_id
|
||||
`u.id IN (SELECT user_id
|
||||
FROM user_auth
|
||||
WHERE auth_module=?)`,
|
||||
)
|
||||
@@ -464,14 +472,15 @@ func SearchUsers(query *models.SearchUsersQuery) error {
|
||||
|
||||
offset := query.Limit * (query.Page - 1)
|
||||
sess.Limit(query.Limit, offset)
|
||||
sess.Cols("id", "email", "name", "login", "is_admin", "is_disabled", "last_seen_at")
|
||||
sess.Cols("u.id", "u.email", "u.name", "u.login", "u.is_admin", "u.is_disabled", "u.last_seen_at", "user_auth.auth_module")
|
||||
sess.OrderBy("u.id")
|
||||
if err := sess.Find(&query.Result.Users); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// get total
|
||||
user := models.User{}
|
||||
countSess := x.Table("user")
|
||||
countSess := x.Table("user").Alias("u")
|
||||
|
||||
if len(whereConditions) > 0 {
|
||||
countSess.Where(strings.Join(whereConditions, " AND "), whereParams...)
|
||||
|
||||
Reference in New Issue
Block a user