Search in sources :

Example 6 with JaxbRole

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

the class UserIdRoleProvider method findRoles.

/**
 * @see org.opencastproject.security.api.RoleProvider#findRoles(String,Role.Target, int, int)
 */
@Override
public Iterator<Role> findRoles(String query, Role.Target target, int offset, int limit) {
    if (query == null)
        throw new IllegalArgumentException("Query must be set");
    // These roles are not meaningful for users/groups
    if (target == Role.Target.USER) {
        return Collections.emptyIterator();
    }
    logger.debug("findRoles(query={} offset={} limit={})", query, offset, limit);
    HashSet<Role> foundRoles = new HashSet<Role>();
    Organization organization = securityService.getOrganization();
    // Return authenticated user role if it matches the query pattern
    if (like(ROLE_USER, query)) {
        foundRoles.add(new JaxbRole(ROLE_USER, JaxbOrganization.fromOrganization(organization), "The authenticated user role", Role.Type.SYSTEM));
    }
    // (iterating through users may be slow)
    if (!"%".equals(query) && !query.startsWith(userRolePrefix)) {
        return foundRoles.iterator();
    }
    String userQuery = "%";
    if (query.startsWith(userRolePrefix)) {
        userQuery = query.substring(userRolePrefix.length());
    }
    Iterator<User> users = userDirectoryService.findUsers(userQuery, offset, limit);
    while (users.hasNext()) {
        User u = users.next();
        // We exclude the digest user, but then add the global ROLE_USER above
        if (!"system".equals(u.getProvider())) {
            foundRoles.add(new JaxbRole(getUserIdRole(u.getUsername()), JaxbOrganization.fromOrganization(u.getOrganization()), "User id role", Role.Type.SYSTEM));
        }
    }
    return foundRoles.iterator();
}
Also used : Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) Organization(org.opencastproject.security.api.Organization) JaxbRole(org.opencastproject.security.api.JaxbRole) User(org.opencastproject.security.api.User) HashSet(java.util.HashSet)

Example 7 with JaxbRole

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

the class UserIdRoleProvider method getRolesForUser.

/**
 * @see org.opencastproject.security.api.RoleProvider#getRolesForUser(String)
 */
@Override
public List<Role> getRolesForUser(String userName) {
    Organization organization = securityService.getOrganization();
    List<Role> roles = new ArrayList<Role>();
    roles.add(new JaxbRole(getUserIdRole(userName), JaxbOrganization.fromOrganization(organization), "The user id role", Role.Type.SYSTEM));
    roles.add(new JaxbRole(ROLE_USER, JaxbOrganization.fromOrganization(organization), "The authenticated user role", Role.Type.SYSTEM));
    return Collections.unmodifiableList(roles);
}
Also used : Role(org.opencastproject.security.api.Role) JaxbRole(org.opencastproject.security.api.JaxbRole) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) Organization(org.opencastproject.security.api.Organization) JaxbRole(org.opencastproject.security.api.JaxbRole) ArrayList(java.util.ArrayList)

Example 8 with JaxbRole

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

the class UserTrackingRestServiceTest method setUp.

@Before
public void setUp() throws UserTrackingException {
    SecurityService security = EasyMock.createMock(SecurityService.class);
    EasyMock.expect(security.getUser()).andReturn(new JaxbUser(MOCK_USER, "test", new DefaultOrganization(), new JaxbRole("ROLE_USER", new DefaultOrganization()))).anyTimes();
    BundleContext bc = EasyMock.createMock(BundleContext.class);
    EasyMock.expect(bc.getProperty(OpencastConstants.SERVER_URL_PROPERTY)).andReturn("http://www.example.org:8080").anyTimes();
    @SuppressWarnings("rawtypes") Dictionary dict = EasyMock.createMock(Dictionary.class);
    EasyMock.expect(dict.get(RestConstants.SERVICE_PATH_PROPERTY)).andReturn("/usertracking").anyTimes();
    ComponentContext context = EasyMock.createMock(ComponentContext.class);
    EasyMock.expect(context.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.expect(context.getProperties()).andReturn(dict).anyTimes();
    UserActionImpl ua = EasyMock.createMock(UserActionImpl.class);
    EasyMock.expect(ua.getId()).andReturn(4L).anyTimes();
    UserTrackingService usertracking = EasyMock.createMock(UserTrackingService.class);
    EasyMock.expect(usertracking.addUserFootprint(EasyMock.isA(UserAction.class), EasyMock.isA(UserSession.class))).andReturn(ua).anyTimes();
    EasyMock.replay(security, bc, dict, context, ua, usertracking);
    service = new UserTrackingRestService();
    service.setSecurityService(security);
    service.setService(usertracking);
    service.activate(context);
}
Also used : Dictionary(java.util.Dictionary) UserTrackingService(org.opencastproject.usertracking.api.UserTrackingService) JaxbRole(org.opencastproject.security.api.JaxbRole) ComponentContext(org.osgi.service.component.ComponentContext) UserTrackingRestService(org.opencastproject.usertracking.endpoint.UserTrackingRestService) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 9 with JaxbRole

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

the class VideoEditorTest method setUp.

/**
 * Setup for the video editor service, including creation of a mock workspace and all dependencies.
 *
 * @throws Exception
 *           if setup fails
 */
@Before
public void setUp() throws Exception {
    File tmpDir = folder.newFolder(getClass().getName());
    // output file
    tempFile1 = new File(tmpDir, "testoutput.mp4");
    /* mock the workspace for the input/output file */
    // workspace.get(new URI(sourceTrackUri));
    Workspace workspace = EasyMock.createMock(Workspace.class);
    EasyMock.expect(workspace.rootDirectory()).andReturn(tmpDir.getAbsolutePath());
    EasyMock.expect(workspace.get(track1.getURI())).andReturn(new File(track1.getURI())).anyTimes();
    EasyMock.expect(workspace.get(track2.getURI())).andReturn(new File(track2.getURI())).anyTimes();
    EasyMock.expect(workspace.putInCollection(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject(InputStream.class))).andAnswer(() -> {
        InputStream in = (InputStream) EasyMock.getCurrentArguments()[2];
        IOUtils.copy(in, new FileOutputStream(tempFile1));
        return tempFile1.toURI();
    });
    /* mock the role/org/security dependencies */
    User anonymous = new JaxbUser("anonymous", "test", new DefaultOrganization(), new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, new DefaultOrganization()));
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
    Organization organization = new DefaultOrganization();
    OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
    EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
    /* mock the osgi init for the video editor itself */
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    File storageDir = folder.newFolder();
    logger.info("storageDir: {}", storageDir);
    EasyMock.expect(bc.getProperty("org.opencastproject.storage.dir")).andReturn(storageDir.getPath()).anyTimes();
    EasyMock.expect(bc.getProperty("org.opencastproject.composer.ffmpegpath")).andReturn(FFMPEG_BINARY).anyTimes();
    EasyMock.expect(bc.getProperty(FFmpegAnalyzer.FFPROBE_BINARY_CONFIG)).andReturn("ffprobe").anyTimes();
    ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
    EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.replay(bc, cc, workspace, userDirectoryService, organizationDirectoryService, securityService);
    /* mock inspector output so that the job will alway pass */
    String sourceTrackXml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" + "<track xmlns=\"http://mediapackage.opencastproject.org\" type='presentation/source' id='deadbeef-a926-4ba9-96d9-2fafbcc30d2a'>" + "<audio id='audio-1'><encoder type='MP3 (MPEG audio layer 3)'/><channels>2</channels>" + "<bitrate>96000.0</bitrate></audio><video id='video-1'><device/>" + "<encoder type='FLV / Sorenson Spark / Sorenson H.263 (Flash Video)'/>" + "<bitrate>512000.0</bitrate><framerate>15.0</framerate>" + "<resolution>854x480</resolution></video>" + "<mimetype>video/mpeg</mimetype><url>video.mp4</url></track>";
    inspectedTrack = (Track) MediaPackageElementParser.getFromXml(sourceTrackXml);
    veditor = new VideoEditorServiceImpl() {

        @Override
        protected Job inspect(Job job, URI workspaceURI) throws MediaInspectionException, ProcessFailedException {
            Job inspectionJob = EasyMock.createNiceMock(Job.class);
            try {
                EasyMock.expect(inspectionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(inspectedTrack));
            } catch (MediaPackageException e) {
                throw new MediaInspectionException(e);
            }
            EasyMock.replay(inspectionJob);
            return inspectionJob;
        }
    };
    /* set up video editor */
    veditor.activate(cc);
    veditor.setWorkspace(workspace);
    veditor.setSecurityService(securityService);
    veditor.setUserDirectoryService(userDirectoryService);
    veditor.setSmilService(smilService);
    veditor.setOrganizationDirectoryService(organizationDirectoryService);
    serviceRegistry = EasyMock.createMock(ServiceRegistry.class);
    final Capture<String> type = EasyMock.newCapture();
    final Capture<String> operation = EasyMock.newCapture();
    final Capture<List<String>> args = EasyMock.newCapture();
    EasyMock.expect(serviceRegistry.createJob(capture(type), capture(operation), capture(args), EasyMock.anyFloat())).andAnswer(() -> {
        Job job = new JobImpl(0);
        logger.error("type: {}", type.getValue());
        job.setJobType(type.getValue());
        job.setOperation(operation.getValue());
        job.setArguments(args.getValue());
        job.setPayload(veditor.process(job));
        return job;
    }).anyTimes();
    EasyMock.replay(serviceRegistry);
    veditor.setServiceRegistry(serviceRegistry);
}
Also used : User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) URI(java.net.URI) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) SecurityService(org.opencastproject.security.api.SecurityService) List(java.util.List) ArrayList(java.util.ArrayList) Job(org.opencastproject.job.api.Job) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) JobImpl(org.opencastproject.job.api.JobImpl) ComponentContext(org.osgi.service.component.ComponentContext) InputStream(java.io.InputStream) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) JaxbRole(org.opencastproject.security.api.JaxbRole) FileOutputStream(java.io.FileOutputStream) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) ProcessFailedException(org.opencastproject.videoeditor.api.ProcessFailedException) File(java.io.File) Workspace(org.opencastproject.workspace.api.Workspace) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Example 10 with JaxbRole

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

the class LdapUserProviderInstance method loadUserFromLdap.

/**
 * Loads a user from LDAP.
 *
 * @param userName
 *          the username
 * @return the user
 */
protected User loadUserFromLdap(String userName) {
    if (delegate == null || cache == null) {
        throw new IllegalStateException("The LDAP user detail service has not yet been configured");
    }
    ldapLoads.incrementAndGet();
    UserDetails userDetails = null;
    Thread currentThread = Thread.currentThread();
    ClassLoader originalClassloader = currentThread.getContextClassLoader();
    try {
        currentThread.setContextClassLoader(LdapUserProviderFactory.class.getClassLoader());
        try {
            userDetails = delegate.loadUserByUsername(userName);
        } catch (UsernameNotFoundException e) {
            cache.put(userName, nullToken);
            return null;
        }
        JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
        // Get the roles and add the extra roles
        Collection<GrantedAuthority> authorities = new HashSet<>();
        authorities.addAll(userDetails.getAuthorities());
        authorities.addAll(setExtraRoles);
        Set<JaxbRole> roles = new HashSet<>();
        if (authorities != null) {
            /*
         * Please note the prefix logic for roles:
         *
         * - Roles that start with any of the "exclude prefixes" are left intact
         * - In any other case, the "role prefix" is prepended to the roles read from LDAP
         *
         * This only applies to the prefix addition. The conversion to uppercase is independent from these
         * considerations
         */
            for (GrantedAuthority authority : authorities) {
                String strAuthority = authority.getAuthority();
                boolean hasExcludePrefix = false;
                for (String excludePrefix : setExcludePrefixes) {
                    if (strAuthority.startsWith(excludePrefix)) {
                        hasExcludePrefix = true;
                        break;
                    }
                }
                if (!hasExcludePrefix) {
                    strAuthority = rolePrefix + strAuthority;
                }
                // Finally, add the role itself
                roles.add(new JaxbRole(strAuthority, jaxbOrganization));
            }
        }
        User user = new JaxbUser(userDetails.getUsername(), PROVIDER_NAME, jaxbOrganization, roles);
        cache.put(userName, user);
        return user;
    } finally {
        currentThread.setContextClassLoader(originalClassloader);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) UserDetails(org.springframework.security.core.userdetails.UserDetails) JaxbRole(org.opencastproject.security.api.JaxbRole) HashSet(java.util.HashSet)

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