Search in sources :

Example 31 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class NotificationService method saveRequest.

/**
 * Save a request.
 *
 * @param request the entity to save
 * @return the persisted entity
 */
public Notification saveRequest(Notification request) {
    log.debug("Request to save Notification : {}", request);
    // Getting the current user
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // If is a new notification the date must be now
    request.setCreation(ZonedDateTime.now());
    // If the current user hasn't the ROLE_ADMIN sets by default the user for security reasons
    if (!SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        request.setUser(user);
    }
    request.setType(NotificationType.REQUEST);
    return notificationRepository.save(request);
}
Also used : User(com.arnaugarcia.uplace.domain.User)

Example 32 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class NotificationService method deletes.

public void deletes(List<Notification> notificationList) {
    log.debug("Request to delete a list of Notification : {}", notificationList.size());
    // TODO: Check if the notification exists and is a notification
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // Checks if the user is the correct user
    notificationList.forEach(notification -> {
        if (!notification.getUser().equals(user) && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
            throw new BadRequestAlertException("This notification doesn't belongs to you :)", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_USER);
        }
    });
    notificationRepository.delete(notificationList);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User)

Example 33 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class NotificationService method saveNotification.

/**
 * Save a notification.
 *
 * @param notification the entity to save
 * @return the persisted entity
 */
public Notification saveNotification(Notification notification) {
    log.debug("Request to save Notification : {}", notification);
    // Getting the current user
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // If is a new notification the date must be now
    notification.setCreation(ZonedDateTime.now());
    // If the current user hasn't the ROLE_ADMIN sets by default the user for security reasons
    if (!SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        notification.setUser(user);
    }
    // Setting the type to notification
    notification.setType(NotificationType.NOTIFICATION);
    return notificationRepository.save(notification);
}
Also used : User(com.arnaugarcia.uplace.domain.User)

Example 34 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class UserService method removeNotActivatedUsers.

/**
 * Not activated users should be automatically deleted after 3 days.
 * <p>
 * This is scheduled to get fired everyday, at 01:00 (am).
 */
@Scheduled(cron = "0 0 1 * * ?")
public void removeNotActivatedUsers() {
    List<User> users = userRepository.findAllByActivatedIsFalseAndCreatedDateBefore(Instant.now().minus(3, ChronoUnit.DAYS));
    for (User user : users) {
        log.debug("Deleting not activated user {}", user.getLogin());
        userRepository.delete(user);
        cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).evict(user.getLogin());
        cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).evict(user.getEmail());
    }
}
Also used : User(com.arnaugarcia.uplace.domain.User) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 35 with User

use of com.arnaugarcia.uplace.domain.User in project uplace.es by Uplace.

the class UserService method registerUser.

public User registerUser(UserDTO userDTO, String password) {
    User newUser = new User();
    Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
    Set<Authority> authorities = new HashSet<>();
    String encryptedPassword = passwordEncoder.encode(password);
    newUser.setLogin(userDTO.getLogin());
    // new user gets initially a generated password
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(userDTO.getFirstName());
    newUser.setLastName(userDTO.getLastName());
    newUser.setEmail(userDTO.getEmail());
    newUser.setImageUrl(userDTO.getImageUrl());
    newUser.setLangKey(userDTO.getLangKey());
    // new user is not active
    newUser.setActivated(false);
    // new user gets registration key
    newUser.setActivationKey(RandomUtil.generateActivationKey());
    authorities.add(authority);
    newUser.setAuthorities(authorities);
    userRepository.save(newUser);
    cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).evict(newUser.getLogin());
    cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).evict(newUser.getEmail());
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(com.arnaugarcia.uplace.domain.User) Authority(com.arnaugarcia.uplace.domain.Authority)

Aggregations

User (com.arnaugarcia.uplace.domain.User)61 Test (org.junit.Test)41 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)41 Transactional (org.springframework.transaction.annotation.Transactional)39 WithMockUser (org.springframework.security.test.context.support.WithMockUser)21 ManagedUserVM (com.arnaugarcia.uplace.web.rest.vm.ManagedUserVM)15 BadRequestAlertException (com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException)6 UserDTO (com.arnaugarcia.uplace.service.dto.UserDTO)5 Authority (com.arnaugarcia.uplace.domain.Authority)4 Instant (java.time.Instant)4 MimeMessage (javax.mail.internet.MimeMessage)4 Notification (com.arnaugarcia.uplace.domain.Notification)3 KeyAndPasswordVM (com.arnaugarcia.uplace.web.rest.vm.KeyAndPasswordVM)2 LoginVM (com.arnaugarcia.uplace.web.rest.vm.LoginVM)2 Timed (com.codahale.metrics.annotation.Timed)2 Before (org.junit.Before)2 Agent (com.arnaugarcia.uplace.domain.Agent)1 UserRepository (com.arnaugarcia.uplace.repository.UserRepository)1 EmailAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.LoginAlreadyUsedException)1