Search in sources :

Example 1 with JpaUserReference

use of org.opencastproject.security.impl.jpa.JpaUserReference in project opencast by opencast.

the class ConfigurableLoginHandler method newUserLogin.

/**
 * Handle a new user login.
 *
 * @param id
 *          The identity of the user, ideally the Shibboleth persistent unique identifier
 * @param request
 *          The request, for accessing any other Shibboleth variables
 */
@Override
public void newUserLogin(String id, HttpServletRequest request) {
    String name = extractName(request);
    String email = extractEmail(request);
    Date loginDate = new Date();
    JpaOrganization organization = fromOrganization(securityService.getOrganization());
    // Compile the list of roles
    Set<JpaRole> roles = extractRoles(id, request);
    // Create the user reference
    JpaUserReference userReference = new JpaUserReference(id, name, email, MECH_SHIBBOLETH, loginDate, organization, roles);
    logger.debug("Shibboleth user '{}' logged in for the first time", id);
    userReferenceProvider.addUserReference(userReference, MECH_SHIBBOLETH);
}
Also used : JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaUserReference(org.opencastproject.security.impl.jpa.JpaUserReference) Date(java.util.Date)

Example 2 with JpaUserReference

use of org.opencastproject.security.impl.jpa.JpaUserReference in project opencast by opencast.

the class JpaUserReferenceProvider method updateUserReference.

/**
 * {@inheritDoc}
 */
public void updateUserReference(JpaUserReference user) {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaUserReference foundUserRef = findUserReference(user.getUsername(), user.getOrganization().getId(), emf);
        if (foundUserRef == null) {
            throw new IllegalStateException("User '" + user.getUsername() + "' does not exist");
        } else {
            foundUserRef.setName(user.getName());
            foundUserRef.setEmail(user.getEmail());
            foundUserRef.setLastLogin(new Date());
            foundUserRef.setRoles(UserDirectoryPersistenceUtil.saveRoles(user.getRoles(), emf));
            em.merge(foundUserRef);
        }
        tx.commit();
        cache.put(user.getUsername() + DELIMITER + user.getOrganization().getId(), user.toUser(PROVIDER_NAME));
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JpaUserReference(org.opencastproject.security.impl.jpa.JpaUserReference) Date(java.util.Date)

Example 3 with JpaUserReference

use of org.opencastproject.security.impl.jpa.JpaUserReference in project opencast by opencast.

the class JpaUserReferenceProvider method findUsers.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.security.api.UserProvider#findUsers(String, int, int)
 */
@Override
public Iterator<User> findUsers(String query, int offset, int limit) {
    if (query == null)
        throw new IllegalArgumentException("Query must be set");
    String orgId = securityService.getOrganization().getId();
    List<User> users = new ArrayList<User>();
    for (JpaUserReference userRef : findUserReferencesByQuery(orgId, query, limit, offset, emf)) {
        users.add(userRef.toUser(PROVIDER_NAME));
    }
    return users.iterator();
}
Also used : User(org.opencastproject.security.api.User) ArrayList(java.util.ArrayList) JpaUserReference(org.opencastproject.security.impl.jpa.JpaUserReference)

Example 4 with JpaUserReference

use of org.opencastproject.security.impl.jpa.JpaUserReference in project opencast by opencast.

the class JpaUserReferenceProvider method findUserReference.

/**
 * Returns the persisted user reference by the user name and organization id
 *
 * @param userName
 *          the user name
 * @param organizationId
 *          the organization id
 * @param emf
 *          the entity manager factory
 * @return the user or <code>null</code> if not found
 */
private JpaUserReference findUserReference(String userName, String organizationId, EntityManagerFactory emf) {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("UserReference.findByUsername");
        q.setParameter("u", userName);
        q.setParameter("org", organizationId);
        return (JpaUserReference) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) JpaUserReference(org.opencastproject.security.impl.jpa.JpaUserReference) NoResultException(javax.persistence.NoResultException)

Example 5 with JpaUserReference

use of org.opencastproject.security.impl.jpa.JpaUserReference in project opencast by opencast.

the class JpaUserReferenceProvider method addUserReference.

/**
 * {@inheritDoc}
 */
public void addUserReference(JpaUserReference user, String mechanism) {
    // Create a JPA user with an encoded password.
    Set<JpaRole> roles = UserDirectoryPersistenceUtil.saveRoles(user.getRoles(), emf);
    JpaOrganization organization = UserDirectoryPersistenceUtil.saveOrganization((JpaOrganization) user.getOrganization(), emf);
    JpaUserReference userReference = new JpaUserReference(user.getUsername(), user.getName(), user.getEmail(), mechanism, new Date(), organization, roles);
    // Then save the user reference
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaUserReference foundUserRef = findUserReference(user.getUsername(), user.getOrganization().getId(), emf);
        if (foundUserRef == null) {
            em.persist(userReference);
        } else {
            throw new IllegalStateException("User '" + user.getUsername() + "' already exists");
        }
        tx.commit();
        cache.put(user.getUsername() + DELIMITER + user.getOrganization().getId(), user.toUser(PROVIDER_NAME));
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        if (em != null)
            em.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaUserReference(org.opencastproject.security.impl.jpa.JpaUserReference) Date(java.util.Date)

Aggregations

JpaUserReference (org.opencastproject.security.impl.jpa.JpaUserReference)7 Date (java.util.Date)4 EntityManager (javax.persistence.EntityManager)3 JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)3 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)3 ArrayList (java.util.ArrayList)2 EntityTransaction (javax.persistence.EntityTransaction)2 User (org.opencastproject.security.api.User)2 NoResultException (javax.persistence.NoResultException)1 Query (javax.persistence.Query)1 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)1 Organization (org.opencastproject.security.api.Organization)1