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