1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19 package org.apache.james.userrepository;
20
21 import org.apache.james.security.DigestUtil;
22 import org.apache.james.services.User;
23 import org.apache.james.services.UsersRepository;
24
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
29
30 public class MockUsersRepository implements UsersRepository {
31
32 private final HashMap m_users = new HashMap();
33
34 /***
35 * force the repository to hold implementations of JamesUser interface, instead of User
36 * JamesUser is _not_ required as of the UsersRepository interface, so the necessarity forcing it
37 * is due to code using UsersRepository while at the same time expecting it to hold JamesUsers
38 * (like in RemoteManagerHandler)
39 */
40 private boolean m_forceUseJamesUser = false;
41
42 public void setForceUseJamesUser() {
43 m_forceUseJamesUser = true;
44 }
45
46 public boolean addUser(User user) {
47
48 if (m_forceUseJamesUser && user instanceof DefaultUser ) {
49 DefaultUser aUser = (DefaultUser)user;
50 user = new DefaultJamesUser(aUser.getUserName(),
51 aUser.getHashedPassword(),
52 aUser.getHashAlgorithm());
53 }
54
55 String key = user.getUserName();
56 if (m_users.containsKey(key)) return false;
57 m_users.put(key, user);
58 return true;
59 }
60
61 public void addUser(String name, Object attributes) {
62 try {
63 String passwordHash = DigestUtil.digestString(((String) attributes), "SHA");
64
65 User user;
66
67 if (m_forceUseJamesUser) {
68 user = new DefaultJamesUser(name, passwordHash, "SHA");
69 } else {
70 user = new DefaultUser(name, passwordHash, "SHA");
71 }
72
73 addUser(user);
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 }
78
79 public boolean addUser(String username, String password) {
80 if (m_users.containsKey(username)) return false;
81 try {
82 String passwordHash = DigestUtil.digestString((password), "SHA");
83
84 User user;
85
86 if (m_forceUseJamesUser) {
87 user = new DefaultJamesUser(username, passwordHash, "SHA");
88 } else {
89 user = new DefaultUser(username, passwordHash, "SHA");
90 }
91
92 return addUser(user);
93 } catch (Exception e) {
94 e.printStackTrace();
95 }
96 return false;
97 }
98
99 public Object getAttributes(String name) {
100 return null;
101 }
102
103 public User getUserByName(String name) {
104 return (User) m_users.get(name);
105 }
106
107 public User getUserByNameCaseInsensitive(String name) {
108 return null;
109 }
110
111 public String getRealName(String name) {
112 return ((User) m_users.get(name)).getUserName();
113 }
114
115 public boolean updateUser(User user) {
116 return false;
117 }
118
119 public void removeUser(String name) {
120 m_users.remove(name);
121 }
122
123 public boolean contains(String name) {
124 return m_users.containsKey(name);
125 }
126
127 public boolean containsCaseInsensitive(String name) {
128 return false;
129 }
130
131 public boolean test(String name, Object attributes) {
132 return false;
133 }
134
135 public boolean test(String name, String password) {
136 User user = getUserByName(name);
137 if (user == null) return false;
138 return user.verifyPassword(password);
139 }
140
141 public int countUsers() {
142 return m_users.size();
143 }
144
145 protected List listUserNames() {
146 Iterator users = m_users.values().iterator();
147 List userNames = new LinkedList();
148 while ( users.hasNext() ) {
149 User user = (User)users.next();
150 userNames.add(user.getUserName());
151 }
152
153 return userNames;
154 }
155 public Iterator list() {
156 return listUserNames().iterator();
157 }
158 }