Search in sources :

Example 6 with EntityNotFoundException

use of org.alfresco.rest.framework.core.exceptions.EntityNotFoundException in project alfresco-remote-api by Alfresco.

the class WorkflowRestImpl method validateIfUserAllowedToWorkWithProcess.

/**
 * Validates if the logged in user is allowed to get information about a specific process instance.
 * If the user is not allowed an exception is thrown.
 *
 * @param processId identifier of the process instance
 */
protected List<HistoricVariableInstance> validateIfUserAllowedToWorkWithProcess(String processId) {
    List<HistoricVariableInstance> variableInstances = activitiProcessEngine.getHistoryService().createHistoricVariableInstanceQuery().processInstanceId(processId).list();
    Map<String, Object> variableMap = new HashMap<String, Object>();
    if (variableInstances != null && variableInstances.size() > 0) {
        for (HistoricVariableInstance variableInstance : variableInstances) {
            variableMap.put(variableInstance.getVariableName(), variableInstance.getValue());
        }
    } else {
        throw new EntityNotFoundException(processId);
    }
    if (tenantService.isEnabled()) {
        String tenantDomain = (String) variableMap.get(ActivitiConstants.VAR_TENANT_DOMAIN);
        if (TenantUtil.getCurrentDomain().equals(tenantDomain) == false) {
            throw new PermissionDeniedException("Process is running in another tenant");
        }
    }
    // MNT-17918 - required for initiator variable already updated as NodeRef type
    Object initiator = variableMap.get(WorkflowConstants.PROP_INITIATOR);
    String nodeId = ((initiator instanceof ActivitiScriptNode) ? ((ActivitiScriptNode) initiator).getNodeRef().getId() : ((NodeRef) initiator).getId());
    if (initiator != null && AuthenticationUtil.getRunAsUser().equals(nodeId)) {
        // user is allowed
        return variableInstances;
    }
    String username = AuthenticationUtil.getRunAsUser();
    if (authorityService.isAdminAuthority(username)) {
        // Admin is allowed to read all processes in the current tenant
        return variableInstances;
    } else {
        // MNT-12382 check for membership in the assigned group
        ActivitiScriptNode group = (ActivitiScriptNode) variableMap.get("bpm_groupAssignee");
        if (group != null) {
            // check that the process is unclaimed
            Task task = activitiProcessEngine.getTaskService().createTaskQuery().processInstanceId(processId).singleResult();
            if ((task != null) && (task.getAssignee() == null) && isUserInGroup(username, group.getNodeRef())) {
                return variableInstances;
            }
        }
        // If non-admin user, involvement in the task is required (either owner, assignee or externally involved).
        HistoricTaskInstanceQuery query = activitiProcessEngine.getHistoryService().createHistoricTaskInstanceQuery().processInstanceId(processId).taskInvolvedUser(AuthenticationUtil.getRunAsUser());
        List<HistoricTaskInstance> taskList = query.list();
        if (org.apache.commons.collections.CollectionUtils.isEmpty(taskList)) {
            throw new PermissionDeniedException("user is not allowed to access information about process " + processId);
        }
    }
    return variableInstances;
}
Also used : Task(org.activiti.engine.task.Task) HistoricTaskInstance(org.activiti.engine.history.HistoricTaskInstance) HashMap(java.util.HashMap) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) HistoricTaskInstanceQuery(org.activiti.engine.history.HistoricTaskInstanceQuery) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) HistoricVariableInstance(org.activiti.engine.history.HistoricVariableInstance) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)

Example 7 with EntityNotFoundException

use of org.alfresco.rest.framework.core.exceptions.EntityNotFoundException in project alfresco-remote-api by Alfresco.

the class ResultMapperTests method setupTests.

@BeforeClass
public static void setupTests() throws Exception {
    Map<String, UserInfo> mapUserInfo = new HashMap<>();
    mapUserInfo.put(AuthenticationUtil.getSystemUserName(), new UserInfo(AuthenticationUtil.getSystemUserName(), "sys", "sys"));
    Map<QName, Serializable> nodeProps = new HashMap<>();
    NodesImpl nodes = mock(NodesImpl.class);
    ServiceRegistry sr = mock(ServiceRegistry.class);
    DeletedNodes deletedNodes = mock(DeletedNodes.class);
    nodes.setServiceRegistry(sr);
    VersionService versionService = mock(VersionService.class);
    VersionHistory versionHistory = mock(VersionHistory.class);
    Map<String, Serializable> versionProperties = new HashMap<>();
    versionProperties.put(Version.PROP_DESCRIPTION, "ver desc");
    versionProperties.put(Version2Model.PROP_VERSION_TYPE, "v type");
    when(versionHistory.getVersion(anyString())).thenAnswer(invocation -> {
        return new VersionImpl(versionProperties, new NodeRef(StoreMapper.STORE_REF_VERSION2_SPACESSTORE, GUID.generate()));
    });
    NodeService nodeService = mock(NodeService.class);
    when(versionService.getVersionHistory(notNull(NodeRef.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        NodeRef aNode = (NodeRef) args[0];
        return versionHistory;
    });
    when(nodeService.getProperties(notNull(NodeRef.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        NodeRef aNode = (NodeRef) args[0];
        if (StoreMapper.STORE_REF_VERSION2_SPACESSTORE.equals(aNode.getStoreRef())) {
            nodeProps.put(Version2Model.PROP_QNAME_FROZEN_NODE_REF, new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, FROZEN_ID + aNode.getId()));
            nodeProps.put(Version2Model.PROP_QNAME_VERSION_LABEL, FROZEN_VER);
        }
        return nodeProps;
    });
    when(sr.getVersionService()).thenReturn(versionService);
    when(sr.getNodeService()).thenReturn(nodeService);
    when(nodes.validateOrLookupNode(notNull(String.class), anyString())).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String aNode = (String) args[0];
        if (aNode.endsWith("" + VERSIONED_ID)) {
            throw new EntityNotFoundException("" + VERSIONED_ID);
        } else {
            return new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, aNode);
        }
    });
    // // NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
    when(nodes.getFolderOrDocument(notNull(NodeRef.class), any(), any(), any(), any())).thenAnswer(new Answer<Node>() {

        @Override
        public Node answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            NodeRef aNode = (NodeRef) args[0];
            if (StoreRef.STORE_REF_ARCHIVE_SPACESSTORE.equals(aNode.getStoreRef())) {
                // Return NULL if its from the archive store.
                return null;
            }
            return new Node(aNode, (NodeRef) args[1], nodeProps, mapUserInfo, sr);
        }
    });
    when(deletedNodes.getDeletedNode(notNull(String.class), any(), anyBoolean(), any())).thenAnswer(new Answer<Node>() {

        @Override
        public Node answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            String nodeId = (String) args[0];
            if (FROZEN_ID.equals(nodeId))
                throw new EntityNotFoundException(nodeId);
            NodeRef aNode = new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, nodeId);
            return new Node(aNode, new NodeRef(StoreRef.STORE_REF_ARCHIVE_SPACESSTORE, "unknown"), nodeProps, mapUserInfo, sr);
        }
    });
    PersonPropertyLookup propertyLookups = mock(PersonPropertyLookup.class);
    when(propertyLookups.supports()).thenReturn(Stream.of("creator", "modifier").collect(Collectors.toSet()));
    when(propertyLookups.lookup(notNull(String.class))).thenAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String value = (String) args[0];
        if ("mjackson".equals(value))
            return "Michael Jackson";
        return null;
    });
    PropertyLookupRegistry propertyLookupRegistry = new PropertyLookupRegistry();
    propertyLookupRegistry.setLookups(Arrays.asList(propertyLookups));
    mapper = new ResultMapper();
    mapper.setNodes(nodes);
    mapper.setStoreMapper(new StoreMapper());
    mapper.setPropertyLookup(propertyLookupRegistry);
    mapper.setDeletedNodes(deletedNodes);
    mapper.setServiceRegistry(sr);
    NodeVersionsRelation nodeVersionsRelation = new NodeVersionsRelation();
    nodeVersionsRelation.setNodes(nodes);
    nodeVersionsRelation.setServiceRegistry(sr);
    nodeVersionsRelation.afterPropertiesSet();
    mapper.setNodeVersions(nodeVersionsRelation);
    helper = new SerializerTestHelper();
    searchMapper.setStoreMapper(new StoreMapper());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Node(org.alfresco.rest.api.model.Node) StoreMapper(org.alfresco.rest.api.search.impl.StoreMapper) VersionService(org.alfresco.service.cmr.version.VersionService) UserInfo(org.alfresco.rest.api.model.UserInfo) Matchers.anyString(org.mockito.Matchers.anyString) VersionImpl(org.alfresco.repo.version.common.VersionImpl) NodeRef(org.alfresco.service.cmr.repository.NodeRef) DeletedNodes(org.alfresco.rest.api.DeletedNodes) QName(org.alfresco.service.namespace.QName) PersonPropertyLookup(org.alfresco.rest.api.lookups.PersonPropertyLookup) NodeService(org.alfresco.service.cmr.repository.NodeService) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) NodesImpl(org.alfresco.rest.api.impl.NodesImpl) PropertyLookupRegistry(org.alfresco.rest.api.lookups.PropertyLookupRegistry) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultMapper(org.alfresco.rest.api.search.impl.ResultMapper) JSONObject(org.json.JSONObject) ServiceRegistry(org.alfresco.service.ServiceRegistry) NodeVersionsRelation(org.alfresco.rest.api.nodes.NodeVersionsRelation) BeforeClass(org.junit.BeforeClass)

Example 8 with EntityNotFoundException

use of org.alfresco.rest.framework.core.exceptions.EntityNotFoundException in project alfresco-remote-api by Alfresco.

the class AuditImpl method deleteAuditEntry.

@Override
public void deleteAuditEntry(String auditAppId, long auditEntryId, Parameters parameters) {
    checkEnabled();
    AuditService.AuditApplication auditApplication = findAuditAppByIdOr404(auditAppId);
    int deleted = auditService.clearAuditByIdRange(auditApplication.getName(), auditEntryId, auditEntryId + 1);
    if (deleted != 1) {
        throw new EntityNotFoundException("" + auditEntryId);
    }
}
Also used : AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) AuditService(org.alfresco.service.cmr.audit.AuditService)

Example 9 with EntityNotFoundException

use of org.alfresco.rest.framework.core.exceptions.EntityNotFoundException in project alfresco-remote-api by Alfresco.

the class ProcessDefinitionsImpl method getProcessDefinition.

@Override
public ProcessDefinition getProcessDefinition(String definitionId) {
    ProcessDefinitionQuery query = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(definitionId);
    if (tenantService.isEnabled() && deployWorkflowsInTenant) {
        query.processDefinitionKeyLike("@" + TenantUtil.getCurrentDomain() + "@%");
    }
    org.activiti.engine.repository.ProcessDefinition processDefinition = query.singleResult();
    if (processDefinition == null) {
        throw new EntityNotFoundException(definitionId);
    }
    ProcessDefinition deploymentRest = createProcessDefinitionRest((ProcessDefinitionEntity) processDefinition);
    return deploymentRest;
}
Also used : ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) ProcessDefinition(org.alfresco.rest.workflow.api.model.ProcessDefinition) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)

Example 10 with EntityNotFoundException

use of org.alfresco.rest.framework.core.exceptions.EntityNotFoundException in project alfresco-remote-api by Alfresco.

the class ProcessesImpl method deleteProcess.

@Override
public void deleteProcess(String id) {
    validateIfUserAllowedToWorkWithProcess(id);
    ProcessInstance processInstance = activitiProcessEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(id).singleResult();
    if (processInstance == null) {
        throw new EntityNotFoundException(id);
    }
    activitiProcessEngine.getRuntimeService().deleteProcessInstance(id, "deleted through REST API call");
}
Also used : ProcessInstance(org.activiti.engine.runtime.ProcessInstance) HistoricProcessInstance(org.activiti.engine.history.HistoricProcessInstance) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)

Aggregations

EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)66 NodeRef (org.alfresco.service.cmr.repository.NodeRef)28 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)24 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)16 QName (org.alfresco.service.namespace.QName)15 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)12 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)12 HashMap (java.util.HashMap)11 InvalidNodeRefException (org.alfresco.service.cmr.repository.InvalidNodeRefException)11 ArrayList (java.util.ArrayList)9 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)8 InvalidSharedIdException (org.alfresco.service.cmr.quickshare.InvalidSharedIdException)7 Serializable (java.io.Serializable)6 FileInfo (org.alfresco.service.cmr.model.FileInfo)6 HistoricProcessInstance (org.activiti.engine.history.HistoricProcessInstance)5 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)5 ActivitiScriptNode (org.alfresco.repo.workflow.activiti.ActivitiScriptNode)5 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)5 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)4 HistoricVariableInstance (org.activiti.engine.history.HistoricVariableInstance)4