Search in sources :

Example 1 with QuickShareDTO

use of org.alfresco.service.cmr.quickshare.QuickShareDTO in project alfresco-remote-api by Alfresco.

the class QuickShareLinksImpl method create.

/**
 * Create quick share.
 * <p>
 * Requires authenticated access.
 *
 * @param nodeIds
 * @param parameters
 * @return
 */
public List<QuickShareLink> create(List<QuickShareLink> nodeIds, Parameters parameters) {
    checkEnabled();
    List<QuickShareLink> result = new ArrayList<>(nodeIds.size());
    List<String> includeParam = parameters != null ? parameters.getInclude() : Collections.<String>emptyList();
    for (QuickShareLink qs : nodeIds) {
        String nodeId = qs.getNodeId();
        if (nodeId == null) {
            throw new InvalidArgumentException("A valid nodeId must be specified !");
        }
        NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, nodeId);
        try {
            // Note: will throw InvalidNodeRefException (=> 404) if node does not exist
            String sharedId = (String) nodeService.getProperty(nodeRef, QuickShareModel.PROP_QSHARE_SHAREDID);
            if (sharedId != null) {
                throw new ConstraintViolatedException("sharedId already exists: " + nodeId + " [" + sharedId + "]");
            }
            // Note: since we already check node exists above, we can assume that InvalidNodeRefException (=> 404) here means not content (see type check)
            try {
                QuickShareDTO qsDto = quickShareService.shareContent(nodeRef, qs.getExpiresAt());
                result.add(getQuickShareInfo(qsDto.getId(), false, includeParam));
            } catch (InvalidNodeRefException inre) {
                throw new InvalidArgumentException("Unable to create shared link to non-file content: " + nodeId);
            } catch (QuickShareLinkExpiryActionException ex) {
                throw new InvalidArgumentException(ex.getMessage());
            }
        } catch (AccessDeniedException ade) {
            throw new PermissionDeniedException("Unable to create shared link to node that does not exist: " + nodeId);
        } catch (InvalidNodeRefException inre) {
            logger.warn("Unable to create shared link: [" + nodeRef + "]");
            throw new EntityNotFoundException(nodeId);
        }
    }
    return result;
}
Also used : AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) ArrayList(java.util.ArrayList) QuickShareLinkExpiryActionException(org.alfresco.repo.quickshare.QuickShareLinkExpiryActionException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QuickShareDTO(org.alfresco.service.cmr.quickshare.QuickShareDTO) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) QuickShareLink(org.alfresco.rest.api.model.QuickShareLink)

Example 2 with QuickShareDTO

use of org.alfresco.service.cmr.quickshare.QuickShareDTO in project alfresco-repository by Alfresco.

the class QuickShareServiceIntegrationTest method unshare.

@Test
public void unshare() {
    final QuickShareDTO dto = share(testNode, user1.getUsername());
    unshare(dto.getId(), user1.getUsername());
    AuthenticationUtil.runAsSystem(new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            assertFalse(nodeService.getAspects(testNode).contains(QuickShareModel.ASPECT_QSHARE));
            assertNull(nodeService.getProperty(testNode, QuickShareModel.PROP_QSHARE_SHAREDID));
            assertNull(nodeService.getProperty(testNode, QuickShareModel.PROP_QSHARE_SHAREDBY));
            return null;
        }
    });
}
Also used : QuickShareDTO(org.alfresco.service.cmr.quickshare.QuickShareDTO) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) Test(org.junit.Test)

Example 3 with QuickShareDTO

use of org.alfresco.service.cmr.quickshare.QuickShareDTO in project alfresco-repository by Alfresco.

the class QuickShareServiceIntegrationTest method testSharedLinkExpiryScheduling.

/**
 * Test the quick share link expiry date action.
 */
@Test
public void testSharedLinkExpiryScheduling() throws Exception {
    // First record the number of available schedules
    final int numOfSchedules = listSchedules();
    // 1 day from now
    Date expiryDate = DateTime.now().plusDays(1).toDate();
    QuickShareDTO quickShareDTO = share(testNode, user1.getUsername(), expiryDate);
    assertTrue(hasQuickShareAspect(testNode));
    assertEquals(quickShareDTO.getId(), getProperty(testNode, QuickShareModel.PROP_QSHARE_SHAREDID));
    assertNotNull(quickShareDTO.getExpiresAt());
    assertEquals(expiryDate, quickShareDTO.getExpiresAt());
    // Check that the expiry action is persisted
    QuickShareLinkExpiryAction expiryAction = getExpiryActionAndAttachSchedule(quickShareDTO.getId());
    assertEquals(quickShareDTO.getId(), expiryAction.getSharedId());
    assertEquals(quickShareDTO.getExpiresAt(), expiryAction.getScheduleStart());
    assertNull("We haven't set interval count.", expiryAction.getScheduleIntervalCount());
    assertNull("We haven't set interval period.", expiryAction.getScheduleIntervalPeriod());
    // Try to share the already shared node with a different expiry date.
    // This basically will update the expiry action start time
    expiryDate = DateTime.now().plusDays(7).toDate();
    quickShareDTO = share(testNode, user1.getUsername(), expiryDate);
    assertEquals(expiryDate, quickShareDTO.getExpiresAt());
    assertEquals(expiryDate, getProperty(testNode, QuickShareModel.PROP_QSHARE_EXPIRY_DATE));
    assertTrue(hasQuickShareAspect(testNode));
    assertEquals(quickShareDTO.getId(), getProperty(testNode, QuickShareModel.PROP_QSHARE_SHAREDID));
    // Check that the expiry action is persisted
    expiryAction = getExpiryActionAndAttachSchedule(quickShareDTO.getId());
    assertEquals(quickShareDTO.getId(), expiryAction.getSharedId());
    assertEquals(quickShareDTO.getExpiresAt(), expiryAction.getScheduleStart());
    assertNull("We haven't set interval count.", expiryAction.getScheduleIntervalCount());
    assertNull("We haven't set interval period.", expiryAction.getScheduleIntervalPeriod());
    // Delete the expiry action
    deleteExpiryAction(expiryAction);
    // Check that the expiry action has been deleted
    QuickShareLinkExpiryAction deletedExpiryAction = getExpiryAction(quickShareDTO.getId());
    assertNull(deletedExpiryAction);
    assertNull(getProperty(testNode, QuickShareModel.PROP_QSHARE_EXPIRY_DATE));
    // Unshare
    unshare(quickShareDTO.getId(), user1.getUsername());
    // Share the testNode, with expiry date of 1 day from now
    expiryDate = DateTime.now().plusDays(1).toDate();
    quickShareDTO = share(testNode, user1.getUsername(), expiryDate);
    assertTrue(hasQuickShareAspect(testNode));
    expiryAction = getExpiryActionAndAttachSchedule(quickShareDTO.getId());
    assertEquals(expiryDate, expiryAction.getScheduleStart());
    assertEquals(numOfSchedules + 1, listSchedules());
    // Now update the schedule to be executed in 5 seconds.
    expiryAction.setScheduleStart(DateTime.now().plusSeconds(5).toDate());
    // Here we'll bypass the QuickShareService in order to force the new time.
    // As the QuickShareService by default will enforce the expiry date to not be less than 24 hours.
    forceSaveNewExpiryTime(expiryAction);
    // wait 10 seconds
    Thread.sleep(10000L);
    // Check that the expiry action was successful and it removed the shared link
    assertFalse(hasQuickShareAspect(testNode));
    // Also check the expiry date property is removed
    assertNull(getProperty(testNode, QuickShareModel.PROP_QSHARE_EXPIRY_DATE));
    // Check that the persisted expiry action is removed
    assertNull(getExpiryAction(quickShareDTO.getId()));
    // Check that the persisted schedule is removed as well
    assertEquals(numOfSchedules, listSchedules());
    // Share the testNode, with expiry date of 1 day from now
    expiryDate = DateTime.now().plusDays(1).toDate();
    quickShareDTO = share(testNode, user1.getUsername(), expiryDate);
    assertTrue(hasQuickShareAspect(testNode));
    expiryAction = getExpiryActionAndAttachSchedule(quickShareDTO.getId());
    assertEquals(expiryDate, expiryAction.getScheduleStart());
    // Delete the shared testNode as user1
    AuthenticationUtil.runAs(() -> {
        nodeService.deleteNode(testNode);
        return null;
    }, user1.getUsername());
    // Check that the persisted expiry action is removed, as we have deleted the source node
    assertNull(getExpiryAction(quickShareDTO.getId()));
    // Check that the persisted schedule is removed as well
    assertEquals(numOfSchedules, listSchedules());
    // Restore the testNode as user1
    AuthenticationUtil.runAs(() -> {
        final NodeRef archivedNode = nodeArchiveService.getArchivedNode(testNode);
        RestoreNodeReport restoreNodeReport = nodeArchiveService.restoreArchivedNode(archivedNode);
        assertNotNull(restoreNodeReport);
        assertTrue(restoreNodeReport.getStatus() == RestoreStatus.SUCCESS);
        testNode = restoreNodeReport.getRestoredNodeRef();
        return null;
    }, user1.getUsername());
    // Check that restoring the node hasn't brought back the shared aspect or the persisted expiry action
    assertFalse(hasQuickShareAspect(testNode));
    assertNull(getExpiryAction(quickShareDTO.getId()));
    assertEquals(numOfSchedules, listSchedules());
}
Also used : RestoreNodeReport(org.alfresco.repo.node.archive.RestoreNodeReport) NodeRef(org.alfresco.service.cmr.repository.NodeRef) QuickShareDTO(org.alfresco.service.cmr.quickshare.QuickShareDTO) QuickShareLinkExpiryAction(org.alfresco.service.cmr.quickshare.QuickShareLinkExpiryAction) Date(java.util.Date) Test(org.junit.Test)

Example 4 with QuickShareDTO

use of org.alfresco.service.cmr.quickshare.QuickShareDTO in project alfresco-remote-api by Alfresco.

the class ShareContentPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    if (!isEnabled()) {
        throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "QuickShare is disabled system-wide");
    }
    // create map of params (template vars)
    Map<String, String> params = req.getServiceMatch().getTemplateVars();
    final NodeRef nodeRef = WebScriptUtil.getNodeRef(params);
    if (nodeRef == null) {
        String msg = "A valid NodeRef must be specified!";
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, msg);
    }
    try {
        QuickShareDTO dto = quickShareService.shareContent(nodeRef);
        Map<String, Object> model = new HashMap<String, Object>(1);
        model.put("sharedDTO", dto);
        return model;
    } catch (InvalidNodeRefException inre) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: " + nodeRef);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) QuickShareDTO(org.alfresco.service.cmr.quickshare.QuickShareDTO) HashMap(java.util.HashMap) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 5 with QuickShareDTO

use of org.alfresco.service.cmr.quickshare.QuickShareDTO in project alfresco-repository by Alfresco.

the class QuickShareServiceImpl method shareContent.

@Override
public QuickShareDTO shareContent(NodeRef nodeRef, Date expiryDate) throws QuickShareDisabledException, InvalidNodeRefException {
    checkEnabled();
    // Check the node is the correct type
    final QName typeQName = nodeService.getType(nodeRef);
    if (isSharable(typeQName) == false) {
        throw new InvalidNodeRefException(nodeRef);
    }
    final String sharedId;
    // If it is retura dto built from the existing properties.
    if (!nodeService.getAspects(nodeRef).contains(QuickShareModel.ASPECT_QSHARE)) {
        UUID uuid = UUIDGenerator.getInstance().generateRandomBasedUUID();
        // => 22 chars (eg. q3bEKPeDQvmJYgt4hJxOjw)
        sharedId = Base64.encodeBase64URLSafeString(uuid.toByteArray());
        final Map<QName, Serializable> props = new HashMap<QName, Serializable>(2);
        props.put(QuickShareModel.PROP_QSHARE_SHAREDID, sharedId);
        props.put(QuickShareModel.PROP_QSHARE_SHAREDBY, AuthenticationUtil.getRunAsUser());
        // Disable audit to preserve modifier and modified date
        // see MNT-11960
        behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
        try {
            // consumer/contributor should be able to add "shared" aspect (MNT-10366)
            AuthenticationUtil.runAsSystem(new RunAsWork<Void>() {

                public Void doWork() {
                    nodeService.addAspect(nodeRef, QuickShareModel.ASPECT_QSHARE, props);
                    return null;
                }
            });
        } finally {
            behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
        }
        final NodeRef tenantNodeRef = tenantService.getName(nodeRef);
        TenantUtil.runAsDefaultTenant(new TenantRunAsWork<Void>() {

            public Void doWork() throws Exception {
                attributeService.setAttribute(tenantNodeRef, ATTR_KEY_SHAREDIDS_ROOT, sharedId);
                return null;
            }
        });
        final StringBuffer sb = new StringBuffer();
        sb.append("{").append("\"sharedId\":\"").append(sharedId).append("\"").append("}");
        eventPublisher.publishEvent(new EventPreparator() {

            @Override
            public Event prepareEvent(String user, String networkId, String transactionId) {
                return new ActivityEvent("quickshare", transactionId, networkId, user, nodeRef.getId(), null, typeQName.toString(), Client.asType(ClientType.webclient), sb.toString(), null, null, 0l, null);
            }
        });
        if (logger.isInfoEnabled()) {
            logger.info("QuickShare - shared content: " + sharedId + " [" + nodeRef + "]");
        }
    } else {
        sharedId = (String) nodeService.getProperty(nodeRef, QuickShareModel.PROP_QSHARE_SHAREDID);
        if (logger.isDebugEnabled()) {
            logger.debug("QuickShare - content already shared: " + sharedId + " [" + nodeRef + "]");
        }
    }
    if (expiryDate != null) {
        AuthenticationUtil.runAsSystem((RunAsWork<Void>) () -> {
            // Create and save the expiry action
            saveSharedLinkExpiryAction(sharedId, expiryDate);
            // if we get here, it means the expiry date is validated and the action
            // is created and saved, so now set the expiryDate property.
            behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
            try {
                nodeService.setProperty(nodeRef, QuickShareModel.PROP_QSHARE_EXPIRY_DATE, expiryDate);
            } finally {
                behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
            }
            return null;
        });
    }
    return new QuickShareDTO(sharedId, expiryDate);
}
Also used : Serializable(java.io.Serializable) ActivityEvent(org.alfresco.sync.events.types.ActivityEvent) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ClientAppNotFoundException(org.alfresco.repo.client.config.ClientAppNotFoundException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) QuickShareDisabledException(org.alfresco.service.cmr.quickshare.QuickShareDisabledException) NoSuchPersonException(org.alfresco.service.cmr.security.NoSuchPersonException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) EventPreparator(org.alfresco.sync.repo.events.EventPreparator) QuickShareDTO(org.alfresco.service.cmr.quickshare.QuickShareDTO) Event(org.alfresco.sync.events.types.Event) ActivityEvent(org.alfresco.sync.events.types.ActivityEvent) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) UUID(org.safehaus.uuid.UUID)

Aggregations

QuickShareDTO (org.alfresco.service.cmr.quickshare.QuickShareDTO)10 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)8 NodeRef (org.alfresco.service.cmr.repository.NodeRef)7 Test (org.junit.Test)7 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)6 InvalidSharedIdException (org.alfresco.service.cmr.quickshare.InvalidSharedIdException)5 HashMap (java.util.HashMap)2 RestoreNodeReport (org.alfresco.repo.node.archive.RestoreNodeReport)2 QName (org.alfresco.service.namespace.QName)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ClientAppNotFoundException (org.alfresco.repo.client.config.ClientAppNotFoundException)1 QuickShareLinkExpiryActionException (org.alfresco.repo.quickshare.QuickShareLinkExpiryActionException)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 TenantRunAsWork (org.alfresco.repo.tenant.TenantUtil.TenantRunAsWork)1 QuickShareLink (org.alfresco.rest.api.model.QuickShareLink)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1