...
|
...
|
@@ -37,6 +37,8 @@ func (l *SystemUpdateRoleLogic) SystemUpdateRole(req *types.RoleUpdateRequest) ( |
|
|
|
|
|
userRemoveRoleList []*domain.User
|
|
|
userAddRoleList []*domain.User
|
|
|
|
|
|
userRoles []*domain.UserRole
|
|
|
)
|
|
|
if role, err = l.svcCtx.RoleRepository.FindOne(l.ctx, conn, req.Id); err != nil {
|
|
|
return nil, xerr.NewErrMsgErr("角色不存在", err)
|
...
|
...
|
@@ -46,11 +48,12 @@ func (l *SystemUpdateRoleLogic) SystemUpdateRole(req *types.RoleUpdateRequest) ( |
|
|
return nil, xerr.NewErrMsg(fmt.Sprintf("unknown auth %d ", authId))
|
|
|
}
|
|
|
}
|
|
|
lo.ForEach(role.Users, func(item domain.User, index int) {
|
|
|
beforeUserIds = append(beforeUserIds, item.Id)
|
|
|
_, userRoles, _ = l.svcCtx.UserRoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(role.CompanyId)().WithKV("roleId", role.Id).WithFindOnly())
|
|
|
beforeUserIds = domain.Values(userRoles, func(item *domain.UserRole) int64 {
|
|
|
return item.UserId
|
|
|
})
|
|
|
tempRole := NewDomainRole(l.ctx, req.Role)
|
|
|
lo.ForEach(tempRole.Users, func(item domain.User, index int) {
|
|
|
lo.ForEach(req.Role.Users, func(item types.RoleUser, index int) {
|
|
|
afterUserIds = append(afterUserIds, item.Id)
|
|
|
})
|
|
|
removeList, addList := lo.Difference(beforeUserIds, afterUserIds)
|
...
|
...
|
@@ -69,26 +72,36 @@ func (l *SystemUpdateRoleLogic) SystemUpdateRole(req *types.RoleUpdateRequest) ( |
|
|
userAddRoleList = append(userAddRoleList, user)
|
|
|
}
|
|
|
if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
|
|
|
// 更新角色
|
|
|
role.Name = tempRole.Name
|
|
|
role.Auths = tempRole.Auths
|
|
|
role.Remark = tempRole.Remark
|
|
|
role.Users = tempRole.Users
|
|
|
if role, err = l.svcCtx.RoleRepository.UpdateWithVersion(ctx, conn, role); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
// 移除角色用户
|
|
|
for _, user := range userRemoveRoleList {
|
|
|
user.RemoveRole(role.Id)
|
|
|
if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if err = l.svcCtx.UserRoleRepository.DeleteByUserAndRole(ctx, conn, user.Id, role.Id); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
}
|
|
|
// 新增角色用户
|
|
|
for _, user := range userAddRoleList {
|
|
|
user.AddRole(role.Id)
|
|
|
if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
if _, err = l.svcCtx.UserRoleRepository.Insert(l.ctx, conn, &domain.UserRole{
|
|
|
CompanyId: user.CompanyId,
|
|
|
UserId: user.Id,
|
|
|
RoleId: role.Id,
|
|
|
}); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}, true); err != nil {
|
...
|
...
|
|