Search in sources :

Example 6 with UserEntity

use of py.org.fundacionparaguaya.pspserver.security.entities.UserEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class PasswordResetTokenServiceImpl method validatePasswordResetToken.

@Override
public void validatePasswordResetToken(String token, Long userId, String password, String repeatPassword) {
    checkArgument(userId > 0, i18n.translate("argument.nonNegative", userId));
    UserEntity userEntity = userRepository.findOne(userId);
    if (userEntity == null) {
        throw new UnknownResourceException(i18n.translate("user.notExist", userId));
    }
    PasswordResetTokenEntity passwordResetTokenEntity = passwordTokenRepository.findByToken(token);
    if (passwordResetTokenEntity == null || passwordResetTokenEntity.getUser().getId().longValue() != userId.longValue()) {
        throw new CustomParameterizedException(i18n.translate("email.invalidToken", token));
    }
    Calendar cal = Calendar.getInstance();
    if ((passwordResetTokenEntity.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
        throw new CustomParameterizedException(i18n.translate("email.expiredToken", token));
    }
    if (!password.equals(repeatPassword)) {
        throw new CustomParameterizedException("Password does not match the confirm password");
    }
    userEntity.setPass(encryptPassword(repeatPassword));
    userRepository.save(userEntity);
}
Also used : PasswordResetTokenEntity(py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) Calendar(java.util.Calendar) UnknownResourceException(py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)

Example 7 with UserEntity

use of py.org.fundacionparaguaya.pspserver.security.entities.UserEntity in project FP-PSP-SERVER by FundacionParaguaya.

the class UserServiceImpl method addUserWithRoleAndApplication.

@Override
public UserDTO addUserWithRoleAndApplication(UserRoleApplicationDTO userRoleApplicationDTO, UserDetailsDTO userDetails) {
    userRepository.findOneByUsername(userRoleApplicationDTO.getUsername()).ifPresent(user -> {
        throw new CustomParameterizedException("User already exists.", new ImmutableMultimap.Builder<String, String>().put("username", user.getUsername()).build().asMap());
    });
    UserEntity user = new UserEntity();
    user.setUsername(userRoleApplicationDTO.getUsername());
    user.setEmail(userRoleApplicationDTO.getEmail());
    user.setPass(new BCryptPasswordEncoder().encode(userRoleApplicationDTO.getPass()));
    user.setActive(true);
    UserEntity newUser = userRepository.save(user);
    if (userRoleApplicationDTO.getRole() != null) {
        createUserRole(newUser, userRoleApplicationDTO.getRole());
    }
    if (userRoleApplicationDTO.getOrganizationId() != null) {
        createUserOrganization(newUser, userRoleApplicationDTO);
    } else if (userRoleApplicationDTO.getApplicationId() != null) {
        createUserApplication(newUser, userRoleApplicationDTO);
    }
    return userMapper.entityToDto(newUser);
}
Also used : CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Aggregations

UserEntity (py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)7 CustomParameterizedException (py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)4 UnknownResourceException (py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 NoSuchElementException (java.util.NoSuchElementException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)1 ApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity)1 OrganizationEntity (py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity)1 UserApplicationEntity (py.org.fundacionparaguaya.pspserver.network.entities.UserApplicationEntity)1 PasswordResetTokenEntity (py.org.fundacionparaguaya.pspserver.security.entities.PasswordResetTokenEntity)1 TermCondPolEntity (py.org.fundacionparaguaya.pspserver.security.entities.TermCondPolEntity)1 UserRoleEntity (py.org.fundacionparaguaya.pspserver.security.entities.UserRoleEntity)1 SnapshotDraftEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotDraftEntity)1 SnapshotEconomicEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SnapshotEconomicEntity)1 SurveyEntity (py.org.fundacionparaguaya.pspserver.surveys.entities.SurveyEntity)1