1 19 20 package org.apache.james.userrepository; 21 22 import org.apache.james.security.DigestUtil; 23 import org.apache.james.services.User; 24 25 import java.security.NoSuchAlgorithmException ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Locale ; 31 32 public class MockUsersRepository extends AbstractUsersRepository { 33 34 private final HashMap m_users = new HashMap (); 35 36 42 private boolean m_forceUseJamesUser = false; 43 44 public void setForceUseJamesUser() { 45 m_forceUseJamesUser = true; 46 } 47 48 public boolean addUser(User user) { 49 50 if (m_forceUseJamesUser && user instanceof DefaultUser ) { 51 DefaultUser aUser = (DefaultUser)user; 52 user = new DefaultJamesUser(aUser.getUserName(), 53 aUser.getHashedPassword(), 54 aUser.getHashAlgorithm()); 55 } 56 57 String key = user.getUserName(); 58 if (m_users.containsKey(key)) return false; 59 m_users.put(key, user); 60 return true; 61 } 62 63 public void addUser(String name, Object attributes) { 64 if (!(attributes instanceof String )) { 65 throw new IllegalArgumentException (); 66 } 67 try { 68 String passwordHash = DigestUtil.digestString(((String ) attributes), "SHA"); 69 70 User user; 71 72 if (m_forceUseJamesUser) { 73 user = new DefaultJamesUser(name, passwordHash, "SHA"); 74 } else { 75 user = new DefaultUser(name, passwordHash, "SHA"); 76 } 77 78 addUser(user); 79 } catch (Exception e) { 80 e.printStackTrace(); } 82 } 83 84 public boolean addUser(String username, String password) { 85 if (m_users.containsKey(username)) return false; 86 try { 87 String passwordHash = DigestUtil.digestString((password), "SHA"); 88 89 User user; 90 91 if (m_forceUseJamesUser) { 92 user = new DefaultJamesUser(username, passwordHash, "SHA"); 93 } else { 94 user = new DefaultUser(username, passwordHash, "SHA"); 95 } 96 97 return addUser(user); 98 } catch (NoSuchAlgorithmException e) { 99 e.printStackTrace(); } 101 return false; 102 } 103 104 public Object getAttributes(String name) { 105 return null; } 107 108 public User getUserByName(String name) { 109 if (ignoreCase) { 110 return getUserByNameCaseInsensitive(name); 111 } else { 112 return (User) m_users.get(name); 113 } 114 } 115 116 public User getUserByNameCaseInsensitive(String name) { 117 return (User) m_users.get(name.toLowerCase(Locale.US)); 118 } 119 120 public String getRealName(String name) { 121 if (ignoreCase) { 122 return m_users.get(name.toLowerCase(Locale.US)) != null ? ((User) m_users.get(name.toLowerCase(Locale.US))).getUserName() : null; 123 } else { 124 return m_users.get(name) != null ? name : null; 125 } 126 } 127 128 public boolean updateUser(User user) { 129 if (m_users.containsKey(user.getUserName())) { 130 m_users.put(user.getUserName(), user); 131 return true; 132 } else return false; } 134 135 public void removeUser(String name) { 136 m_users.remove(name); 137 } 138 139 public boolean contains(String name) { 140 if (ignoreCase) { 141 return containsCaseInsensitive(name); 142 } else { 143 return m_users.containsKey(name); 144 } 145 } 146 147 public boolean containsCaseInsensitive(String name) { 148 throw new UnsupportedOperationException ("mock"); 149 } 150 151 public boolean test(String name, Object attributes) { 152 throw new UnsupportedOperationException ("mock"); 153 } 154 155 public boolean test(String name, String password) { 156 User user = getUserByName(name); 157 if (user == null) return false; 158 return user.verifyPassword(password); 159 } 160 161 public int countUsers() { 162 return m_users.size(); 163 } 164 165 protected List listUserNames() { 166 Iterator users = m_users.values().iterator(); 167 List userNames = new LinkedList (); 168 while ( users.hasNext() ) { 169 User user = (User)users.next(); 170 userNames.add(user.getUserName()); 171 } 172 173 return userNames; 174 } 175 public Iterator list() { 176 return listUserNames().iterator(); 177 } 178 179 protected void doAddUser(User user) { 180 } 182 183 protected void doUpdateUser(User user) { 184 } 186 } 187 | Popular Tags |