public async Task <Response <RoleResponse> > UpdateRoleAccount([FromRoute] Guid roleId, [FromBody] UpdateRoleAccountRequest request) { return(await _roleService.UpdateRoleAccountAsync(roleId, request)); }
/// <summary> /// Cập nhật quyền tài khoản: thêm xóa tài khoản vào quyền /// </summary> /// <param name="roleId"></param> /// <param name="request"></param> /// <returns></returns> public async Task <Response <RoleResponse> > UpdateRoleAccountAsync(Guid roleId, UpdateRoleAccountRequest request) { var role = await _roleManager.FindByIdAsync(roleId.ToString()); if (role == null) { _logger.LogError("Not find role with id"); return(new Response <RoleResponse>(Constant.STATUS_ERROR, new List <string> { "Not find role with id" })); } //Thêm tài khoản vào quyền foreach (var item in request.AddAccountIds ?? new List <Guid> { }) { var account = await _userManager.FindByIdAsync(item.ToString()); var isInRole = await _userManager.IsInRoleAsync(account, role.Name); if (account != null && !isInRole) { var result = await _userManager.AddToRoleAsync(account, role.Name); if (!result.Succeeded) { _logger.LogError("Add user to role failed"); return(new Response <RoleResponse>(Constant.STATUS_ERROR, result.Errors.Select(x => x.Description))); } } } //Xóa tài khoản khỏi quyền foreach (var item in request.RemoveAccountIds ?? new List <Guid> { }) { var account = await _userManager.FindByIdAsync(item.ToString()); if (account != null) { var result = await _userManager.RemoveFromRoleAsync(account, role.Name); if (!result.Succeeded) { _logger.LogError("Remove user from role failed"); return(new Response <RoleResponse>(Constant.STATUS_ERROR, result.Errors.Select(x => x.Description))); } } } _logger.LogInformation("Update role menu sucess"); return(new Response <RoleResponse>(Constant.STATUS_SUCESS)); }