public IHttpActionResult AddUser(UpdateUserRoleModel model) { var role = db.Role.Where(r => r.Id == model.RoleId && r.Display).FirstOrDefault(); if (role == null) { return(NotFound()); } foreach (var userid in model.UserId) { var userrole = db.UserRole.Where(r => r.RoleId == model.RoleId && r.UserId == userid).FirstOrDefault(); if (userrole == null) { db.UserRole.Add(new UserRole { RoleId = model.RoleId, UserId = userid }); } } db.SaveChanges(); return(Ok(true)); }
public async Task <IActionResult> UpdateUserRole(UpdateUserRoleModel vm) { if (vm.UserEmail != null && vm.Role != null) { AspNetUser user = await _context.AspNetUsers.Where(u => u.Email == vm.UserEmail).FirstOrDefaultAsync(); AspNetRole role = await _context.AspNetRoles.Where(r => r.NormalizedName == vm.Role).FirstOrDefaultAsync(); AspNetUserRole userRole = _context.AspNetUserRoles.Where(r => r.RoleId.Equals(role.Id)).Where(u => u.UserId.Equals(user.Id)).FirstOrDefault(); if (vm.DeleteRole) { _context.AspNetUserRoles.Remove(userRole); } else { _context.AspNetUserRoles.Add(new AspNetUserRole { RoleId = role.Id, UserId = user.Id }); } await _context.SaveChangesAsync(); } return(NoContent()); }
public async Task <IActionResult> Index() { UpdateUserRoleModel model = new UpdateUserRoleModel(); var currentUser = await GetCurrentUserAsync(); model.users = await _userManager.Users.Where(m => m.Id != currentUser.Id).Include(item => item.Roles).ToListAsync(); model.roles = await _roleManager.Roles.ToListAsync(); return(View(model)); }
public async Task <IActionResult> UpdateUserRole([FromBody] UpdateUserRoleModel updateUserModel) { if (_user.GetRole() != 0) { return(Ok(new { Success = false, Message = "权限不足,请联系管理员使用启用该操作", StatusCode = 200 })); } var data = await _sysUserServices.UpdateUserAdminRole(updateUserModel.UserId, updateUserModel.Flag); return(Ok(new { Success = data, this.HttpContext.Response.StatusCode })); }
public async Task <ActionResult> AddUser(int RoleId, int[] Ids) { var model = new UpdateUserRoleModel { RoleId = RoleId, UserId = Ids.ToList() }; var response = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Post, $"Administration/Role/AddUser", model); if (response.isSuccess) { TempData["SuccessMessage"] = "User successfully added to the role."; await LogActivity(Modules.Setting, "Add User To Role", model); } else { TempData["ErrorMessage"] = "Fail to add user."; } return(RedirectToAction("UserRole", "Role", new { area = "Administrator", id = RoleId })); }
public async Task <IActionResult> AddUserToRole(UpdateUserRoleModel model) { string accessToken = await HttpContext.GetTokenAsync("access_token"); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); if (ModelState.IsValid) { StringContent httpContent = new StringContent(model.ToJson(), Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(_configuration["URLAPI"] + $"api/Admin/UpdateUserRole", httpContent); if (response.StatusCode != HttpStatusCode.NoContent) { return(BadRequest()); } return(RedirectToAction(nameof(Roles))); } return(View()); }
/// <summary> /// </summary> /// <param name="targetAllianceUserId"></param> /// <param name="targetUserId"></param> /// <param name="targetRoleId"></param> /// <exception cref="ArgumentNullException">Error.ConnectionUserNotExist</exception> /// <exception cref="ArgumentNullException">Error.ConnectionUserNotConnected</exception> /// <returns></returns> public async Task <object> AllianceUpdateUserRole(int targetAllianceUserId, int targetUserId, byte targetRoleId) { return(await _contextActionAsync(async connection => { var cr = _getCurrentUser(connection); var curUserManagerRole = AllianceRoleHelper.GetByRoleId(cr.AllianceRoleId); if (!curUserManagerRole.CanManagePermition) { throw new SecurityException(Error.NotPermitted); } var newTarAu = _allianceService.UpdateUserRole(connection, cr.UserId, cr.AllianceId, targetUserId, targetRoleId, targetAllianceUserId); var targetUser = _getOnlineSingleUser(connection, newTarAu.UserId); var aGroup = cr.CreateAllianceGroupName(); var exIds = new List <string>(); var newTargetUserRole = AllianceRoleHelper.GetByRoleId(targetRoleId); var updateUserRoleModel = new UpdateUserRoleModel(newTarAu.AllianceId, newTarAu.UserId, newTargetUserRole); if (targetUser != null) { exIds.Add(targetUser.ConnectionId); var oldTargetUserRole = AllianceRoleHelper.GetByRoleId(targetUser.AllianceRoleId); await targetUser.UpdateAllianceGroupsByPermitionsAsync(Groups, oldTargetUserRole, newTargetUserRole); targetUser.AllianceRoleId = newTargetUserRole.Id; var updatedTargetUser = _hubCache.AddOrUpdateLocal(targetUser, false); updateUserRoleModel.SetUserData(connection, _allianceService, newTarAu, updatedTargetUser); await Clients.Client(targetUser.ConnectionId).InvokeAsync("onAllianceUpdateUserRole", updateUserRoleModel.GetUserModel()); } // todo не доделано не верный формат возвращемых данныъ var allianceResponceModel = updateUserRoleModel.GetAllianceModel(); try { foreach (var exceptedId in exIds) { await Groups.RemoveFromGroupAsync(exceptedId, aGroup); } await Clients.Group(aGroup).InvokeAsync("onAllianceUpdateUserRole", allianceResponceModel); } finally { foreach (var exceptedId in exIds) { await Groups.AddToGroupAsync(exceptedId, aGroup); } } return allianceResponceModel; })); }