KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > TestWithUserUtils


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.util;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.service.cmr.repository.NodeRef;
24 import org.alfresco.service.cmr.repository.NodeService;
25 import org.alfresco.service.cmr.security.AuthenticationService;
26 import org.alfresco.service.namespace.NamespaceService;
27 import org.alfresco.service.namespace.QName;
28
29 /**
30  * Utility class containing some useful methods to help when writing tets that require authenticated users
31  *
32  * @author Roy Wetherall
33  */

34 public abstract class TestWithUserUtils extends BaseSpringTest
35 {
36     /**
37      * Create a new user, including the corresponding person node.
38      *
39      * @param userName the user name
40      * @param password the password
41      * @param rootNodeRef the root node reference
42      * @param nodeService the node service
43      * @param authenticationService the authentication service
44      */

45     public static void createUser(
46             String JavaDoc userName,
47             String JavaDoc password,
48             NodeRef rootNodeRef,
49             NodeService nodeService,
50             AuthenticationService authenticationService)
51     {
52         QName children = ContentModel.ASSOC_CHILDREN;
53         QName system = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "system");
54         QName container = ContentModel.TYPE_CONTAINER;
55         QName types = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, "people");
56         
57         NodeRef systemNodeRef = nodeService.createNode(rootNodeRef, children, system, container).getChildRef();
58         NodeRef typesNodeRef = nodeService.createNode(systemNodeRef, children, types, container).getChildRef();
59         
60         HashMap JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
61         properties.put(ContentModel.PROP_USERNAME, userName);
62         NodeRef goodUserPerson = nodeService.createNode(typesNodeRef, children, ContentModel.TYPE_PERSON, container, properties).getChildRef();
63         
64         // Create the users
65

66         authenticationService.createAuthentication(userName, password.toCharArray());
67     }
68
69     /**
70      * Autneticate the user with the specified password
71      *
72      * @param userName the user name
73      * @param password the password
74      * @param rootNodeRef the root node reference
75      * @param authenticationService the authentication service
76      */

77     public static void authenticateUser(
78             String JavaDoc userName,
79             String JavaDoc password,
80             NodeRef rootNodeRef,
81             AuthenticationService authenticationService)
82     {
83         authenticationService.authenticate(userName, password.toCharArray());
84     }
85     
86     /**
87      * Get the current user node reference
88      *
89      * @param authenticationService the authentication service
90      * @return the currenlty authenticated user's node reference
91      */

92     public static String JavaDoc getCurrentUser(AuthenticationService authenticationService)
93     {
94         String JavaDoc un = authenticationService.getCurrentUserName();
95         if (un != null)
96         {
97             return un;
98         }
99         else
100         {
101             throw new RuntimeException JavaDoc("The current user could not be retrieved.");
102         }
103         
104     }
105
106     public static void deleteUser(String JavaDoc user_name, String JavaDoc pwd, NodeRef ref, NodeService service, AuthenticationService service2)
107     {
108         service2.deleteAuthentication(user_name);
109     }
110
111 }
112
Popular Tags