1 15 16 package org.apache.lenya.ac.impl; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.cocoon.environment.Request; 20 import org.apache.lenya.ac.AccessControlException; 21 import org.apache.lenya.ac.AccreditableManager; 22 import org.apache.lenya.ac.Authenticator; 23 import org.apache.lenya.ac.Identity; 24 import org.apache.lenya.ac.User; 25 26 30 public class UserAuthenticator extends AbstractLogEnabled implements Authenticator { 31 32 36 public boolean authenticate(AccreditableManager accreditableManager, Request request) 37 throws AccessControlException { 38 String username = request.getParameter("username"); 39 String password = request.getParameter("password"); 40 41 if (getLogger().isDebugEnabled()) { 42 getLogger().debug( 43 "Authenticating username [" + username + "] with password [" + password + "]"); 44 } 45 46 if (username == null || password == null) { 47 throw new AccessControlException("Username or password is null!"); 48 } 49 50 Identity identity = (Identity) request.getSession(false).getAttribute( 51 Identity.class.getName()); 52 boolean authenticated = authenticate(accreditableManager, username, password, identity); 53 return authenticated; 54 } 55 56 66 protected boolean authenticate(AccreditableManager accreditableManager, String username, 67 String password, Identity identity) throws AccessControlException { 68 69 User user = accreditableManager.getUserManager().getUser(username); 70 if (getLogger().isDebugEnabled()) { 71 getLogger().debug("Authenticating user: [" + user + "]"); 72 } 73 74 boolean authenticated = false; 75 if (user != null && user.authenticate(password)) { 76 if (getLogger().isDebugEnabled()) { 77 getLogger().debug("User [" + user + "] authenticated."); 78 } 79 80 if (!identity.contains(user)) { 81 User oldUser = identity.getUser(); 82 if (oldUser != null) { 83 if (getLogger().isDebugEnabled()) { 84 getLogger().debug("Removing user [" + oldUser + "] from identity."); 85 } 86 identity.removeIdentifiable(oldUser); 87 } 88 identity.addIdentifiable(user); 89 } 90 authenticated = true; 91 } else { 92 if (getLogger().isDebugEnabled()) { 93 if (user == null) { 94 getLogger().debug("No such user: [" + username + "]"); 95 } 96 getLogger().debug("User [" + username + "] not authenticated."); 97 } 98 } 99 100 return authenticated; 101 } 102 103 } | Popular Tags |