1 package org.apache.fulcrum.security.impl.db; 2 3 56 57 import java.util.ArrayList ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 61 import org.apache.fulcrum.security.TurbineSecurity; 62 import org.apache.fulcrum.security.UserManager; 63 import org.apache.fulcrum.security.entity.User; 64 import org.apache.fulcrum.security.impl.db.entity.TurbineUser; 65 import org.apache.fulcrum.security.impl.db.entity.TurbineUserPeer; 66 import org.apache.fulcrum.security.util.DataBackendException; 67 import org.apache.fulcrum.security.util.EntityExistsException; 68 import org.apache.fulcrum.security.util.PasswordMismatchException; 69 import org.apache.fulcrum.security.util.UnknownEntityException; 70 import org.apache.torque.util.Criteria; 71 72 88 public class DBUserManager implements UserManager 89 { 90 91 94 private static final boolean DEBUG = false; 95 96 106 public boolean accountExists( User user ) 107 throws DataBackendException 108 { 109 return accountExists(user.getUserName()); 110 } 111 112 122 public boolean accountExists( String username ) 123 throws DataBackendException 124 { 125 Criteria criteria = new Criteria(); 126 criteria.add(TurbineUserPeer.USERNAME, username); 127 List users; 128 try 129 { 130 users = TurbineUserPeer.doSelect(criteria); 131 } 132 catch(Exception e) 133 { 134 throw new DataBackendException( 135 "Failed to check account's presence", e); 136 } 137 if ( users.size() > 1 ) 138 { 139 throw new DataBackendException( 140 "Multiple Users with same username '" + username + "'"); 141 } 142 return(users.size() == 1); 143 } 144 145 156 public User retrieve( String username ) 157 throws UnknownEntityException, DataBackendException 158 { 159 Criteria criteria = new Criteria(); 160 criteria.add( TurbineUserPeer.USERNAME, username ); 161 List users; 162 try 163 { 164 users = TurbineUserPeer.doSelect(criteria); 165 } 166 catch(Exception e) 167 { 168 throw new DataBackendException("Failed to retrieve user '" + 169 username + "'", e); 170 } 171 if ( users.size() > 1 ) 172 { 173 throw new DataBackendException( 174 "Multiple Users with same username '" + username + "'"); 175 } 176 if ( users.size() == 1 ) 177 { 178 return (User)users.get(0); 179 } 180 throw new UnknownEntityException("Unknown user '" + username + "'"); 181 } 182 183 197 public User[] retrieve( Criteria criteria ) 198 throws DataBackendException 199 { 200 Iterator keys = criteria.keySet().iterator(); 201 while(keys.hasNext()) 202 { 203 String key = (String )keys.next(); 204 Criteria.Criterion[] criterion = criteria 206 .getCriterion(key).getAttachedCriterion(); 207 for (int i=0;i<criterion.length;i++) 208 { 209 String table = criterion[i].getTable(); 210 if ( table == null || "".equals(table) ) 211 { 212 criterion[i].setTable(TurbineUserPeer.getTableName()); 213 } 214 } 215 } 216 List users = new ArrayList (0); 217 try 218 { 219 users = TurbineUserPeer.doSelect(criteria); 220 } 221 catch(Exception e) 222 { 223 throw new DataBackendException("Failed to retrieve users", e); 224 } 225 return (User[])users.toArray(new User[0]); 226 } 227 228 244 public User retrieve( String username, String password ) 245 throws PasswordMismatchException, UnknownEntityException, 246 DataBackendException 247 { 248 User user = retrieve(username); 249 authenticate(user, password); 250 return user; 251 } 252 253 263 public void store(User user) 264 throws UnknownEntityException, DataBackendException 265 { 266 if (!accountExists(user)) 267 { 268 throw new UnknownEntityException("The account '" + 269 user.getUserName() + "' does not exist"); 270 } 271 272 try 273 { 274 ((TurbineUser)user).setNew(false); 279 ((TurbineUser)user).setModified(true); 280 ((TurbineUser) user).save(); 281 } 282 catch(Exception e) 283 { 284 throw new DataBackendException("Failed to save user object", e); 285 } 286 } 287 288 302 public void authenticate( User user, String password ) 303 throws PasswordMismatchException, UnknownEntityException, 304 DataBackendException 305 { 306 if (!accountExists(user)) 307 { 308 throw new UnknownEntityException("The account '" + 309 user.getUserName() + "' does not exist"); 310 } 311 String encrypted = TurbineSecurity.encryptPassword(password); 312 313 if (DEBUG) 314 { 315 System.out.println ("Supplied Pass: " + password); 316 System.out.println ("User Pass: " + user.getPassword()); 317 System.out.println ("Encrypted Pass: " + encrypted ); 318 } 319 if (!user.getPassword().equals(encrypted)) 320 { 321 throw new PasswordMismatchException("The passwords do not match"); 322 } 323 } 324 325 337 public void changePassword( User user, String oldPassword, 338 String newPassword ) 339 throws PasswordMismatchException, UnknownEntityException, 340 DataBackendException 341 { 342 String encrypted = TurbineSecurity.encryptPassword(oldPassword); 343 if (!accountExists(user)) 344 { 345 throw new UnknownEntityException("The account '" + 346 user.getUserName() + "' does not exist"); 347 } 348 if (!user.getPassword().equals(encrypted)) 349 { 350 throw new PasswordMismatchException( 351 "The supplied old password for '" + user.getUserName() + 352 "' was incorrect"); 353 } 354 user.setPassword(TurbineSecurity.encryptPassword(newPassword)); 355 store(user); 359 } 360 361 376 public void forcePassword( User user, String password ) 377 throws UnknownEntityException, DataBackendException 378 { 379 if (!accountExists(user)) 380 { 381 throw new UnknownEntityException("The account '" + 382 user.getUserName() + "' does not exist"); 383 } 384 user.setPassword(TurbineSecurity.encryptPassword(password)); 385 store(user); 389 } 390 391 399 public void createAccount( User user, String initialPassword ) 400 throws EntityExistsException, DataBackendException 401 { 402 if (accountExists(user)) 403 { 404 throw new EntityExistsException("The account '" + 405 user.getUserName() + "' already exists"); 406 } 407 String encrypted = TurbineSecurity.encryptPassword(initialPassword); 408 user.setPassword(encrypted); 409 try 410 { 411 ((TurbineUser)user).setNew(true); 416 ((TurbineUser)user).setModified(true); 417 ((TurbineUser) user).save(); 420 } 421 catch(Exception e) 422 { 423 throw new DataBackendException("Failed to create account '" + 424 user.getUserName() + "'", e); 425 } 426 } 427 428 436 public void removeAccount( User user ) 437 throws UnknownEntityException, DataBackendException 438 { 439 if (!accountExists(user)) 440 { 441 throw new UnknownEntityException("The account '" + 442 user.getUserName() + "' does not exist"); 443 } 444 Criteria criteria = new Criteria(); 445 criteria.add(TurbineUserPeer.USERNAME, user.getUserName()); 446 try 447 { 448 TurbineUserPeer.doDelete(criteria); 449 } 450 catch(Exception e) 451 { 452 throw new DataBackendException("Failed to remove account '" + 453 user.getUserName() + "'", e); 454 } 455 } 456 } 457 | Popular Tags |