public async Task <IActionResult> DeleteUserRole(string id)
        {
            var user = await GetUserById(id);

            var model = new AddUserRole
            {
                Roles  = GetAllRoles(),
                UserId = id,
                Email  = user.Email
            };

            return(View(model));
        }
        public async Task <IActionResult> AddUserRole(AddUserRole addUserRole)
        {
            var user = await GetUserById(addUserRole.UserId);

            if (ModelState.IsValid)
            {
                var result = await _userManager.AddToRoleAsync(user, addUserRole.NewRole);

                if (result.Succeeded)
                {
                    return(RedirectToAction("ManageUsers"));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(error.Code, error.Description);
                }
            }
            addUserRole.Email = user.Email;
            addUserRole.Roles = GetAllRoles();
            return(View(addUserRole));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddRole(AddUserRole model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByNameAsync(model.UserName);

            if (user == null)
            {
                return(BadRequest());
            }

            var role = await _roleManager.FindByNameAsync(model.SelectedRole);

            if (role == null)
            {
                return(BadRequest());
            }

            var result = await _userManager.AddToRoleAsync(user, role.Name);

            if (result.Succeeded)
            {
                await _userManager.UpdateSecurityStampAsync(user);

                await _signInManager.RefreshSignInAsync(user);

                return(RedirectToRoute("GetUserRoles", new { userName = user.UserName }));
            }

            AddErrors(result);

            return(View(model));
        }
Exemplo n.º 4
0
        private void BtnAddRole_Click(object sender, EventArgs e)
        {
            AddUserRole addUserRole = new AddUserRole();

            addUserRole.ShowDialog();
        }
Exemplo n.º 5
0
 public void Setup()
 {
     tokenProvider      = new Mock <IAccessTokenProvider>();
     userRoleRepository = new Mock <IUserRoleRepository>();
     _fixture           = new AddUserRole(tokenProvider.Object, userRoleRepository.Object);
 }