/// <summary> /// 增加功能角色映射信息 /// </summary> /// <param name="dto">功能角色映射信息DTO</param> /// <returns>业务操作结果</returns> public virtual async Task <OperationResult> AddFunctionRoleMapAsync(TFunctionRoleMapDto dto) { dto.CheckNotNull("dto"); if (await FunctionRoleMapRepository.CheckExistsAsync(m => m.Function.Id.Equals(dto.FunctionId) && m.Role.Id.Equals(dto.RoleId))) { return(new OperationResult(OperationResultType.Error, "指定功能与角色的功能角色映射信息已存在")); } TFunction function = await FunctionRepository.GetByKeyAsync(dto.FunctionId); if (function == null) { return(new OperationResult(OperationResultType.QueryNull, "指定编号的功能信息不存在")); } TRole role = await RoleRepository.GetByKeyAsync(dto.RoleId); if (role == null) { return(new OperationResult(OperationResultType.QueryNull, "指定编号的角色信息不存在")); } TFunctionRoleMap map = dto.MapTo <TFunctionRoleMap>(); map.Function = function; map.Role = role; return(await FunctionRoleMapRepository.InsertAsync(map) > 0 ? new OperationResult(OperationResultType.Success, "功能角色映射信息添加成功") : OperationResult.NoChanged); }
/// <summary> /// 设置角色功能 /// </summary> /// <param name="roleId">角色Id</param> /// <param name="functionIds">功能Id集合</param> /// <returns>业务操作结果</returns> public async Task <OperationResult> SetRoleFunctions(int roleId, Guid[] functionIds) { SysRole role = await RoleRepository.GetByKeyAsync(roleId); if (role == null) { return(new OperationResult(OperationResultType.QueryNull, "指定编号的角色信息不存在")); } Guid[] existIds = FunctionRoleMapRepository.Entities.Where(m => m.Role.Id == roleId).Select(m => m.Function.Id).ToArray(); Guid[] addIds = functionIds.Except(existIds).ToArray(); Guid[] removeIds = existIds.Except(functionIds).ToArray(); FunctionRoleMapRepository.UnitOfWork.TransactionEnabled = true; foreach (Guid addId in addIds) { Function function = await FunctionRepository.GetByKeyAsync(addId); if (function == null) { return(new OperationResult(OperationResultType.QueryNull, "指定编号的功能信息不存在")); } FunctionRoleMap map = new FunctionRoleMap() { Function = function, Role = role, BeginTime = DateTime.Now }; await FunctionRoleMapRepository.InsertAsync(map); } await FunctionRoleMapRepository.DeleteAsync(m => m.Role.Id == roleId && removeIds.Contains(m.Function.Id)); return(await FunctionRoleMapRepository.UnitOfWork.SaveChangesAsync() > 0 ? new OperationResult(OperationResultType.Success, "角色“{0}”指派功能操作成功".FormatWith(role.Name)) : OperationResult.NoChanged); }