Search in sources :

Example 21 with User

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

the class UserResourceIntTest method testUserDTOtoUser.

@Test
public void testUserDTOtoUser() {
    UserDTO userDTO = new UserDTO();
    userDTO.setId(DEFAULT_ID);
    userDTO.setLogin(DEFAULT_LOGIN);
    userDTO.setFirstName(DEFAULT_FIRSTNAME);
    userDTO.setLastName(DEFAULT_LASTNAME);
    userDTO.setEmail(DEFAULT_EMAIL);
    userDTO.setActivated(true);
    userDTO.setImageUrl(DEFAULT_IMAGEURL);
    userDTO.setLangKey(DEFAULT_LANGKEY);
    userDTO.setCreatedBy(DEFAULT_LOGIN);
    userDTO.setLastModifiedBy(DEFAULT_LOGIN);
    userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    User user = userMapper.userDTOToUser(userDTO);
    assertThat(user.getId()).isEqualTo(DEFAULT_ID);
    assertThat(user.getLogin()).isEqualTo(DEFAULT_LOGIN);
    assertThat(user.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
    assertThat(user.getLastName()).isEqualTo(DEFAULT_LASTNAME);
    assertThat(user.getEmail()).isEqualTo(DEFAULT_EMAIL);
    assertThat(user.getActivated()).isEqualTo(true);
    assertThat(user.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
    assertThat(user.getCreatedBy()).isNull();
    assertThat(user.getCreatedDate()).isNotNull();
    assertThat(user.getLastModifiedBy()).isNull();
    assertThat(user.getLastModifiedDate()).isNotNull();
    assertThat(user.getAuthorities()).extracting("name").containsExactly(AuthoritiesConstants.USER);
}
Also used : User(com.arnaugarcia.uplace.domain.User) UserDTO(com.arnaugarcia.uplace.service.dto.UserDTO) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with User

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

the class UserResourceIntTest method createUserWithExistingEmail.

@Test
@Transactional
public void createUserWithExistingEmail() throws Exception {
    // Initialize the database
    userRepository.saveAndFlush(user);
    int databaseSizeBeforeCreate = userRepository.findAll().size();
    ManagedUserVM managedUserVM = new ManagedUserVM();
    managedUserVM.setLogin("anotherlogin");
    managedUserVM.setPassword(DEFAULT_PASSWORD);
    managedUserVM.setFirstName(DEFAULT_FIRSTNAME);
    managedUserVM.setLastName(DEFAULT_LASTNAME);
    // this email should already be used
    managedUserVM.setEmail(DEFAULT_EMAIL);
    managedUserVM.setActivated(true);
    managedUserVM.setImageUrl(DEFAULT_IMAGEURL);
    managedUserVM.setLangKey(DEFAULT_LANGKEY);
    managedUserVM.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
    // Create the User
    restUserMockMvc.perform(post("/api/users").contentType(TestUtil.APPLICATION_JSON_UTF8).content(TestUtil.convertObjectToJsonBytes(managedUserVM))).andExpect(status().isBadRequest());
    // Validate the User in the database
    List<User> userList = userRepository.findAll();
    assertThat(userList).hasSize(databaseSizeBeforeCreate);
}
Also used : User(com.arnaugarcia.uplace.domain.User) ManagedUserVM(com.arnaugarcia.uplace.web.rest.vm.ManagedUserVM) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with User

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

the class DomainUserDetailsService method loadUserByUsername.

@Override
@Transactional
public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);
    String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
    Optional<User> userByEmailFromDatabase = userRepository.findOneWithAuthoritiesByEmail(lowercaseLogin);
    return userByEmailFromDatabase.map(user -> createSpringSecurityUser(lowercaseLogin, user)).orElseGet(() -> {
        Optional<User> userByLoginFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
        return userByLoginFromDatabase.map(user -> createSpringSecurityUser(lowercaseLogin, user)).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " + "database"));
    });
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collectors(java.util.stream.Collectors) UserRepository(com.arnaugarcia.uplace.repository.UserRepository) GrantedAuthority(org.springframework.security.core.GrantedAuthority) User(com.arnaugarcia.uplace.domain.User) Component(org.springframework.stereotype.Component) UserDetails(org.springframework.security.core.userdetails.UserDetails) Transactional(org.springframework.transaction.annotation.Transactional) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.arnaugarcia.uplace.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with User

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

the class NotificationService method delete.

/*
     * Delete the notification by id.
     *
     * @param id the id of the entity
     */
public void delete(Long id) {
    log.debug("Request to delete Notification : {}", id);
    Notification notification = notificationRepository.findOne(id);
    User user = userService.getUserWithAuthoritiesByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
    // If the notification.user does not match and isn't admin... error
    if (!notification.getUser().equals(user) && !SecurityUtils.isCurrentUserInRole(AuthoritiesConstants.ADMIN)) {
        throw new BadRequestAlertException("This notification doesn't belongs to you :)", ENTITY_NOTIFICATION, ErrorConstants.ERR_BAD_USER);
    }
    notificationRepository.delete(id);
}
Also used : BadRequestAlertException(com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException) User(com.arnaugarcia.uplace.domain.User) Notification(com.arnaugarcia.uplace.domain.Notification)

Example 25 with User

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

Aggregations

User (com.arnaugarcia.uplace.domain.User)61 Test (org.junit.Test)41 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)41 Transactional (org.springframework.transaction.annotation.Transactional)39 WithMockUser (org.springframework.security.test.context.support.WithMockUser)21 ManagedUserVM (com.arnaugarcia.uplace.web.rest.vm.ManagedUserVM)15 BadRequestAlertException (com.arnaugarcia.uplace.web.rest.errors.BadRequestAlertException)6 UserDTO (com.arnaugarcia.uplace.service.dto.UserDTO)5 Authority (com.arnaugarcia.uplace.domain.Authority)4 Instant (java.time.Instant)4 MimeMessage (javax.mail.internet.MimeMessage)4 Notification (com.arnaugarcia.uplace.domain.Notification)3 KeyAndPasswordVM (com.arnaugarcia.uplace.web.rest.vm.KeyAndPasswordVM)2 LoginVM (com.arnaugarcia.uplace.web.rest.vm.LoginVM)2 Timed (com.codahale.metrics.annotation.Timed)2 Before (org.junit.Before)2 Agent (com.arnaugarcia.uplace.domain.Agent)1 UserRepository (com.arnaugarcia.uplace.repository.UserRepository)1 EmailAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (com.arnaugarcia.uplace.web.rest.errors.LoginAlreadyUsedException)1