示例#1
0
        public IActionResult EditRole(string user)
        {
            var getUser = _identityRepo.GetSingleUser(user);

            var userRoles = _userManager.GetRolesAsync(getUser).Result;

            var role = userRoles.Count == 0 ? "Not set" : userRoles[0];

            var roles = _identityRepo.Roles.Select(p => new SelectListItem()
            {
                Value = p.Id,
                Text  = p.Name
            }).OrderBy(o => o.Text).ToList();

            // Remove admin role from selectable list
            var adminRole = roles.Single(x => x.Text == "Admin");

            roles.Remove(adminRole);

            var model = new AdminUsersEditRoleViewModel()
            {
                Roles = roles,
                Role  = role,
                User  = getUser
            };

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> EditRole(AdminUsersEditRoleViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var user = _identityRepo.GetSingleUser(vm.User.UserName);

                // Get roles
                var roles = await _userManager.GetRolesAsync(user);

                // Remove roles
                foreach (var item in roles)
                {
                    await _userManager.RemoveFromRoleAsync(user, item);
                }

                var role = _identityRepo.GetSingleRole(vm.SelectedRole);

                // Set new role
                await _userManager.AddToRoleAsync(user, role.Name);

                return(RedirectToAction("Index"));
            }
            return(View());
        }