public IActionResult Details(string id, EditRoleRequest request) { if (!ModelState.IsValid) { return(NotFoundView()); } Result <RoleDetailViewModel> role = _roleDataService.GetDetails(id); if (role.Failure) { return(NotFoundView()); } Result editResult = _roleService.EditRole(id, request, GetUserId()); if (editResult.Failure) { ModelState.AddErrors(editResult.Errors); role.Value.StatusAlert = StatusAlertViewExtension.Get(editResult); return(View(role.Value)); } role.Value.StatusAlert = StatusAlertViewExtension.Get("Role updated"); return(View(role.Value)); }
public async Task <IActionResult> EditRole([FromBody] EditRoleRequest request) { var result = _securityService.EditRole(request.RoleId, request.AccessRightsIds, request.DeniedRightsIds, request.FunctionIds); return(Ok(await result)); }
public Result EditRole(string id, EditRoleRequest editRoleRequest, string adminId) { ValidationResult validationResult = _editRoleValidator.Validate(editRoleRequest); if (!validationResult.IsValid) { _logger.LogError($"Invalid EditRoleRequest. admin {adminId}"); return(Result.Fail(ResultUtils.ToResultError(validationResult.Errors))); } BaseSpecification <RoleEntity> roleSpecification = new BaseSpecification <RoleEntity>(); roleSpecification.AddFilter(x => x.Id == id); RoleEntity role = _roleRepository.SingleOrDefault(roleSpecification); if (role == null) { _logger.LogError($"No role. Admin id {adminId}"); return(Result.Fail("no_role", "No Role")); } role.Description = editRoleRequest.Description; bool result = _roleRepository.Update(role); if (!result) { _logger.LogError($"Failed to update role with id {id}. Admin {adminId}"); return(Result.Fail("error", "Error")); } return(Result.Ok()); }
public async Task <IActionResult> Put([FromBody] EditRoleRequest request) { EditRoleResponse response = new EditRoleResponse(); try { IdentityRole identityRole = await _roleManager.FindByIdAsync(request.Id); if (identityRole == null) { throw new Exception("Role not found"); } identityRole.Name = request.Name; IdentityResult result = await _roleManager.UpdateAsync(identityRole); if (result.Succeeded) { response.Id = identityRole.Id; response.IsSuccess = true; } else { response.Errors = result.Errors.Select(x => x.Description); } return(Ok(response)); } catch (Exception ex) { _logger.LogError($"Post error: {ex.Message} - Name: {request.Name}"); return(BadRequest()); } }
public async Task <ActionResult <EditRoleResponse> > EditRole(int id, EditRoleRequest request) { var response = await _adminService.EditRole(id, request.Role); if (response.OperationSucceded) { return(response); } return(Problem(statusCode: (int)HttpStatusCode.InternalServerError, title: "Operation failed")); }
public HttpResponseMessage UpdateRole([FromUri] EditRoleRequest request) { string result = ""; try { this._roleService.UpdateRole(request); result = "删除成功"; } catch (Exception ex) { result = "删除失败:" + ex.Message; } return(ToJson(result)); }
/// <summary> /// 修改岗位 /// </summary> /// <param name="request"></param> /// <returns></returns> public RoleView UpdateRole(EditRoleRequest request) { Role model = this._roleRepository.FindBy(request.Id); if (model == null) { throw new EntityIsInvalidException <string>(model.ToString()); } model.UpdateRole(request.Name, request.Description); this._uow.Commit(); return(model.ConvertToRoleView()); }
public async Task <ActionResult <UserDto> > AddRolesAsync(int id, [FromBody] EditRoleRequest roleEditDto) { if (roleEditDto == null) { throw new ArgumentNullException(nameof(roleEditDto)); } if (roleEditDto.RoleNames == null || roleEditDto.RoleNames.Count == 0) { return(this.BadRequest("At least one role must be specified.")); } var user = await this.userRepository.GetByIdAsync(id); var roles = await this.userRepository.GetRolesAsync(); var userRoles = user.UserRoles.Select(ur => ur.Role.Name.ToUpperInvariant()).ToHashSet(); var selectedRoles = roleEditDto.RoleNames.Select(role => role.ToUpperInvariant()).ToHashSet(); var rolesToAdd = roles.Where(role => { var upperName = role.Name.ToUpperInvariant(); return(selectedRoles.Contains(upperName) && !userRoles.Contains(upperName)); }); if (!rolesToAdd.Any()) { return(this.Ok(this.mapper.Map <UserDto>(user))); } user.UserRoles.AddRange(rolesToAdd.Select(role => new UserRole { Role = role })); var success = await this.userRepository.SaveAllAsync(); if (!success) { return(this.BadRequest("Failed to add roles.")); } var userToReturn = this.mapper.Map <UserDto>(user); return(this.Ok(userToReturn)); }
public Task <RoleInfo> EditRole(string serverId, string roleId, string name = null, uint?permissions = null, uint?color = null, bool?hoist = null) { if (serverId == null) { throw new ArgumentNullException(nameof(serverId)); } if (roleId == null) { throw new ArgumentNullException(nameof(roleId)); } var request = new EditRoleRequest { Name = name, Permissions = permissions, Hoist = hoist, Color = color }; return(_rest.Patch <RoleInfo>(Endpoints.ServerRole(serverId, roleId), request)); }
public ActionResult Edit(EditRoleRequest request) { var flag = false; var msg = string.Empty; var rs = _roleService.EditRole(request, loginInfo); if (rs.ReturnCode == ReturnCodeType.Success && rs.Content == true) { flag = true; msg = "修改成功!"; } else { msg = rs.Message.IsNullOrEmpty() ? "修改失败!" : rs.Message; } return(Json(new { success = flag, msg = msg }, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult <EditRoleResponse> > CreateRole(EditRoleRequest request) { var response = new EditRoleResponse(); var identityRole = new IdentityRole { Name = request.Name }; var result = await _roleManager.CreateAsync(identityRole); if (result.Succeeded) { identityRole = await _roleManager.FindByNameAsync(identityRole.Name); response.Id = identityRole.Id; response.IsSuccess = true; } else { response.Errors = result.Errors.Select(x => x.Description); } return(Ok(response)); }
public ActionResult Edit(EditRoleRequest request) { var flag = false; var msg = string.Empty; using (var factory = new ChannelFactory <IRightsRoleService>("*")) { var client = factory.CreateChannel(); var rs = client.EditRole(request, loginInfo); if (rs.ReturnCode == ReturnCodeType.Success && rs.Content == true) { flag = true; msg = "修改成功!"; } else { msg = rs.Message.IsNullOrEmpty() ? "修改失败!" : rs.Message; } } return(Json(new { success = flag, msg = msg }, JsonRequestBehavior.AllowGet)); }
public async Task <IActionResult> UpdateRole(string id, EditRoleRequest request) { if (id != request.Id) { return(BadRequest()); } var response = new EditRoleResponse(); var identityRole = await _roleManager.FindByIdAsync(request.Id); if (identityRole == null) { return(NotFound()); } identityRole.Name = request.Name; try { var result = await _roleManager.UpdateAsync(identityRole); if (result.Succeeded) { response.Id = identityRole.Id; response.IsSuccess = true; } else { response.Errors = result.Errors.Select(x => x.Description); } return(Ok(response)); } catch (DbUpdateConcurrencyException) when(!_roleManager.Roles.AsNoTracking().Any(r => r.Id == id)) { return(NotFound()); } }
/// <summary> /// 修改角色 /// </summary> /// <param name="request"></param> /// <param name="loginInfo"></param> /// <returns></returns> public ServiceResult <bool> EditRole(EditRoleRequest request, TRightsUser loginInfo) { //需要检查是否已存在相同名称的角色 var result = new ServiceResult <bool> { ReturnCode = ReturnCodeType.Error }; var role = _roleDao.GetRoleByName(request.NewName); if (request.NewName != request.OriginalName && role != null) { result.Message = "已存在相同名称的角色!"; return(result); } var item = _roleDao.GetById(request.Id); if (item != null) { item.Name = request.NewName; item.Description = request.Description; item.OrganizationId = request.OrgId; item.LastUpdatedBy = loginInfo.Id; item.LastUpdatedTime = DateTime.Now; var rs = _roleDao.Update(item); if (rs == true) { result.ReturnCode = ReturnCodeType.Success; result.Content = true; } } return(result); }