Search in sources :

Example 26 with User

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

the class UserMapper method userFromId.

public User userFromId(Long id) {
    if (id == null) {
        return null;
    }
    User user = new User();
    user.setId(id);
    return user;
}
Also used : User(com.arnaugarcia.uplace.domain.User)

Example 27 with User

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

the class AccountResource method registerAccount.

/**
 * POST  /register : register the user.
 *
 * @param managedUserVM the managed user View Model
 * @throws InvalidPasswordException 400 (Bad Request) if the password is incorrect
 * @throws EmailAlreadyUsedException 400 (Bad Request) if the email is already used
 * @throws LoginAlreadyUsedException 400 (Bad Request) if the login is already used
 */
@PostMapping("/register")
@Timed
@ResponseStatus(HttpStatus.CREATED)
public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
    if (!checkPasswordLength(managedUserVM.getPassword())) {
        throw new InvalidPasswordException();
    }
    userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).ifPresent(u -> {
        throw new LoginAlreadyUsedException();
    });
    userRepository.findOneByEmailIgnoreCase(managedUserVM.getEmail()).ifPresent(u -> {
        throw new EmailAlreadyUsedException();
    });
    User user = userService.registerUser(managedUserVM, managedUserVM.getPassword());
    mailService.sendActivationEmail(user);
}
Also used : User(com.arnaugarcia.uplace.domain.User) Timed(com.codahale.metrics.annotation.Timed)

Example 28 with User

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

the class NotificationService method updates.

@Transactional()
public List<Notification> updates(List<Notification> notifications) {
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // if there are a notification that does not belongs to the user and isn't admin... error
    notifications.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);
        }
    });
    return notificationRepository.save(notifications);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with User

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

the class NotificationService method markAs.

@Transactional(readOnly = true)
public List<Notification> markAs(List<Notification> notifications, Boolean status) {
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    notifications.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);
        } else {
            notification.setRead(status);
        }
    });
    return notificationRepository.save(notifications);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 30 with User

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

the class NotificationService method findOneByType.

/**
 * Get one notification by id.
 *
 * @param id the id of the entity
 * @return the entity
 */
@Transactional(readOnly = true)
public Notification findOneByType(NotificationType notificationType, Long id) {
    log.debug("Request to get Notification : {}", id);
    Notification notification = null;
    switch(notificationType) {
        case NOTIFICATION:
            notification = notificationRepository.findOneByType(NotificationType.NOTIFICATION, id);
            break;
        case REQUEST:
            notification = notificationRepository.findOneByType(NotificationType.REQUEST, id);
            break;
        case ALERT:
            throw new BadRequestAlertException("Not yet implemented", ENTITY_REQUEST, ErrorConstants.NOT_IMPLEMENTED);
    }
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    if (notification == null || !notification.getType().equals(NotificationType.NOTIFICATION)) {
        throw new BadRequestAlertException("No notification was found with this ID", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_ID);
    }
    // If the notification.user does not match and isn't admin... error
    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);
    }
    return notification;
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Notification(com.arnaugarcia.uplace.domain.Notification) Transactional(org.springframework.transaction.annotation.Transactional)

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