Exemplo n.º 1
0
        /// <summary>
        /// 设置用户的角色
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <param name="roleIds">角色编号集合</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> SetUserRoles(int id, int[] roleIds)
        {
            SysUser user = await UserRepository.GetByKeyAsync(id);

            if (user == null)
            {
                return(new OperationResult(OperationResultType.QueryNull, "指定编号的用户信息不存在"));
            }
            int[] existIds  = UserRoleMapRepository.Entities.Where(m => m.User.Id == id).Select(m => m.Role.Id).ToArray();
            int[] addIds    = roleIds.Except(existIds).ToArray();
            int[] removeIds = existIds.Except(roleIds).ToArray();
            UserRoleMapRepository.UnitOfWork.TransactionEnabled = true;
            foreach (int addId in addIds)
            {
                SysRole role = await RoleRepository.GetByKeyAsync(addId);

                if (role == null)
                {
                    return(new OperationResult(OperationResultType.QueryNull, "指定编号的角色信息不存在"));
                }
                SysUserRoleMap map = new SysUserRoleMap()
                {
                    User = user, Role = role
                };
                await UserRoleMapRepository.InsertAsync(map);
            }
            await UserRoleMapRepository.DeleteAsync(m => m.User.Id == id && removeIds.Contains(m.Role.Id));

            return(await UserRoleMapRepository.UnitOfWork.SaveChangesAsync() > 0
                ? new OperationResult(OperationResultType.Success, "用户“{0}”指派角色操作成功".FormatWith(user.UserName))
                : OperationResult.NoChanged);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 设置用户的角色
 /// </summary>
 /// <param name="id">用户编号</param>
 /// <param name="roleIds">角色编号集合</param>
 /// <returns>业务操作结果</returns>
 public async Task<OperationResult> SetUserRoles(int id, int[] roleIds)
 {
     SysUser user = await UserRepository.GetByKeyAsync(id);
     if (user == null)
     {
         return new OperationResult(OperationResultType.QueryNull, "指定编号的用户信息不存在");
     }
     int[] existIds = UserRoleMapRepository.Entities.Where(m => m.User.Id == id).Select(m => m.Role.Id).ToArray();
     int[] addIds = roleIds.Except(existIds).ToArray();
     int[] removeIds = existIds.Except(roleIds).ToArray();
     UserRoleMapRepository.UnitOfWork.TransactionEnabled = true;
     foreach (int addId in addIds)
     {
         SysRole role = await RoleRepository.GetByKeyAsync(addId);
         if (role == null)
         {
             return new OperationResult(OperationResultType.QueryNull, "指定编号的角色信息不存在");
         }
         SysUserRoleMap map = new SysUserRoleMap() { User = user, Role = role };
         await UserRoleMapRepository.InsertAsync(map);
     }
     await UserRoleMapRepository.DeleteAsync(m => m.User.Id == id && removeIds.Contains(m.Role.Id));
     return await UserRoleMapRepository.UnitOfWork.SaveChangesAsync() > 0
         ? new OperationResult(OperationResultType.Success, "用户“{0}”指派角色操作成功".FormatWith(user.UserName))
         : OperationResult.NoChanged;
 }