Beispiel #1
0
        private async Task <IActionResult> ChangeRole(UserRoleRequestModel model, bool removeRole = false)
        {
            var user = await userManager.FindByIdAsync(model.UserId);

            if (user == null)
            {
                TempData.AddErrorMessage("Invalid user Id.");
                return(RedirectToAction(nameof(All), "Users"));
            }

            var role = await roleManager.FindByIdAsync(model.Role);

            if (role == null)
            {
                TempData.AddErrorMessage("Invalid role Id.");
                return(RedirectToAction(nameof(EditRoles), new { Id = model.UserId }));
            }

            var result = removeRole ?
                         await userManager.RemoveFromRoleAsync(user, role.Name)
                : await userManager.AddToRoleAsync(user, role.Name);

            if (!result.Succeeded)
            {
                TempData.AddErrorMessage(result.Errors.FirstOrDefault()?.Description ?? "Something went wrong.");
            }

            await RefreshCurrentUserCookie(model.UserId);

            TempData.AddSuccessMessage();

            return(null);
        }
Beispiel #2
0
        public async Task <IActionResult> RemoveRole(UserRoleRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectOnInvalidStateInRoleEdit(model.UserId));
            }

            var actionResult = await ChangeRole(model, true);

            if (actionResult != null)
            {
                return(actionResult);
            }

            return(RedirectToAction(nameof(EditRoles), new { Id = model.UserId }));
        }
Beispiel #3
0
        public async Task <IActionResult> GetUsersOfRole(UserRoleRequestModel model)
        {
            var users = await this.service.GetUsersOfRole(model.RoleId, model.Payload);

            return(this.Ok(users));
        }