Search in sources :

Example 6 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

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(de.tum.in.www1.artemis.domain.User) Authority(de.tum.in.www1.artemis.domain.Authority)

Example 7 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class UserResource method getAllUsers.

/**
 * GET  /users : get all users.
 *
 * @param pageable the pagination information
 * @return the ResponseEntity with status 200 (OK) and with body all users
 */
@GetMapping("/users")
@Timed
public ResponseEntity<List<UserDTO>> getAllUsers(@ApiParam Pageable pageable) {
    final Page<UserDTO> page = userService.getAllManagedUsers(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/users");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) Timed(com.codahale.metrics.annotation.Timed)

Example 8 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class UserService method createUser.

public User createUser(UserDTO userDTO) {
    User user = new User();
    user.setLogin(userDTO.getLogin());
    user.setFirstName(userDTO.getFirstName());
    user.setLastName(userDTO.getLastName());
    user.setEmail(userDTO.getEmail());
    user.setImageUrl(userDTO.getImageUrl());
    if (userDTO.getLangKey() == null) {
        // default language
        user.setLangKey(Constants.DEFAULT_LANGUAGE);
    } else {
        user.setLangKey(userDTO.getLangKey());
    }
    if (userDTO.getAuthorities() != null) {
        Set<Authority> authorities = userDTO.getAuthorities().stream().map(authorityRepository::findOne).collect(Collectors.toSet());
        user.setAuthorities(authorities);
    }
    String encryptedPassword = passwordEncoder().encode(RandomUtil.generatePassword());
    user.setPassword(encryptedPassword);
    user.setResetKey(RandomUtil.generateResetKey());
    user.setResetDate(Instant.now());
    user.setActivated(true);
    userRepository.save(user);
    log.debug("Created Information for User: {}", user);
    return user;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Authority(de.tum.in.www1.artemis.domain.Authority)

Example 9 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class AccountResourceIntTest method testSaveExistingEmail.

@Test
@Transactional
@WithMockUser("save-existing-email")
public void testSaveExistingEmail() throws Exception {
    User user = new User();
    user.setLogin("save-existing-email");
    user.setEmail("save-existing-email@example.com");
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    userRepository.saveAndFlush(user);
    User anotherUser = new User();
    anotherUser.setLogin("save-existing-email2");
    anotherUser.setEmail("save-existing-email2@example.com");
    anotherUser.setPassword(RandomStringUtils.random(60));
    anotherUser.setActivated(true);
    userRepository.saveAndFlush(anotherUser);
    UserDTO userDTO = new UserDTO(// id
    null, // login
    "not-used", // firstName
    "firstname", // lastName
    "lastname", // email
    "save-existing-email2@example.com", // activated
    false, // imageUrl
    "http://placehold.it/50x50", // langKey
    Constants.DEFAULT_LANGUAGE, // createdBy
    null, // createdDate
    null, // lastModifiedBy
    null, // lastModifiedDate
    null, new HashSet<>(Collections.singletonList(AuthoritiesConstants.ADMIN)), new ArrayList<String>());
    restMvc.perform(post("/api/account").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(userDTO))).andExpect(status().isBadRequest());
    User updatedUser = userRepository.findOneByLogin("save-existing-email").orElse(null);
    assertThat(updatedUser.getEmail()).isEqualTo("save-existing-email@example.com");
}
Also used : User(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserDTO

use of de.tum.in.www1.artemis.service.dto.UserDTO in project ArTEMiS by ls1intum.

the class UserResourceIntTest method testUserToUserDTO.

@Test
public void testUserToUserDTO() {
    user.setId(DEFAULT_ID);
    user.setCreatedBy(DEFAULT_LOGIN);
    user.setCreatedDate(Instant.now());
    user.setLastModifiedBy(DEFAULT_LOGIN);
    user.setLastModifiedDate(Instant.now());
    Set<Authority> authorities = new HashSet<>();
    Authority authority = new Authority();
    authority.setName(AuthoritiesConstants.USER);
    authorities.add(authority);
    user.setAuthorities(authorities);
    UserDTO userDTO = userMapper.userToUserDTO(user);
    assertThat(userDTO.getId()).isEqualTo(DEFAULT_ID);
    assertThat(userDTO.getLogin()).isEqualTo(DEFAULT_LOGIN);
    assertThat(userDTO.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
    assertThat(userDTO.getLastName()).isEqualTo(DEFAULT_LASTNAME);
    assertThat(userDTO.getEmail()).isEqualTo(DEFAULT_EMAIL);
    assertThat(userDTO.isActivated()).isEqualTo(true);
    assertThat(userDTO.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    assertThat(userDTO.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
    assertThat(userDTO.getCreatedBy()).isEqualTo(DEFAULT_LOGIN);
    assertThat(userDTO.getCreatedDate()).isEqualTo(user.getCreatedDate());
    assertThat(userDTO.getLastModifiedBy()).isEqualTo(DEFAULT_LOGIN);
    assertThat(userDTO.getLastModifiedDate()).isEqualTo(user.getLastModifiedDate());
    assertThat(userDTO.getAuthorities()).containsExactly(AuthoritiesConstants.USER);
    assertThat(userDTO.toString()).isNotNull();
}
Also used : Authority(de.tum.in.www1.artemis.domain.Authority) UserDTO(de.tum.in.www1.artemis.service.dto.UserDTO) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (de.tum.in.www1.artemis.domain.User)8 UserDTO (de.tum.in.www1.artemis.service.dto.UserDTO)7 Test (org.junit.Test)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 Authority (de.tum.in.www1.artemis.domain.Authority)4 WithMockUser (org.springframework.security.test.context.support.WithMockUser)4 Transactional (org.springframework.transaction.annotation.Transactional)4 HashSet (java.util.HashSet)2 Timed (com.codahale.metrics.annotation.Timed)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1