Search in sources :

Example 1 with JpaOrganization

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

the class ContributorsListProviderTest method setUp.

@Before
public void setUp() throws Exception {
    String expectedFormat = "{\"organization\":\"mh_default\",\"username\":\"akm220\",\"presentation\":\"Adam McKenzie\"}";
    organization = new JpaOrganization(ORG_ID, "name", null, null, null, null);
    users = new ArrayList<User>();
    user1 = new JpaUser("user1", "pass", organization, "User 1", "email1", "provider1", true);
    user2 = new JpaUser("user2", "pass", organization, "User 2", "email1", "provider1", true);
    user3 = new JpaUser("user3", "pass", organization, null, "email1", "provider1", true);
    user4 = new JpaUser("user4", "pass", organization, "User 1", "email3", "provider1", true);
    users.add(user1);
    users.add(user2);
    users.add(user3);
    users.add(user4);
    userDirectoryService = new UserDirectoryService() {

        private Iterator<User> iterator;

        @Override
        public User loadUser(String userName) {
            return null;
        }

        @Override
        public Iterator<User> getUsers() {
            return null;
        }

        @Override
        public Iterator<User> findUsers(String query, int offset, int limit) {
            iterator = users.iterator();
            List<User> filteredList = new ArrayList<User>();
            int i = 0;
            while ((filteredList.size() < limit || limit == 0) && iterator.hasNext()) {
                User item = iterator.next();
                if (i++ >= offset) {
                    filteredList.add(item);
                }
            }
            return filteredList.iterator();
        }

        @Override
        public long countUsers() {
            return users.size();
        }

        @Override
        public void invalidate(String userName) {
            return;
        }
    };
    List<String> contributors = new ArrayList<String>();
    contributors.add("User 1");
    contributors.add("User 5");
    searchIndex = EasyMock.createNiceMock(AbstractSearchIndex.class);
    EasyMock.expect(searchIndex.getTermsForField(EasyMock.anyString(), EasyMock.anyObject(Option.class))).andReturn(contributors).anyTimes();
    contributorsListProvider = new ContributorsListProvider();
    contributorsListProvider.setUserDirectoryService(userDirectoryService);
    contributorsListProvider.setIndex(searchIndex);
    EasyMock.replay(searchIndex);
}
Also used : User(org.opencastproject.security.api.User) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) ArrayList(java.util.ArrayList) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) AbstractSearchIndex(org.opencastproject.index.service.impl.index.AbstractSearchIndex) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Before(org.junit.Before)

Example 2 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization 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 3 with JpaOrganization

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

the class JpaGroupRoleProvider method createGroup.

@POST
@Path("")
@RestQuery(name = "createGroup", description = "Add a group", returnDescription = "Return the status codes", restParameters = { @RestParameter(name = "name", description = "The group name", isRequired = true, type = Type.STRING), @RestParameter(name = "description", description = "The group description", isRequired = false, type = Type.STRING), @RestParameter(name = "roles", description = "A comma seperated string of additional group roles", isRequired = false, type = Type.TEXT), @RestParameter(name = "users", description = "A comma seperated string of group members", isRequired = false, type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "Group created"), @RestResponse(responseCode = SC_BAD_REQUEST, description = "Name too long"), @RestResponse(responseCode = SC_FORBIDDEN, description = "Not enough permissions to create a group with the admin role."), @RestResponse(responseCode = SC_CONFLICT, description = "An group with this name already exists.") })
public Response createGroup(@FormParam("name") String name, @FormParam("description") String description, @FormParam("roles") String roles, @FormParam("users") String users) {
    JpaOrganization organization = (JpaOrganization) securityService.getOrganization();
    HashSet<JpaRole> roleSet = new HashSet<JpaRole>();
    if (roles != null) {
        for (String role : StringUtils.split(roles, ",")) {
            roleSet.add(new JpaRole(StringUtils.trim(role), organization));
        }
    }
    HashSet<String> members = new HashSet<String>();
    if (users != null) {
        for (String member : StringUtils.split(users, ",")) {
            members.add(StringUtils.trim(member));
        }
    }
    final String groupId = name.toLowerCase().replaceAll("\\W", "_");
    JpaGroup existingGroup = UserDirectoryPersistenceUtil.findGroup(groupId, organization.getId(), emf);
    if (existingGroup != null)
        return Response.status(SC_CONFLICT).build();
    try {
        addGroup(new JpaGroup(groupId, organization, name, description, roleSet, members));
    } catch (IllegalArgumentException e) {
        logger.warn(e.getMessage());
        return Response.status(Status.BAD_REQUEST).build();
    } catch (UnauthorizedException e) {
        return Response.status(SC_FORBIDDEN).build();
    }
    return Response.status(Status.CREATED).build();
}
Also used : JpaGroup(org.opencastproject.security.impl.jpa.JpaGroup) JpaOrganization(org.opencastproject.security.impl.jpa.JpaOrganization) JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 4 with JpaOrganization

use of org.opencastproject.security.impl.jpa.JpaOrganization 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)

Example 5 with JpaOrganization

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

the class UserDirectoryPersistenceUtil method saveOrganization.

/**
 * Persist an organization
 *
 * @param organization
 *          the organization to persist
 * @param emf
 *          the entity manager factory
 * @return the persisted organization
 */
public static JpaOrganization saveOrganization(JpaOrganization organization, EntityManagerFactory emf) {
    EntityManager em = null;
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        JpaOrganization org = findOrganization(organization, emf);
        if (org == null) {
            em.persist(organization);
        } else {
            organization = em.merge(org);
        }
        tx.commit();
        return organization;
    } 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)

Aggregations

JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)29 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)13 NotFoundException (org.opencastproject.util.NotFoundException)13 EntityManager (javax.persistence.EntityManager)10 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)10 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)8 HashSet (java.util.HashSet)7 EntityTransaction (javax.persistence.EntityTransaction)6 Path (javax.ws.rs.Path)6 RestQuery (org.opencastproject.util.doc.rest.RestQuery)6 HashMap (java.util.HashMap)5 NoResultException (javax.persistence.NoResultException)5 User (org.opencastproject.security.api.User)4 Query (javax.persistence.Query)3 POST (javax.ws.rs.POST)3 PUT (javax.ws.rs.PUT)3 JSONArray (org.json.simple.JSONArray)3 Before (org.junit.Before)3 Test (org.junit.Test)3 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)3