public async Task <IActionResult> AddToRole(AddToRoleFormModel roleModel)
        {
            var roleExsists = await this.roleManager.RoleExistsAsync(roleModel.Role);

            var user = await this.userManager.FindByIdAsync(roleModel.UserId);

            var userExists = user != null;

            if (!roleExsists || !userExists)
            {
                this.ModelState.AddModelError(String.Empty, "Invalid identity details.");
            }

            if (!this.ModelState.IsValid)
            {
                this.TempData.AddErrorMessage("Failed to add user to role.");

                return(this.RedirectToAction(nameof(this.Index)));
            }

            await this.userManager.AddToRoleAsync(user, roleModel.Role);

            this.TempData.AddSuccessMessage($"Successfuly added '{user.Name}' to '{roleModel.Role}' Role.");

            return(this.RedirectToAction(nameof(this.Index)));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddToRole(AddToRoleFormModel model)
        {
            var user = await this.userManager.FindByIdAsync(model.Id);

            var userExists = user != null;
            var role       = await this.roles.GetByIdAsync(model.RoleId);

            if (!userExists || role == null)
            {
                ModelState.AddModelError(string.Empty, "Invalid identity details.");
                var currentUserRoles = await this.GetUserRoles(user);

                return(View(await roles.GetByUserIdAsync(model.Id, currentUserRoles)));
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            await this.userManager.AddToRoleAsync(user, role.Name);

            TempData.AddSuccessMessage($"User '{user.UserName}' has been successfully added to '{role.Name}' role!");
            return(RedirectToAction(nameof(Index)));
        }
Esempio n. 3
0
        public async Task <AddToRoleFormModel> GetByUserIdAsync(string userId, IList <string> currentUserRoles)
        {
            var roles = await this.dropDownService
                        .GetAvailableRolesAsync(currentUserRoles);

            var model = new AddToRoleFormModel
            {
                Id    = userId,
                Roles = roles
            };

            return(model);
        }
        public async Task <IActionResult> AddToRole(AddToRoleFormModel model)
        {
            var roleExists = await this.roleManager.RoleExistsAsync(model.Role);

            var user = await this.userManager.FindByIdAsync(model.UserId);

            var userExists = user != null;

            if (!roleExists || !userExists)
            {
                ModelState.AddModelError(string.Empty, "Invalid identity details");
            }

            if (!ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }

            await this.userManager.AddToRoleAsync(user, model.Role);

            TempData.AddSuccessMessage($"User {user.UserName} syccessfully added to the {model.Role} role.");
            return(RedirectToAction(nameof(Index)));
        }