Search in sources :

Example 1 with User

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

the class AssetManagerWithSecurityTest method runWith.

/**
 * Evaluate product <code>p</code> in a given security context.
 *
 * @param user
 *     the user under whose roles and organization <code>p</code> shall be evaluated
 * @param assertAccess
 *     if true assert that the current user is authorized to evaluate <code>p</code>,
 *     if false assert that the current user is prohibited to evaluate <code>p</code>
 * @param p
 *     the product that contains the calls to the asset manager
 * @return the result of the evaluation of <code>p</code>
 */
private <A> A runWith(User user, boolean assertAccess, P1<A> p) {
    final User stashedUser = currentUser;
    currentUser = user;
    A result = null;
    try {
        result = p.get1();
        if (!assertAccess) {
            fail("Access should be prohibited");
        }
    } catch (Exception e) {
        if ((e instanceof UnauthorizedException) && assertAccess) {
            fail("Access should be granted");
        } else if (!(e instanceof UnauthorizedException)) {
            Prelude.chuck(e);
        }
    }
    currentUser = stashedUser;
    return result;
}
Also used : User(org.opencastproject.security.api.User) TestUser(org.opencastproject.assetmanager.impl.util.TestUser) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException)

Example 2 with User

use of org.opencastproject.security.api.User 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 3 with User

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

the class InboxScannerService method updated.

// synchronized with activate(ComponentContext)
@Override
public synchronized void updated(Dictionary properties) throws ConfigurationException {
    // build scanner configuration
    final String orgId = getCfg(properties, USER_ORG);
    final String userId = getCfg(properties, USER_NAME);
    final String mediaFlavor = getCfg(properties, MEDIA_FLAVOR);
    final String workflowDefinition = getCfg(properties, WORKFLOW_DEFINITION);
    final Map<String, String> workflowConfig = getCfgAsMap(properties, WORKFLOW_CONFIG);
    final int interval = getCfgAsInt(properties, INBOX_POLL);
    final File inbox = new File(getCfg(properties, INBOX_PATH));
    if (!inbox.isDirectory()) {
        try {
            FileUtils.forceMkdir(inbox);
        } catch (IOException e) {
            throw new ConfigurationException(INBOX_PATH, String.format("%s does not exists and could not be created", inbox.getAbsolutePath()));
        }
    }
    /* We need to be able to read from the inbox to get files from there */
    if (!inbox.canRead()) {
        throw new ConfigurationException(INBOX_PATH, String.format("Cannot read from %s", inbox.getAbsolutePath()));
    }
    /* We need to be able to write to the inbox to remove files after they have been ingested */
    if (!inbox.canWrite()) {
        throw new ConfigurationException(INBOX_PATH, String.format("Cannot write to %s", inbox.getAbsolutePath()));
    }
    final int maxthreads = option(cc.getBundleContext().getProperty("org.opencastproject.inbox.threads")).bind(Strings.toInt).getOrElse(1);
    final Option<SecurityContext> secCtx = getUserAndOrganization(securityService, orgDir, orgId, userDir, userId).bind(new Function<Tuple<User, Organization>, Option<SecurityContext>>() {

        @Override
        public Option<SecurityContext> apply(Tuple<User, Organization> a) {
            return some(new SecurityContext(securityService, a.getB(), a.getA()));
        }
    });
    // Only setup new inbox if security context could be aquired
    if (secCtx.isSome()) {
        // remove old file install configuration
        fileInstallCfg.foreach(removeFileInstallCfg);
        // set up new file install config
        fileInstallCfg = some(configureFileInstall(cc.getBundleContext(), inbox, interval));
        // create new scanner
        ingestor = some(new Ingestor(ingestService, workingFileRepository, secCtx.get(), workflowDefinition, workflowConfig, mediaFlavor, inbox, maxthreads));
        logger.info("Now watching inbox {}", inbox.getAbsolutePath());
    } else {
        logger.warn("Cannot create security context for user {}, organization {}. " + "Either the organization or the user does not exist", userId, orgId);
    }
}
Also used : User(org.opencastproject.security.api.User) SecurityUtil.getUserAndOrganization(org.opencastproject.security.util.SecurityUtil.getUserAndOrganization) Organization(org.opencastproject.security.api.Organization) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SecurityContext(org.opencastproject.security.util.SecurityContext) Option(org.opencastproject.util.data.Option) File(java.io.File) Tuple(org.opencastproject.util.data.Tuple)

Example 4 with User

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

the class IngestServiceImplTest method setUp.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Before
public void setUp() throws Exception {
    FileUtils.forceMkdir(ingestTempDir);
    // set up service and mock workspace
    wfr = EasyMock.createNiceMock(WorkingFileRepository.class);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlTrack);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlAttachment);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlTrack1);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlTrack2);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog1);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog2);
    EasyMock.expect(wfr.put((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlTrack1);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlTrack2);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog1);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog2);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlCatalog);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlPackage);
    EasyMock.expect(wfr.putInCollection((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(urlPackageOld);
    workflowInstance = EasyMock.createNiceMock(WorkflowInstance.class);
    EasyMock.expect(workflowInstance.getId()).andReturn(workflowInstanceID);
    EasyMock.expect(workflowInstance.getState()).andReturn(WorkflowState.STOPPED);
    final Capture<MediaPackage> mp = EasyMock.newCapture();
    workflowService = EasyMock.createNiceMock(WorkflowService.class);
    EasyMock.expect(workflowService.start((WorkflowDefinition) EasyMock.anyObject(), EasyMock.capture(mp), (Map) EasyMock.anyObject())).andReturn(workflowInstance);
    EasyMock.expect(workflowInstance.getMediaPackage()).andAnswer(new IAnswer<MediaPackage>() {

        @Override
        public MediaPackage answer() throws Throwable {
            return mp.getValue();
        }
    });
    EasyMock.expect(workflowService.start((WorkflowDefinition) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject(), (Map) EasyMock.anyObject())).andReturn(workflowInstance);
    EasyMock.expect(workflowService.start((WorkflowDefinition) EasyMock.anyObject(), (MediaPackage) EasyMock.anyObject())).andReturn(workflowInstance);
    EasyMock.expect(workflowService.getWorkflowDefinitionById((String) EasyMock.anyObject())).andReturn(new WorkflowDefinitionImpl());
    EasyMock.expect(workflowService.getWorkflowById(EasyMock.anyLong())).andReturn(workflowInstance);
    SchedulerService schedulerService = EasyMock.createNiceMock(SchedulerService.class);
    Map<String, String> properties = new HashMap<>();
    properties.put(CaptureParameters.INGEST_WORKFLOW_DEFINITION, "sample");
    properties.put("agent-name", "matterhorn-agent");
    EasyMock.expect(schedulerService.getCaptureAgentConfiguration(EasyMock.anyString())).andReturn(properties).anyTimes();
    EasyMock.expect(schedulerService.getDublinCore(EasyMock.anyString())).andReturn(DublinCores.read(urlCatalog1.toURL().openStream())).anyTimes();
    MediaPackage schedulerMediaPackage = MediaPackageParser.getFromXml(IOUtils.toString(getClass().getResourceAsStream("/source-manifest.xml"), "UTF-8"));
    EasyMock.expect(schedulerService.getMediaPackage(EasyMock.anyString())).andReturn(schedulerMediaPackage).anyTimes();
    EasyMock.replay(wfr, workflowInstance, workflowService, schedulerService);
    User anonymous = new JaxbUser("anonymous", "test", new DefaultOrganization(), new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, new DefaultOrganization(), "test"));
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
    EasyMock.replay(userDirectoryService);
    Organization organization = new DefaultOrganization();
    OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
    EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
    EasyMock.replay(organizationDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
    EasyMock.replay(securityService);
    HttpEntity entity = EasyMock.createMock(HttpEntity.class);
    InputStream is = getClass().getResourceAsStream("/av.mov");
    byte[] movie = IOUtils.toByteArray(is);
    IOUtils.closeQuietly(is);
    EasyMock.expect(entity.getContent()).andReturn(new ByteArrayInputStream(movie)).anyTimes();
    EasyMock.replay(entity);
    StatusLine statusLine = EasyMock.createMock(StatusLine.class);
    EasyMock.expect(statusLine.getStatusCode()).andReturn(200).anyTimes();
    EasyMock.replay(statusLine);
    Header contentDispositionHeader = EasyMock.createMock(Header.class);
    EasyMock.expect(contentDispositionHeader.getValue()).andReturn("attachment; filename=fname.mp4").anyTimes();
    EasyMock.replay(contentDispositionHeader);
    HttpResponse httpResponse = EasyMock.createMock(HttpResponse.class);
    EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLine).anyTimes();
    EasyMock.expect(httpResponse.getFirstHeader("Content-Disposition")).andReturn(contentDispositionHeader).anyTimes();
    EasyMock.expect(httpResponse.getEntity()).andReturn(entity).anyTimes();
    EasyMock.replay(httpResponse);
    TrustedHttpClient httpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    EasyMock.expect(httpClient.execute((HttpGet) EasyMock.anyObject())).andReturn(httpResponse).anyTimes();
    EasyMock.replay(httpClient);
    AuthorizationService authorizationService = EasyMock.createNiceMock(AuthorizationService.class);
    EasyMock.expect(authorizationService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(new AccessControlList(), AclScope.Series)).anyTimes();
    EasyMock.replay(authorizationService);
    MediaInspectionService mediaInspectionService = EasyMock.createNiceMock(MediaInspectionService.class);
    EasyMock.expect(mediaInspectionService.enrich(EasyMock.anyObject(MediaPackageElement.class), EasyMock.anyBoolean())).andAnswer(new IAnswer<Job>() {

        private int i = 0;

        @Override
        public Job answer() throws Throwable {
            TrackImpl element = (TrackImpl) EasyMock.getCurrentArguments()[0];
            element.setDuration(20000L);
            if (i % 2 == 0) {
                element.addStream(new VideoStreamImpl());
            } else {
                element.addStream(new AudioStreamImpl());
            }
            i++;
            JobImpl succeededJob = new JobImpl();
            succeededJob.setStatus(Status.FINISHED);
            succeededJob.setPayload(MediaPackageElementParser.getAsXml(element));
            return succeededJob;
        }
    }).anyTimes();
    EasyMock.replay(mediaInspectionService);
    service = new IngestServiceImpl();
    service.setHttpClient(httpClient);
    service.setAuthorizationService(authorizationService);
    service.setWorkingFileRepository(wfr);
    service.setWorkflowService(workflowService);
    service.setSecurityService(securityService);
    service.setSchedulerService(schedulerService);
    service.setMediaInspectionService(mediaInspectionService);
    serviceRegistry = new ServiceRegistryInMemoryImpl(service, securityService, userDirectoryService, organizationDirectoryService, EasyMock.createNiceMock(IncidentService.class));
    serviceRegistry.registerService(service);
    service.setServiceRegistry(serviceRegistry);
    service.defaultWorkflowDefinionId = "sample";
    serviceRegistry.registerService(service);
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SchedulerService(org.opencastproject.scheduler.api.SchedulerService) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) HttpGet(org.apache.http.client.methods.HttpGet) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) JaxbUser(org.opencastproject.security.api.JaxbUser) MediaInspectionService(org.opencastproject.inspection.api.MediaInspectionService) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) WorkflowService(org.opencastproject.workflow.api.WorkflowService) SecurityService(org.opencastproject.security.api.SecurityService) WorkingFileRepository(org.opencastproject.workingfilerepository.api.WorkingFileRepository) ServiceRegistryInMemoryImpl(org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl) TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) JobImpl(org.opencastproject.job.api.JobImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) StatusLine(org.apache.http.StatusLine) IAnswer(org.easymock.IAnswer) JaxbRole(org.opencastproject.security.api.JaxbRole) Header(org.apache.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) Before(org.junit.Before)

Example 5 with User

use of org.opencastproject.security.api.User 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)

Aggregations

User (org.opencastproject.security.api.User)156 Organization (org.opencastproject.security.api.Organization)61 JaxbUser (org.opencastproject.security.api.JaxbUser)60 JaxbRole (org.opencastproject.security.api.JaxbRole)49 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)44 SecurityService (org.opencastproject.security.api.SecurityService)43 NotFoundException (org.opencastproject.util.NotFoundException)32 Before (org.junit.Before)31 Test (org.junit.Test)27 ArrayList (java.util.ArrayList)26 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)24 JaxbOrganization (org.opencastproject.security.api.JaxbOrganization)23 AccessControlList (org.opencastproject.security.api.AccessControlList)21 Role (org.opencastproject.security.api.Role)21 UserDirectoryService (org.opencastproject.security.api.UserDirectoryService)21 HashSet (java.util.HashSet)20 OrganizationDirectoryService (org.opencastproject.security.api.OrganizationDirectoryService)18 JpaUser (org.opencastproject.security.impl.jpa.JpaUser)17 IOException (java.io.IOException)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)16