1 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 ; 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 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 name; 38 private MMBaseCop manager; 39 40 public void load(Document document, long validKey, String name, MMBaseCop manager) throws SecurityException { 41 this.document = document; 42 this.validKey = validKey; 43 this.name = name; 44 this.manager =manager; 45 } 46 47 public abstract ContextUserContext login(Map userLoginInfo, Object [] userParameters) throws SecurityException ; 48 49 protected ContextUserContext getValidUserContext(String username, Rank rank) throws SecurityException { 50 return new ContextUserContext(username, rank, validKey, manager, name); 51 } 52 53 54 55 56 protected Rank getRank(String username) throws SecurityException { 57 return getRank(username, name); 58 } 59 60 63 protected Rank getRank(final String username, final String identifyType) throws SecurityException { 64 final String 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 te) { 77 String msg = "error executing query: '" + xpath + "'"; 78 log.error(msg); 79 log.error( Logging.stackTrace(te)); 80 throw new java.lang.SecurityException (msg); 81 } 82 if(found == null) { 83 log.warn("user '" + username + "' was not found for module: " + name); 84 return null; 85 } 86 87 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 99 protected Element getAccount(String userName) throws SecurityException { 100 return getAccount(userName, name, null); 101 } 102 105 protected String getModuleValue(String userName) throws SecurityException { 106 Element node = getAccount(userName, name, null); 107 if (node == null) return null; 108 return org.mmbase.util.xml.DocumentReader.getNodeTextValue(node); 110 } 111 112 117 protected Element getAccount(final String userName, final String identifyType, final String rank) throws SecurityException { 118 String userCons = ""; 119 if (userName != null) { 120 userCons = "[@name='" + userName + "']"; 121 } 122 final String xpath; 123 if (identifyType != null || (rank != null && ! "anonymous".equals(rank))) { 124 StringBuffer identifyCons = new StringBuffer (); 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 te) { 145 String msg = "error executing query: '" + xpath + "'"; 146 log.error(msg); 147 log.error(Logging.stackTrace(te)); 148 throw new java.lang.SecurityException (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 |