Search in sources :

Example 1 with NuxeoPrincipal

use of org.nuxeo.ecm.core.api.NuxeoPrincipal in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveGenerateConflictedItemName method run.

@OperationMethod
public Blob run() throws IOException {
    String extension = "";
    if (name.contains(".")) {
        // Split on the last occurrence of . using a negative lookahead
        // regexp.
        String[] parts = name.split("\\.(?=[^\\.]+$)");
        name = parts[0];
        extension = "." + parts[1];
    }
    NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal();
    // fallback
    String userName = principal.getName();
    if (!StringUtils.isBlank(principal.getLastName()) && !StringUtils.isBlank(principal.getFirstName())) {
        // build more user friendly name from user info
        userName = principal.getFirstName() + " " + principal.getLastName();
    }
    Calendar userDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm");
    dateFormat.setCalendar(userDate);
    String formatedDate = dateFormat.format(userDate.getTime());
    String contextSection = String.format(" (%s - %s)", userName, formatedDate);
    String conflictedName = name + contextSection + extension;
    return Blobs.createJSONBlobFromValue(conflictedName);
}
Also used : Calendar(java.util.Calendar) NuxeoPrincipal(org.nuxeo.ecm.core.api.NuxeoPrincipal) SimpleDateFormat(java.text.SimpleDateFormat) OperationMethod(org.nuxeo.ecm.automation.core.annotations.OperationMethod)

Example 2 with NuxeoPrincipal

use of org.nuxeo.ecm.core.api.NuxeoPrincipal in project nuxeo-drive-server by nuxeo.

the class MockChangeFinder method getFileSystemChanges.

@Override
public List<FileSystemItemChange> getFileSystemChanges(CoreSession session, Set<IdRef> lastActiveRootRefs, SynchronizationRoots activeRoots, long lastSuccessfulSyncDate, long syncDate, int limit) throws TooManyChangesException {
    List<FileSystemItemChange> docChanges = new ArrayList<FileSystemItemChange>();
    if (!activeRoots.paths.isEmpty()) {
        StringBuilder querySb = new StringBuilder();
        querySb.append("SELECT * FROM Document WHERE (%s) AND (%s) ORDER BY dc:modified DESC");
        String query = String.format(querySb.toString(), getRootPathClause(activeRoots.paths), getDateClause(lastSuccessfulSyncDate, syncDate));
        if (log.isDebugEnabled()) {
            log.debug("Querying repository for document changes: " + query);
        }
        NuxeoPrincipal principal = (NuxeoPrincipal) session.getPrincipal();
        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
        for (String repositoryName : repositoryManager.getRepositoryNames()) {
            try (CloseableCoreSession repoSession = CoreInstance.openCoreSession(repositoryName, principal)) {
                docChanges.addAll(getDocumentChanges(repoSession, query, limit));
            }
        }
    }
    return docChanges;
}
Also used : ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RepositoryManager(org.nuxeo.ecm.core.api.repository.RepositoryManager) NuxeoPrincipal(org.nuxeo.ecm.core.api.NuxeoPrincipal)

Example 3 with NuxeoPrincipal

use of org.nuxeo.ecm.core.api.NuxeoPrincipal in project nuxeo-drive-server by nuxeo.

the class TestIntegrationTestOperations method testIntegrationTestsSetupAndTearDown.

@Test
public void testIntegrationTestsSetupAndTearDown() throws Exception {
    // ---------------------------------------------------------
    // Setup the integration tests environment as Administrator
    // ---------------------------------------------------------
    Blob testUserCredentialsBlob = (Blob) clientSession.newRequest(NuxeoDriveSetupIntegrationTests.ID).set("userNames", "joe,jack").set("permission", "ReadWrite").execute();
    assertNotNull(testUserCredentialsBlob);
    // Invalidate VCS cache
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // Check test users
    String testUserCredentials = IOUtils.toString(testUserCredentialsBlob.getStream(), "UTF-8");
    assertNotNull(testUserCredentials);
    String[] testUserCrendentialsArray = StringUtils.split(testUserCredentials, ",");
    assertEquals(2, testUserCrendentialsArray.length);
    assertTrue(testUserCrendentialsArray[0].startsWith("drivejoe:"));
    assertTrue(testUserCrendentialsArray[1].startsWith("drivejack:"));
    // useMembersGroup is false by default
    NuxeoPrincipal joePrincipal = userManager.getPrincipal("drivejoe");
    assertNotNull(joePrincipal);
    assertFalse(joePrincipal.getGroups().contains("members"));
    NuxeoPrincipal jackPrincipal = userManager.getPrincipal("drivejack");
    assertNotNull(jackPrincipal);
    assertFalse(jackPrincipal.getGroups().contains("members"));
    // Check test workspace
    DocumentRef testWorkspaceRef = new PathRef(testWorkspacePath);
    DocumentModel testWorkspace = session.getDocument(testWorkspaceRef);
    assertEquals("Workspace", testWorkspace.getType());
    assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
    assertTrue(session.hasPermission(joePrincipal, testWorkspaceRef, SecurityConstants.WRITE));
    assertTrue(session.hasPermission(jackPrincipal, testWorkspaceRef, SecurityConstants.WRITE));
    // Create test users' personal workspaces for cleanup check
    userWorkspaceService.getUserPersonalWorkspace("drivejoe", session.getRootDocument());
    userWorkspaceService.getUserPersonalWorkspace("drivejack", session.getRootDocument());
    assertNotNull(session.getDocument(new PathRef(userWorkspaceParentPath + "/drivejoe")));
    assertNotNull(session.getDocument(new PathRef(userWorkspaceParentPath + "/drivejack")));
    // Save personal workspaces
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ----------------------------------------------------------------------
    // Setup the integration tests environment with other user names without
    // having teared it down previously => should start by cleaning it up
    // ----------------------------------------------------------------------
    testUserCredentialsBlob = (Blob) clientSession.newRequest(NuxeoDriveSetupIntegrationTests.ID).set("userNames", "sarah").set("useMembersGroup", true).set("permission", "ReadWrite").execute();
    assertNotNull(testUserCredentialsBlob);
    // Check cleanup
    assertNull(userManager.getPrincipal("drivejoe"));
    assertNull(userManager.getPrincipal("drivejack"));
    // Process invalidations
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try {
        session.getDocument(new PathRef(userWorkspaceParentPath + "/drivejoe"));
        fail("User workspace should not exist.");
    } catch (DocumentNotFoundException e) {
        assertEquals(userWorkspaceParentPath + "/drivejoe", e.getMessage());
    }
    try {
        session.getDocument(new PathRef(userWorkspaceParentPath + "/drivejack"));
        fail("User workspace should not exist.");
    } catch (DocumentNotFoundException e) {
        assertEquals(userWorkspaceParentPath + "/drivejack", e.getMessage());
    }
    // Check test users
    testUserCredentials = IOUtils.toString(testUserCredentialsBlob.getStream(), "UTF-8");
    assertNotNull(testUserCredentials);
    testUserCrendentialsArray = StringUtils.split(testUserCredentials, ",");
    assertEquals(1, testUserCrendentialsArray.length);
    assertTrue(testUserCrendentialsArray[0].startsWith("drivesarah:"));
    NuxeoPrincipal sarahPrincipal = userManager.getPrincipal("drivesarah");
    assertNotNull(sarahPrincipal);
    assertTrue(sarahPrincipal.getGroups().contains("members"));
    // Check test workspace
    testWorkspace = session.getDocument(testWorkspaceRef);
    assertEquals("Nuxeo Drive Test Workspace", testWorkspace.getTitle());
    assertTrue(session.hasPermission(sarahPrincipal, testWorkspaceRef, SecurityConstants.WRITE));
    // Create test users' personal workspaces for cleanup check
    userWorkspaceService.getUserPersonalWorkspace("drivesarah", session.getRootDocument());
    assertNotNull(session.getDocument(new PathRef(userWorkspaceParentPath + "/drivesarah")));
    // Save personal workspaces
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    // ----------------------------------------------------------------------
    // Try to setup the integration tests environment as an unauthorized
    // user => should fail
    // ----------------------------------------------------------------------
    String sarahCredentials = testUserCrendentialsArray[0];
    String sarahPassword = sarahCredentials.substring(sarahCredentials.indexOf(':') + 1);
    Session unauthorizedSession = automationClient.getSession("drivesarah", sarahPassword);
    try {
        unauthorizedSession.newRequest(NuxeoDriveSetupIntegrationTests.ID).set("userNames", "john,bob").set("permission", "ReadWrite").execute();
        fail("NuxeoDrive.SetupIntegrationTests operation should not be callable by a non administrator.");
    } catch (Exception e) {
    // Expected
    }
    // ----------------------------------------------------------------------
    try {
        unauthorizedSession.newRequest(NuxeoDriveTearDownIntegrationTests.ID).execute();
        fail("NuxeoDrive.TearDownIntegrationTests operation should not be callable by a non administrator.");
    } catch (Exception e) {
    // Expected
    }
    // ----------------------------------------------------------------------
    // Tear down the integration tests environment as Administrator
    // ----------------------------------------------------------------------
    clientSession.newRequest(NuxeoDriveTearDownIntegrationTests.ID).execute();
    assertTrue(userManager.searchUsers("drive").isEmpty());
    // Process invalidations
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    try {
        session.getDocument(new PathRef(userWorkspaceParentPath + "/drivesarah"));
        fail("User workspace should not exist.");
    } catch (DocumentNotFoundException e) {
        assertEquals(userWorkspaceParentPath + "/drivesarah", e.getMessage());
    }
    assertFalse(session.exists(testWorkspaceRef));
}
Also used : Blob(org.nuxeo.ecm.automation.client.model.Blob) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) PathRef(org.nuxeo.ecm.core.api.PathRef) NuxeoPrincipal(org.nuxeo.ecm.core.api.NuxeoPrincipal) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) Session(org.nuxeo.ecm.automation.client.Session) CoreSession(org.nuxeo.ecm.core.api.CoreSession) Test(org.junit.Test)

Aggregations

NuxeoPrincipal (org.nuxeo.ecm.core.api.NuxeoPrincipal)3 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Test (org.junit.Test)1 Session (org.nuxeo.ecm.automation.client.Session)1 Blob (org.nuxeo.ecm.automation.client.model.Blob)1 OperationMethod (org.nuxeo.ecm.automation.core.annotations.OperationMethod)1 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)1 CoreSession (org.nuxeo.ecm.core.api.CoreSession)1 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)1 DocumentNotFoundException (org.nuxeo.ecm.core.api.DocumentNotFoundException)1 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)1 PathRef (org.nuxeo.ecm.core.api.PathRef)1 RepositoryManager (org.nuxeo.ecm.core.api.repository.RepositoryManager)1