Search in sources :

Example 11 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class MoodleUserProviderInstance method loadUserFromMoodle.

// ///////////////
// Helper methods
/**
 * Loads a user from Moodle.
 *
 * @param username The username.
 * @return The user.
 */
private User loadUserFromMoodle(String username) {
    logger.debug("loadUserFromMoodle({})", username);
    if (cache == null)
        throw new IllegalStateException("The Moodle user detail service has not yet been configured");
    // Don't answer for admin, anonymous or empty user
    if ("admin".equals(username) || "".equals(username) || "anonymous".equals(username)) {
        logger.debug("We don't answer for: " + username);
        return null;
    }
    JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
    // update cache statistics
    moodleWebServiceRequests.incrementAndGet();
    Thread currentThread = Thread.currentThread();
    ClassLoader originalClassloader = currentThread.getContextClassLoader();
    try {
        // Load user
        List<MoodleUser> moodleUsers = client.coreUserGetUsersByField(CoreUserGetUserByFieldFilters.username, Collections.singletonList(username));
        if (moodleUsers.isEmpty()) {
            logger.debug("User {} not found in Moodle system", username);
            return null;
        }
        MoodleUser moodleUser = moodleUsers.get(0);
        // Load Roles
        List<String> courseIdsInstructor = client.toolOpencastGetCoursesForInstructor(username);
        List<String> courseIdsLearner = client.toolOpencastGetCoursesForLearner(username);
        // Create Opencast Objects
        Set<JaxbRole> roles = new HashSet<>();
        roles.add(new JaxbRole(Group.ROLE_PREFIX + "MOODLE", jaxbOrganization, "Moodle Users", Role.Type.EXTERNAL_GROUP));
        for (String courseId : courseIdsInstructor) {
            roles.add(new JaxbRole(courseId + "_" + INSTRUCTOR_ROLE_SUFFIX, jaxbOrganization, "Moodle external role", Role.Type.EXTERNAL));
        }
        for (String courseId : courseIdsLearner) {
            roles.add(new JaxbRole(courseId + "_" + LEARNER_ROLE_SUFFIX, jaxbOrganization, "Moodle external role", Role.Type.EXTERNAL));
        }
        return new JaxbUser(moodleUser.getUsername(), null, moodleUser.getFullname(), moodleUser.getEmail(), this.getName(), true, jaxbOrganization, roles);
    } catch (Exception e) {
        logger.warn("Exception loading Moodle user {} at {}: {}", username, client.getURL(), e.getMessage());
    } finally {
        currentThread.setContextClassLoader(originalClassloader);
    }
    return null;
}
Also used : JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException) JaxbRole(org.opencastproject.security.api.JaxbRole) HashSet(java.util.HashSet)

Example 12 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class SakaiUserProviderInstance method loadUserFromSakai.

/**
 * Loads a user from Sakai.
 *
 * @param userName
 *          the username
 * @return the user
 */
protected User loadUserFromSakai(String userName) {
    if (cache == null) {
        throw new IllegalStateException("The Sakai user detail service has not yet been configured");
    }
    // Don't answer for admin, anonymous or empty user
    if ("admin".equals(userName) || "".equals(userName) || "anonymous".equals(userName)) {
        cache.put(userName, nullToken);
        logger.debug("we don't answer for: " + userName);
        return null;
    }
    logger.debug("In loadUserFromSakai, currently processing user : {}", userName);
    JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
    // update cache statistics
    sakaiLoads.incrementAndGet();
    Thread currentThread = Thread.currentThread();
    ClassLoader originalClassloader = currentThread.getContextClassLoader();
    try {
        // Sakai userId (internal id), email address and display name
        String[] sakaiUser = getSakaiUser(userName);
        if (sakaiUser == null) {
            // user not known to this provider
            logger.debug("User {} not found in Sakai system", userName);
            cache.put(userName, nullToken);
            return null;
        }
        String userId = sakaiUser[0];
        String email = sakaiUser[1];
        String displayName = sakaiUser[2];
        // Get the set of Sakai roles for the user
        String[] sakaiRoles = getRolesFromSakai(userId);
        // if Sakai doesn't know about this user we need to return
        if (sakaiRoles == null) {
            cache.put(userName, nullToken);
            return null;
        }
        logger.debug("Sakai roles for eid " + userName + " id " + userId + ": " + Arrays.toString(sakaiRoles));
        Set<JaxbRole> roles = new HashSet<JaxbRole>();
        boolean isInstructor = false;
        for (String r : sakaiRoles) {
            roles.add(new JaxbRole(r, jaxbOrganization, "Sakai external role", Role.Type.EXTERNAL));
            if (r.endsWith(LTI_INSTRUCTOR_ROLE))
                isInstructor = true;
        }
        // Group role for all Sakai users
        roles.add(new JaxbRole(Group.ROLE_PREFIX + "SAKAI", jaxbOrganization, "Sakai Users", Role.Type.EXTERNAL_GROUP));
        // Group role for Sakai users who are an instructor in one more sites
        if (isInstructor)
            roles.add(new JaxbRole(Group.ROLE_PREFIX + "SAKAI_INSTRUCTOR", jaxbOrganization, "Sakai Instructors", Role.Type.EXTERNAL_GROUP));
        logger.debug("Returning JaxbRoles: " + roles);
        // JaxbUser(String userName, String password, String name, String email, String provider, boolean canLogin, JaxbOrganization organization, Set<JaxbRole> roles)
        User user = new JaxbUser(userName, null, displayName, email, PROVIDER_NAME, true, jaxbOrganization, roles);
        cache.put(userName, user);
        logger.debug("Returning user {}", userName);
        return user;
    } finally {
        currentThread.setContextClassLoader(originalClassloader);
    }
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) HashSet(java.util.HashSet)

Example 13 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class UserAndRoleDirectoryServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    org = new DefaultOrganization();
    userName = "sampleUser";
    role1 = new JaxbRole("role1", org);
    role2 = new JaxbRole("role2", org);
    role3 = new JaxbRole("role3", org);
    JaxbUser user1 = new JaxbUser(userName, "matterhorn", org, role1, role2);
    user1.setManageable(true);
    User user2 = new JaxbUser(userName, "secret", "test", true, org, role2, role3);
    User user3 = new JaxbUser("userSample", "test", org, role2, role3);
    List<User> users = new ArrayList<User>();
    users.add(user1);
    UserProvider provider1 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider1.loadUser((String) EasyMock.anyObject())).andReturn(user1).anyTimes();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.findUsers("%mple%", 0, 0)).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getUsers()).andReturn(users.iterator()).once();
    EasyMock.expect(provider1.getName()).andReturn("test").once();
    List<User> users2 = new ArrayList<User>();
    users2.add(user3);
    UserProvider provider2 = EasyMock.createNiceMock(UserProvider.class);
    EasyMock.expect(provider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(provider2.loadUser((String) EasyMock.anyObject())).andReturn(user2).anyTimes();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.findUsers("%mple%", 0, 0)).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getUsers()).andReturn(users2.iterator()).once();
    EasyMock.expect(provider2.getName()).andReturn("matterhorn").once();
    List<Role> roles1 = new ArrayList<Role>();
    roles1.add(new JaxbRole("ROLE_ASTRO_2011", org));
    roles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> rolesForUser1 = new ArrayList<Role>();
    rolesForUser1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    List<Role> findRoles1 = new ArrayList<Role>();
    findRoles1.add(new JaxbRole("ROLE_ASTRO_2012", org));
    RoleProvider roleProvider1 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider1.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider1.getRoles()).andReturn(roles1.iterator()).anyTimes();
    EasyMock.expect(roleProvider1.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser1).anyTimes();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    EasyMock.expect(roleProvider1.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles1.iterator()).once();
    List<Role> roles2 = new ArrayList<Role>();
    roles2.add(new JaxbRole("ROLE_MATH_2011", org));
    roles2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> rolesForUser2 = new ArrayList<Role>();
    rolesForUser2.add(new JaxbRole("ROLE_MATH_2012", org));
    List<Role> findRoles2 = new ArrayList<Role>();
    findRoles2.add(new JaxbRole("ROLE_MATH_2012", org));
    RoleProvider roleProvider2 = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(roleProvider2.getOrganization()).andReturn(org.getId()).anyTimes();
    EasyMock.expect(roleProvider2.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(roleProvider2.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    EasyMock.expect(roleProvider2.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(findRoles2.iterator()).once();
    RoleProvider otherOrgRoleProvider = EasyMock.createNiceMock(RoleProvider.class);
    EasyMock.expect(otherOrgRoleProvider.getOrganization()).andReturn("otherOrg").anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRoles()).andReturn(roles2.iterator()).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.getRolesForUser((String) EasyMock.anyObject())).andReturn(rolesForUser2).anyTimes();
    EasyMock.expect(otherOrgRoleProvider.findRoles("%2012%", Role.Target.ALL, 0, 0)).andReturn(new ArrayList<Role>().iterator()).anyTimes();
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(org).anyTimes();
    EasyMock.replay(provider1, provider2, roleProvider1, roleProvider2, otherOrgRoleProvider, securityService);
    directory = new UserAndRoleDirectoryServiceImpl();
    directory.activate(null);
    directory.setSecurityService(securityService);
    directory.addUserProvider(provider1);
    directory.addUserProvider(provider2);
    directory.addRoleProvider(roleProvider1);
    directory.addRoleProvider(roleProvider2);
    directory.addRoleProvider(otherOrgRoleProvider);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) ArrayList(java.util.ArrayList) JaxbUser(org.opencastproject.security.api.JaxbUser) RoleProvider(org.opencastproject.security.api.RoleProvider) Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbRole(org.opencastproject.security.api.JaxbRole) UserProvider(org.opencastproject.security.api.UserProvider) SecurityService(org.opencastproject.security.api.SecurityService) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Before(org.junit.Before)

Example 14 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class AnnotationServiceJpaImplTest method setUp.

@Before
public void setUp() throws Exception {
    // Set up a mock security service that always returns "me" as the current user
    DefaultOrganization organization = new DefaultOrganization();
    JaxbRole role = new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, organization, "");
    HashSet<JaxbRole> roles = new HashSet<JaxbRole>();
    roles.add(role);
    User me = new JaxbUser("me", "test", organization, roles);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(me).anyTimes();
    EasyMock.replay(securityService);
    // Set up the annotation service
    annotationService = new AnnotationServiceJpaImpl();
    annotationService.setEntityManagerFactory(newTestEntityManagerFactory(AnnotationServiceJpaImpl.PERSISTENCE_UNIT));
    annotationService.setSecurityService(securityService);
}
Also used : JaxbRole(org.opencastproject.security.api.JaxbRole) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 15 with JaxbRole

use of org.opencastproject.security.api.JaxbRole in project opencast by opencast.

the class StreamingDistributionServiceTest method setUp.

@Before
public void setUp() throws Exception {
    final File mediaPackageRoot = new File(getClass().getResource("/mediapackage.xml").toURI()).getParentFile();
    mp = MediaPackageParser.getFromXml(IOUtils.toString(getClass().getResourceAsStream("/mediapackage.xml"), "UTF-8"));
    distributionRoot = new File(mediaPackageRoot, "static");
    service = new StreamingDistributionServiceImpl();
    defaultOrganization = new DefaultOrganization();
    User anonymous = new JaxbUser("anonymous", "test", defaultOrganization, new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, defaultOrganization));
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
    EasyMock.replay(userDirectoryService);
    service.setUserDirectoryService(userDirectoryService);
    OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
    EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(defaultOrganization).anyTimes();
    EasyMock.replay(organizationDirectoryService);
    service.setOrganizationDirectoryService(organizationDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(defaultOrganization).anyTimes();
    EasyMock.replay(securityService);
    service.setSecurityService(securityService);
    serviceRegistry = new ServiceRegistryInMemoryImpl(service, securityService, userDirectoryService, organizationDirectoryService, EasyMock.createNiceMock(IncidentService.class));
    service.setServiceRegistry(serviceRegistry);
    final Workspace workspace = EasyMock.createNiceMock(Workspace.class);
    EasyMock.expect(workspace.get((URI) EasyMock.anyObject())).andAnswer(new IAnswer<File>() {

        @Override
        public File answer() throws Throwable {
            final URI uri = (URI) EasyMock.getCurrentArguments()[0];
            final String[] pathElems = uri.getPath().split("/");
            final String file = pathElems[pathElems.length - 1];
            return new File(mediaPackageRoot, file);
        }
    }).anyTimes();
    EasyMock.replay(workspace);
    service.setWorkspace(workspace);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getProperty("org.opencastproject.streaming.url")).andReturn("rtmp://localhost/").anyTimes();
    EasyMock.expect(bc.getProperty("org.opencastproject.streaming.directory")).andReturn(distributionRoot.getPath()).anyTimes();
    EasyMock.replay(bc);
    ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
    EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.replay(cc);
    service.activate(cc);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) ComponentContext(org.osgi.service.component.ComponentContext) JaxbUser(org.opencastproject.security.api.JaxbUser) URI(java.net.URI) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) IAnswer(org.easymock.IAnswer) JaxbRole(org.opencastproject.security.api.JaxbRole) SecurityService(org.opencastproject.security.api.SecurityService) File(java.io.File) ServiceRegistryInMemoryImpl(org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) Workspace(org.opencastproject.workspace.api.Workspace) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

JaxbRole (org.opencastproject.security.api.JaxbRole)66 JaxbUser (org.opencastproject.security.api.JaxbUser)53 User (org.opencastproject.security.api.User)45 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)39 SecurityService (org.opencastproject.security.api.SecurityService)39 Before (org.junit.Before)30 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)25 HashSet (java.util.HashSet)18 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 Organization (org.opencastproject.security.api.Organization)17 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)17 Test (org.junit.Test)14 Role (org.opencastproject.security.api.Role)13 Workspace (org.opencastproject.workspace.api.Workspace)13 ServiceRegistryInMemoryImpl (org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl)11 URI (java.net.URI)10 BundleContext (org.osgi.framework.BundleContext)10 ComponentContext (org.osgi.service.component.ComponentContext)10 File (java.io.File)9 ArrayList (java.util.ArrayList)9