public async Task <IActionResult> EditRole(EditRoleViewModel model)
        {
            var role = await Rolemanager.FindByIdAsync(model.Id);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role with Id = {model.Id} cannot be found";
                return(View("NotFound"));
            }
            else
            {
                role.Name = model.RoleName;

                // Update the Role using UpdateAsync
                var result = await Rolemanager.UpdateAsync(role);

                if (result.Succeeded)
                {
                    return(RedirectToAction("GetAllRoles"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }

                return(View(model));
            }
        }
        public async Task <IActionResult> EditRole(string id)
        {
            // Find the role by Role ID
            var role = await Rolemanager.FindByIdAsync(id);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
                return(View("NotFound"));
            }

            var model = new EditRoleViewModel
            {
                Id       = role.Id,
                RoleName = role.Name
            };

            // Retrieve all the Users
            foreach (var user in Usermanager.Users)
            {
                // If the user is in this role, add the username to
                // Users property of EditRoleViewModel. This model
                // object is then passed to the view for display
                if (await Usermanager.IsInRoleAsync(user, role.Name))
                {
                    model.Users.Add(user.UserName);
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> EditUsersInRole(List <UserRoleViewModel> model, string roleId)
        {
            var role = await Rolemanager.FindByIdAsync(roleId);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found";
                return(View("NotFound"));
            }

            for (int i = 0; i < model.Count; i++)
            {
                var user = await Usermanager.FindByIdAsync(model[i].UserId);

                IdentityResult result = null;


                if (model[i].IsSelected && !(await Usermanager.IsInRoleAsync(user, role.Name)))
                {
                    result = await Usermanager.AddToRoleAsync(user, role.Name);
                }
                else if (!model[i].IsSelected && await Usermanager.IsInRoleAsync(user, role.Name))
                {
                    result = await Usermanager.RemoveFromRoleAsync(user, role.Name);
                }
                else
                {
                    continue;
                }

                if (result.Succeeded)
                {
                    if (i < (model.Count - 1))
                    {
                        continue;
                    }
                    else
                    {
                        return(RedirectToAction("EditRole", new { Id = roleId }));
                    }
                }
            }

            return(RedirectToAction("EditRole", new { Id = roleId }));
        }
        public async Task <IActionResult> DeleteRole(string id)
        {
            var role = await Rolemanager.FindByIdAsync(id);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
                return(View("NotFound"));
            }
            else
            {
                try
                {
                    var result = await Rolemanager.DeleteAsync(role);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("GetAllRoles"));
                    }

                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }

                    return(View("GetAllRoles"));
                }
                catch
                {
                    //Log the exception to a file. We discussed logging to a file
                    // using Nlog in Part 63 of ASP.NET Core tutorial

                    // Pass the ErrorTitle and ErrorMessage that you want to show to
                    // the user using ViewBag. The Error view retrieves this data
                    // from the ViewBag and displays to the user.
                    ViewBag.ErrorTitle   = $"{role.Name} role is in use";
                    ViewBag.ErrorMessage = $"{role.Name} role cannot be deleted as there are users in this role. If you want to delete this role, please remove the users from the role and then try to delete";
                    return(View("Error"));
                }
            }
        }
        public async Task <IActionResult> EditUsersInRole(string roleId)
        {
            ViewBag.roleId = roleId;

            var role = await Rolemanager.FindByIdAsync(roleId);

            if (role == null)
            {
                ViewBag.ErrorMessage = $"Role with Id = {roleId} cannot be found";
                return(View("NotFound"));
            }

            var model = new List <UserRoleViewModel>();

            foreach (var user in Usermanager.Users)
            {
                if (User.IsInRole("Super Admin") || User.IsInRole("Admin") && !(await Usermanager.IsInRoleAsync(user, "Admin") || await Usermanager.IsInRoleAsync(user, "Super Admin")))
                {
                    var userRoleViewModel = new UserRoleViewModel
                    {
                        UserId   = user.Id,
                        UserName = user.UserName
                    };

                    if (await Usermanager.IsInRoleAsync(user, role.Name))
                    {
                        userRoleViewModel.IsSelected = true;
                    }
                    else
                    {
                        userRoleViewModel.IsSelected = false;
                    }

                    model.Add(userRoleViewModel);
                }
            }

            return(View(model));
        }