public ApplicationUser UpdateApplicationUser(string userId, UpdateApplicationUserDto updateApplicationUserDto)
        {
            var user = _context.ApplicationUsers.Find(userId);

            if (user != null)
            {
                user.UserName = updateApplicationUserDto.Name;
                _context.SaveChanges();
            }
            return(user);
        }
        public async Task <IActionResult> EditUser(UpdateApplicationUserDto dto)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser applicationUserToUpdate = await _db.Users.Where(u => u.Id == dto.ApplicationUserId)
                                                          .Include(u => u.Roles) /* Really Important */
                                                          .FirstOrDefaultAsync();

                if (applicationUserToUpdate is null)
                {
                    return(NotFound());
                }

                /* Easy :) */
                List <ApplicationRole> selectedRoles = await _db.Roles.Where(r => dto.Roles.Contains(r.Name)).ToListAsync();

                applicationUserToUpdate.Roles = selectedRoles;
                /* Easy :) */

                applicationUserToUpdate.FirstName        = dto.FirstName;
                applicationUserToUpdate.LastName         = dto.LastName;
                applicationUserToUpdate.Email            = dto.Email;
                applicationUserToUpdate.PhoneNumber      = dto.PhoneNumber;
                applicationUserToUpdate.ConcurrencyStamp = Guid.NewGuid().ToString();

                await _userManager.UpdateNormalizedEmailAsync(applicationUserToUpdate);    // deferred

                await _userManager.UpdateNormalizedUserNameAsync(applicationUserToUpdate); // deferred

                await _db.SaveChangesAsync();

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

            await GetRolesSelectList();

            return(View(dto));
        }
Exemple #3
0
 public ActionResult <ApplicationUser> UpdateApplicationUser(string userId, UpdateApplicationUserDto updateApplicationUserDto)
 {
     return(applicationUserService.UpdateApplicationUser(userId, updateApplicationUserDto));
 }