1 17 package org.alfresco.repo.security.permissions.impl; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.security.authentication.AuthenticationComponent; 25 import org.alfresco.repo.security.authentication.MutableAuthenticationDao; 26 import org.alfresco.repo.security.permissions.PermissionReference; 27 import org.alfresco.repo.security.permissions.PermissionServiceSPI; 28 import org.alfresco.service.ServiceRegistry; 29 import org.alfresco.service.cmr.dictionary.DictionaryService; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.alfresco.service.cmr.repository.NodeService; 32 import org.alfresco.service.cmr.repository.StoreRef; 33 import org.alfresco.service.cmr.security.AuthenticationService; 34 import org.alfresco.service.cmr.security.AuthorityService; 35 import org.alfresco.service.cmr.security.PersonService; 36 import org.alfresco.service.namespace.NamespacePrefixResolver; 37 import org.alfresco.service.namespace.NamespaceService; 38 import org.alfresco.service.namespace.QName; 39 import org.alfresco.util.BaseSpringTest; 40 import org.springframework.orm.hibernate3.LocalSessionFactoryBean; 41 42 public class AbstractPermissionTest extends BaseSpringTest 43 { 44 45 protected static final String ROLE_AUTHENTICATED = "ROLE_AUTHENTICATED"; 46 47 protected NodeService nodeService; 48 49 protected DictionaryService dictionaryService; 50 51 protected PermissionServiceSPI permissionService; 52 53 protected AuthenticationService authenticationService; 54 55 private MutableAuthenticationDao authenticationDAO; 56 57 protected LocalSessionFactoryBean sessionFactory; 58 59 protected NodeRef rootNodeRef; 60 61 protected NamespacePrefixResolver namespacePrefixResolver; 62 63 protected ServiceRegistry serviceRegistry; 64 65 protected NodeRef systemNodeRef; 66 67 protected AuthenticationComponent authenticationComponent; 68 69 protected ModelDAO permissionModelDAO; 70 71 protected PersonService personService; 72 73 protected AuthorityService authorityService; 74 75 public AbstractPermissionTest() 76 { 77 super(); 78 } 80 81 protected void onSetUpInTransaction() throws Exception 82 { 83 nodeService = (NodeService) applicationContext.getBean("nodeService"); 84 dictionaryService = (DictionaryService) applicationContext.getBean(ServiceRegistry.DICTIONARY_SERVICE 85 .getLocalName()); 86 permissionService = (PermissionServiceSPI) applicationContext.getBean("permissionService"); 87 namespacePrefixResolver = (NamespacePrefixResolver) applicationContext 88 .getBean(ServiceRegistry.NAMESPACE_SERVICE.getLocalName()); 89 authenticationService = (AuthenticationService) applicationContext.getBean("authenticationService"); 90 authenticationComponent = (AuthenticationComponent) applicationContext.getBean("authenticationComponent"); 91 serviceRegistry = (ServiceRegistry) applicationContext.getBean(ServiceRegistry.SERVICE_REGISTRY); 92 permissionModelDAO = (ModelDAO) applicationContext.getBean("permissionsModelDAO"); 93 personService = (PersonService) applicationContext.getBean("personService"); 94 authorityService = (AuthorityService) applicationContext.getBean("authorityService"); 95 96 authenticationComponent.setCurrentUser(authenticationComponent.getSystemUserName()); 97 authenticationDAO = (MutableAuthenticationDao) applicationContext.getBean("alfDaoImpl"); 98 99 100 StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.nanoTime()); 101 rootNodeRef = nodeService.getRootNode(storeRef); 102 103 QName children = ContentModel.ASSOC_CHILDREN; 104 QName system = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "system"); 105 QName container = ContentModel.TYPE_CONTAINER; 106 QName types = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "people"); 107 108 systemNodeRef = nodeService.createNode(rootNodeRef, children, system, container).getChildRef(); 109 NodeRef typesNodeRef = nodeService.createNode(systemNodeRef, children, types, container).getChildRef(); 110 Map <QName, Serializable > props = createPersonProperties("andy"); 111 nodeService.createNode(typesNodeRef, children, ContentModel.TYPE_PERSON, container, props).getChildRef(); 112 props = createPersonProperties("lemur"); 113 nodeService.createNode(typesNodeRef, children, ContentModel.TYPE_PERSON, container, props).getChildRef(); 114 115 if(authenticationDAO.userExists("andy")) 117 { 118 authenticationService.deleteAuthentication("andy"); 119 } 120 authenticationService.createAuthentication("andy", "andy".toCharArray()); 121 122 if(authenticationDAO.userExists("lemur")) 123 { 124 authenticationService.deleteAuthentication("lemur"); 125 } 126 authenticationService.createAuthentication("lemur", "lemur".toCharArray()); 127 128 if(authenticationDAO.userExists("admin")) 129 { 130 authenticationService.deleteAuthentication("admin"); 131 } 132 authenticationService.createAuthentication("admin", "admin".toCharArray()); 133 134 authenticationComponent.clearCurrentSecurityContext(); 135 } 136 137 protected void onTearDownInTransaction() throws Exception 138 { 139 flushAndClear(); 140 super.onTearDownInTransaction(); 141 } 142 143 protected void runAs(String userName) 144 { 145 authenticationService.authenticate(userName, userName.toCharArray()); 146 assertNotNull(authenticationService.getCurrentUserName()); 147 152 } 153 154 private Map <QName, Serializable > createPersonProperties(String userName) 155 { 156 HashMap <QName, Serializable > properties = new HashMap <QName, Serializable >(); 157 properties.put(ContentModel.PROP_USERNAME, userName); 158 return properties; 159 } 160 161 protected PermissionReference getPermission(String permission) 162 { 163 return permissionModelDAO.getPermissionReference(null, permission); 164 } 165 166 } 167 | Popular Tags |