public async Task Edit_InvalidId_RedirectToListWithErrorMessage() { // Arrange var userService = Substitute.For <IUserService>(); userService.GetUserByIdAsync(_user1.Id).Returns((User)null); var sut = new UsersController(null, null, _domainService, _logManager, null, null, userService, null); var model = new UserCreateUpdateModel { Id = _user1.Id, FirstName = _user1.FirstName }; // Act var decoratorResult = await sut.Edit(model) as AlertDecoratorResult; // Assert await userService.Received(1).GetUserByIdAsync(Arg.Any <int>()); await userService.Received(0).UpdateUserAsync(Arg.Any <User>()); Assert.IsNotNull(decoratorResult); Assert.AreEqual(decoratorResult.Message, "Please select a user."); var routeResult = decoratorResult.InnerResult as RedirectToRouteResult; Assert.IsNotNull(routeResult); Assert.IsFalse(routeResult.Permanent); Assert.AreEqual(routeResult.RouteValues["action"], "List"); }
public UserViewModel UpdateUser(int userId, UserCreateUpdateModel model) { User entity = _repo.GetUser(userId); if (entity == null) { throw new NotFoundApiException("User does not exist"); } if (model.Password == null) { model.Password = entity.Password; } // validation var results = _userValidator.Validate(model).ToArray(); if (results.Length > 0) { throw new ValidationApiException(results); } entity = _userMapper.EntityMapper(model); entity.Id = userId; entity = _repo.UpdateUser(entity); return(_userMapper.ViewMapper(entity)); }
public async Task <ActionResult> Edit(UserCreateUpdateModel model) { if (ModelState.IsValid) { var user = await _userService.GetUserByIdAsync(model.Id); if (user == null) { return(RedirectToAction("List").WithError("Please select a user.")); } user.FirstName = model.FirstName; user.LastName = model.LastName; user.Active = model.Active; user.ModifiedOn = _dateTime.Now; user.ModifiedBy = _webUserSession.UserName; bool isDeveloper = _webUserSession.IsInRole(Constants.RoleNames.Developer); var allRoles = await _roleService.GetAllRoles(); foreach (var role in allRoles) { // Only developer can add developer role. if (role.Name == Constants.RoleNames.Developer && !isDeveloper) { continue; } if (model.SelectedRoleIds.Any(r => r == role.Id)) { if (user.Roles.All(r => r.Id != role.Id)) { user.Roles.Add(role); } } else { if (user.Roles.Any(r => r.Id == role.Id)) { user.Roles.Remove(role); } } } await _userService.UpdateUserAsync(user); return(RedirectToAction("List").WithSuccess($"{user.FirstName}'s account was updated successfully.")); } // If we got this far, something failed, redisplay form model.AvailableRoleNames = await GetAvailableRoleNames(); model.AvailableDomains = await GetAvailableDomains(); return(View("Edit", model)); }
public async Task <ActionResult> Create() { var model = new UserCreateUpdateModel { AvailableRoleNames = await GetAvailableRoleNames(), AvailableDomains = await GetAvailableDomains(), Active = true }; return(View("Create", model)); }
public async Task <ActionResult> Create(UserCreateUpdateModel model) { if (ModelState.IsValid) { var checkingUser = await _userService.GetUserByUserNameAsync(model.UserName); if (checkingUser != null) { return(RedirectToAction("List").WithError($"User with same username {model.UserName} alredy exists.")); } var user = new User { UserName = model.UserName.ToLowerInvariant(), FirstName = model.FirstName, LastName = model.LastName, Active = model.Active, LastLoginDate = _dateTime.Now, CreatedBy = _webUserSession.UserName, CreatedOn = _dateTime.Now, ModifiedBy = _webUserSession.UserName, ModifiedOn = _dateTime.Now }; bool isDeveloper = _webUserSession.IsInRole(Constants.RoleNames.Developer); var allRoles = await _roleService.GetAllRoles(); foreach (var role in allRoles) { // Only developer can add developer role. if (role.Name == Constants.RoleNames.Developer && !isDeveloper) { continue; } if (model.SelectedRoleIds.Any(r => r == role.Id)) { user.Roles.Add(role); } } await _userService.AddUserAsync(user); await _messageService.SendAddNewUserNotification(user); return(RedirectToAction("List").WithSuccess($"{user.FirstName}'s account was created successfully.")); } // If we got this far, something failed, redisplay form model.AvailableRoleNames = await GetAvailableRoleNames(); return(View("Create", model)); }
public User EntityMapper(UserCreateUpdateModel model) { return(new User() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, PhoneNumber = model.PhoneNumber, Password = model.Password, AdminRole = model.AdminRole, Locked = model.Locked }); }
public async Task Create_PostValidUser_RedirectToListWithSuccessfulMessage() { // Arrange var userService = Substitute.For <IUserService>(); userService.AddUserAsync(Arg.Any <User>()).Returns(1); userService.GetUserByUserNameAsync(Arg.Any <string>()).Returns((User)null); var roleService = Substitute.For <IRoleService>(); roleService.GetAllRoles().Returns(_roles); var webUserSession = Substitute.For <IWebUserSession>(); webUserSession.IsInRole(Constants.RoleNames.Developer).Returns(true); var sut = new UsersController(null, _dateTime, _domainService, _logManager, _messageService, roleService, userService, webUserSession); var model = new UserCreateUpdateModel { UserName = _user1.UserName, FirstName = _user1.FirstName }; // Act var decoratorResult = await sut.Create(model) as AlertDecoratorResult; // Assert await userService.Received(1).GetUserByUserNameAsync(Arg.Any <string>()); await userService.Received(1).AddUserAsync(Arg.Any <User>()); await roleService.Received(1).GetAllRoles(); await _messageService.Received(1).SendAddNewUserNotification(Arg.Any <User>()); Assert.IsNotNull(decoratorResult); Assert.AreEqual(decoratorResult.Message, $"{_user1.FirstName}'s account was created successfully."); var routeResult = decoratorResult.InnerResult as RedirectToRouteResult; Assert.IsNotNull(routeResult); Assert.IsFalse(routeResult.Permanent); Assert.AreEqual(routeResult.RouteValues["action"], "List"); }
public UserViewModel CreateUser(UserCreateUpdateModel model) { // validation var results = _userValidator.Validate(model).ToArray(); if (results.Length > 0) { throw new ValidationApiException(results); } if (_repo.GetUsers().Any(user => user.Email == model.Email)) { throw new DuplicateUserApiException("Email address already in use"); } User entity = _userMapper.EntityMapper(model); entity = _repo.CreateUser(entity); return(_userMapper.ViewMapper(entity)); }
public async Task Edit_PostValidId_RedirectToListWithSuccessfulMessage() { // Arrange var userService = Substitute.For <IUserService>(); userService.GetUserByIdAsync(_user1.Id).Returns(_user1); var roleService = Substitute.For <IRoleService>(); roleService.GetAllRoles().Returns(_roles); var webUserSession = Substitute.For <IWebUserSession>(); webUserSession.IsInRole(Arg.Any <string>()).Returns(true); var sut = new UsersController(null, _dateTime, _domainService, _logManager, _messageService, roleService, userService, webUserSession); var model = new UserCreateUpdateModel { Id = _user1.Id, FirstName = _user1.FirstName }; // Act var decoratorResult = await sut.Edit(model) as AlertDecoratorResult; // Assert await userService.Received(1).GetUserByIdAsync(Arg.Any <int>()); await roleService.Received(1).GetAllRoles(); await userService.Received(1).UpdateUserAsync(Arg.Any <User>()); Assert.IsNotNull(decoratorResult); Assert.AreEqual(decoratorResult.Message, $"{_user1.FirstName}'s account was updated successfully."); var routeResult = decoratorResult.InnerResult as RedirectToRouteResult; Assert.IsNotNull(routeResult); Assert.IsFalse(routeResult.Permanent); Assert.AreEqual(routeResult.RouteValues["action"], "List"); }
public IEnumerable <ValidationResult> Validate(UserCreateUpdateModel model) { var firstNameResult = ValidateFirstName(model.FirstName); if (firstNameResult != null) { yield return(firstNameResult); } var lastNameResult = ValidateLastName(model.LastName); if (lastNameResult != null) { yield return(lastNameResult); } var emailResult = ValidateEmail(model.Email); if (emailResult != null) { yield return(emailResult); } var phoneNumberResult = ValidatePhoneNumber(model.PhoneNumber); if (phoneNumberResult != null) { yield return(phoneNumberResult); } var passwordResult = ValidatePassword(model.Password); if (passwordResult != null) { yield return(passwordResult); } }
public async Task Create_PostUserAlreadyExists_RedirectToListWithErrorMessage() { // Arrange var userService = Substitute.For <IUserService>(); userService.GetUserByUserNameAsync(Arg.Any <string>()).Returns(_user1); var roleService = Substitute.For <IRoleService>(); var webUserSession = Substitute.For <IWebUserSession>(); var sut = new UsersController(null, null, _domainService, _logManager, _messageService, roleService, userService, webUserSession); var model = new UserCreateUpdateModel { UserName = "******" }; // Act var decoratorResult = await sut.Create(model) as AlertDecoratorResult; // Assert await userService.Received(1).GetUserByUserNameAsync(Arg.Any <string>()); await userService.Received(0).AddUserAsync(Arg.Any <User>()); await roleService.Received(0).GetAllRoles(); await _messageService.Received(0).SendAddNewUserNotification(Arg.Any <User>()); Assert.IsNotNull(decoratorResult); Assert.AreEqual(decoratorResult.Message, $"User with same username {model.UserName} alredy exists."); var routeResult = decoratorResult.InnerResult as RedirectToRouteResult; Assert.IsNotNull(routeResult); Assert.IsFalse(routeResult.Permanent); Assert.AreEqual(routeResult.RouteValues["action"], "List"); }
public ActionResult <UserViewModel> UpdateUser(int userId, UserCreateUpdateModel model) { var user = _userService.UpdateUser(userId, model); return(Ok(user)); }
public ActionResult <UserViewModel> RegisterUser(UserCreateUpdateModel model) { var user = _userService.CreateUser(model); return(CreatedAtAction(nameof(GetUserById), new { id = user.Id }, user)); }
public Task <UserModel> Insert([FromBody] UserCreateUpdateModel model) { return(_userCommonController.Insert(model)); }
public Task <UserModel> Update(string id, [FromBody] UserCreateUpdateModel model) { return(_userCommonController.Update(id, model)); }
public static User ToDal(this UserCreateUpdateModel model, User user = null) { return(GetInstance().Map(model, user)); }