Search in sources :

Example 1 with Authority

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

the class UserResourceIntTest method testAuthorityEquals.

@Test
public void testAuthorityEquals() throws Exception {
    Authority authorityA = new Authority();
    assertThat(authorityA).isEqualTo(authorityA);
    assertThat(authorityA).isNotEqualTo(null);
    assertThat(authorityA).isNotEqualTo(new Object());
    assertThat(authorityA.hashCode()).isEqualTo(0);
    assertThat(authorityA.toString()).isNotNull();
    Authority authorityB = new Authority();
    assertThat(authorityA).isEqualTo(authorityB);
    authorityB.setName(AuthoritiesConstants.ADMIN);
    assertThat(authorityA).isNotEqualTo(authorityB);
    authorityA.setName(AuthoritiesConstants.USER);
    assertThat(authorityA).isNotEqualTo(authorityB);
    authorityB.setName(AuthoritiesConstants.USER);
    assertThat(authorityA).isEqualTo(authorityB);
    assertThat(authorityA.hashCode()).isEqualTo(authorityB.hashCode());
}
Also used : Authority(com.arnaugarcia.uplace.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Authority

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

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);
    cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).evict(user.getLogin());
    cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).evict(user.getEmail());
    log.debug("Created Information for User: {}", user);
    return user;
}
Also used : User(com.arnaugarcia.uplace.domain.User) Authority(com.arnaugarcia.uplace.domain.Authority)

Example 3 with Authority

use of com.arnaugarcia.uplace.domain.Authority 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)

Example 4 with Authority

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

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.arnaugarcia.uplace.domain.User) Authority(com.arnaugarcia.uplace.domain.Authority)

Example 5 with Authority

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

the class AccountResourceIntTest method testGetExistingAccount.

@Test
public void testGetExistingAccount() throws Exception {
    Set<Authority> authorities = new HashSet<>();
    Authority authority = new Authority();
    authority.setName(AuthoritiesConstants.ADMIN);
    authorities.add(authority);
    User user = new User();
    user.setLogin("test");
    user.setFirstName("john");
    user.setLastName("doe");
    user.setEmail("john.doe@jhipster.com");
    user.setImageUrl("http://placehold.it/50x50");
    user.setLangKey("en");
    user.setAuthorities(authorities);
    when(mockUserService.getUserWithAuthorities()).thenReturn(Optional.of(user));
    restUserMockMvc.perform(get("/api/account").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(jsonPath("$.login").value("test")).andExpect(jsonPath("$.firstName").value("john")).andExpect(jsonPath("$.lastName").value("doe")).andExpect(jsonPath("$.email").value("john.doe@jhipster.com")).andExpect(jsonPath("$.imageUrl").value("http://placehold.it/50x50")).andExpect(jsonPath("$.langKey").value("en")).andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
}
Also used : User(com.arnaugarcia.uplace.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Authority(com.arnaugarcia.uplace.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Authority (com.arnaugarcia.uplace.domain.Authority)6 User (com.arnaugarcia.uplace.domain.User)4 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 UserDTO (com.arnaugarcia.uplace.service.dto.UserDTO)1 WithMockUser (org.springframework.security.test.context.support.WithMockUser)1