Search in sources :

Example 31 with Authority

use of io.github.jhipster.sample.domain.Authority in project jhipster-sample-app-dto by jhipster.

the class UserService method registerUser.

public User registerUser(UserDTO userDTO, String password) {
    User newUser = new User();
    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());
    Set<Authority> authorities = new HashSet<>();
    authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add);
    newUser.setAuthorities(authorities);
    userRepository.save(newUser);
    this.clearUserCaches(newUser);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(io.github.jhipster.sample.domain.User) Authority(io.github.jhipster.sample.domain.Authority)

Example 32 with Authority

use of io.github.jhipster.sample.domain.Authority in project jhipster-sample-app-dto by jhipster.

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::findById).filter(Optional::isPresent).map(Optional::get).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);
    this.clearUserCaches(user);
    log.debug("Created Information for User: {}", user);
    return user;
}
Also used : User(io.github.jhipster.sample.domain.User) Authority(io.github.jhipster.sample.domain.Authority)

Example 33 with Authority

use of io.github.jhipster.sample.domain.Authority in project jhipster-sample-app-oauth2 by jhipster.

the class AccountResourceIntTest method testGetExistingAccount.

@Test
@Transactional
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.setId(RandomStringUtils.randomAlphanumeric(50));
    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);
    userRepository.save(user);
    // create security-aware mockMvc
    restUserMockMvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
    restUserMockMvc.perform(get("/api/account").with(user(user.getLogin()).roles("ADMIN")).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(io.github.jhipster.sample.domain.User) Authority(io.github.jhipster.sample.domain.Authority) HashSet(java.util.HashSet) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 34 with Authority

use of io.github.jhipster.sample.domain.Authority in project jhipster-sample-app-oauth2 by jhipster.

the class UserService method getUserFromAuthentication.

/**
 * Returns the user for a OAuth2 authentication.
 * Synchronizes the user in the local repository
 *
 * @param authentication OAuth2 authentication
 * @return the user from the authentication
 */
@SuppressWarnings("unchecked")
public UserDTO getUserFromAuthentication(OAuth2Authentication authentication) {
    Map<String, Object> details = (Map<String, Object>) authentication.getUserAuthentication().getDetails();
    User user = getUser(details);
    Set<Authority> userAuthorities = extractAuthorities(authentication, details);
    user.setAuthorities(userAuthorities);
    // convert Authorities to GrantedAuthorities
    Set<GrantedAuthority> grantedAuthorities = userAuthorities.stream().map(Authority::getName).map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    UsernamePasswordAuthenticationToken token = getToken(details, user, grantedAuthorities);
    authentication = new OAuth2Authentication(authentication.getOAuth2Request(), token);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return new UserDTO(syncUserWithIdP(details, user));
}
Also used : User(io.github.jhipster.sample.domain.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Authority(io.github.jhipster.sample.domain.Authority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UserDTO(io.github.jhipster.sample.service.dto.UserDTO) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 35 with Authority

use of io.github.jhipster.sample.domain.Authority in project jhipster-sample-app-dto by jhipster.

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(io.github.jhipster.sample.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Authority(io.github.jhipster.sample.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Authority (io.github.jhipster.sample.domain.Authority)37 User (io.github.jhipster.sample.domain.User)24 Test (org.junit.Test)18 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 UserDTO (io.github.jhipster.sample.service.dto.UserDTO)7 WithMockUser (org.springframework.security.test.context.support.WithMockUser)5 ChangeSet (com.github.mongobee.changeset.ChangeSet)2 HashSet (java.util.HashSet)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1 Transactional (org.springframework.transaction.annotation.Transactional)1