Search in sources :

Example 11 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project records-management by Alfresco.

the class RecordableVersionServiceImpl method createRecordFromLatestVersion.

/**
 * @see org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionService#createRecordFromLatestVersion(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
 */
@Override
public NodeRef createRecordFromLatestVersion(final NodeRef filePlan, final NodeRef nodeRef) {
    ParameterCheck.mandatory("filePlan", filePlan);
    ParameterCheck.mandatory("nodeRef", nodeRef);
    NodeRef record = null;
    // check for versionable aspect
    if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE)) {
        createSnapshotVersion(nodeRef);
        // get the latest version
        final Version currentVersion = getCurrentVersion(nodeRef);
        if (currentVersion != null && !isRecordedVersion(currentVersion)) {
            // create the record from the current frozen state
            record = authenticationUtil.runAsSystem(new RunAsWork<NodeRef>() {

                public NodeRef doWork() throws Exception {
                    // get the documents readers and writers
                    Pair<Set<String>, Set<String>> readersAndWriters = extendedPermissionService.getReadersAndWriters(nodeRef);
                    // grab the frozen state
                    NodeRef currentFrozenState = currentVersion.getFrozenStateNodeRef();
                    // determine the type of the object
                    QName type = nodeService.getType(currentFrozenState);
                    // grab all the properties
                    Map<QName, Serializable> properties = nodeService.getProperties(currentFrozenState);
                    // grab all the aspects
                    Set<QName> aspects = nodeService.getAspects(currentFrozenState);
                    // create the record
                    NodeRef record = recordService.createRecordFromContent(filePlan, (String) properties.get(ContentModel.PROP_NAME), type, properties, null);
                    // apply aspects to record
                    for (QName aspect : aspects) {
                        // add the aspect, properties have already been set
                        nodeService.addAspect(record, aspect, null);
                    }
                    // apply version record aspect to record
                    PropertyMap versionRecordProps = new PropertyMap(3);
                    versionRecordProps.put(PROP_VERSIONED_NODEREF, nodeRef);
                    versionRecordProps.put(RecordableVersionModel.PROP_VERSION_LABEL, currentVersion.getVersionLabel());
                    versionRecordProps.put(RecordableVersionModel.PROP_VERSION_DESCRIPTION, currentVersion.getDescription());
                    versionRecordProps.put(ContentModel.PROP_VERSION_TYPE, currentVersion.getVersionType());
                    nodeService.addAspect(record, ASPECT_VERSION_RECORD, versionRecordProps);
                    // wire record up to previous record
                    linkToPreviousVersionRecord(nodeRef, record);
                    // set the extended security
                    extendedSecurityService.set(record, readersAndWriters);
                    return record;
                }
            });
            // get the version history
            NodeRef versionHistoryRef = getVersionHistoryNodeRef(nodeRef);
            // get details from the version before we remove it
            int versionNumber = getVersionNumber(currentVersion);
            Map<QName, Serializable> versionProperties = getVersionAspectProperties(currentVersion);
            QName sourceTypeRef = getVersionType(currentVersion);
            // patch-up owner information, which needs to be frozen for recorded versions
            String owner = (String) nodeService.getProperty(currentVersion.getFrozenStateNodeRef(), ContentModel.PROP_OWNER);
            if (owner != null) {
                versionProperties.put(PROP_FROZEN_OWNER, owner);
            }
            // delete the current version
            this.dbNodeService.deleteNode(convertNodeRef(currentVersion.getFrozenStateNodeRef()));
            // create a new version history if we need to
            if (!nodeService.exists(versionHistoryRef)) {
                versionHistoryRef = createVersionHistory(nodeRef);
            }
            // create recorded version nodeRef
            ChildAssociationRef childAssocRef = dbNodeService.createNode(versionHistoryRef, Version2Model.CHILD_QNAME_VERSIONS, QName.createQName(Version2Model.NAMESPACE_URI, Version2Model.CHILD_VERSIONS + "-" + versionNumber), sourceTypeRef, null);
            NodeRef versionNodeRef = childAssocRef.getChildRef();
            // add aspect with the standard version properties to the 'version' node
            nodeService.addAspect(versionNodeRef, Version2Model.ASPECT_VERSION, versionProperties);
            // add the recordedVersion aspect with link to record
            nodeService.addAspect(versionNodeRef, ASPECT_RECORDED_VERSION, Collections.singletonMap(PROP_RECORD_NODE_REF, (Serializable) record));
        }
    }
    return record;
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) PropertyMap(org.alfresco.util.PropertyMap) Version(org.alfresco.service.cmr.version.Version)

Example 12 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project alfresco-remote-api by Alfresco.

the class AbstractRatingScheme method getActivityData.

@SuppressWarnings("unchecked")
private JSONObject getActivityData(final NodeRef nodeRef, String siteId) {
    JSONObject activityData = null;
    if (siteId != null) {
        // may not be able to read these nodes, but we need to for the activity processing so run as system
        activityData = AuthenticationUtil.runAsSystem(new RunAsWork<JSONObject>() {

            @Override
            public JSONObject doWork() throws Exception {
                JSONObject activityData = new JSONObject();
                activityData.put("title", nodeService.getProperty(nodeRef, ContentModel.PROP_NAME));
                try {
                    StringBuilder sb = new StringBuilder("document-details?nodeRef=");
                    sb.append(URLEncoder.encode(nodeRef.toString(), "UTF-8"));
                    activityData.put("page", sb.toString());
                } catch (UnsupportedEncodingException e) {
                    logger.warn("Unable to urlencode page for create nodeRating activity");
                }
                return activityData;
            }
        });
    }
    return activityData;
}
Also used : JSONObject(org.json.simple.JSONObject) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project alfresco-remote-api by Alfresco.

the class RepositoryContainer method onApplicationEvent.

/* (non-Javadoc)
     * @see org.alfresco.web.scripts.AbstractRuntimeContainer#onApplicationEvent(org.springframework.context.ApplicationEvent)
     */
@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent) event;
        ApplicationContext refreshContext = refreshEvent.getApplicationContext();
        if (refreshContext != null && refreshContext.equals(applicationContext)) {
            RunAsWork<Object> work = new RunAsWork<Object>() {

                public Object doWork() throws Exception {
                    reset();
                    return null;
                }
            };
            AuthenticationUtil.runAs(work, AuthenticationUtil.getSystemUserName());
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent)

Example 14 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project alfresco-remote-api by Alfresco.

the class InvitationDelete method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    final String siteShortName = templateVars.get("shortname");
    final String invitationId = templateVars.get("invitationId");
    validateParameters(siteShortName, invitationId);
    try {
        // MNT-9905 Pending Invites created by one site manager aren't visible to other site managers
        String currentUser = AuthenticationUtil.getRunAsUser();
        if (siteShortName != null && (SiteModel.SITE_MANAGER).equals(siteService.getMembersRole(siteShortName, currentUser))) {
            RunAsWork<Void> runAsSystem = new RunAsWork<Void>() {

                @Override
                public Void doWork() throws Exception {
                    checkAndCancelTheInvitation(invitationId, siteShortName);
                    return null;
                }
            };
            AuthenticationUtil.runAs(runAsSystem, AuthenticationUtil.getSystemUserName());
        } else {
            checkAndCancelTheInvitation(invitationId, siteShortName);
        }
    } catch (InvitationExceptionForbidden fe) {
        throw new WebScriptException(Status.STATUS_FORBIDDEN, "Unable to cancel workflow", fe);
    } catch (AccessDeniedException ade) {
        throw new WebScriptException(Status.STATUS_FORBIDDEN, "Unable to cancel workflow", ade);
    }
    return model;
}
Also used : AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) InvitationExceptionForbidden(org.alfresco.service.cmr.invitation.InvitationExceptionForbidden)

Example 15 with RunAsWork

use of org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork in project alfresco-remote-api by Alfresco.

the class NodeArchiveServiceRestApiTest method tearDown.

@Override
public void tearDown() throws Exception {
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    super.tearDown();
    // Tidy up any test nodes that are hanging around.
    transactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            AuthenticationUtil.runAs(new RunAsWork<Void>() {

                public Void doWork() throws Exception {
                    for (NodeRef nodeRef : createdNodes) {
                        if (nodeRef != null && nodeService.exists(nodeRef)) {
                            nodeService.deleteNode(nodeRef);
                        }
                    }
                    for (StoreRef store : createdStores) {
                        if (store != null && nodeService.exists(store)) {
                            nodeService.deleteStore(store);
                        }
                    }
                    return null;
                }
            }, AuthenticationUtil.getSystemUserName());
            return null;
        }
    });
    // Delete users
    for (String userName : this.createdPeople) {
        personService.deletePerson(userName);
    }
    // Clear the lists
    this.createdPeople.clear();
    this.createdNodes.clear();
    this.createdStores.clear();
    AuthenticationUtil.clearCurrentSecurityContext();
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) StoreRef(org.alfresco.service.cmr.repository.StoreRef) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)

Aggregations

RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)34 NodeRef (org.alfresco.service.cmr.repository.NodeRef)26 HashMap (java.util.HashMap)11 Serializable (java.io.Serializable)8 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)7 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileExistsException (org.alfresco.service.cmr.model.FileExistsException)4 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)4 QName (org.alfresco.service.namespace.QName)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Set (java.util.Set)3 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)3 Version (org.alfresco.service.cmr.version.Version)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2