Search in sources :

Example 1 with TestSiteAndMemberInfo

use of org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo in project alfresco-repository by Alfresco.

the class SiteServiceImplMoreTest method deleteSiteDeleteAuthoritiesAndRestoreEnsuringSiteGroupsWasRecreated.

/**
 * This test ensures that site deleted before MNT-10109 fix (i.e. deleted with their associated authorities) could be restored
 * with site-groups recreation
 *
 * @throws Exception
 */
@Test
public void deleteSiteDeleteAuthoritiesAndRestoreEnsuringSiteGroupsWasRecreated() throws Exception {
    final String siteShortName = "testsite-" + System.currentTimeMillis();
    final SiteServiceImpl siteServiceImpl = (SiteServiceImpl) SITE_SERVICE;
    log.debug("Creating test site called: " + siteShortName);
    // Create site
    final TestSiteAndMemberInfo testSiteAndMemberInfo = perMethodTestSites.createTestSiteWithUserPerRole(siteShortName, "sitePreset", SiteVisibility.PUBLIC, AuthenticationUtil.getAdminUserName());
    // Delete permissions and site
    final Map<String, String> membersBefore = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Map<String, String>>() {

        public Map<String, String> execute() throws Throwable {
            NodeRef siteNodeRef = testSiteAndMemberInfo.siteInfo.getNodeRef();
            Map<String, String> membersBefore = SITE_SERVICE.listMembers(siteShortName, null, null, 0, true);
            log.debug(membersBefore.size() + " members...");
            for (Map.Entry<String, String> entry : membersBefore.entrySet()) {
                log.debug(entry);
            }
            Map<String, Set<String>> groupsMemberships = new HashMap<String, Set<String>>();
            log.debug("About to delete site-related groups.");
            // delete authorities
            Set<String> permissions = PERMISSION_SERVICE.getSettablePermissions(SiteModel.TYPE_SITE);
            for (String permission : permissions) {
                String prefixSiteRoleGroup = siteServiceImpl.getSiteRoleGroup(siteShortName, permission, true);
                Set<String> groupUsers = AUTHORITY_SERVICE.getContainedAuthorities(null, prefixSiteRoleGroup, true);
                groupsMemberships.put(prefixSiteRoleGroup, groupUsers);
                AUTHORITY_SERVICE.deleteAuthority(prefixSiteRoleGroup);
            }
            // emulate onDelete site behavior before MNT-10109 fix
            NODE_SERVICE.setProperty(siteNodeRef, QName.createQName(null, "memberships"), (Serializable) groupsMemberships);
            log.debug("Site-related groups deleted.");
            log.debug("About to delete site.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("Site deleted.");
            return membersBefore;
        }
    });
    // restore the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            assertThatArchivedNodeExists(testSiteAndMemberInfo.siteInfo.getNodeRef(), "Site node not found in archive.");
            // ensure there are no authorities
            Set<String> permissions = PERMISSION_SERVICE.getSettablePermissions(SiteModel.TYPE_SITE);
            for (String permission : permissions) {
                String permissionGroupShortName = siteServiceImpl.getSiteRoleGroup(siteShortName, permission, false);
                String authorityName = AUTHORITY_SERVICE.getName(AuthorityType.GROUP, permissionGroupShortName);
                assertTrue("Authotiry should not exist : " + authorityName, !AUTHORITY_SERVICE.authorityExists(authorityName));
            }
            log.debug("About to restore site node from archive");
            final NodeRef archivedSiteNode = NODE_ARCHIVE_SERVICE.getArchivedNode(testSiteAndMemberInfo.siteInfo.getNodeRef());
            RestoreNodeReport report = NODE_ARCHIVE_SERVICE.restoreArchivedNode(archivedSiteNode);
            // ...which should work
            assertEquals("Failed to restore site from archive", RestoreStatus.SUCCESS, report.getStatus());
            log.debug("Successfully restored site from arhive.");
            return null;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Map<String, String>>() {

        public Map<String, String> execute() throws Throwable {
            // The site itself should have been restored, of course...
            assertTrue("The site noderef was not restored as expected", NODE_SERVICE.exists(testSiteAndMemberInfo.siteInfo.getNodeRef()));
            Map<String, String> members = SITE_SERVICE.listMembers(siteShortName, null, null, 0, true);
            assertEquals("Not all member have been restored", membersBefore.size(), members.size());
            log.debug(members.size() + " members...");
            for (Map.Entry<String, String> entry : SITE_SERVICE.listMembers(siteShortName, null, null, 0, true).entrySet()) {
                log.debug(entry);
            }
            // Group authority nodes should be restored or recreated
            for (String role : SITE_SERVICE.getSiteRoles()) {
                final String siteGroup = SITE_SERVICE.getSiteRoleGroup(siteShortName, role);
                assertTrue("Site group for role " + role + " did not exist after site restoration", AUTHORITY_SERVICE.authorityExists(siteGroup));
            }
            Set<String> currentManagers = AUTHORITY_SERVICE.getContainedAuthorities(AuthorityType.USER, siteServiceImpl.getSiteRoleGroup(siteShortName, SiteModel.SITE_MANAGER, true), false);
            // ensure that there is at least one site manager
            log.debug("Current Managers " + currentManagers);
            assertTrue("There should be at least one site manager", !currentManagers.isEmpty());
            return null;
        }
    });
    // remove site completely
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("About to delete site completely.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("About to purge site from trashcan.");
            // get archive node reference
            String storePath = "archive://SpacesStore";
            StoreRef storeRef = new StoreRef(storePath);
            NodeRef archivedNodeRef = new NodeRef(storeRef, testSiteAndMemberInfo.siteInfo.getNodeRef().getId());
            NODE_ARCHIVE_SERVICE.purgeArchivedNode(archivedNodeRef);
            return null;
        }
    });
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) Serializable(java.io.Serializable) Set(java.util.Set) TestSiteAndMemberInfo(org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo) RestoreNodeReport(org.alfresco.repo.node.archive.RestoreNodeReport) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Map(java.util.Map) HashMap(java.util.HashMap) PropertyMap(org.alfresco.util.PropertyMap) TemporarySitesTest(org.alfresco.util.test.junitrules.TemporarySitesTest) Test(org.junit.Test)

Example 2 with TestSiteAndMemberInfo

use of org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo in project alfresco-repository by Alfresco.

the class SiteServiceImplMoreTest method deleteSiteDeleteAuthoritiesAndRestoreEnsuringLocksAreRestored.

/**
 * Added as part of MNT-14671 : Site with document locked for Edit Offline cannot be deleted.
 * This test checks that the owner of a site can delete the site even if there are locked files,
 * that belong to the owner or to other members of that site;
 *
 * This test also checks that after restore the locks are restored correctly for all the locked
 * files;
 *
 * MNT-15855:
 * Checks the case when there is a working copy (simulate lock for offline edit)
 *
 * @throws Exception
 */
@Test
public void deleteSiteDeleteAuthoritiesAndRestoreEnsuringLocksAreRestored() throws Exception {
    final String userOwner = "UserOwner";
    final String userCollaborator = "UserColaborator";
    // delete and restore test
    final String userPrefix = "dart";
    // create the users
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            createUser(userOwner, userPrefix);
            createUser(userCollaborator, userPrefix);
            return null;
        }
    });
    final String siteShortName = "testsite-" + System.currentTimeMillis();
    log.debug("Creating test site called: " + siteShortName);
    // Create site
    final TestSiteAndMemberInfo testSiteAndMemberInfo = perMethodTestSites.createTestSiteWithUserPerRole(siteShortName, "sitePreset", SiteVisibility.PUBLIC, userOwner);
    // create some documents into the test site as the owner of the site
    AUTHENTICATION_COMPONENT.setCurrentUser(userOwner);
    /*
         * [site] {siteShortName}
         *    |
         *    --- [siteContainer] {componentId}
         *          |
         *          --- [cm:content] fileFolderPrefix + "file.txt"
         *          |
         *          |-- [folder] fileFolderPrefix + "folder"
         *                  |
         *                  |-- [cm:content] fileFolderPrefix + "fileInFolder.txt"
         *                  |
         *                  |-- [folder] fileFolderPrefix + "subfolder"
         *                         |
         *                         |-- [cm:content] fileFolderPrefix + "fileInSubfolder.txt"
         *          --- [cm:content] fileFolderPrefix + "fileEditOfline.txt"
         */
    String fileFolderPrefix = "TESTLOCK_";
    String componentId = "doclib";
    NodeRef siteContainer = SITE_SERVICE.createContainer(siteShortName, componentId, ContentModel.TYPE_FOLDER, null);
    final FileInfo fileInfo = FILE_FOLDER_SERVICE.create(siteContainer, fileFolderPrefix + "file.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer = FILE_FOLDER_SERVICE.getWriter(fileInfo.getNodeRef());
    writer.putContent("Just some old content that doesn't mean anything");
    FileInfo folder1Info = FILE_FOLDER_SERVICE.create(siteContainer, fileFolderPrefix + "folder", ContentModel.TYPE_FOLDER);
    FileInfo fileInfo2 = FILE_FOLDER_SERVICE.create(folder1Info.getNodeRef(), fileFolderPrefix + "fileInFolder.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer2 = FILE_FOLDER_SERVICE.getWriter(fileInfo2.getNodeRef());
    writer2.putContent("Just some old content that doesn't mean anything");
    FileInfo folder2Info = FILE_FOLDER_SERVICE.create(folder1Info.getNodeRef(), fileFolderPrefix + "subfolder", ContentModel.TYPE_FOLDER);
    FileInfo fileInfo3 = FILE_FOLDER_SERVICE.create(folder2Info.getNodeRef(), fileFolderPrefix + "fileInSubfolder.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer3 = FILE_FOLDER_SERVICE.getWriter(fileInfo3.getNodeRef());
    writer3.putContent("Just some old content that doesn't mean anything");
    // Make sure there are no locks on the fileInfo yet
    assertEquals(LockStatus.NO_LOCK, LOCK_SERVICE.getLockStatus(fileInfo.getNodeRef()));
    // Lock a file as userOwner
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            LOCK_SERVICE.lock(fileInfo.getNodeRef(), LockType.WRITE_LOCK);
            return null;
        }
    });
    // Make sure we have a lock now
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileInfo.getNodeRef()));
    checkThatNonMembersCanNotCreateFiles(userCollaborator, fileFolderPrefix, folder2Info);
    // Make sure we are running as userOwner
    AUTHENTICATION_COMPONENT.setCurrentUser(userOwner);
    // Make userCollaborator a member of the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Throwable {
            SITE_SERVICE.setMembership(siteShortName, userCollaborator, SiteModel.SITE_COLLABORATOR);
            return null;
        }
    });
    // Now, as userCollaborator create a file and lock it
    AUTHENTICATION_COMPONENT.setCurrentUser(userCollaborator);
    final FileInfo fileInfoForCollaboratorUser = FILE_FOLDER_SERVICE.create(folder2Info.getNodeRef(), fileFolderPrefix + "userCollaborator.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer4 = FILE_FOLDER_SERVICE.getWriter(fileInfoForCollaboratorUser.getNodeRef());
    writer4.putContent("Just some old content that doesn't mean anything");
    // Check that the node is not currently locked
    assertEquals(LockStatus.NO_LOCK, LOCK_SERVICE.getLockStatus(fileInfoForCollaboratorUser.getNodeRef()));
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // lock the file as userCollaborator
            LOCK_SERVICE.lock(fileInfoForCollaboratorUser.getNodeRef(), LockType.WRITE_LOCK);
            return null;
        }
    });
    // Test valid lock
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileInfoForCollaboratorUser.getNodeRef()));
    // Create a file to test Edit offline
    final FileInfo fileEditOffline = FILE_FOLDER_SERVICE.create(siteContainer, fileFolderPrefix + "fileEditOfline.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writerEO = FILE_FOLDER_SERVICE.getWriter(fileEditOffline.getNodeRef());
    writerEO.putContent("Just some old content that doesn't mean anything");
    // Make sure there are no locks on the fileEditOffline yet
    assertEquals(LockStatus.NO_LOCK, LOCK_SERVICE.getLockStatus(fileEditOffline.getNodeRef()));
    // Check out the document - simulate Edit offline
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            NodeRef workingCopy = COCI_SERVICE.checkout(fileEditOffline.getNodeRef());
            assertNotNull(workingCopy);
            return null;
        }
    });
    // Make sure we have a lock now on fileEditOffline
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileEditOffline.getNodeRef()));
    // Switch back to userOwner so we can call delete the site
    AUTHENTICATION_COMPONENT.setCurrentUser(userOwner);
    // Delete the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("About to delete site.");
            AUTHENTICATION_COMPONENT.getCurrentUserName();
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("Site deleted.");
            return null;
        }
    });
    // Restore the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            assertThatArchivedNodeExists(testSiteAndMemberInfo.siteInfo.getNodeRef(), "Site node not found in archive.");
            log.debug("About to restore site node from archive");
            final NodeRef archivedSiteNode = NODE_ARCHIVE_SERVICE.getArchivedNode(testSiteAndMemberInfo.siteInfo.getNodeRef());
            RestoreNodeReport report = NODE_ARCHIVE_SERVICE.restoreArchivedNode(archivedSiteNode);
            // ...which should work
            assertEquals("Failed to restore site from archive", RestoreStatus.SUCCESS, report.getStatus());
            log.debug("Successfully restored site from arhive.");
            return null;
        }
    });
    // Check that the files have been restored and all the locks are present
    AUTHENTICATION_COMPONENT.setCurrentUser(userCollaborator);
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileInfoForCollaboratorUser.getNodeRef()));
    // Check that the file for edit offline has been restored and has the expected lock owner
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileEditOffline.getNodeRef()));
    AUTHENTICATION_COMPONENT.setCurrentUser(userOwner);
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileInfo.getNodeRef()));
    // Check that the file for edit offline has been restored and is locked
    assertEquals(LockStatus.LOCKED, LOCK_SERVICE.getLockStatus(fileEditOffline.getNodeRef()));
    // Remove site completely
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // if we apply the sys:temporary aspect to the site, the NodeService will not
            // archive it i.e. this bit of clean up will be a bit faster.
            NODE_SERVICE.addAspect(testSiteAndMemberInfo.siteInfo.getNodeRef(), ContentModel.ASPECT_TEMPORARY, null);
            log.debug("About to delete site completely.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("About to purge site from trashcan.");
            // get archive node reference
            String storePath = "archive://SpacesStore";
            StoreRef storeRef = new StoreRef(storePath);
            NodeRef archivedNodeRef = new NodeRef(storeRef, testSiteAndMemberInfo.siteInfo.getNodeRef().getId());
            NODE_ARCHIVE_SERVICE.purgeArchivedNode(archivedNodeRef);
            return null;
        }
    });
}
Also used : RestoreNodeReport(org.alfresco.repo.node.archive.RestoreNodeReport) NodeRef(org.alfresco.service.cmr.repository.NodeRef) StoreRef(org.alfresco.service.cmr.repository.StoreRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) TestSiteAndMemberInfo(org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo) TemporarySitesTest(org.alfresco.util.test.junitrules.TemporarySitesTest) Test(org.junit.Test)

Example 3 with TestSiteAndMemberInfo

use of org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo in project alfresco-repository by Alfresco.

the class SiteServiceImplMoreTest method deleteSiteAndRestoreEnsuringSiteGroupsAreRecovered.

/**
 * This test ensures that when sites are deleted (moved to the trashcan) and then restored, that the 4 role-based groups are
 * restored correctly and that any users who were members of those groups are made members once more.
 *
 * @see SiteServiceImpl#deleteSite(String)
 * @see SiteServiceImpl#onRestoreNode(org.alfresco.service.cmr.repository.ChildAssociationRef)
 */
@Test
public void deleteSiteAndRestoreEnsuringSiteGroupsAreRecovered() throws Exception {
    // Implementation note: as of Alfresco 4.2, authorities cannot be archived and are always hard-deleted.
    // Therefore, on site restore, the SiteService *recreates* the deleted groups associated with the restored site.
    // Having recreated them, it then needs to add any old members (users, TODO what about groups?) into those newly created site groups.
    // It does this by writing the site members and their roles onto a property on the st:site node in the archive & reading them on restore.
    // Choose a site name that will link back to this test case...
    final String siteShortName = testName.getMethodName();
    log.debug("Creating test site called: " + siteShortName);
    // ...and create the site
    final TestSiteAndMemberInfo testSiteAndMemberInfo = perMethodTestSites.createTestSiteWithUserPerRole(siteShortName, "sitePreset", SiteVisibility.PUBLIC, AuthenticationUtil.getAdminUserName());
    // Now get the various site-related data that we want to examine after deletion & restoration
    final TestData testData = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<TestData>() {

        public TestData execute() throws Throwable {
            Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
            properties.put(ContentModel.PROP_NAME, "testcontent");
            properties.put(ContentModel.PROP_DESCRIPTION, "content - test doc for test");
            ChildAssociationRef testDoc = NODE_SERVICE.createNode(testSiteAndMemberInfo.doclib, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testcontent"), ContentModel.TYPE_CONTENT, properties);
            NodeRef testDocNodeRef = testDoc.getChildRef();
            // change all groups to have the permissions from a contributor
            PERMISSION_SERVICE.deletePermissions(testDocNodeRef);
            PERMISSION_SERVICE.setInheritParentPermissions(testDocNodeRef, false);
            assertTrue("Permissions should be cleared", PERMISSION_SERVICE.getAllSetPermissions(testDocNodeRef).isEmpty());
            Set<String> permissions = PERMISSION_SERVICE.getSettablePermissions(SiteModel.TYPE_SITE);
            for (String permission : permissions) {
                String siteRoleGroup = SITE_SERVICE.getSiteRoleGroup(siteShortName, permission);
                PERMISSION_SERVICE.setPermission(testDocNodeRef, siteRoleGroup, "SiteContributor", true);
            }
            final Map<String, String> userNameToRoleMap = new HashMap<String, String>();
            // Which users are members of which groups?
            for (String role : SITE_SERVICE.getSiteRoles()) {
                // putAll here shouldn't overwrite any keys in the above map as each authority should only have one role in a site.
                userNameToRoleMap.putAll(SITE_SERVICE.listMembers(siteShortName, null, role, 0, true));
            }
            // Some sanity checking before we delete the site
            final String siteContributorGroup = SITE_SERVICE.getSiteRoleGroup(siteShortName, "SiteContributor");
            assertTrue("Site contributor user was not in site contributors group", AUTHORITY_SERVICE.getContainedAuthorities(AuthorityType.USER, siteContributorGroup, true).contains(testSiteAndMemberInfo.siteContributor));
            assertEquals("Site contributor user did not have expected Contributor role", SiteModel.SITE_CONTRIBUTOR, userNameToRoleMap.get(testSiteAndMemberInfo.siteContributor));
            log.debug("About to delete site.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("Site deleted.");
            return new TestData(userNameToRoleMap, testDocNodeRef);
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            assertThatArchivedNodeExists(testSiteAndMemberInfo.siteInfo.getNodeRef(), "Site node not found in archive.");
            // At this point we might assert that the groups associated with the site were gone, but that's an implementation detail really.
            log.debug("About to restore site node from archive");
            final NodeRef archivedSiteNode = NODE_ARCHIVE_SERVICE.getArchivedNode(testSiteAndMemberInfo.siteInfo.getNodeRef());
            RestoreNodeReport report = NODE_ARCHIVE_SERVICE.restoreArchivedNode(archivedSiteNode);
            // ...which should work
            assertEquals("Failed to restore site from archive", RestoreStatus.SUCCESS, report.getStatus());
            log.debug("Successfully restored site from arhive.");
            return null;
        }
    });
    final Map<String, String> associatedGroups = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Map<String, String>>() {

        public Map<String, String> execute() throws Throwable {
            // The site itself should have been restored, of course...
            assertTrue("The site noderef was not restored as expected", NODE_SERVICE.exists(testSiteAndMemberInfo.siteInfo.getNodeRef()));
            // But the group authority nodes should be restored (recreated) as well.
            for (String role : SITE_SERVICE.getSiteRoles()) {
                final String siteGroup = SITE_SERVICE.getSiteRoleGroup(siteShortName, role);
                assertTrue("Site group for role " + role + " did not exist after site restoration", AUTHORITY_SERVICE.authorityExists(siteGroup));
            }
            log.debug(SITE_SERVICE.listMembers(siteShortName, null, null, 0, true).size() + " members...");
            for (Map.Entry<String, String> entry : SITE_SERVICE.listMembers(siteShortName, null, null, 0, true).entrySet()) {
                log.debug(entry);
            }
            // And finally, the original members of the site should have been given the same membership that they had before.
            for (Map.Entry<String, String> entry : testData.userNameToRoleMap.entrySet()) {
                assertEquals("Unexpected role for site user: " + entry.getKey(), entry.getValue(), SITE_SERVICE.getMembersRole(siteShortName, entry.getKey()));
            }
            // When the site is restored custom permissions on contents should be also restored
            Set<AccessPermission> accessPermissions = PERMISSION_SERVICE.getAllSetPermissions(testData.testDocNodeRef);
            Map<String, String> associatedGroups = new HashMap<String, String>();
            for (AccessPermission access : accessPermissions) {
                associatedGroups.put(access.getAuthority(), access.getPermission());
            }
            Set<String> permissions = PERMISSION_SERVICE.getSettablePermissions(SiteModel.TYPE_SITE);
            for (String permission : permissions) {
                String siteRoleGroup = SITE_SERVICE.getSiteRoleGroup(siteShortName, permission);
                assertTrue("all groups should have the permissions from a contributor on test content", "SiteContributor".equals(associatedGroups.get(siteRoleGroup)));
            }
            return associatedGroups;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("About to delete site completely.");
            SITE_SERVICE.deleteSite(siteShortName);
            for (String authority : associatedGroups.keySet()) {
                assertTrue("Associated groups should remain after site delete", AUTHORITY_SERVICE.authorityExists(authority));
            }
            return null;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("About to purge site from trashcan.");
            // get archive node reference
            String storePath = "archive://SpacesStore";
            StoreRef storeRef = new StoreRef(storePath);
            NodeRef archivedNodeRef = new NodeRef(storeRef, testSiteAndMemberInfo.siteInfo.getNodeRef().getId());
            NODE_ARCHIVE_SERVICE.purgeArchivedNode(archivedNodeRef);
            for (String authority : associatedGroups.keySet()) {
                assertTrue("Associated groups should be deleted on site purge", !AUTHORITY_SERVICE.authorityExists(authority));
            }
            return null;
        }
    });
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) Serializable(java.io.Serializable) Set(java.util.Set) QName(org.alfresco.service.namespace.QName) TestSiteAndMemberInfo(org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) RestoreNodeReport(org.alfresco.repo.node.archive.RestoreNodeReport) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Map(java.util.Map) HashMap(java.util.HashMap) PropertyMap(org.alfresco.util.PropertyMap) TemporarySitesTest(org.alfresco.util.test.junitrules.TemporarySitesTest) Test(org.junit.Test)

Example 4 with TestSiteAndMemberInfo

use of org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo in project alfresco-repository by Alfresco.

the class SiteServiceImplMoreTest method testSiteRolesPermissionsToDeleteWorkingCopy.

/**
 * MNT-16043: Site Owner and Site Manager can delete working copy, Site Collaborator cannot
 *
 * @throws Exception
 */
@Test
public void testSiteRolesPermissionsToDeleteWorkingCopy() throws Exception {
    final String userSiteOwner = "UserSiteOwner";
    final String userSiteManager = "UserSiteManager";
    final String userSiteCollaborator = "UserSiteCollaborator";
    final String userPrefix = "delete-working-copy-file";
    // create the users
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            createUser(userSiteOwner, userPrefix);
            createUser(userSiteManager, userPrefix);
            createUser(userSiteCollaborator, userPrefix);
            return null;
        }
    });
    final String siteShortName = userPrefix + "Site" + System.currentTimeMillis();
    final String dummyContent = "Just some old content that doesn't mean anything";
    // Create site
    final TestSiteAndMemberInfo testSiteAndMemberInfo = perMethodTestSites.createTestSiteWithUserPerRole(siteShortName, "sitePreset", SiteVisibility.PUBLIC, userSiteOwner);
    // create 1 file into the site as the owner of the site
    AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
    NodeRef siteContainer = SITE_SERVICE.createContainer(siteShortName, "doclib", ContentModel.TYPE_FOLDER, null);
    final FileInfo fileInfo1 = FILE_FOLDER_SERVICE.create(siteContainer, "fileInfo1.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer1 = FILE_FOLDER_SERVICE.getWriter(fileInfo1.getNodeRef());
    writer1.putContent(dummyContent);
    final FileInfo fileInfo2 = FILE_FOLDER_SERVICE.create(siteContainer, "fileInfo2.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer2 = FILE_FOLDER_SERVICE.getWriter(fileInfo2.getNodeRef());
    writer2.putContent(dummyContent);
    final FileInfo fileInfo3 = FILE_FOLDER_SERVICE.create(siteContainer, "fileInfo3.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer3 = FILE_FOLDER_SERVICE.getWriter(fileInfo2.getNodeRef());
    writer3.putContent(dummyContent);
    // Site COLLABORATOR - cannot delete working copy or original file
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // checkout file again as userOwner
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
            NodeRef workingCopy = COCI_SERVICE.checkout(fileInfo3.getNodeRef());
            assertNotNull(workingCopy);
            // make userSiteCollaborator a member of the site
            SITE_SERVICE.setMembership(siteShortName, userSiteCollaborator, SiteModel.SITE_COLLABORATOR);
            // make sure we are running as userSiteCollaborator
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteCollaborator);
            // try to delete working copy file
            try {
                NODE_SERVICE.deleteNode(workingCopy);
                fail("You do not have the appropriate permissions to perform this operation");
            } catch (AccessDeniedException ex) {
            // do nothing - is expected
            }
            // try to delete original checked-out
            try {
                NODE_SERVICE.deleteNode(fileInfo3.getNodeRef());
                fail("You do not have the appropriate permissions to perform this operation");
            } catch (AccessDeniedException ex) {
            // do nothing - is expected
            }
            return null;
        }
    });
    // Site Owner - can delete working copy (or original)
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // checkout a file as userOwner
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
            NodeRef workingCopy = COCI_SERVICE.checkout(fileInfo2.getNodeRef());
            assertNotNull(workingCopy);
            NODE_SERVICE.deleteNode(workingCopy);
            // checkout file again as userOwner
            workingCopy = COCI_SERVICE.checkout(fileInfo2.getNodeRef());
            assertNotNull(workingCopy);
            NODE_SERVICE.deleteNode(fileInfo2.getNodeRef());
            return null;
        }
    });
    // Site Manager - can delete working copy (or original)
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // checkout a file as userOwner
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
            NodeRef workingCopy = COCI_SERVICE.checkout(fileInfo1.getNodeRef());
            assertNotNull(workingCopy);
            // make userSiteManager a member of the site
            SITE_SERVICE.setMembership(siteShortName, userSiteManager, SiteModel.SITE_MANAGER);
            // make sure we are running as userSiteManager
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteManager);
            NODE_SERVICE.deleteNode(workingCopy);
            // checkout file again as userOwner
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
            workingCopy = COCI_SERVICE.checkout(fileInfo1.getNodeRef());
            assertNotNull(workingCopy);
            // make sure we are running as userSiteManager
            AUTHENTICATION_COMPONENT.setCurrentUser(userSiteManager);
            NODE_SERVICE.deleteNode(fileInfo1.getNodeRef());
            return null;
        }
    });
    AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
    // Delete the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            AUTHENTICATION_COMPONENT.getCurrentUserName();
            SITE_SERVICE.deleteSite(siteShortName);
            return null;
        }
    });
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) FileInfo(org.alfresco.service.cmr.model.FileInfo) TestSiteAndMemberInfo(org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo) TemporarySitesTest(org.alfresco.util.test.junitrules.TemporarySitesTest) Test(org.junit.Test)

Example 5 with TestSiteAndMemberInfo

use of org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo in project alfresco-repository by Alfresco.

the class SiteServiceImplMoreTest method deleteSiteRestoreSiteWithLocks.

/**
 * REPO-1688: Restore Site should disable lockable behaviour (to be
 * symmetric with delete/purge). See also MNT-17093 for background on this.
 *
 * @throws Exception
 */
@Test
public void deleteSiteRestoreSiteWithLocks() throws Exception {
    final String userSiteOwner = "UserSiteOwner";
    final String userSiteManager = "UserSiteManager";
    final String userSiteCollaborator = "UserSiteCollaborator";
    final String userPrefix = "restore-with-lock";
    // create the users
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            createUser(userSiteOwner, userPrefix);
            createUser(userSiteManager, userPrefix);
            createUser(userSiteCollaborator, userPrefix);
            return null;
        }
    });
    final String siteShortName = "testsite-" + System.currentTimeMillis();
    final SiteServiceImpl siteServiceImpl = (SiteServiceImpl) SITE_SERVICE;
    log.debug("Creating test site called: " + siteShortName);
    // Create site
    final TestSiteAndMemberInfo testSiteAndMemberInfo = perMethodTestSites.createTestSiteWithUserPerRole(siteShortName, "sitePreset", SiteVisibility.PUBLIC, userSiteOwner);
    // Add Site COLLABORATOR - create file
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            // make userSiteCollaborator a member of the site
            SITE_SERVICE.setMembership(siteShortName, userSiteCollaborator, SiteModel.SITE_COLLABORATOR);
            return null;
        }
    });
    // create document as userSiteCollaborator
    AUTHENTICATION_COMPONENT.setCurrentUser(userSiteCollaborator);
    String fileFolderPrefix = "TESTLOCK_";
    String componentId = "doclib";
    NodeRef siteContainer = SITE_SERVICE.createContainer(siteShortName, componentId, ContentModel.TYPE_FOLDER, null);
    final FileInfo fileInfo = FILE_FOLDER_SERVICE.create(siteContainer, fileFolderPrefix + "file.txt", ContentModel.TYPE_CONTENT);
    ContentWriter writer = FILE_FOLDER_SERVICE.getWriter(fileInfo.getNodeRef());
    writer.putContent("Just some old content that doesn't mean anything");
    // Make sure there are no locks on the fileInfo yet
    assertEquals(LockStatus.NO_LOCK, LOCK_SERVICE.getLockStatus(fileInfo.getNodeRef()));
    // Lock a file as userSiteCollaborator
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            LOCK_SERVICE.lock(fileInfo.getNodeRef(), LockType.READ_ONLY_LOCK);
            return null;
        }
    });
    // Make sure we have a lock now
    assertEquals(LockStatus.LOCK_OWNER, LOCK_SERVICE.getLockStatus(fileInfo.getNodeRef()));
    AUTHENTICATION_COMPONENT.setCurrentUser(userSiteOwner);
    // Delete site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Map<String, String>>() {

        public Map<String, String> execute() throws Throwable {
            log.debug("About to delete site.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("Site deleted.");
            return null;
        }
    });
    // Add custom behaviour that triggers onCreateNode to cause update ... and hence triggers REPO-1688
    PolicyComponent policyComponent = (PolicyComponent) APP_CONTEXT_INIT.getApplicationContext().getBean("policyComponent");
    policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateNodePolicy.QNAME, ContentModel.TYPE_CONTENT, new JavaBehaviour(this, "onCreateNodeSetTitle"));
    // restore the site
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            assertThatArchivedNodeExists(testSiteAndMemberInfo.siteInfo.getNodeRef(), "Site node not found in archive.");
            log.debug("About to restore site node from archive");
            final NodeRef archivedSiteNode = NODE_ARCHIVE_SERVICE.getArchivedNode(testSiteAndMemberInfo.siteInfo.getNodeRef());
            RestoreNodeReport report = NODE_ARCHIVE_SERVICE.restoreArchivedNode(archivedSiteNode);
            // ...which should work
            assertEquals("Failed to restore site from archive", RestoreStatus.SUCCESS, report.getStatus());
            log.debug("Successfully restored site from arhive.");
            return null;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Map<String, String>>() {

        public Map<String, String> execute() throws Throwable {
            // The site itself should have been restored, of course...
            assertTrue("The site noderef was not restored as expected", NODE_SERVICE.exists(testSiteAndMemberInfo.siteInfo.getNodeRef()));
            return null;
        }
    });
    // remove site completely
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>() {

        public Void execute() throws Throwable {
            log.debug("About to delete site completely.");
            SITE_SERVICE.deleteSite(siteShortName);
            log.debug("About to purge site from trashcan.");
            // get archive node reference
            String storePath = "archive://SpacesStore";
            StoreRef storeRef = new StoreRef(storePath);
            NodeRef archivedNodeRef = new NodeRef(storeRef, testSiteAndMemberInfo.siteInfo.getNodeRef().getId());
            NODE_ARCHIVE_SERVICE.purgeArchivedNode(archivedNodeRef);
            return null;
        }
    });
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) PolicyComponent(org.alfresco.repo.policy.PolicyComponent) TestSiteAndMemberInfo(org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo) RestoreNodeReport(org.alfresco.repo.node.archive.RestoreNodeReport) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) FileInfo(org.alfresco.service.cmr.model.FileInfo) JavaBehaviour(org.alfresco.repo.policy.JavaBehaviour) Map(java.util.Map) HashMap(java.util.HashMap) PropertyMap(org.alfresco.util.PropertyMap) TemporarySitesTest(org.alfresco.util.test.junitrules.TemporarySitesTest) Test(org.junit.Test)

Aggregations

NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 TestSiteAndMemberInfo (org.alfresco.util.test.junitrules.TemporarySites.TestSiteAndMemberInfo)5 TemporarySitesTest (org.alfresco.util.test.junitrules.TemporarySitesTest)5 Test (org.junit.Test)5 RestoreNodeReport (org.alfresco.repo.node.archive.RestoreNodeReport)4 StoreRef (org.alfresco.service.cmr.repository.StoreRef)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FileInfo (org.alfresco.service.cmr.model.FileInfo)3 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)3 PropertyMap (org.alfresco.util.PropertyMap)3 Serializable (java.io.Serializable)2 Set (java.util.Set)2 JavaBehaviour (org.alfresco.repo.policy.JavaBehaviour)1 PolicyComponent (org.alfresco.repo.policy.PolicyComponent)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)1 AccessPermission (org.alfresco.service.cmr.security.AccessPermission)1 QName (org.alfresco.service.namespace.QName)1