Search in sources :

Example 16 with User

use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.

the class UserService method editUserProfile.

/**
 * Save User Profile with new encoded password (if it's not blank).
 *
 * @param userProfileDto User Profile to be updated.
 * @return saved User
 */
public User editUserProfile(UUID userId, final UserProfileDto userProfileDto) {
    User existingUser = getUser(userId);
    final boolean encodeNewPassword = !StringUtils.isEmpty(userProfileDto.getPassword());
    if (encodeNewPassword) {
        changeUserPassword(existingUser.getUsername(), userProfileDto.getNewPassword(), userProfileDto.getPassword());
    }
    userMapper.updateFromUserProfileDto(userProfileDto, existingUser);
    return userRepository.save(existingUser);
}
Also used : User(org.motechproject.mots.domain.security.User)

Example 17 with User

use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.

the class UserService method changeUserPassword.

/**
 * Updates user's password.
 *
 * @param username of user which password is about to change.
 * @param newPassword is new password value for user.
 * @param currentPassword is current user's password.
 */
public void changeUserPassword(String username, String newPassword, String currentPassword) {
    User user = getUserByUserName(username);
    if (!passwordsMatch(currentPassword, user.getPassword())) {
        throw new IllegalArgumentException("Current password is incorrect.");
    }
    String newPasswordEncoded = new BCryptPasswordEncoder().encode(newPassword);
    user.setPassword(newPasswordEncoded);
    userRepository.save(user);
}
Also used : User(org.motechproject.mots.domain.security.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Aggregations

User (org.motechproject.mots.domain.security.User)17 HashSet (java.util.HashSet)4 UserRole (org.motechproject.mots.domain.security.UserRole)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Test (org.junit.Test)2 AssignedModules (org.motechproject.mots.domain.AssignedModules)2 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)2 DistrictAssignmentLog (org.motechproject.mots.domain.DistrictAssignmentLog)2 Module (org.motechproject.mots.domain.Module)2 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)2 IvrException (org.motechproject.mots.exception.IvrException)2 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1