ALF-11318: Add policy to prevent copying of rateable aspect and associated aspects.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@37313 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alex Miller
2012-05-31 14:53:01 +00:00
parent ebb756825e
commit b3f62c505c
3 changed files with 178 additions and 1 deletions

View File

@@ -107,6 +107,12 @@
</property>
</bean>
<!-- Behaviours and policies for Ratings -->
<bean id="ratingAspect" class="org.alfresco.repo.rating.RateableAspect" init-method="init">
<property name="policyComponent" ref="policyComponent"/>
<property name="ratingNamingConventions" ref="rollupNamingConventions"/>
<property name="ratingSchemeRegistry" ref="ratingSchemeRegistry"/>
</bean>
<!-- JavaScript API support -->
<bean id="ratingServiceScript" parent="baseJavaScriptExtension"

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rating;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.copy.CopyBehaviourCallback;
import org.alfresco.repo.copy.CopyDetails;
import org.alfresco.repo.copy.CopyServicePolicies;
import org.alfresco.repo.copy.DefaultCopyBehaviourCallback;
import org.alfresco.repo.copy.DoNothingCopyBehaviourCallback;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.service.cmr.rating.RatingScheme;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Rateable aspect behaviour bean. When any node with the rateable aspect is
* copied, then ensure the ratings and roll ups are not copied.
*
* @author Alex Miller
*/
public class RateableAspect implements CopyServicePolicies.OnCopyNodePolicy
{
/** logger */
private static final Log logger = LogFactory.getLog(RateableAspect.class);
/** Services */
private PolicyComponent policyComponent;
private RatingNamingConventionsUtil ratingNamingConventions;
private RatingSchemeRegistry ratingSchemeRegistry;
/**
* Set the policy component
*
* @param policyComponent policy component
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* Set the rating scheme registry
*
* @param ratingSchemeRegistry The rating scheme registry
*/
public void setRatingSchemeRegistry(RatingSchemeRegistry ratingSchemeRegistry)
{
this.ratingSchemeRegistry = ratingSchemeRegistry;
}
/**
* Set the rating naming conventions service.
*
* @param
*/
public void setRatingNamingConventions(RatingNamingConventionsUtil ratingNamingConventions)
{
this.ratingNamingConventions = ratingNamingConventions;
}
/**
* Initialise method
*/
public void init()
{
// Prevent the ratebale aspect from being copied
bindNoCopyBehaviour(ContentModel.ASPECT_RATEABLE);
// Prevent the roll up aspects from being copied
for (RatingScheme ratingScheme : ratingSchemeRegistry.getRatingSchemes().values())
{
if (ratingScheme.getPropertyRollups() != null && ratingScheme.getPropertyRollups().size() > 0)
{
QName rollupAspectName = ratingNamingConventions.getRollupAspectNameFor(ratingScheme);
bindNoCopyBehaviour(rollupAspectName);
}
}
}
private void bindNoCopyBehaviour(QName rollupAspectName)
{
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "getCopyCallback"),
rollupAspectName, new JavaBehaviour(this, "getCopyCallback"));
}
/**
* @return Returns {@link RateableAspectCopyBehaviourCallback}
*/
public CopyBehaviourCallback getCopyCallback(QName classRef, CopyDetails copyDetails)
{
return DoNothingCopyBehaviourCallback.getInstance();
}
}

View File

@@ -41,17 +41,19 @@ import org.alfresco.service.cmr.rating.RatingScheme;
import org.alfresco.service.cmr.rating.RatingService;
import org.alfresco.service.cmr.rating.RatingServiceException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.test.junitrules.AlfrescoPerson;
import org.alfresco.util.test.junitrules.ApplicationContextInit;
import org.alfresco.util.test.junitrules.RunAsFullyAuthenticatedRule;
import org.alfresco.util.test.junitrules.TemporaryNodes;
import org.alfresco.util.test.junitrules.RunAsFullyAuthenticatedRule.RunAsUser;
import org.alfresco.util.test.junitrules.TemporaryNodes;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
@@ -89,6 +91,7 @@ public class RatingServiceIntegrationTest
@Rule public RunAsFullyAuthenticatedRule runAsRule = new RunAsFullyAuthenticatedRule(TEST_USER1);
// Various services
private static CopyService COPY_SERVICE;
private static NodeService NODE_SERVICE;
private static RatingService RATING_SERVICE;
private static RetryingTransactionHelper TRANSACTION_HELPER;
@@ -98,8 +101,10 @@ public class RatingServiceIntegrationTest
private static NodeRef COMPANY_HOME;
// These NodeRefs are used by the test methods.
private static NodeRef COPY_DEST_FOLDER;
private static NodeRef TEST_FOLDER;
private NodeRef testDoc_Admin;
private NodeRef testDoc_Copy;
private NodeRef testDoc_UserOne;
private NodeRef testDoc_UserTwo;
@@ -109,6 +114,7 @@ public class RatingServiceIntegrationTest
@BeforeClass public static void initStaticData() throws Exception
{
COPY_SERVICE = (CopyService) APP_CONTEXT_INIT.getApplicationContext().getBean("copyService");
NODE_SERVICE = (NodeService) APP_CONTEXT_INIT.getApplicationContext().getBean("nodeService");
RATING_NAMING_CONVENTIONS = (RatingNamingConventionsUtil) APP_CONTEXT_INIT.getApplicationContext().getBean("rollupNamingConventions");
RATING_SERVICE = (RatingService) APP_CONTEXT_INIT.getApplicationContext().getBean("ratingService");
@@ -120,6 +126,7 @@ public class RatingServiceIntegrationTest
// Create some static test content
TEST_FOLDER = STATIC_TEST_NODES.createNode(COMPANY_HOME, "testFolder", ContentModel.TYPE_FOLDER, AuthenticationUtil.getAdminUserName());
COPY_DEST_FOLDER = STATIC_TEST_NODES.createNode(COMPANY_HOME, "testCopyDestinationFolder", ContentModel.TYPE_FOLDER, AuthenticationUtil.getAdminUserName());
}
@@ -432,4 +439,52 @@ public class RatingServiceIntegrationTest
}
});
}
@Test public void copyNodeNotRatings()
{
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
// Apply ratings to node
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
RATING_SERVICE.applyRating(testDoc_UserTwo, 2.0f, FIVE_STAR_SCHEME_NAME);
// A new score in a different rating scheme by the same user should not fail.
RATING_SERVICE.applyRating(testDoc_UserTwo, 1.0f, LIKES_SCHEME_NAME);
// There should be two rating child nodes under the rated node.
assertEquals("Wrong number of child nodes", 2 , NODE_SERVICE.getChildAssocs(testDoc_UserTwo).size());
List<Rating> ratings = RATING_SERVICE.getRatingsByCurrentUser(testDoc_UserTwo);
assertEquals(2, ratings.size());
assertEquals(FIVE_STAR_SCHEME_NAME, ratings.get(0).getScheme().getName());
assertEquals(LIKES_SCHEME_NAME, ratings.get(1).getScheme().getName());
// Copy the node
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
final QName childName = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "userTwosDoc");;
final NodeRef testDocCopy = COPY_SERVICE.copy(testDoc_UserTwo, COPY_DEST_FOLDER, ContentModel.ASSOC_CONTAINS, childName, true);
testNodes.addNodeRef(testDocCopy);
// Check that the user ratings aren't copied
AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername());
List<Rating> copiedRatings = RATING_SERVICE.getRatingsByCurrentUser(testDocCopy);
assertEquals(0, copiedRatings.size());
// Check that the roll ups aren't copied
final int likesRatingCount = RATING_SERVICE.getRatingsCount(testDocCopy, LIKES_SCHEME_NAME);
final int fiveStarRatingCount = RATING_SERVICE.getRatingsCount(testDocCopy, FIVE_STAR_SCHEME_NAME);
assertEquals(0, fiveStarRatingCount);
assertEquals(0, likesRatingCount);
return null;
}
});
}
}