Search in sources :

Example 6 with UserAccount

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

the class PersistentTokenRepositoryImpl method createNewToken.

/**
     * Creates a new remember me token.
     *
     * @param token the remember me token
     */
@Override
@Transactional
public final void createNewToken(final PersistentRememberMeToken token) {
    final Query query = entityManager.createQuery("SELECT u FROM UserAccount u WHERE username = :username");
    query.setParameter("username", token.getUsername());
    final UserAccount user = (UserAccount) query.getSingleResult();
    if (user.getPersistentUser() == null) {
        user.setPersistentUser(new PersistentUser(user.getKey(), token.getUsername()));
    }
    if (user.getPersistentUser().getPersistentLogins() == null) {
        user.getPersistentUser().setPersistentLogins(new ArrayList<PersistentLogin>());
    }
    user.getPersistentUser().getPersistentLogins().add(createPersistentLogin(user.getPersistentUser().getKey(), token));
    entityManager.persist(user);
    memcacheService.put(user.getUsername(), user, Expiration.byDeltaSeconds(DEFAULT_EXPIRATION));
}
Also used : PersistentUser(com.jappstart.model.auth.PersistentUser) Query(javax.persistence.Query) PersistentLogin(com.jappstart.model.auth.PersistentLogin) UserAccount(com.jappstart.model.auth.UserAccount) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 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 8 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)

Example 9 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)

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