KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > AbstractPermissionTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.permissions.impl;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
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 JavaDoc 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         // TODO Auto-generated constructor stub
79
}
80
81     protected void onSetUpInTransaction() throws Exception JavaDoc
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 JavaDoc<QName, Serializable JavaDoc> 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         // create an authentication object e.g. the user
116
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 JavaDoc
138     {
139         flushAndClear();
140         super.onTearDownInTransaction();
141     }
142
143     protected void runAs(String JavaDoc userName)
144     {
145         authenticationService.authenticate(userName, userName.toCharArray());
146         assertNotNull(authenticationService.getCurrentUserName());
147         // for(GrantedAuthority authority : woof.getAuthorities())
148
// {
149
// System.out.println("Auth = "+authority.getAuthority());
150
// }
151

152     }
153
154     private Map JavaDoc<QName, Serializable JavaDoc> createPersonProperties(String JavaDoc userName)
155     {
156         HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
157         properties.put(ContentModel.PROP_USERNAME, userName);
158         return properties;
159     }
160
161     protected PermissionReference getPermission(String JavaDoc permission)
162     {
163         return permissionModelDAO.getPermissionReference(null, permission);
164     }
165
166 }
167
Popular Tags