From a5d245b5f6063b9d1abd5df20810511e012fbdbd Mon Sep 17 00:00:00 2001 From: cagache Date: Mon, 19 Aug 2019 15:02:40 +0300 Subject: [PATCH 1/3] Throw PermissionDeniedException instead of AccessDeniedException and internationalized the messages. --- .../messages/hold-service.properties | 6 ++- .../model/rma/aspect/FrozenAspect.java | 12 ++--- .../hold/UpdateHeldActiveContentTest.java | 53 ++++--------------- 3 files changed, 22 insertions(+), 49 deletions(-) diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/messages/hold-service.properties b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/messages/hold-service.properties index ae21a7ddb8..6e3cc58d4f 100644 --- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/messages/hold-service.properties +++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/messages/hold-service.properties @@ -1,4 +1,8 @@ rm.hold.not-hold=The node {0} is not a hold. rm.hold.add-to-hold-invalid-type={0} is neither a record nor a record folder nor active content. Only records, record \ folders or active content can be added to a hold. -rm.hold.add-to-hold-archived-node=Archived nodes can't be added to hold. \ No newline at end of file +rm.hold.add-to-hold-archived-node=Archived nodes can't be added to hold. +rm.hold.delete-frozen-node=Frozen nodes can not be deleted. +rm.hold.delete-node-frozen-children=Can not delete node, because it contains a frozen child node. +rm.hold.move-frozen-node=Frozen nodes can not be moved. +rm.hold.update-frozen-node=Frozen nodes can not be updated. \ No newline at end of file diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspect.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspect.java index c6a5a35853..18cbd5f544 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspect.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspect.java @@ -45,10 +45,11 @@ import org.alfresco.repo.policy.annotation.BehaviourBean; import org.alfresco.repo.policy.annotation.BehaviourKind; 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.rest.framework.core.exceptions.PermissionDeniedException; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.namespace.QName; +import org.springframework.extensions.surf.util.I18NUtil; /** * rma:frozen behaviour bean @@ -95,7 +96,7 @@ public class FrozenAspect extends BaseBehaviourBean if (nodeService.exists(nodeRef) && freezeService.isFrozen(nodeRef)) { // never allow to delete a frozen node - throw new AccessDeniedException("Frozen nodes can not be deleted."); + throw new PermissionDeniedException(I18NUtil.getMessage("rm.hold.delete-frozen-node")); } // check children @@ -121,7 +122,7 @@ public class FrozenAspect extends BaseBehaviourBean if (freezeService.isFrozen(nodeRef)) { // never allow to delete a node with a frozen child - throw new AccessDeniedException("Can not delete node, because it contains a frozen child node."); + throw new PermissionDeniedException(I18NUtil.getMessage("rm.hold.delete-node-frozen-children")); } // check children @@ -214,7 +215,7 @@ public class FrozenAspect extends BaseBehaviourBean if (nodeService.exists(oldChildAssocRef.getChildRef()) && freezeService.isFrozen(oldChildAssocRef.getChildRef())) { - throw new AccessDeniedException("Frozen nodes can not be moved."); + throw new PermissionDeniedException(I18NUtil.getMessage("rm.hold.move-frozen-node")); } return null; }); @@ -232,14 +233,13 @@ public class FrozenAspect extends BaseBehaviourBean notificationFrequency = NotificationFrequency.FIRST_EVENT ) public void onUpdateProperties(NodeRef nodeRef, Map before, Map after) - { AuthenticationUtil.runAsSystem((RunAsWork) () -> { // check to not throw exception when the aspect is being added if (nodeService.exists(nodeRef) && freezeService.isFrozen(nodeRef) && !transactionalResourceHelper.getSet("frozen").contains(nodeRef) ) { - throw new AccessDeniedException("Frozen nodes can not be updated."); + throw new PermissionDeniedException(I18NUtil.getMessage("rm.hold.update-frozen-node")); } return null; }); diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java index b981a84c5d..a5e8ac9cd0 100644 --- a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java @@ -30,6 +30,7 @@ import org.alfresco.model.ContentModel; import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.security.permissions.AccessDeniedException; +import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException; import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.repository.ContentData; import org.alfresco.service.cmr.repository.NodeRef; @@ -56,7 +57,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testDeleteHeldDocument() { - doBehaviourDrivenTest(new BehaviourDrivenTest() + doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) { public void given() { @@ -69,15 +70,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - try - { - fileFolderService.delete(dmDocument); - fail("Expected AccessDeniedException to be thrown"); - } - catch (AccessDeniedException ade) - { - assertTrue(ade.getMessage().contains("Frozen nodes can not be deleted.")); - } + fileFolderService.delete(dmDocument); } }); } @@ -114,7 +107,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testMoveHeldDocument() { - doBehaviourDrivenTest(new BehaviourDrivenTest() + doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) { public void given() { @@ -127,15 +120,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() throws FileNotFoundException { - try - { - fileFolderService.move(dmDocument, dmFolder1, null); - fail("Expected AccessDeniedException to be thrown"); - } - catch (AccessDeniedException ade) - { - assertTrue(ade.getMessage().contains("Frozen nodes can not be moved.")); - } + fileFolderService.move(dmDocument, dmFolder1, null); } }); } @@ -148,7 +133,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testUpdateHeldDocumentProperties() { - doBehaviourDrivenTest(new BehaviourDrivenTest() + doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) { public void given() { @@ -161,15 +146,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - try - { - nodeService.setProperty(dmDocument, ContentModel.PROP_DESCRIPTION, "description"); - fail("Expected AccessDeniedException to be thrown"); - } - catch (AccessDeniedException ade) - { - assertTrue(ade.getMessage().contains("Frozen nodes can not be updated.")); - } + nodeService.setProperty(dmDocument, ContentModel.PROP_DESCRIPTION, "description"); } }); } @@ -181,7 +158,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testUpdateHeldDocumentContent() { - doBehaviourDrivenTest(new BehaviourDrivenTest() + doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) { public void given() { @@ -194,17 +171,9 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - try - { - ContentData content = (ContentData) nodeService.getProperty(dmDocument, PROP_CONTENT); - nodeService.setProperty(dmDocument, PROP_CONTENT, ContentData.setMimetype(content, - MimetypeMap.MIMETYPE_TEXT_PLAIN)); - fail("Expected AccessDeniedException to be thrown"); - } - catch (AccessDeniedException ade) - { - assertTrue(ade.getMessage().contains("Frozen nodes can not be updated.")); - } + ContentData content = (ContentData) nodeService.getProperty(dmDocument, PROP_CONTENT); + nodeService.setProperty(dmDocument, PROP_CONTENT, ContentData.setMimetype(content, + MimetypeMap.MIMETYPE_TEXT_PLAIN)); } }); } From b769ce2892d04fc2759d109d39a6fb5104807255 Mon Sep 17 00:00:00 2001 From: cagache Date: Mon, 19 Aug 2019 17:12:58 +0300 Subject: [PATCH 2/3] Check exception messages --- .../hold/UpdateHeldActiveContentTest.java | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java index a5e8ac9cd0..a50c65a3b8 100644 --- a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java +++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/integration/hold/UpdateHeldActiveContentTest.java @@ -57,7 +57,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testDeleteHeldDocument() { - doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) + doBehaviourDrivenTest(new BehaviourDrivenTest() { public void given() { @@ -70,7 +70,15 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - fileFolderService.delete(dmDocument); + try + { + fileFolderService.delete(dmDocument); + fail("Expected PermissionDeniedException to be thrown"); + } + catch (PermissionDeniedException pde) + { + assertTrue(pde.getMessage().contains("Frozen nodes can not be deleted.")); + } } }); } @@ -107,7 +115,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testMoveHeldDocument() { - doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) + doBehaviourDrivenTest(new BehaviourDrivenTest() { public void given() { @@ -120,7 +128,15 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() throws FileNotFoundException { - fileFolderService.move(dmDocument, dmFolder1, null); + try + { + fileFolderService.move(dmDocument, dmFolder1, null); + fail("Expected PermissionDeniedException to be thrown"); + } + catch (PermissionDeniedException pde) + { + assertTrue(pde.getMessage().contains("Frozen nodes can not be moved.")); + } } }); } @@ -133,7 +149,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testUpdateHeldDocumentProperties() { - doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) + doBehaviourDrivenTest(new BehaviourDrivenTest() { public void given() { @@ -146,7 +162,15 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - nodeService.setProperty(dmDocument, ContentModel.PROP_DESCRIPTION, "description"); + try + { + nodeService.setProperty(dmDocument, ContentModel.PROP_DESCRIPTION, "description"); + fail("Expected PermissionDeniedException to be thrown"); + } + catch (PermissionDeniedException pde) + { + assertTrue(pde.getMessage().contains("Frozen nodes can not be updated.")); + } } }); } @@ -158,7 +182,7 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase */ public void testUpdateHeldDocumentContent() { - doBehaviourDrivenTest(new BehaviourDrivenTest(PermissionDeniedException.class) + doBehaviourDrivenTest(new BehaviourDrivenTest() { public void given() { @@ -171,9 +195,17 @@ public class UpdateHeldActiveContentTest extends BaseRMTestCase public void when() { - ContentData content = (ContentData) nodeService.getProperty(dmDocument, PROP_CONTENT); - nodeService.setProperty(dmDocument, PROP_CONTENT, ContentData.setMimetype(content, - MimetypeMap.MIMETYPE_TEXT_PLAIN)); + try + { + ContentData content = (ContentData) nodeService.getProperty(dmDocument, PROP_CONTENT); + nodeService.setProperty(dmDocument, PROP_CONTENT, ContentData.setMimetype(content, + MimetypeMap.MIMETYPE_TEXT_PLAIN)); + fail("Expected PermissionDeniedException to be thrown"); + } + catch (PermissionDeniedException pde) + { + assertTrue(pde.getMessage().contains("Frozen nodes can not be updated.")); + } } }); } From ac4b8d4b9a10fba440ee75de5a8125a41b7408df Mon Sep 17 00:00:00 2001 From: Ross Gale Date: Mon, 19 Aug 2019 15:56:10 +0100 Subject: [PATCH 3/3] RM-6873 adding changes from update branch and updating tests --- .../rma/aspect/FrozenAspectUnitTest.java | 41 +++++-------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspectUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspectUnitTest.java index e94f2ad103..8a323d794b 100644 --- a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspectUnitTest.java +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/model/rma/aspect/FrozenAspectUnitTest.java @@ -43,6 +43,7 @@ import org.alfresco.model.ContentModel; import org.alfresco.module.org_alfresco_module_rm.freeze.FreezeService; import org.alfresco.module.org_alfresco_module_rm.util.TransactionalResourceHelper; import org.alfresco.repo.security.permissions.AccessDeniedException; +import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.NodeRef; @@ -165,7 +166,7 @@ public class FrozenAspectUnitTest /** * Test before delete throws an error if a node is frozen */ - @Test(expected = AccessDeniedException.class) + @Test(expected = PermissionDeniedException.class) public void testBeforeDeleteNodeThrowsExceptionIfNodeFrozen() { when(mockFreezeService.isFrozen(content)).thenReturn(true); @@ -186,7 +187,7 @@ public class FrozenAspectUnitTest /** * Test before delete throws an error for a node with frozen children */ - @Test (expected = AccessDeniedException.class) + @Test (expected = PermissionDeniedException.class) public void testBeforeDeleteThrowsExceptionForFrozenChild() { when(mockChildRef.getChildRef()).thenReturn(child); @@ -225,22 +226,22 @@ public class FrozenAspectUnitTest } /** - * Test on move throws an error for a frozen node + * Test before move throws an error for a frozen node */ - @Test(expected = AccessDeniedException.class) - public void testOnMoveThrowsExceptionForFrozenNode() + @Test(expected = PermissionDeniedException.class) + public void testBeforeMoveThrowsExceptionForFrozenNode() { - when(mockNewRef.getParentRef()).thenReturn(parent); - when(mockNewRef.getChildRef()).thenReturn(child); - when(mockNodeService.exists(parent)).thenReturn(true); + when(mockOldRef.getParentRef()).thenReturn(parent); + when(mockOldRef.getChildRef()).thenReturn(child); when(mockNodeService.exists(child)).thenReturn(true); - frozenAspect.onMoveNode(mockOldRef, mockNewRef); + when(mockFreezeService.isFrozen(child)).thenReturn(true); + frozenAspect.beforeMoveNode(mockOldRef, null); } /** * Test update properties throws an error for frozen nodes */ - @Test(expected = AccessDeniedException.class) + @Test(expected = PermissionDeniedException.class) public void testUpdatePropertiesThrowsExceptionForFrozenNode() { when(mockFreezeService.isFrozen(content)).thenReturn(true); @@ -248,24 +249,4 @@ public class FrozenAspectUnitTest when(mockSet.contains("frozen")).thenReturn(false); frozenAspect.onUpdateProperties(content, null, null); } - - /** - * Test on content update throws an error for frozen nodes - */ - @Test(expected = AccessDeniedException.class) - public void testOnContentUpdateThrowsExceptionForFrozenNode() - { - when(mockFreezeService.isFrozen(content)).thenReturn(true); - frozenAspect.onContentUpdate(content, false); - } - - /** - * Test before copy throws an error for frozen node - */ - @Test(expected = AccessDeniedException.class) - public void testBeforeCopyThrowsExceptionForFrozenNode() - { - when(mockFreezeService.isFrozen(content)).thenReturn(true); - frozenAspect.beforeCopy(null, content, null); - } }