Search in sources :

Example 6 with User

use of com.dubion.domain.User in project dubion by valsamiq.

the class SocialService method createUserIfNotExist.

private User createUserIfNotExist(UserProfile userProfile, String langKey, String providerId, String imageUrl) {
    String email = userProfile.getEmail();
    String userName = userProfile.getUsername();
    if (!StringUtils.isBlank(userName)) {
        userName = userName.toLowerCase(Locale.ENGLISH);
    }
    if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) {
        log.error("Cannot create social user because email and login are null");
        throw new IllegalArgumentException("Email and login cannot be null");
    }
    if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) {
        log.error("Cannot create social user because email is null and login already exist, login -> {}", userName);
        throw new IllegalArgumentException("Email cannot be null with an existing login");
    }
    if (!StringUtils.isBlank(email)) {
        Optional<User> user = userRepository.findOneByEmailIgnoreCase(email);
        if (user.isPresent()) {
            log.info("User already exist associate the connection to this account");
            return user.get();
        }
    }
    String login = getLoginDependingOnProviderId(userProfile, providerId);
    String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10));
    Set<Authority> authorities = new HashSet<>(1);
    authorities.add(authorityRepository.findOne(AuthoritiesConstants.USER));
    User newUser = new User();
    newUser.setLogin(login);
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(userProfile.getFirstName());
    newUser.setLastName(userProfile.getLastName());
    newUser.setEmail(email);
    newUser.setActivated(true);
    newUser.setAuthorities(authorities);
    newUser.setLangKey(langKey);
    newUser.setImageUrl(imageUrl);
    return userRepository.save(newUser);
}
Also used : User(com.dubion.domain.User) Authority(com.dubion.domain.Authority)

Example 7 with User

use of com.dubion.domain.User in project dubion by valsamiq.

the class SocialService method createSocialUser.

public void createSocialUser(Connection<?> connection, String langKey) {
    if (connection == null) {
        log.error("Cannot create social user because connection is null");
        throw new IllegalArgumentException("Connection cannot be null");
    }
    UserProfile userProfile = connection.fetchUserProfile();
    String providerId = connection.getKey().getProviderId();
    String imageUrl = connection.getImageUrl();
    User user = createUserIfNotExist(userProfile, langKey, providerId, imageUrl);
    createSocialConnection(user.getLogin(), connection);
    mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
Also used : User(com.dubion.domain.User) UserProfile(org.springframework.social.connect.UserProfile)

Example 8 with User

use of com.dubion.domain.User in project dubion by valsamiq.

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

Example 9 with User

use of com.dubion.domain.User in project dubion by valsamiq.

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> userFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
    return userFromDatabase.map(user -> {
        if (!user.getActivated()) {
            throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated");
        }
        List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toList());
        return new org.springframework.security.core.userdetails.User(lowercaseLogin, user.getPassword(), grantedAuthorities);
    }).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) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Component(org.springframework.stereotype.Component) UserRepository(com.dubion.repository.UserRepository) User(com.dubion.domain.User) UserDetails(org.springframework.security.core.userdetails.UserDetails) Transactional(org.springframework.transaction.annotation.Transactional) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(com.dubion.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with User

use of com.dubion.domain.User in project dubion by valsamiq.

the class UserMapper method userFromId.

public User userFromId(Long id) {
    if (id == null) {
        return null;
    }
    User user = new User();
    user.setId(id);
    return user;
}
Also used : User(com.dubion.domain.User)

Aggregations

User (com.dubion.domain.User)10 Authority (com.dubion.domain.Authority)4 Timed (com.codahale.metrics.annotation.Timed)2 UserRepository (com.dubion.repository.UserRepository)1 BadRequestAlertException (com.dubion.web.rest.errors.BadRequestAlertException)1 EmailAlreadyUsedException (com.dubion.web.rest.errors.EmailAlreadyUsedException)1 LoginAlreadyUsedException (com.dubion.web.rest.errors.LoginAlreadyUsedException)1 URI (java.net.URI)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 Secured (org.springframework.security.access.annotation.Secured)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 UserProfile (org.springframework.social.connect.UserProfile)1