用户角色设置
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
@@ -137,15 +135,22 @@ func UpdateRole(ctx *jxcontext.Context, roleID int, name string, isDelete bool)
|
||||
}
|
||||
|
||||
func GetUserRole(ctx *jxcontext.Context, userID string) (userRoles []*model.UserRole, err error) {
|
||||
return dao.GetUserRole(dao.GetDB(), userID)
|
||||
return dao.GetUserRole(dao.GetDB(), []string{userID}, nil)
|
||||
}
|
||||
|
||||
func UpdateUserRole(ctx *jxcontext.Context, userIDs []string, roleIDs []int) (err error) {
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
userRoleMap = make(map[string][]int)
|
||||
db = dao.GetDB()
|
||||
roleIDMap = make(map[int]int)
|
||||
nowRoleIDMap = make(map[int]int)
|
||||
userRoleMap = make(map[string][]int)
|
||||
addUserRoleMap = make(map[string][]int)
|
||||
deleteUserRoleMap = make(map[string][]int)
|
||||
)
|
||||
userRoles, err := dao.GetUserRole(db, "")
|
||||
for _, v := range roleIDs {
|
||||
roleIDMap[v] = 1
|
||||
}
|
||||
userRoles, err := dao.GetUserRole(db, userIDs, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -154,6 +159,51 @@ func UpdateUserRole(ctx *jxcontext.Context, userIDs []string, roleIDs []int) (er
|
||||
userRoleMap[v.UserID] = append(userRoleMap[v.UserID], v.RoleID)
|
||||
}
|
||||
}
|
||||
fmt.Println("teeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", utils.Format4Output(userRoleMap, false))
|
||||
for _, userID := range userIDs {
|
||||
nowRoleIDs := userRoleMap[userID]
|
||||
for _, nowRoleID := range nowRoleIDs {
|
||||
if roleIDMap[nowRoleID] == 0 {
|
||||
deleteUserRoleMap[userID] = append(deleteUserRoleMap[userID], nowRoleID)
|
||||
}
|
||||
nowRoleIDMap[nowRoleID] = 1
|
||||
}
|
||||
for _, roleID := range roleIDMap {
|
||||
if nowRoleIDMap[roleID] == 0 {
|
||||
addUserRoleMap[userID] = append(addUserRoleMap[userID], roleID)
|
||||
}
|
||||
}
|
||||
}
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db)
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
if len(addUserRoleMap) > 0 {
|
||||
for userID, roleIDs := range addUserRoleMap {
|
||||
for _, roleID := range roleIDs {
|
||||
userRole := &model.UserRole{
|
||||
UserID: userID,
|
||||
RoleID: roleID,
|
||||
}
|
||||
dao.WrapAddIDCULDEntity(userRole, ctx.GetUserName())
|
||||
err = dao.CreateEntity(db, userRole)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(deleteUserRoleMap) > 0 {
|
||||
for userID, roleIDs := range deleteUserRoleMap {
|
||||
for _, roleID := range roleIDs {
|
||||
userRoles, _ := dao.GetUserRole(db, []string{userID}, []int{roleID})
|
||||
if len(userRoles) > 0 {
|
||||
userRoles[0].DeletedAt = time.Now()
|
||||
userRoles[0].LastOperator = ctx.GetUserName()
|
||||
_, err = dao.UpdateEntity(db, userRoles[0], "DeletedAt", "LastOperator")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dao.Commit(db)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ func GetRole(db *DaoDB, name string) (roles []*model.Role, err error) {
|
||||
return roles, err
|
||||
}
|
||||
|
||||
func GetUserRole(db *DaoDB, userID string) (userRoles []*model.UserRole, err error) {
|
||||
func GetUserRole(db *DaoDB, userIDs []string, roleIDs []int) (userRoles []*model.UserRole, err error) {
|
||||
sql := `
|
||||
SELECT *
|
||||
FROM user_role
|
||||
@@ -59,9 +59,13 @@ func GetUserRole(db *DaoDB, userID string) (userRoles []*model.UserRole, err err
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
}
|
||||
if userID != "" {
|
||||
sql += " AND user_id = ?"
|
||||
sqlParams = append(sqlParams, userID)
|
||||
if len(userIDs) > 0 {
|
||||
sql += " AND user_id IN (" + GenQuestionMarks(len(userIDs)) + ")"
|
||||
sqlParams = append(sqlParams, userIDs)
|
||||
}
|
||||
if len(roleIDs) > 0 {
|
||||
sql += " AND role_id IN (" + GenQuestionMarks(len(roleIDs)) + ")"
|
||||
sqlParams = append(sqlParams, roleIDs)
|
||||
}
|
||||
err = GetRows(db, &userRoles, sql, sqlParams)
|
||||
return userRoles, err
|
||||
|
||||
Reference in New Issue
Block a user