Search in sources :

Example 1 with UserAccount

use of com.jappstart.model.auth.UserAccount in project google-app-engine-jappstart by taylorleese.

the class UserDetailsServiceImpl method loadUserByUsername.

/**
 * Locates the user based on the username.
 *
 * @param username string the username
 * @return the user details
 */
@Override
public final UserDetails loadUserByUsername(final String username) {
    final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    UserAccount user = (UserAccount) memcacheService.get(username);
    if (user == null) {
        final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
        query.setParameter(USERNAME, username);
        try {
            user = (UserAccount) query.getSingleResult();
            memcacheService.put(username, user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
        } catch (NoResultException e) {
            throw new UsernameNotFoundException("Username not found.", e);
        }
    }
    authorities.add(new SimpleGrantedAuthority(user.getRole()));
    return new EnhancedUser(user.getUsername(), user.getEmail(), user.getDisplayName(), user.getPassword(), user.getSalt(), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(), authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Query(javax.persistence.Query) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) NoResultException(javax.persistence.NoResultException) UserAccount(com.jappstart.model.auth.UserAccount)

Example 2 with UserAccount

use of com.jappstart.model.auth.UserAccount in project google-app-engine-jappstart by taylorleese.

the class UserDetailsServiceImpl method activationEmailSent.

/**
 * Updates the activation e-mail sent status.
 *
 * @param username the username
 */
@Override
@Transactional
public final void activationEmailSent(final String username) {
    final Query query = entityManager.createQuery(SELECT_USER);
    query.setParameter(USERNAME, username);
    try {
        final UserAccount user = (UserAccount) query.getSingleResult();
        user.setActivationEmailSent(true);
        entityManager.persist(user);
        memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
    } catch (NoResultException e) {
        throw new UsernameNotFoundException("Username not found.", e);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) UserAccount(com.jappstart.model.auth.UserAccount) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with UserAccount

use of com.jappstart.model.auth.UserAccount in project google-app-engine-jappstart by taylorleese.

the class UserDetailsServiceImpl method getUser.

/**
 * Returns the user account for the given username.
 *
 * @param username the username
 * @return the user account
 */
@Override
public final UserAccount getUser(final String username) {
    UserAccount user = (UserAccount) memcacheService.get(username);
    if (user == null) {
        final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
        query.setParameter(USERNAME, username);
        try {
            user = (UserAccount) query.getSingleResult();
            memcacheService.put(username, user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
        } catch (NoResultException e) {
            return null;
        }
    }
    return user;
}
Also used : Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) UserAccount(com.jappstart.model.auth.UserAccount)

Example 4 with UserAccount

use of com.jappstart.model.auth.UserAccount in project google-app-engine-jappstart by taylorleese.

the class UserDetailsServiceImpl method activateUser.

/**
 * Activates the user with the given activation key.
 *
 * @param key the activation key
 * @return true if successful; false otherwise
 */
@Override
@Transactional
public final boolean activateUser(final String key) {
    final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE activationKey = :key");
    query.setParameter("key", key);
    try {
        final UserAccount user = (UserAccount) query.getSingleResult();
        user.setEnabled(true);
        entityManager.persist(user);
        memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
        return true;
    } catch (NoResultException e) {
        return false;
    }
}
Also used : Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) UserAccount(com.jappstart.model.auth.UserAccount) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserAccount

use of com.jappstart.model.auth.UserAccount in project google-app-engine-jappstart by taylorleese.

the class UserDetailsServiceImpl method addUser.

/**
 * Adds a user.
 *
 * @param user the user
 * @param locale the locale
 */
@Override
@Transactional
public final void addUser(final UserAccount user, final Locale locale) {
    final UserAccount cachedUser = (UserAccount) memcacheService.get(user.getUsername());
    if (cachedUser != null) {
        throw new DuplicateUserException();
    }
    final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
    query.setParameter(USERNAME, user.getUsername());
    @SuppressWarnings("unchecked") final List results = query.getResultList();
    if (results != null && !results.isEmpty()) {
        throw new DuplicateUserException();
    }
    entityManager.persist(user);
    memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
    final TaskOptions taskOptions = TaskOptions.Builder.withUrl(mailTaskUrl).param("username", user.getUsername()).param("locale", locale.toString());
    final Queue queue = QueueFactory.getQueue(mailTaskName);
    queue.add(datastoreService.getCurrentTransaction(), taskOptions);
}
Also used : TaskOptions(com.google.appengine.api.taskqueue.TaskOptions) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) List(java.util.List) UserAccount(com.jappstart.model.auth.UserAccount) DuplicateUserException(com.jappstart.exception.DuplicateUserException) Queue(com.google.appengine.api.taskqueue.Queue) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UserAccount (com.jappstart.model.auth.UserAccount)9 Query (javax.persistence.Query)7 NoResultException (javax.persistence.NoResultException)5 Transactional (org.springframework.transaction.annotation.Transactional)4 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 DuplicateUserException (com.jappstart.exception.DuplicateUserException)2 ArrayList (java.util.ArrayList)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Queue (com.google.appengine.api.taskqueue.Queue)1 TaskOptions (com.google.appengine.api.taskqueue.TaskOptions)1 PersistentLogin (com.jappstart.model.auth.PersistentLogin)1 PersistentUser (com.jappstart.model.auth.PersistentUser)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 FieldError (org.springframework.validation.FieldError)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1