mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.ownable.impl;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
|
||||
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||
import org.alfresco.service.cmr.security.OwnableService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
public class OwnableServiceImpl implements OwnableService, InitializingBean
|
||||
{
|
||||
private NodeService nodeService;
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
|
||||
public OwnableServiceImpl()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
// IOC
|
||||
|
||||
public void setNodeService(NodeService nodeService)
|
||||
{
|
||||
this.nodeService = nodeService;
|
||||
}
|
||||
|
||||
public void setAuthenticationService(AuthenticationService authenticationService)
|
||||
{
|
||||
this.authenticationService = authenticationService;
|
||||
}
|
||||
|
||||
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
if(nodeService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("A node service must be set");
|
||||
}
|
||||
if(authenticationService == null)
|
||||
{
|
||||
throw new IllegalArgumentException("An authentication service must be set");
|
||||
}
|
||||
}
|
||||
|
||||
// OwnableService implmentation
|
||||
|
||||
|
||||
public String getOwner(NodeRef nodeRef)
|
||||
{
|
||||
String userName = null;
|
||||
// If ownership is not explicitly set then we fall back to the creator
|
||||
//
|
||||
if(nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
|
||||
{
|
||||
userName = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(nodeRef, ContentModel.PROP_OWNER));
|
||||
}
|
||||
else if(nodeService.hasAspect(nodeRef, ContentModel.ASPECT_AUDITABLE))
|
||||
{
|
||||
userName = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR));
|
||||
}
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setOwner(NodeRef nodeRef, String userName)
|
||||
{
|
||||
if(!nodeService.hasAspect(nodeRef, ContentModel.ASPECT_OWNABLE))
|
||||
{
|
||||
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
|
||||
properties.put(ContentModel.PROP_OWNER, userName);
|
||||
nodeService.addAspect(nodeRef, ContentModel.ASPECT_OWNABLE, properties);
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeService.setProperty(nodeRef, ContentModel.PROP_OWNER, userName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void takeOwnership(NodeRef nodeRef)
|
||||
{
|
||||
setOwner(nodeRef, authenticationService.getCurrentUserName());
|
||||
}
|
||||
|
||||
public boolean hasOwner(NodeRef nodeRef)
|
||||
{
|
||||
return getOwner(nodeRef) != null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.ownable.impl;
|
||||
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.security.OwnableService;
|
||||
|
||||
/**
|
||||
* A simple implementation that does not support ownership.
|
||||
*
|
||||
* @author Andy Hind
|
||||
*/
|
||||
public class OwnableServiceNOOPImpl implements OwnableService
|
||||
{
|
||||
|
||||
public OwnableServiceNOOPImpl()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public String getOwner(NodeRef nodeRef)
|
||||
{
|
||||
// Return null as there is no owner.
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setOwner(NodeRef nodeRef, String userName)
|
||||
{
|
||||
// No action.
|
||||
}
|
||||
|
||||
public void takeOwnership(NodeRef nodeRef)
|
||||
{
|
||||
// No action.
|
||||
}
|
||||
|
||||
public boolean hasOwner(NodeRef nodeRef)
|
||||
{
|
||||
// There is no owner for any node.
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.repo.ownable.impl;
|
||||
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationComponent;
|
||||
import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
|
||||
import org.alfresco.repo.security.permissions.dynamic.OwnerDynamicAuthority;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.security.AccessStatus;
|
||||
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||
import org.alfresco.service.cmr.security.OwnableService;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
public class OwnableServiceTest extends TestCase
|
||||
{
|
||||
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
|
||||
|
||||
private NodeService nodeService;
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
|
||||
private AuthenticationComponent authenticationComponent;
|
||||
|
||||
private MutableAuthenticationDao authenticationDAO;
|
||||
|
||||
private OwnableService ownableService;
|
||||
|
||||
private NodeRef rootNodeRef;
|
||||
|
||||
private UserTransaction userTransaction;
|
||||
|
||||
private PermissionService permissionService;
|
||||
|
||||
private OwnerDynamicAuthority dynamicAuthority;
|
||||
|
||||
public OwnableServiceTest()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public OwnableServiceTest(String arg0)
|
||||
{
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
nodeService = (NodeService) ctx.getBean("nodeService");
|
||||
authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
|
||||
authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
|
||||
ownableService = (OwnableService) ctx.getBean("ownableService");
|
||||
permissionService = (PermissionService) ctx.getBean("permissionService");
|
||||
|
||||
authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName());
|
||||
authenticationDAO = (MutableAuthenticationDao) ctx.getBean("alfDaoImpl");
|
||||
|
||||
|
||||
TransactionService transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName());
|
||||
userTransaction = transactionService.getUserTransaction();
|
||||
userTransaction.begin();
|
||||
|
||||
StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
|
||||
rootNodeRef = nodeService.getRootNode(storeRef);
|
||||
permissionService.setPermission(rootNodeRef, PermissionService.ALL_AUTHORITIES, PermissionService.ADD_CHILDREN, true);
|
||||
|
||||
if(authenticationDAO.userExists("andy"))
|
||||
{
|
||||
authenticationService.deleteAuthentication("andy");
|
||||
}
|
||||
authenticationService.createAuthentication("andy", "andy".toCharArray());
|
||||
|
||||
dynamicAuthority = new OwnerDynamicAuthority();
|
||||
dynamicAuthority.setOwnableService(ownableService);
|
||||
|
||||
authenticationComponent.clearCurrentSecurityContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception
|
||||
{
|
||||
authenticationComponent.clearCurrentSecurityContext();
|
||||
userTransaction.rollback();
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
public void testSetup()
|
||||
{
|
||||
assertNotNull(nodeService);
|
||||
assertNotNull(authenticationService);
|
||||
assertNotNull(ownableService);
|
||||
}
|
||||
|
||||
public void testUnSet()
|
||||
{
|
||||
assertNull(ownableService.getOwner(rootNodeRef));
|
||||
assertFalse(ownableService.hasOwner(rootNodeRef));
|
||||
}
|
||||
|
||||
public void testCMObject()
|
||||
{
|
||||
authenticationService.authenticate("andy", "andy".toCharArray());
|
||||
NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, ContentModel.TYPE_CMOBJECT, null).getChildRef();
|
||||
permissionService.setPermission(rootNodeRef, "andy", PermissionService.TAKE_OWNERSHIP, true);
|
||||
assertEquals("andy", ownableService.getOwner(testNode));
|
||||
assertTrue(ownableService.hasOwner(testNode));
|
||||
assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
|
||||
assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
|
||||
assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
|
||||
|
||||
assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, PermissionService.TAKE_OWNERSHIP));
|
||||
assertEquals(AccessStatus.ALLOWED, permissionService.hasPermission(rootNodeRef, PermissionService.SET_OWNER));
|
||||
|
||||
ownableService.setOwner(testNode, "muppet");
|
||||
assertEquals("muppet", ownableService.getOwner(testNode));
|
||||
ownableService.takeOwnership(testNode);
|
||||
assertEquals("andy", ownableService.getOwner(testNode));
|
||||
assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
|
||||
assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
|
||||
assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
|
||||
}
|
||||
|
||||
public void testContainer()
|
||||
{
|
||||
authenticationService.authenticate("andy", "andy".toCharArray());
|
||||
NodeRef testNode = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, ContentModel.TYPE_PERSON, ContentModel.TYPE_CONTAINER, null).getChildRef();
|
||||
assertNull(ownableService.getOwner(testNode));
|
||||
assertFalse(ownableService.hasOwner(testNode));
|
||||
assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
|
||||
assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
|
||||
assertFalse(dynamicAuthority.hasAuthority(testNode, "andy"));
|
||||
|
||||
assertFalse(permissionService.hasPermission(testNode, PermissionService.READ) == AccessStatus.ALLOWED);
|
||||
assertFalse(permissionService.hasPermission(testNode, permissionService.getAllPermission()) == AccessStatus.ALLOWED);
|
||||
|
||||
permissionService.setPermission(rootNodeRef, permissionService.getOwnerAuthority(), permissionService.getAllPermission(), true);
|
||||
|
||||
ownableService.setOwner(testNode, "muppet");
|
||||
assertEquals("muppet", ownableService.getOwner(testNode));
|
||||
ownableService.takeOwnership(testNode);
|
||||
assertEquals("andy", ownableService.getOwner(testNode));
|
||||
assertFalse(nodeService.hasAspect(testNode, ContentModel.ASPECT_AUDITABLE));
|
||||
assertTrue(nodeService.hasAspect(testNode, ContentModel.ASPECT_OWNABLE));
|
||||
assertTrue(dynamicAuthority.hasAuthority(testNode, "andy"));
|
||||
|
||||
assertTrue(permissionService.hasPermission(testNode, PermissionService.READ) == AccessStatus.ALLOWED);
|
||||
assertTrue(permissionService.hasPermission(testNode, permissionService.getAllPermission())== AccessStatus.ALLOWED);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user