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);
}
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();
}
}
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();
}
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();
}
}
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();
}
}
Aggregations