Search in sources :

Example 1 with JpaUser

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

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

the class UsersListProviderTest method setUp.

@Before
public void setUp() throws Exception {
    users = new ArrayList<User>();
    user1 = new JpaUser("user1", "pass", null, "user1", "email1", "provider1", true);
    user2 = new JpaUser("user2", "pass", null, "user2", "email1", "provider1", true);
    user3 = new JpaUser("user3", "pass", null, "user3", "email1", "provider1", true);
    user4 = new JpaUser("user4", "pass", null, "user1", "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;
        }
    };
    usersListProvider = new UsersListProvider();
    usersListProvider.setUserDirectoryService(userDirectoryService);
}
Also used : User(org.opencastproject.security.api.User) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) Before(org.junit.Before)

Example 3 with JpaUser

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

the class UserAndSeriesLoader method loadLdapUser.

/**
 * Load a user for testing the ldap provider
 *
 * @param organizationId
 *          the organization
 */
protected void loadLdapUser(String organizationId) {
    Set<JpaRole> ldapUserRoles = new HashSet<>();
    ldapUserRoles.add(new JpaRole(USER_ROLE, getOrganization(organizationId)));
    // This is the public identifier for Josh Holtzman in the UC Berkeley Directory, which is available for anonymous
    // binding.
    String ldapUserId = "231693";
    if (jpaUserProvider.loadUser(ldapUserId, organizationId) == null) {
        try {
            jpaUserProvider.addUser(new JpaUser(ldapUserId, "ldap", getOrganization(organizationId), jpaUserProvider.getName(), true, ldapUserRoles));
            logger.debug("Added ldap user '{}' into organization '{}'", ldapUserId, organizationId);
        } catch (UnauthorizedException ex) {
            logger.error("Unable to add an administrative user because you have not enough permissions.");
        }
    }
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) HashSet(java.util.HashSet)

Example 4 with JpaUser

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

the class UserAndSeriesLoader method load.

/**
 * Loads demo users into persistence.
 *
 * @param rolePrefix
 *          the role prefix
 * @param numPerSeries
 *          the number of users to load per series
 * @param additionalRoles
 *          any additional roles to add for each user
 * @param orgId
 *          the organization id
 */
protected void load(String rolePrefix, int numPerSeries, String[] additionalRoles, String orgId) {
    String lowerCasePrefix = rolePrefix.toLowerCase();
    int totalUsers = numPerSeries * NUM_SERIES;
    logger.info("Adding sample {}s, usernames and passwords are {}1/{}1... {}{}/{}{}", lowerCasePrefix, lowerCasePrefix, lowerCasePrefix, lowerCasePrefix, totalUsers, lowerCasePrefix, totalUsers);
    for (int i = 1; i <= totalUsers; i++) {
        if (jpaUserProvider.loadUser(lowerCasePrefix + i, orgId) == null) {
            Set<JpaRole> roleSet = new HashSet<>();
            for (String additionalRole : additionalRoles) {
                roleSet.add(new JpaRole(additionalRole, getOrganization(orgId)));
            }
            roleSet.add(new JpaRole(SERIES_PREFIX + (((i - 1) % NUM_SERIES) + 1) + "_" + rolePrefix, getOrganization(orgId)));
            JpaUser user = new JpaUser(lowerCasePrefix + i, lowerCasePrefix + i, getOrganization(orgId), jpaUserProvider.getName(), true, roleSet);
            try {
                jpaUserProvider.addUser(user);
                logger.debug("Added {}", user);
            } catch (Exception e) {
                logger.warn("Can not add {}: {}", user, e);
            }
        }
    }
}
Also used : JpaRole(org.opencastproject.security.impl.jpa.JpaRole) JpaUser(org.opencastproject.security.impl.jpa.JpaUser) SeriesException(org.opencastproject.series.api.SeriesException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) HashSet(java.util.HashSet)

Example 5 with JpaUser

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

the class UserDirectoryPersistenceUtil method findUser.

/**
 * Returns the persisted user by the user id and organization id
 *
 * @param id
 *          the user's unique id
 * @param organizationId
 *          the organization id
 * @param emf
 *          the entity manager factory
 * @return the user or <code>null</code> if not found
 */
public static JpaUser findUser(long id, String organizationId, EntityManagerFactory emf) {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("User.findByIdAndOrg");
        q.setParameter("id", id);
        q.setParameter("org", organizationId);
        return (JpaUser) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException) JpaUser(org.opencastproject.security.impl.jpa.JpaUser)

Aggregations

JpaUser (org.opencastproject.security.impl.jpa.JpaUser)35 Test (org.junit.Test)19 JpaRole (org.opencastproject.security.impl.jpa.JpaRole)18 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)13 User (org.opencastproject.security.api.User)12 NotFoundException (org.opencastproject.util.NotFoundException)9 HashSet (java.util.HashSet)8 JpaOrganization (org.opencastproject.security.impl.jpa.JpaOrganization)8 EntityManager (javax.persistence.EntityManager)5 Path (javax.ws.rs.Path)4 SecurityService (org.opencastproject.security.api.SecurityService)4 RestQuery (org.opencastproject.util.doc.rest.RestQuery)4 EntityTransaction (javax.persistence.EntityTransaction)3 NoResultException (javax.persistence.NoResultException)3 Before (org.junit.Before)3 Role (org.opencastproject.security.api.Role)3 JpaGroup (org.opencastproject.security.impl.jpa.JpaGroup)3 JObject (com.entwinemedia.fn.data.json.JObject)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2