public async Task <ActionResult> Edit(EditUserViewModel viewModel) { if (ModelState.IsValid) { var dto = new GymUserDto() { Id = viewModel.Id, Email = viewModel.Email, UserName = viewModel.UserName, FirstName = viewModel.FirstName, LastName = viewModel.LastName, RoleName = viewModel.RoleName }; var isSuccess = await _accountService.EditUser(dto); if (!isSuccess) { _logger.Info($"An error occurred while editing account ID {dto.Id}"); return(View(viewModel)); } _logger.Info($"User Id {dto.Id} account details have been edited by admin"); } return(RedirectToAction("Index")); }
public IEnumerable <string> AddGymUser(GymUserDto dto) { try { var userManager = new UserManager <GymUser>(new UserStore <GymUser>(_context)); var gymUser = new GymUser() { Email = dto.Email, UserName = dto.UserName ?? dto.Email, FirstName = dto.FirstName ?? "", LastName = dto.LastName ?? "", DateCreated = DateTime.UtcNow }; var result = userManager.Create(gymUser, dto.Password); if (result.Succeeded) { userManager.AddToRole(gymUser.Id, RoleNames.AttendeeName); _logger.Info($"Gym user ID {gymUser.Id} successfully created"); return(null); } return(result.Errors); } catch (Exception e) { _logger.Error(e); return(new List <string>() { "Error occurred creating a new member" }); } }
public EditAccountViewModel(GymUserDto dto) { Id = dto.Id; FirstName = dto.FirstName; LastName = dto.LastName; UserName = dto.UserName; Email = dto.Email; RoleName = dto.RoleName; }
public async Task <ActionResult> Register(RegisterViewModel model) //public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { //var user = new GymUser { UserName = model.Email, Email = model.Email }; //var result = await UserManager.CreateAsync(user, model.Password); //if (result.Succeeded) //{ // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // // Send an email with this link // // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); // return RedirectToAction("Index", "Home"); //} var gymUser = new GymUserDto() { UserName = model.UserName, Email = model.Email, Password = model.Password }; var errors = await _accountService.AddUserByRegistration(gymUser); if (errors != null) { foreach (var error in errors) { ModelState.AddModelError("", error); } } else { var newlyCreatedUser = await _accountService.GetUserByEmailAddress(model.Email); var user = new GymUser() { Id = newlyCreatedUser.Id, Email = model.Email, UserName = model.Email }; await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Home")); } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> Delete(GymUserDto gymUser) { var isSuccess = await _accountService.DeleteUser(gymUser); if (!isSuccess) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } return(RedirectToAction("Index")); }
public bool EditUser(GymUserDto dto) { try { if (dto == null) { _logger.Warn("Parameter passed is null"); return(false); } var userManager = new UserManager <GymUser>(new UserStore <GymUser>(_context)); //var user = _context.Users.FirstOrDefault(x => x.Id == gymUser.Id); var user = userManager.Users.FirstOrDefault(x => x.Id == dto.Id); if (user == null) { _logger.Warn($"Gymuser ID {dto.Id} not found in database"); return(false); } // todo: replace with automapper user.DateModified = DateTime.UtcNow; user.Email = dto.Email; user.FirstName = dto.FirstName; user.LastName = dto.LastName; user.UserName = dto.UserName; var currentRoleId = user.Roles.FirstOrDefault().RoleId; var currentRoleName = _context.Roles.FirstOrDefault(x => x.Id == currentRoleId).Name; if (currentRoleName != dto.RoleName) { userManager.RemoveFromRole(user.Id, currentRoleName); userManager.AddToRole(user.Id, dto.RoleName); _logger.Info($"Gymuser ID {dto.Id} has changed role from {currentRoleName} to {dto.RoleName}"); } // todo: might have to do something with password soon _context.Entry(user).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); _logger.Info($"Gymuser ID {dto.Id} has been updated"); return(true); } catch (Exception e) { _logger.Error(e); return(false); } }
public bool DeleteUser(GymUserDto dto) { try { if (dto == null) { _logger.Warn("Parameter passed is null"); return(false); } var user = _context.Users.FirstOrDefault(x => x.Id == dto.Id); if (user == null) { _logger.Warn($"User ID {dto.Id} not found in database"); return(false); } // if instructor, remove association between user and scheduled class. do no remove scheduled class var scheduledClasses = _context.ScheduledClasses.Where(x => x.InstructorId == dto.Id).ToList(); if (scheduledClasses != null && scheduledClasses.Any()) { scheduledClasses.ForEach(sc => { sc.InstructorId = null; }); _logger.Info($"Instructor ID {dto.Id} removed from scheduled classes with IDs {scheduledClasses.Select(x => x.Id).ToList()}"); } // remove associated attendances var attendances = _context.ClassAttendances.Where(x => x.AttendeeId == dto.Id).ToList(); if (attendances != null && attendances.Any()) { _context.ClassAttendances.RemoveRange(attendances); _logger.Info($"Gym user ID {dto.Id} unsigned from class attendances with IDs {attendances.Select(x => x.Id).ToList()}"); } //remove userroles var userRoles = _context.UserRoles.Where(x => x.UserId == dto.Id).ToList(); _context.UserRoles.RemoveRange(userRoles); _context.Users.Remove(user); _context.SaveChanges(); _logger.Info($"Gym user ID {dto.Id} successfully deleted"); return(true); } catch (Exception e) { _logger.Error(e); return(false); } }
public async Task <IEnumerable <string> > AddUserByPortal(CreateUserViewModel viewModel) { var dto = new GymUserDto() { Email = viewModel.Email, UserName = viewModel.UserName ?? viewModel.Email, FirstName = viewModel.FirstName ?? "", LastName = viewModel.LastName ?? "", Password = viewModel.Password }; var listOfErrors = _gymUserWriter.AddGymUser(dto); return(await Task.FromResult(listOfErrors)); }
public async Task <bool> DeleteUser(GymUserDto gymUser) { bool result = _gymUserWriter.DeleteUser(gymUser); return(await Task.FromResult(result)); }
public async Task <IEnumerable <string> > AddUserByRegistration(GymUserDto dto) { var listOfErrors = _gymUserWriter.AddGymUser(dto); return(await Task.FromResult(listOfErrors)); }