Search in sources :

Example 6 with Authority

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

the class JiraAuthenticationProvider method buildAuthoritiesFromGroups.

/**
 * Builds the authorities list from the groups:
 * group contains configured instructor group name -> instructor role
 * otherwise                                       -> student role
 */
private Set<Authority> buildAuthoritiesFromGroups(List<String> groups) {
    Set<Authority> authorities = new HashSet<>();
    // Check if user is admin
    if (groups.contains(ADMIN_GROUP_NAME)) {
        Authority adminAuthority = new Authority();
        adminAuthority.setName(AuthoritiesConstants.ADMIN);
        authorities.add(adminAuthority);
    }
    List<String> instructorGroups = courseService.getAllInstructorGroupNames();
    // Check if user is an instructor in any course
    if (groups.stream().anyMatch(group -> instructorGroups.contains(group))) {
        Authority instructorAuthority = new Authority();
        instructorAuthority.setName(AuthoritiesConstants.INSTRUCTOR);
        authorities.add(instructorAuthority);
    }
    List<String> teachingAssistantGroups = courseService.getAllTeachingAssistantGroupNames();
    // Check if user is a tutor in any course
    if (groups.stream().anyMatch(group -> teachingAssistantGroups.contains(group))) {
        Authority taAuthority = new Authority();
        taAuthority.setName(AuthoritiesConstants.TEACHING_ASSISTANT);
        authorities.add(taAuthority);
    }
    Authority userAuthority = new Authority();
    userAuthority.setName(AuthoritiesConstants.USER);
    authorities.add(userAuthority);
    return authorities;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Authority(de.tum.in.www1.artemis.domain.Authority)

Example 7 with Authority

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

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

Example 9 with Authority

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

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

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