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);
}
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);
}
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);
}
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());
}
}
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;
}
Aggregations