Search in sources :

Example 96 with User

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

the class EventsLoader method addArchiveEntry.

private void addArchiveEntry(final MediaPackage mediaPackage) {
    final User user = securityService.getUser();
    final Organization organization = securityService.getOrganization();
    singleThreadExecutor.execute(new Runnable() {

        @Override
        public void run() {
            SecurityUtil.runAs(securityService, organization, user, new Effect0() {

                @Override
                protected void run() {
                    assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
                }
            });
        }
    });
}
Also used : User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Effect0(org.opencastproject.util.data.Effect0)

Example 97 with User

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

the class CaptureAgentStateServiceImpl method getKnownAgents.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.capture.admin.api.CaptureAgentStateService#getKnownAgents()
 */
@Override
public Map<String, Agent> getKnownAgents() {
    agentCache.cleanUp();
    EntityManager em = null;
    User user = securityService.getUser();
    Organization org = securityService.getOrganization();
    String orgAdmin = org.getAdminRole();
    Set<Role> roles = user.getRoles();
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("Agent.byOrganization");
        q.setParameter("org", securityService.getOrganization().getId());
        // Filter the results in memory if this user is not an administrator
        List<AgentImpl> agents = q.getResultList();
        if (!user.hasRole(SecurityConstants.GLOBAL_ADMIN_ROLE) && !user.hasRole(orgAdmin)) {
            for (Iterator<AgentImpl> iter = agents.iterator(); iter.hasNext(); ) {
                AgentImpl agent = iter.next();
                Set<String> schedulerRoles = agent.getSchedulerRoles();
                // coarse-grained web layer security
                if (schedulerRoles == null || schedulerRoles.isEmpty()) {
                    continue;
                }
                boolean hasSchedulerRole = false;
                for (Role role : roles) {
                    if (schedulerRoles.contains(role.getName())) {
                        hasSchedulerRole = true;
                        break;
                    }
                }
                if (!hasSchedulerRole) {
                    iter.remove();
                }
            }
        }
        // Build the map that the API defines as agent name->agent
        Map<String, Agent> map = new TreeMap<>();
        for (AgentImpl agent : agents) {
            map.put(agent.getName(), updateCachedLastHeardFrom(agent, org.getId()));
        }
        return map;
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : Agent(org.opencastproject.capture.admin.api.Agent) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) Query(javax.persistence.Query) TreeMap(java.util.TreeMap) Role(org.opencastproject.security.api.Role) EntityManager(javax.persistence.EntityManager)

Example 98 with User

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

the class CommentWorkflowOperationHandlerTest method testDuplicateComments.

@Test
public void testDuplicateComments() throws WorkflowOperationException, EventCommentException {
    // Testing that a duplicate comment won't be created but a different one will still be created.
    Long workflowId = 10L;
    String mediaPackageId = "abc-def";
    String action = "create";
    String reason = "Waiting for Trim";
    String description = "The comment description";
    Organization org = createNiceMock(Organization.class);
    expect(org.getId()).andStubReturn("demo");
    replay(org);
    SecurityService secSrv = createNiceMock(SecurityService.class);
    expect(secSrv.getOrganization()).andStubReturn(org);
    replay(secSrv);
    // Setup WorkflowOperation Instance
    WorkflowOperationInstance workflowOperationInstance = EasyMock.createMock(WorkflowOperationInstance.class);
    EasyMock.expect(workflowOperationInstance.getConfiguration(CommentWorkflowOperationHandler.ACTION)).andReturn(action).anyTimes();
    EasyMock.expect(workflowOperationInstance.getConfiguration(CommentWorkflowOperationHandler.REASON)).andReturn(reason).anyTimes();
    EasyMock.expect(workflowOperationInstance.getConfiguration(CommentWorkflowOperationHandler.DESCRIPTION)).andReturn(description).anyTimes();
    // Setup mediaPackage
    MediaPackage mediaPackage = EasyMock.createMock(MediaPackage.class);
    EasyMock.expect(mediaPackage.getIdentifier()).andReturn(new IdImpl(mediaPackageId)).anyTimes();
    // Setup user
    User creator = EasyMock.createMock(User.class);
    // Setup WorkflowInstance
    WorkflowInstance workflowInstance = EasyMock.createMock(WorkflowInstance.class);
    EasyMock.expect(workflowInstance.getId()).andReturn(workflowId).anyTimes();
    EasyMock.expect(workflowInstance.getCurrentOperation()).andReturn(workflowOperationInstance).anyTimes();
    EasyMock.expect(workflowInstance.getMediaPackage()).andReturn(mediaPackage).anyTimes();
    EasyMock.expect(workflowInstance.getCreator()).andReturn(creator).anyTimes();
    // Test no previous comments
    EventCommentService eventCommentService = EasyMock.createMock(EventCommentService.class);
    EasyMock.expect(eventCommentService.getComments(mediaPackageId)).andReturn(new ArrayList<EventComment>());
    Capture<EventComment> comment = EasyMock.newCapture();
    EasyMock.expect(eventCommentService.updateComment(EasyMock.capture(comment))).andReturn(EventComment.create(Option.option(15L), mediaPackageId, org.getId(), description, creator));
    EasyMock.replay(creator, eventCommentService, mediaPackage, workflowInstance, workflowOperationInstance);
    CommentWorkflowOperationHandler commentWorkflowOperationHandler = new CommentWorkflowOperationHandler();
    commentWorkflowOperationHandler.setEventCommentService(eventCommentService);
    commentWorkflowOperationHandler.setSecurityService(secSrv);
    commentWorkflowOperationHandler.start(workflowInstance, null);
    assertTrue(comment.hasCaptured());
    assertEquals(creator, comment.getValue().getAuthor());
    assertEquals(description, comment.getValue().getText());
    assertEquals(reason, comment.getValue().getReason());
    // Test previous comment with same reason and description
    List<EventComment> comments = new ArrayList<EventComment>();
    comments.add(EventComment.create(Option.option(13L), mediaPackageId, org.getId(), description, creator, reason, true));
    eventCommentService = EasyMock.createMock(EventCommentService.class);
    EasyMock.expect(eventCommentService.getComments(mediaPackageId)).andReturn(comments);
    EasyMock.replay(eventCommentService);
    commentWorkflowOperationHandler = new CommentWorkflowOperationHandler();
    commentWorkflowOperationHandler.setEventCommentService(eventCommentService);
    commentWorkflowOperationHandler.start(workflowInstance, null);
    assertTrue(comment.hasCaptured());
    assertEquals(creator, comment.getValue().getAuthor());
    assertEquals(description, comment.getValue().getText());
    assertEquals(reason, comment.getValue().getReason());
    // Test previous comment with different reasons and descriptions
    comments = new ArrayList<EventComment>();
    comments.add(EventComment.create(Option.option(15L), mediaPackageId, org.getId(), "Different description", creator, reason, true));
    comments.add(EventComment.create(Option.option(15L), mediaPackageId, org.getId(), description, creator, "Different reason", true));
    eventCommentService = EasyMock.createMock(EventCommentService.class);
    EasyMock.expect(eventCommentService.getComments(mediaPackageId)).andReturn(comments);
    comment = EasyMock.newCapture();
    EasyMock.expect(eventCommentService.updateComment(EasyMock.capture(comment))).andReturn(EventComment.create(Option.option(15L), mediaPackageId, org.getId(), description, creator));
    EasyMock.replay(eventCommentService);
    commentWorkflowOperationHandler = new CommentWorkflowOperationHandler();
    commentWorkflowOperationHandler.setEventCommentService(eventCommentService);
    commentWorkflowOperationHandler.setSecurityService(secSrv);
    commentWorkflowOperationHandler.start(workflowInstance, null);
    assertTrue(comment.hasCaptured());
    assertEquals(creator, comment.getValue().getAuthor());
    assertEquals(description, comment.getValue().getText());
    assertEquals(reason, comment.getValue().getReason());
}
Also used : Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User) ArrayList(java.util.ArrayList) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) EventComment(org.opencastproject.event.comment.EventComment) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) SecurityService(org.opencastproject.security.api.SecurityService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) EventCommentService(org.opencastproject.event.comment.EventCommentService) Test(org.junit.Test)

Example 99 with User

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

the class CaptureAgentStateServiceImplTest method setupService.

private void setupService() throws Exception {
    service = new CaptureAgentStateServiceImpl();
    service.setEntityManagerFactory(newTestEntityManagerFactory(CaptureAgentStateServiceImpl.PERSISTENCE_UNIT));
    DefaultOrganization organization = new DefaultOrganization();
    HashSet<JaxbRole> roles = new HashSet<>();
    roles.add(new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ADMIN, organization, ""));
    User user = new JaxbUser("testuser", "test", organization, roles);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.replay(securityService);
    service.setSecurityService(securityService);
    setupCC();
    service.activate(cc);
    service.setupAgentCache(1, TimeUnit.HOURS);
}
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)

Example 100 with User

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

the class SeriesMessageReceiverImpl method execute.

@Override
protected void execute(SeriesItem seriesItem) {
    Series series = null;
    String organization = getSecurityService().getOrganization().getId();
    User user = getSecurityService().getUser();
    switch(seriesItem.getType()) {
        case UpdateCatalog:
            logger.debug("Received Update Series for index {}", getSearchIndex().getIndexName());
            DublinCoreCatalog dc = seriesItem.getMetadata();
            String seriesId = dc.getFirst(DublinCoreCatalog.PROPERTY_IDENTIFIER);
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesId, organization, user, getSearchIndex());
                series.setCreator(getSecurityService().getUser().getName());
                SeriesIndexUtils.updateSeries(series, dc);
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesId, ExceptionUtils.getStackTrace(e));
                return;
            }
            // Update the event series titles if they changed
            try {
                SeriesIndexUtils.updateEventSeriesTitles(series, organization, getSecurityService().getUser(), getSearchIndex());
            } catch (SearchIndexException e) {
                logger.error("Error updating the series name of series {} from the associated events: {}", series.getIdentifier(), ExceptionUtils.getStackTrace(e));
            }
            // Persist the series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateAcl:
            logger.debug("Received Update Series ACL for index {}", getSearchIndex().getIndexName());
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                List<ManagedAcl> acls = aclServiceFactory.serviceFor(getSecurityService().getOrganization()).getAcls();
                Option<ManagedAcl> managedAcl = AccessInformationUtil.matchAcls(acls, seriesItem.getAcl());
                if (managedAcl.isSome())
                    series.setManagedAcl(managedAcl.get().getName());
                series.setAccessPolicy(AccessControlParser.toJsonSilent(seriesItem.getAcl()));
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateOptOut:
            logger.debug("Received update opt out status of series {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                series.setOptOut(seriesItem.getOptOut());
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case UpdateProperty:
            logger.debug("Received update property of series {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            if (!THEME_PROPERTY_NAME.equals(seriesItem.getPropertyName()))
                break;
            // Load or create the corresponding series
            try {
                series = SeriesIndexUtils.getOrCreate(seriesItem.getSeriesId(), organization, user, getSearchIndex());
                series.setTheme(Opt.nul(seriesItem.getPropertyValue()).bind(Strings.toLong).orNull());
            } catch (SearchIndexException e) {
                logger.error("Error retrieving series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            // Persist the updated series
            update(seriesItem.getSeriesId(), series);
            break;
        case Delete:
            logger.debug("Received Delete Series Event {} for index {}", seriesItem.getSeriesId(), getSearchIndex().getIndexName());
            // Remove the series from the search index
            try {
                getSearchIndex().delete(Series.DOCUMENT_TYPE, seriesItem.getSeriesId().concat(organization));
                logger.debug("Series {} removed from search index", seriesItem.getSeriesId());
            } catch (SearchIndexException e) {
                logger.error("Error deleting the series {} from the search index: {}", seriesItem.getSeriesId(), ExceptionUtils.getStackTrace(e));
                return;
            }
            return;
        case UpdateElement:
            // nothing to do
            break;
        default:
            throw new IllegalArgumentException("Unhandled type of SeriesItem");
    }
}
Also used : Series(org.opencastproject.index.service.impl.index.series.Series) User(org.opencastproject.security.api.User) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ManagedAcl(org.opencastproject.authorization.xacml.manager.api.ManagedAcl) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

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