public async Task <bool> ChangePasswordAsync(string oldPassword, string newPassword, int userId) { var existing = await _usersRepository.GetUserByIdAsync(userId); if (existing == null) { throw new Exception("User does not exists"); } if (PasswordHelpers.GenerateHashForSaltAndPassword(existing.Salt, oldPassword) != existing.PasswordHash) { throw new Exception("Old password is not valid"); } if (!PasswordHelpers.IsValidPassword(newPassword, new PasswordRequirements())) { throw new Exception("Password doesn't meet requirements"); } string salt; string passwordHash; PasswordHelpers.GenerateSaltAndHash(newPassword, out salt, out passwordHash); return(await _usersRepository.ChangePasswordAsync(userId, passwordHash, salt)); }
public async Task <UserModel> AddAsync(UserModel model, string password, int clientId, int userId) { if (!await _permissionManager.HasPermission(clientId, userId, Permission.CanAddUser)) { throw new Exception("User has not permission to perform this operation"); } var existing = _usersRepository.GetUserByEmail(model.Email); if (existing != null) { throw new Exception("Email already exists"); //errors = "Email already exists"; //return new EnumerableQuery<MonsciergeDataModel.User>(new MonsciergeDataModel.User[0]); } if (!PasswordHelpers.IsValidPassword(password, new PasswordRequirements())) { throw new Exception("Password doesn't meet requirements"); //errors = "Password doesn't meet requirements"; //return new EnumerableQuery<MonsciergeDataModel.User>(new MonsciergeDataModel.User[0]); } var user = new User() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, ClientId = clientId, Role = (int)UserRoles.HRUser, IsActive = true, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; string salt; string passwordHash; PasswordHelpers.GenerateSaltAndHash(password, out salt, out passwordHash); user.Salt = salt; user.PasswordHash = passwordHash; user = await _usersRepository.AddAsync(user); return(_usersMapper.ConvertToModel(user)); }