KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > context > ContextLoginModule


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.context;
11
12 import org.mmbase.security.*;
13 import org.mmbase.security.SecurityException;
14
15 import java.util.Map JavaDoc;
16
17 import org.w3c.dom.*;
18
19 import org.apache.xpath.XPathAPI;
20
21 import org.mmbase.util.logging.Logger;
22 import org.mmbase.util.logging.Logging;
23
24 /**
25  * Class ContextLoginModule
26  * @javadoc
27  *
28  * @author Eduard Witteveen
29  * @version $Id: ContextLoginModule.java,v 1.19 2005/12/29 20:43:16 michiel Exp $
30  */

31
32 public abstract class ContextLoginModule {
33     private static final Logger log = Logging.getLoggerInstance(ContextLoginModule.class);
34
35     private Document document;
36     private long validKey;
37     private String JavaDoc name;
38     private MMBaseCop manager;
39
40     public void load(Document document, long validKey, String JavaDoc name, MMBaseCop manager) throws SecurityException JavaDoc{
41         this.document = document;
42         this.validKey = validKey;
43         this.name = name;
44         this.manager =manager;
45     }
46
47     public abstract ContextUserContext login(Map JavaDoc userLoginInfo, Object JavaDoc[] userParameters) throws SecurityException JavaDoc;
48
49     protected ContextUserContext getValidUserContext(String JavaDoc username, Rank rank) throws SecurityException JavaDoc{
50         return new ContextUserContext(username, rank, validKey, manager, name);
51     }
52
53
54
55
56     protected Rank getRank(String JavaDoc username) throws SecurityException JavaDoc {
57         return getRank(username, name);
58     }
59
60     /**
61      * @since MMBase-1.8
62      */

63     protected Rank getRank(final String JavaDoc username, final String JavaDoc identifyType) throws SecurityException JavaDoc {
64         final String JavaDoc xpath;
65         if (identifyType != null) {
66             xpath = "/contextconfig/accounts/user[@name='" + username + "']/identify[@type='" + identifyType + "']";
67         } else {
68             xpath = "/contextconfig/accounts/user[@name='" + username + "']/identify";
69         }
70         if (log.isDebugEnabled()) {
71             log.debug("going to execute the query: " + xpath);
72         }
73         Node found;
74         try {
75             found = XPathAPI.selectSingleNode(document, xpath);
76         } catch(javax.xml.transform.TransformerException JavaDoc te) {
77             String JavaDoc msg = "error executing query: '" + xpath + "'";
78             log.error(msg);
79             log.error( Logging.stackTrace(te));
80             throw new java.lang.SecurityException JavaDoc(msg);
81         }
82         if(found == null) {
83             log.warn("user '" + username + "' was not found for module: " + name);
84             return null;
85         }
86
87         // retrieve the rank...
88
NamedNodeMap nnm = found.getAttributes();
89         Node rankNode = nnm.getNamedItem("rank");
90         Rank rank = Rank.getRank(rankNode.getNodeValue());
91         if (log.isDebugEnabled()) log.debug("retrieved the rank for user:" + username + " in module: " + name + " rank: " + rank);
92         return rank;
93     }
94
95     /**
96      * Gets accounts for this authentication module
97      * @since MMBase-1.8
98      */

99     protected Element getAccount(String JavaDoc userName) throws SecurityException JavaDoc {
100         return getAccount(userName, name, null);
101     }
102     /**
103      * @deprecated Use {@link #getAccount}
104      */

105     protected String JavaDoc getModuleValue(String JavaDoc userName) throws SecurityException JavaDoc {
106         Element node = getAccount(userName, name, null);
107         if (node == null) return null;
108         // now we have to retrieve the value of the node.
109
return org.mmbase.util.xml.DocumentReader.getNodeTextValue(node);
110     }
111
112     /**
113      * Search an account for a given user name and identify type (the 'module').
114      * @return The user Element.
115      * @since MMBase-1.8
116      */

117     protected Element getAccount(final String JavaDoc userName, final String JavaDoc identifyType, final String JavaDoc rank) throws SecurityException JavaDoc {
118         String JavaDoc userCons = "";
119         if (userName != null) {
120             userCons = "[@name='" + userName + "']";
121         }
122         final String JavaDoc xpath;
123         if (identifyType != null || (rank != null && ! "anonymous".equals(rank))) {
124             StringBuffer JavaDoc identifyCons = new StringBuffer JavaDoc();
125             if (identifyType != null) {
126                 identifyCons.append("@type='").append(identifyType).append("'");
127             }
128             if (rank != null) {
129                 if (identifyCons.length() > 0) identifyCons.append(" and ");
130                 identifyCons.append("@rank='").append(rank).append("'");
131             }
132             xpath = "/contextconfig/accounts/user" + userCons + "/identify[" + identifyCons + "]";
133         } else {
134             xpath = "/contextconfig/accounts/user" + userCons;
135         }
136         
137         if (log.isDebugEnabled()) {
138             log.debug("going to execute the query: " + xpath);
139         }
140         
141         final Element found;
142         try {
143             found = (Element) XPathAPI.selectSingleNode(document, xpath);
144         } catch(javax.xml.transform.TransformerException JavaDoc te) {
145             String JavaDoc msg = "error executing query: '" + xpath + "'";
146             log.error(msg);
147             log.error(Logging.stackTrace(te));
148             throw new java.lang.SecurityException JavaDoc(msg);
149         }
150         if(found == null) {
151             if (rank != null) {
152                 log.warn("No user with rank '" + rank + "' " + (userName != null ? "and username '" + userName + "'": "") + "was not found for identify type: '" + identifyType + "'");
153             } else {
154                 log.warn("No user with username '" + userName + "' was not found for identify type: '" + identifyType + "'");
155             }
156             return null;
157         }
158         if (identifyType != null || rank != null) {
159             return (Element) found.getParentNode();
160         } else {
161             return (Element) found;
162         }
163     }
164 }
165
Popular Tags