KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > cloud > UserBuilder


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security.implementation.cloud;
11
12 import org.mmbase.module.core.MMObjectNode;
13 import org.mmbase.module.core.MMObjectBuilder;
14
15 import org.mmbase.util.logging.Logger;
16 import org.mmbase.util.logging.Logging;
17
18 /**
19  * Security from within MMBase. The mmbaseusers builder used to store nothing more than name/password combination.
20  *
21  * @author Eduard Witteveen
22  * @version $Id: UserBuilder.java,v 1.9 2006/05/16 18:07:20 michiel Exp $
23  */

24 public class UserBuilder extends MMObjectBuilder {
25     private static final Logger log = Logging.getLoggerInstance(UserBuilder.class);
26
27     private org.mmbase.util.Encode encoder = null;
28
29     /**
30      * {@inheritDoc}
31      */

32     public boolean init() {
33         String JavaDoc encoding = (String JavaDoc) getInitParameters().get("encoding");
34         if(encoding==null) {
35             log.warn("no property 'encoding' defined in '"+getTableName()+".xml' using default encoding");
36             encoder = new org.mmbase.util.Encode("MD5");
37         } else {
38             encoder = new org.mmbase.util.Encode(encoding);
39         }
40         log.info("Using " + encoder.getEncoding() + " as our encoding for password");
41         return super.init();
42     }
43
44
45
46     /**
47      * Checks whether the given username/password combination exists and is correct.
48      * If password is null, then only the existence of the user is checked.
49      */

50     boolean exists(String JavaDoc username, String JavaDoc password) {
51         if (log.isTraceEnabled()) {
52             log.trace("username: '" + username + "' password: '" + password + "'");
53         }
54         java.util.Enumeration JavaDoc e = search("WHERE username = '"+username+"' ");
55         while(e.hasMoreElements()) {
56             MMObjectNode node = (MMObjectNode) e.nextElement();
57             if(password == null || encode(password).equals(node.getStringValue("password"))) {
58                 // found it !
59
log.trace("username: '"+username+"' password: '"+password+"' found in node #" + node.getNumber());
60                 return true;
61             } else {
62                 log.trace("username: '"+username+"' found in node #" + node.getNumber()+" --> PASSWORDS NOT EQUAL");
63             }
64         }
65         if (log.isTraceEnabled()) {
66             log.trace("username: '"+username+"' --> USERNAME NOT CORRECT");
67         }
68         return false;
69     }
70
71     protected String JavaDoc encode(String JavaDoc value) {
72         return encoder.encode(value);
73     }
74 }
75
Popular Tags