update GetUsersByPermissionFunc

This commit is contained in:
Hoang 2022-03-29 17:26:35 +07:00
parent eb52943468
commit 59426ad48f
2 changed files with 34 additions and 0 deletions

View File

@ -79,6 +79,33 @@ func roleFindByID(ctx context.Context, id primitive.ObjectID) (model.DBRole, err
return doc, err return doc, err
} }
func roleFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBRole) {
var (
col = database.GetRoleCol()
)
docs = make([]model.DBRole, 0)
cursor, err := col.Find(ctx, cond, opts...)
if err != nil {
logger.Error("usermngmt - Role - Find", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &docs); err != nil {
logger.Error("usermngmt - Role - Decode", logger.LogData{
"cond": cond,
"opts": opts,
"err": err.Error(),
})
return
}
return
}
func permissionFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) { func permissionFindByCondition(ctx context.Context, cond interface{}, opts ...*options.FindOptions) (docs []model.DBPermission) {
var ( var (
col = database.GetPermissionCol() col = database.GetPermissionCol()

View File

@ -225,6 +225,13 @@ func GetUsersByPermission(queryParams model.UserByPermissionQuery) (r model.User
for _, value := range permissions { for _, value := range permissions {
roles = append(roles, value.RoleID) roles = append(roles, value.RoleID)
} }
// Get admin role
adminRoles := roleFindByCondition(ctx, bson.M{"isAdmin": true})
for _, value := range adminRoles {
roles = append(roles, value.ID)
}
if len(roles) < 0 { if len(roles) < 0 {
return return
} }