Search in sources :

Example 1 with QuickShareDisabledException

use of org.alfresco.service.cmr.quickshare.QuickShareDisabledException 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

Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 ClientAppNotFoundException (org.alfresco.repo.client.config.ClientAppNotFoundException)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 InvalidSharedIdException (org.alfresco.service.cmr.quickshare.InvalidSharedIdException)1 QuickShareDTO (org.alfresco.service.cmr.quickshare.QuickShareDTO)1 QuickShareDisabledException (org.alfresco.service.cmr.quickshare.QuickShareDisabledException)1 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 NoSuchPersonException (org.alfresco.service.cmr.security.NoSuchPersonException)1 QName (org.alfresco.service.namespace.QName)1 ActivityEvent (org.alfresco.sync.events.types.ActivityEvent)1 Event (org.alfresco.sync.events.types.Event)1 EventPreparator (org.alfresco.sync.repo.events.EventPreparator)1 UUID (org.safehaus.uuid.UUID)1