1 10 package org.mmbase.security.implementation.cloudcontext; 11 12 import org.mmbase.security.implementation.cloudcontext.builders.*; 13 14 import org.mmbase.bridge.*; 15 import org.mmbase.bridge.util.Queries; 16 import org.w3c.dom.*; 17 import org.w3c.dom.NodeList ; 18 import org.w3c.dom.Node ; 19 20 import org.xml.sax.*; 21 import java.io.*; 22 23 import org.mmbase.util.logging.Logger; 24 import org.mmbase.util.logging.Logging; 25 26 34 public class ConvertTool { 35 private static final Logger log = Logging.getLoggerInstance(ConvertTool.class); 36 37 private Class contextAuthentication; 38 private NodeManager contextManager; 39 private NodeManager groupManager; 40 private NodeManager userManager; 41 private NodeManager rankManager; 42 private RelationManager rankRelationManager; 43 private RelationManager groupContainsRelationManager; 44 private RelationManager userContainsRelationManager; 45 private RelationManager groupGrantsRelationManager; 46 private RelationManager userGrantsRelationManager; 47 private RelationManager allowedRelationManager; 48 49 private StringBuffer result = new StringBuffer (); 50 51 public ConvertTool(Cloud cloud) throws ClassNotFoundException { 52 contextManager = cloud.getNodeManager("mmbasecontexts"); 53 groupManager = cloud.getNodeManager("mmbasegroups"); 54 userManager = cloud.getNodeManager("mmbaseusers"); 55 rankManager = cloud.getNodeManager("mmbaseranks"); 56 rankRelationManager = cloud.getRelationManager(userManager, rankManager, "rank"); 57 groupContainsRelationManager = cloud.getRelationManager(groupManager, groupManager, "contains"); 58 userContainsRelationManager = cloud.getRelationManager(groupManager, userManager, "contains"); 59 60 groupGrantsRelationManager = cloud.getRelationManager(contextManager, groupManager, "grants"); 61 userGrantsRelationManager = cloud.getRelationManager(contextManager, userManager, "grants"); 62 63 allowedRelationManager = cloud.getRelationManager(contextManager, contextManager, "allowed"); 64 65 contextAuthentication = Class.forName("org.mmbase.security.implementation.context.ContextAuthentication"); 66 } 67 68 org.mmbase.bridge.Node getNode(NodeManager nm, String name, String fieldName) { 69 NodeQuery q = nm.createQuery(); 70 Queries.addConstraints(q, fieldName + " = '" + name + "'"); 71 org.mmbase.bridge.NodeList nl = nm.getList(q); 72 if (nl.size() > 0) { 73 return nl.getNode(0); 74 } else { 75 return null; 76 } 77 78 } 79 80 org.mmbase.bridge.Node getNode(NodeManager nm, String name) { 81 return getNode(nm, name, "name"); 82 } 83 84 void log(String message) { 85 log.info(message); 86 result.append(message).append("\n"); 87 } 88 89 public String getResult() { 90 return result.toString(); 91 } 92 93 94 97 98 public Document writeXml(File file) { 99 throw new UnsupportedOperationException ("not yet implemented"); 100 } 101 102 103 104 105 108 public Document readXml(File file) throws SAXException, IOException { 109 110 InputSource in = new InputSource(new FileInputStream(file)); 111 Document document = org.mmbase.util.xml.DocumentReader.getDocumentBuilder(true, 112 new org.mmbase.util.XMLErrorHandler(false, 0), 113 new org.mmbase.util.XMLEntityResolver(true, contextAuthentication) 114 ).parse(in); 115 116 117 log("Creating all non-existing contextes."); 118 NodeList contexts = document.getElementsByTagName("context"); 119 120 for (int i = 0; i < contexts.getLength(); i ++) { 121 Node node = contexts.item(i); 122 String name = node.getAttributes().getNamedItem("name").getNodeValue(); 123 if (getNode(contextManager, name) == null) { 124 org.mmbase.bridge.Node n = contextManager.createNode(); 125 n.setStringValue("name", name); 126 n.setStringValue("description", "imported from " + file); 127 n.commit(); 128 log("Created a context '" + name + "'"); 129 } 130 131 } 132 133 134 135 log("Creating all non-existing groups."); 136 NodeList groups = document.getElementsByTagName("group"); 137 138 for (int i = 0; i < groups.getLength(); i ++) { 139 Node node = groups.item(i); 140 String name = node.getAttributes().getNamedItem("name").getNodeValue(); 141 if (getNode(groupManager, name) == null) { 142 org.mmbase.bridge.Node n = groupManager.createNode(); 143 n.setStringValue("name", name); 144 n.setStringValue("description", "imported from " + file); 145 n.commit(); 146 log("Created a group '" + name + "'"); 147 } 148 149 } 150 151 152 log("Creating all non-existing users."); 153 NodeList users = document.getElementsByTagName("user"); 154 155 for (int i = 0; i < users.getLength(); i ++) { 156 Node node = users.item(i); 157 NamedNodeMap nnm = node.getAttributes(); 158 String name = nnm.getNamedItem("name").getNodeValue(); 159 String context = nnm.getNamedItem("context").getNodeValue(); 160 161 if (getNode(userManager, name, Users.FIELD_USERNAME) == null) { 162 163 Node identify = node.getFirstChild(); 164 if (identify != null && (! (identify instanceof Element))) { 165 identify = identify.getNextSibling(); 166 } 167 if (identify == null) continue; 168 169 String password = identify.getFirstChild().getNodeValue(); 170 String rank = identify.getAttributes().getNamedItem("rank").getNodeValue(); 171 172 org.mmbase.bridge.Node defaultContext = getNode(contextManager, context); 173 174 log("Default context: " + defaultContext.getStringValue("name")); 175 176 177 org.mmbase.bridge.Node n = userManager.createNode(); 178 n.setStringValue(Users.FIELD_USERNAME, name); 179 n.setStringValue(Users.FIELD_PASSWORD, password); 180 n.setIntValue(Users.FIELD_STATUS, 1); 181 182 n.commit(); 183 n.setNodeValue(Users.FIELD_DEFAULTCONTEXT, defaultContext); 184 n.commit(); 185 186 org.mmbase.bridge.Node rankNode = getNode(rankManager, rank); 187 if (rankNode != null) { 188 n.createRelation(rankNode, rankRelationManager).commit(); 189 } 190 log("Created a user '" + name + "' (" + rank + ")"); 191 } 192 193 } 194 195 196 log("Adding group structure"); 197 for (int i = 0; i < groups.getLength(); i ++) { 198 Node group = groups.item(i); 199 org.mmbase.bridge.Node g = getNode(groupManager, group.getAttributes().getNamedItem("name").getNodeValue()); 200 log("found " + group.getChildNodes().getLength() + " for " + g.getStringValue("name")); 201 Node contains = group.getFirstChild(); 202 while (contains != null) { 203 if (contains instanceof Element && contains.getNodeName().equals("contains")) { 204 String type = contains.getAttributes().getNamedItem("type").getNodeValue(); 205 if (type.equals("group")) { 206 org.mmbase.bridge.Node otherGroup = getNode(groupManager, contains.getAttributes().getNamedItem("named").getNodeValue()); 207 g.createRelation(otherGroup, groupContainsRelationManager).commit(); 208 } else if (type.equals("user")) { 209 org.mmbase.bridge.Node u = getNode(userManager, contains.getAttributes().getNamedItem("named").getNodeValue(), Users.FIELD_USERNAME); 210 g.createRelation(u, userContainsRelationManager).commit(); 211 } 212 } 213 contains = contains.getNextSibling(); 214 } 215 216 } 217 218 219 220 log("Adding rights"); 221 222 for (int i = 0; i < contexts.getLength(); i ++) { 223 Node context = contexts.item(i); 224 org.mmbase.bridge.Node c = getNode(contextManager, context.getAttributes().getNamedItem("name").getNodeValue()); 225 log("found " + context.getChildNodes().getLength() + " for context " + c.getStringValue("name")); 226 Node operation = context.getFirstChild(); 227 while (operation != null) { 228 if (operation instanceof Element && operation.getNodeName().equals("operation")) { 229 String type = operation.getAttributes().getNamedItem("type").getNodeValue(); 230 log("found operation '" + type + "'"); 231 Node grant = operation.getFirstChild(); 232 while (grant != null) { 233 try { 234 if (grant instanceof Element && grant.getNodeName().equals("grant")) { 235 Node groupAttribute = grant.getAttributes().getNamedItem("group"); 236 if (groupAttribute != null && groupAttribute.getNodeValue() != null) { 237 org.mmbase.bridge.Node g = getNode(groupManager, groupAttribute.getNodeValue()); 238 Relation r = c.createRelation(g, groupGrantsRelationManager); 239 r.setStringValue("operation", type); 240 r.commit(); 241 } 242 Node userAttribute = grant.getAttributes().getNamedItem("user"); 243 if (userAttribute != null && userAttribute.getNodeValue() != null) { 244 org.mmbase.bridge.Node u = getNode(userManager, userAttribute.getNodeValue(), Users.FIELD_USERNAME); 245 Relation r = c.createRelation(u, userGrantsRelationManager); 246 r.setStringValue("operation", type); 247 r.commit(); 248 } 249 250 } 251 } catch (Exception e) { 252 log("Ignored " + type + " because " + e.getMessage()); 253 } 254 grant = grant.getNextSibling(); 255 } 256 } 257 try { 258 if (operation instanceof Element && operation.getNodeName().equals("possible")) { 259 org.mmbase.bridge.Node otherContext = getNode(contextManager, operation.getAttributes().getNamedItem("context").getNodeValue()); 260 log("found allowed '" + otherContext.getStringValue("name") + "'"); 261 c.createRelation(otherContext, allowedRelationManager).commit(); 262 } 263 } catch (Exception e) { 264 log("Ignored because " + e.getMessage()); 265 } 266 operation = operation.getNextSibling(); 267 } 268 269 } 270 271 272 273 return document; 274 275 } 276 } 277 | Popular Tags |