예제 #1
0
        public async Task <IActionResult> Delete(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            User user = await _userManager.FindByIdAsync(id);

            if (user == null || user.Deleted)
            {
                return(NotFound());
            }
            if (user == await _userManager.GetUserAsync(User))
            {
                return(NotFound());
            }
            ApUserVM model = new ApUserVM()
            {
                Fullname = user.Fullname,
                Username = user.UserName,
                Email    = user.Email,
                Role     = (await _userManager.GetRolesAsync(user))[0]
            };

            return(View(model));
        }
        public async Task <IActionResult> Edit(string id, ApUserVM apUserVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(new ApUserVM
                {
                    Roles = _roleManager.Roles.ToList()
                }));
            }
            if (id == null)
            {
                return(NotFound());
            }
            User user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                return(NotFound());
            }
            user.Name     = apUserVM.Name;
            user.UserName = apUserVM.Username;
            user.Email    = apUserVM.Email;
            string role    = (await _userManager.GetRolesAsync(user))[0];
            string newRole = Request.Form["role"];

            if (apUserVM.Password != null)
            {
                user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, apUserVM.Password);
            }
            IdentityResult identityResult = await _userManager.UpdateAsync(user);

            if (!identityResult.Succeeded)
            {
                foreach (var error in identityResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return(View(new ApUserVM
                {
                    Name = user.Name,
                    Username = user.UserName,
                    Email = user.Email,
                    Role = (await _userManager.GetRolesAsync(user))[0],
                    Roles = _roleManager.Roles.ToList()
                }));
            }
            if (role != newRole)
            {
                await _userManager.RemoveFromRoleAsync(user, role);

                await _userManager.AddToRoleAsync(user, newRole);
            }
            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            User user = await _userManager.FindByIdAsync(id);

            if (user == null || user.Deleted || user.UserName == User.Identity.Name)
            {
                return(NotFound());
            }
            ApUserVM model = new ApUserVM()
            {
                Name     = user.Name,
                Username = user.UserName,
                Email    = user.Email,
                Role     = (await _userManager.GetRolesAsync(user))[0],
                Roles    = _roleManager.Roles.ToList()
            };

            return(View(model));
        }