SHA-1892 : Error appears when Consumer or Contributor is trying to create a link on a document

- After creating the link for a node, add the marker aspect (LINKED) to the original node as System user. 
   - Added test for case 
   - Added a new test for DocumentLinkServiceImplTest for updated fucntionality

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@132612 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ramona Neamtu
2016-11-10 09:39:15 +00:00
parent da4cb3dc21
commit dbe2c44536
2 changed files with 111 additions and 38 deletions

View File

@@ -39,6 +39,7 @@ import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.search.QueryParameterDefImpl;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
@@ -60,8 +61,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.surf.util.I18NUtil;
/**
* Implementation of the document link service
* In addition to the document link service, this class also provides a BeforeDeleteNodePolicy
* Implementation of the document link service In addition to the document link
* service, this class also provides a BeforeDeleteNodePolicy
*
* @author Ana Bozianu
* @since 5.1
@@ -124,9 +125,9 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
}
@Override
public NodeRef createDocumentLink(NodeRef source, NodeRef destination)
public NodeRef createDocumentLink(final NodeRef source, NodeRef destination)
{
if(logger.isDebugEnabled())
if (logger.isDebugEnabled())
{
logger.debug("Creating document link. source: " + source + ", destination: " + destination);
}
@@ -172,9 +173,9 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
ChildAssociationRef childRef = null;
QName sourceType = nodeService.getType(source);
if( checkOutCheckInService.isWorkingCopy(source) || nodeService.hasAspect(source, ContentModel.ASPECT_LOCKABLE))
if (checkOutCheckInService.isWorkingCopy(source) || nodeService.hasAspect(source, ContentModel.ASPECT_LOCKABLE))
{
throw new IllegalArgumentException( "Cannot perform operation since the node (id:" + source.getId() + ") is locked.");
throw new IllegalArgumentException("Cannot perform operation since the node (id:" + source.getId() + ") is locked.");
}
if (dictionaryService.isSubClass(sourceType, ContentModel.TYPE_CONTENT))
@@ -183,7 +184,8 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
childRef = nodeService.createNode(destination, ContentModel.ASSOC_CONTAINS, assocQName, ApplicationModel.TYPE_FILELINK, props);
}
else if (!dictionaryService.isSubClass(sourceType, SiteModel.TYPE_SITE) && dictionaryService.isSubClass(nodeService.getType(source), ContentModel.TYPE_FOLDER))
else if (!dictionaryService.isSubClass(sourceType, SiteModel.TYPE_SITE)
&& dictionaryService.isSubClass(nodeService.getType(source), ContentModel.TYPE_FOLDER))
{
// create Folder link node
childRef = nodeService.createNode(destination, ContentModel.ASSOC_CONTAINS, assocQName, ApplicationModel.TYPE_FOLDERLINK, props);
@@ -193,7 +195,11 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
throw new IllegalArgumentException("unsupported source node type : " + nodeService.getType(source));
}
//add linked aspect to the sourceNode
// add linked aspect to the sourceNode - run as System
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Object>()
{
public Void doWork() throws Exception
{
behaviourFilter.disableBehaviour(source, ContentModel.ASPECT_AUDITABLE);
try
{
@@ -204,6 +210,10 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
behaviourFilter.enableBehaviour(source, ContentModel.ASPECT_AUDITABLE);
}
return null;
}
}, AuthenticationUtil.getSystemUserName());
return childRef.getChildRef();
}
@@ -260,12 +270,12 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
List<NodeRef> nodeLinks = new ArrayList<NodeRef>();
List<NodeRef> nodeRefs;
/* Search for links in all stores */
for(StoreRef store : nodeService.getStores())
for (StoreRef store : nodeService.getStores())
{
/* Get the root node */
NodeRef rootNodeRef = nodeService.getRootNode(store);
/* Execute the query, retrieve links to the document*/
/* Execute the query, retrieve links to the document */
nodeRefs = searchService.selectNodes(rootNodeRef, XPATH_QUERY_LINK_DEST_MATCH, params, namespaceService, true);
nodeLinks.addAll(nodeRefs);
}
@@ -337,18 +347,20 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
public void beforeDeleteLinkNode(NodeRef linkNodeRef)
{
// NodeRef linkNodeRef = childAssocRef.getChildRef();
NodeRef nodeRef = getLinkDestination(linkNodeRef);
final NodeRef nodeRef = getLinkDestination(linkNodeRef);
List<NodeRef> nodeRefLinks = getNodeLinks(nodeRef);
if (nodeRefLinks.size() == 1 && nodeRefLinks.contains(linkNodeRef))
{
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Object>()
{
public Void doWork() throws Exception
{
behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_LOCKABLE);
try
{
// remove linked aspect to the sourceNode
nodeService.removeAspect(nodeRef, ApplicationModel.ASPECT_LINKED);
}
finally
@@ -356,6 +368,10 @@ public class DocumentLinkServiceImpl implements DocumentLinkService, NodeService
behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_LOCKABLE);
}
return null;
}
}, AuthenticationUtil.getSystemUserName());
}
}

View File

@@ -39,6 +39,7 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.DeleteLinksStatusReport;
import org.alfresco.service.cmr.repository.DocumentLinkService;
@@ -79,8 +80,10 @@ public class DocumentLinkServiceImplTest extends TestCase
private SiteService siteService;
private FileFolderService fileFolderService;
private NodeService nodeService;
private CheckOutCheckInService cociService;
// nodes the test user has read/write permission to
private NodeRef site1;
private NodeRef site1File1;
private NodeRef site1File2; // do not create links of this file
private NodeRef site1Folder1;
@@ -108,6 +111,7 @@ public class DocumentLinkServiceImplTest extends TestCase
siteService = serviceRegistry.getSiteService();
fileFolderService = serviceRegistry.getFileFolderService();
nodeService = serviceRegistry.getNodeService();
cociService = serviceRegistry.getCheckOutCheckInService();
// Start the transaction
txn = transactionService.getUserTransaction();
@@ -125,7 +129,7 @@ public class DocumentLinkServiceImplTest extends TestCase
* Create the working test root 1 to which the user has read/write
* permission
*/
NodeRef site1 = siteService.createSite("site1", GUID.generate(), "myTitle", "myDescription", SiteVisibility.PUBLIC).getNodeRef();
site1 = siteService.createSite("site1", GUID.generate(), "myTitle", "myDescription", SiteVisibility.PUBLIC).getNodeRef();
permissionService.setPermission(site1, TEST_USER, PermissionService.ALL_PERMISSIONS, true);
site1Folder1 = fileFolderService.create(site1, site1Folder1Name, ContentModel.TYPE_FOLDER).getNodeRef();
site1File1 = fileFolderService.create(site1Folder1, site1File1Name, ContentModel.TYPE_CONTENT).getNodeRef();
@@ -336,4 +340,57 @@ public class DocumentLinkServiceImplTest extends TestCase
assertEquals(ex.getClass(), AccessDeniedException.class);
}
/**
* Tests the creation of a Site link, an locked node or a checked out node
*
* @throws Exception
*/
public void testCreateLinksNotAllowed() throws Exception
{
NodeRef invalidLinkNodeRef;
// Create link for Site
try
{
invalidLinkNodeRef = documentLinkService.createDocumentLink(site1, site2Folder1);
fail("unsupported source node type : " + nodeService.getType(site1));
}
catch (IllegalArgumentException e)
{
// Expected
}
NodeRef firstLinkNodeRef = documentLinkService.createDocumentLink(site1File1, site1Folder1);
assertEquals(true, nodeService.hasAspect(site1File1, ApplicationModel.ASPECT_LINKED));
// Create link for working copy
NodeRef workingCopyNodeRef = cociService.checkout(site1File1);
try
{
invalidLinkNodeRef = documentLinkService.createDocumentLink(workingCopyNodeRef, site1Folder1);
fail("Cannot perform operation since the node (id:" + workingCopyNodeRef.getId() + ") is locked.");
}
catch (IllegalArgumentException e)
{
// Expected
}
// Create link for locked node (original)
try
{
invalidLinkNodeRef = documentLinkService.createDocumentLink(site1File1, site1Folder1);
fail("Cannot perform operation since the node (id:" + site1File1.getId() + ") is locked.");
}
catch (IllegalArgumentException e)
{
// Expected
}
// Even the node is locked, user can delete previous created links
nodeService.deleteNode(firstLinkNodeRef);
assertEquals(false, nodeService.hasAspect(site1File1, ApplicationModel.ASPECT_LINKED));
cociService.cancelCheckout(workingCopyNodeRef);
}
}