Search in sources :

Example 1 with Authority

use of de.tum.in.www1.artemis.domain.Authority in project ArTEMiS by ls1intum.

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(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(de.tum.in.www1.artemis.domain.User) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Authority(de.tum.in.www1.artemis.domain.Authority) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Authority

use of de.tum.in.www1.artemis.domain.Authority in project ArTEMiS by ls1intum.

the class JiraAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    User user = getOrCreateUser(authentication, false);
    List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toList());
    return new UsernamePasswordAuthenticationToken(user.getLogin(), user.getPassword(), grantedAuthorities);
}
Also used : BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) java.util(java.util) org.springframework.http(org.springframework.http) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) URL(java.net.URL) UserService(de.tum.in.www1.artemis.service.UserService) ProviderNotFoundException(org.springframework.security.authentication.ProviderNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Value(org.springframework.beans.factory.annotation.Value) UserRepository(de.tum.in.www1.artemis.repository.UserRepository) AuthenticationException(org.springframework.security.core.AuthenticationException) RestTemplate(org.springframework.web.client.RestTemplate) HeaderUtil(de.tum.in.www1.artemis.web.rest.util.HeaderUtil) Logger(org.slf4j.Logger) Profile(org.springframework.context.annotation.Profile) Collectors(java.util.stream.Collectors) ComponentScan(org.springframework.context.annotation.ComponentScan) CourseService(de.tum.in.www1.artemis.service.CourseService) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Authority(de.tum.in.www1.artemis.domain.Authority) User(de.tum.in.www1.artemis.domain.User) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Component(org.springframework.stereotype.Component) ArtemisAuthenticationException(de.tum.in.www1.artemis.exception.ArtemisAuthenticationException) Principal(java.security.Principal) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(de.tum.in.www1.artemis.domain.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 3 with Authority

use of de.tum.in.www1.artemis.domain.Authority in project ArTEMiS by ls1intum.

the class UserService method registerUser.

public User registerUser(ManagedUserVM userDTO) {
    User newUser = new User();
    Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
    Set<Authority> authorities = new HashSet<>();
    String encryptedPassword = passwordEncoder().encode(userDTO.getPassword());
    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);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Authority(de.tum.in.www1.artemis.domain.Authority) HashSet(java.util.HashSet)

Example 4 with Authority

use of de.tum.in.www1.artemis.domain.Authority in project ArTEMiS by ls1intum.

the class UserService method createUser.

public User createUser(String login, String password, String firstName, String lastName, String email, String imageUrl, String langKey) {
    User newUser = new User();
    Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
    Set<Authority> authorities = new HashSet<>();
    String encryptedPassword = passwordEncoder().encode(password);
    newUser.setLogin(login);
    // new user gets initially a generated password
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(firstName);
    newUser.setLastName(lastName);
    newUser.setEmail(email);
    newUser.setImageUrl(imageUrl);
    newUser.setLangKey(langKey);
    // 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);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(de.tum.in.www1.artemis.domain.User) Authority(de.tum.in.www1.artemis.domain.Authority) HashSet(java.util.HashSet)

Example 5 with Authority

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

Aggregations

Authority (de.tum.in.www1.artemis.domain.Authority)9 User (de.tum.in.www1.artemis.domain.User)6 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 ArtemisAuthenticationException (de.tum.in.www1.artemis.exception.ArtemisAuthenticationException)1 UserRepository (de.tum.in.www1.artemis.repository.UserRepository)1 CourseService (de.tum.in.www1.artemis.service.CourseService)1 UserService (de.tum.in.www1.artemis.service.UserService)1 UserDTO (de.tum.in.www1.artemis.service.dto.UserDTO)1 HeaderUtil (de.tum.in.www1.artemis.web.rest.util.HeaderUtil)1 URL (java.net.URL)1 Principal (java.security.Principal)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Value (org.springframework.beans.factory.annotation.Value)1