e4cc232e2580986aa7986473da32b53b0e9b65c2
[utils] / security / usermgt / src / test / java / org / wamblee / security / authorization / TestUserAccessor.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.security.authorization;
17
18 import org.wamblee.security.authentication.InMemoryGroupSet;
19 import org.wamblee.security.authentication.InMemoryUserSet;
20 import org.wamblee.security.authentication.Md5HexMessageDigester;
21 import org.wamblee.security.authentication.RegexpNameValidator;
22 import org.wamblee.security.authentication.UserAccessor;
23 import org.wamblee.security.authentication.UserAdministration;
24 import org.wamblee.security.authentication.UserAdministrationImpl;
25 import org.wamblee.security.authentication.UserMgtException.Reason;
26
27
28 /**
29  * User access that always returns a user that belongs to a fixed group.
30  * 
31  * @author Erik Brakkee
32  */
33 public class TestUserAccessor implements UserAccessor {
34     private static final String USER = "erik";
35
36     private static final String PASSWORD = "abc123";
37
38     private static final String GROUP = "users";
39
40     private UserAdministration userAdmin; 
41     
42     public TestUserAccessor() { 
43         userAdmin = new UserAdministrationImpl(
44             new InMemoryUserSet(new RegexpNameValidator(
45                 RegexpNameValidator.PASSWORD_PATTERN, Reason.INVALID_PASSWORD,
46                 "Password must contain at least 6 characters"),
47                 new Md5HexMessageDigester()), new InMemoryGroupSet(),
48             new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
49                 Reason.INVALID_USERNAME, "Invalid user"),
50             new RegexpNameValidator(RegexpNameValidator.ID_PATTERN,
51                 Reason.INVALID_GROUPNAME, "Invalid group"));
52
53             userAdmin.createGroup(GROUP);
54             userAdmin.createUser(USER, PASSWORD);
55             userAdmin.addUserToGroup(USER, GROUP);
56     }
57     
58     // NOTE: This is only for test. Normally the user accessor would not depend
59     // on user administration. This just ensures that a user administration is created
60     // that knows about the given user. 
61     public UserAdministration getUserAdmin() {
62         return userAdmin;
63     }
64     
65     /*
66      * (non-Javadoc)
67      * 
68      * @see org.wamblee.usermgt.UserAccessor#getCurrentUser()
69      */
70     public String getCurrentUser() {
71         return USER;
72     }
73 }