public async Task <UserDTO> Handle(UpdateReaderCommand command, CancellationToken cancellationToken)
        {
            var user = await _userRepository.GetAsync(command.UserId);

            if (user == null)
            {
                throw new UserNotFoundException(command.UserId);
            }
            if (user.Role == UserRolesConsts.Librarian)
            {
                throw new LibraryDomainException("Cannot modify user with role librarian.");
            }

            user.FirstName = command.FirstName;
            user.LastName  = command.LastName;
            user.Email     = new Email(command.Email);
            var(passwordHash, passwordSalt) = _passwordHashService.CreatePasswordHash(command.Password);
            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _userRepository.Update(user);
            await _userRepository.UnitOfWork.SaveChangesAsync();

            return(_mapper.Map(user));
        }
示例#2
0
        private void SetPasswordHash(User user)
        {
            string passwordSalt = GenerateNewSecretKey();
            string passwordHash = passwordHashService.CreatePasswordHash(user.Password, passwordSalt);

            user.Password     = passwordHash;
            user.PasswordSalt = passwordSalt;
        }
示例#3
0
        public async Task Create(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if ((await _userDbOperations.GetAllAsync(e => e.UserId == user.UserId)).Any())
            {
                throw new AppException("UserId \"" + user.UserId + "\" is already taken");
            }

            _passwordHashService.CreatePasswordHash(password, out var passwordHash, out var passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _userDbOperations.CreateAsync(user).ConfigureAwait(false);
        }
        public async Task <User> Register(User user, string password)
        {
            byte[] passwordHash, passwordSalt;
            _hashService.CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }
示例#5
0
        public void SeedUsers()
        {
            if (!_context.Users.Any())
            {
                var userData = System.IO.File.ReadAllText("Data/UserSeedData.json");
                var users    = JsonConvert.DeserializeObject <List <User> >(userData);
                foreach (var user in users)
                {
                    byte[] passwordHash, passwordSalt;
                    _hashService.CreatePasswordHash("password", out passwordHash, out passwordSalt);
                    user.PasswordHash = passwordHash;
                    user.PasswordSalt = passwordSalt;
                    _context.Users.Add(user);
                }

                _context.SaveChanges();
            }
        }
示例#6
0
        public async Task <User> Register(RegisterUserModel userModel)
        {
            var existingUser = await _userRepository.GetAsync(userModel.Email);

            if (existingUser != null)
            {
                throw new LibraryDomainException($"User with email {userModel.Email} already exists.");
            }

            var user = new User(userModel.FirstName, userModel.LastName, new Email(userModel.Email), userModel.Role);

            var(passwordHash, passwordSalt) = _passwordHashService.CreatePasswordHash(userModel.Password);
            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _userRepository.Create(user);
            await _userRepository.UnitOfWork.SaveChangesAsync();

            return(user);
        }