Search in sources :

Example 26 with User

use of com.haozi.app.domain.User in project hello-world by haoziapple.

the class UserMapper method userDTOToUser.

public User userDTOToUser(UserDTO userDTO) {
    if (userDTO == null) {
        return null;
    } else {
        User user = new User();
        user.setId(userDTO.getId());
        user.setLogin(userDTO.getLogin());
        user.setFirstName(userDTO.getFirstName());
        user.setLastName(userDTO.getLastName());
        user.setEmail(userDTO.getEmail());
        user.setImageUrl(userDTO.getImageUrl());
        user.setActivated(userDTO.isActivated());
        user.setLangKey(userDTO.getLangKey());
        Set<Authority> authorities = this.authoritiesFromStrings(userDTO.getAuthorities());
        if (authorities != null) {
            user.setAuthorities(authorities);
        }
        return user;
    }
}
Also used : User(com.haozi.app.domain.User) Authority(com.haozi.app.domain.Authority)

Example 27 with User

use of com.haozi.app.domain.User in project hello-world by haoziapple.

the class UserResource method createUser.

/**
 * POST  /users  : Creates a new user.
 * <p>
 * Creates a new user if the login and email are not already used, and sends an
 * mail with an activation link.
 * The user needs to be activated on creation.
 *
 * @param userDTO the user to create
 * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use
 * @throws URISyntaxException if the Location URI syntax is incorrect
 * @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use
 */
@PostMapping("/users")
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<User> createUser(@Valid @RequestBody UserDTO userDTO) throws URISyntaxException {
    log.debug("REST request to save User : {}", userDTO);
    if (userDTO.getId() != null) {
        throw new BadRequestAlertException("A new user cannot already have an ID", "userManagement", "idexists");
    // Lowercase the user login before comparing with database
    } else if (userRepository.findOneByLogin(userDTO.getLogin().toLowerCase()).isPresent()) {
        throw new LoginAlreadyUsedException();
    } else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) {
        throw new EmailAlreadyUsedException();
    } else {
        User newUser = userService.createUser(userDTO);
        mailService.sendCreationEmail(newUser);
        return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())).headers(HeaderUtil.createAlert("A user is created with identifier " + newUser.getLogin(), newUser.getLogin())).body(newUser);
    }
}
Also used : BadRequestAlertException(com.haozi.app.web.rest.errors.BadRequestAlertException) User(com.haozi.app.domain.User) LoginAlreadyUsedException(com.haozi.app.web.rest.errors.LoginAlreadyUsedException) URI(java.net.URI) EmailAlreadyUsedException(com.haozi.app.web.rest.errors.EmailAlreadyUsedException) Secured(org.springframework.security.access.annotation.Secured) Timed(com.codahale.metrics.annotation.Timed)

Example 28 with User

use of com.haozi.app.domain.User in project hello-world by haoziapple.

the class DomainUserDetailsServiceIntTest method init.

@Before
public void init() {
    userOne = new User();
    userOne.setLogin(USER_ONE_LOGIN);
    userOne.setPassword(RandomStringUtils.random(60));
    userOne.setActivated(true);
    userOne.setEmail(USER_ONE_EMAIL);
    userOne.setFirstName("userOne");
    userOne.setLastName("doe");
    userOne.setLangKey("en");
    userRepository.save(userOne);
    userTwo = new User();
    userTwo.setLogin(USER_TWO_LOGIN);
    userTwo.setPassword(RandomStringUtils.random(60));
    userTwo.setActivated(true);
    userTwo.setEmail(USER_TWO_EMAIL);
    userTwo.setFirstName("userTwo");
    userTwo.setLastName("doe");
    userTwo.setLangKey("en");
    userRepository.save(userTwo);
    userThree = new User();
    userThree.setLogin(USER_THREE_LOGIN);
    userThree.setPassword(RandomStringUtils.random(60));
    userThree.setActivated(false);
    userThree.setEmail(USER_THREE_EMAIL);
    userThree.setFirstName("userThree");
    userThree.setLastName("doe");
    userThree.setLangKey("en");
    userRepository.save(userThree);
}
Also used : User(com.haozi.app.domain.User) Before(org.junit.Before)

Example 29 with User

use of com.haozi.app.domain.User in project hello-world by haoziapple.

the class MailServiceIntTest method testSendEmailFromTemplate.

@Test
public void testSendEmailFromTemplate() throws Exception {
    User user = new User();
    user.setLogin("john");
    user.setEmail("john.doe@example.com");
    user.setLangKey("en");
    mailService.sendEmailFromTemplate(user, "testEmail", "email.test.title");
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    assertThat(message.getSubject()).isEqualTo("test title");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo(user.getEmail());
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent().toString()).isEqualTo("<html>test title, http://127.0.0.1:8080, john</html>\n");
    assertThat(message.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
Also used : User(com.haozi.app.domain.User) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 30 with User

use of com.haozi.app.domain.User in project hello-world by haoziapple.

the class MailServiceIntTest method testCreationEmail.

@Test
public void testCreationEmail() throws Exception {
    User user = new User();
    user.setLangKey(Constants.DEFAULT_LANGUAGE);
    user.setLogin("john");
    user.setEmail("john.doe@example.com");
    mailService.sendCreationEmail(user);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo(user.getEmail());
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent().toString()).isNotEmpty();
    assertThat(message.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
Also used : User(com.haozi.app.domain.User) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.haozi.app.domain.User)52 Test (org.junit.Test)41 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)41 Transactional (org.springframework.transaction.annotation.Transactional)36 WithMockUser (org.springframework.security.test.context.support.WithMockUser)21 ManagedUserVM (com.haozi.app.web.rest.vm.ManagedUserVM)15 UserDTO (com.haozi.app.service.dto.UserDTO)5 Authority (com.haozi.app.domain.Authority)4 Instant (java.time.Instant)4 MimeMessage (javax.mail.internet.MimeMessage)4 Timed (com.codahale.metrics.annotation.Timed)2 KeyAndPasswordVM (com.haozi.app.web.rest.vm.KeyAndPasswordVM)2 LoginVM (com.haozi.app.web.rest.vm.LoginVM)2 Before (org.junit.Before)2 UserRepository (com.haozi.app.repository.UserRepository)1 BadRequestAlertException (com.haozi.app.web.rest.errors.BadRequestAlertException)1 EmailAlreadyUsedException (com.haozi.app.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (com.haozi.app.web.rest.errors.LoginAlreadyUsedException)1 URI (java.net.URI)1 java.util (java.util)1