1 10 11 package org.mmbase.module; 12 13 import java.util.*; 14 15 import org.mmbase.module.core.*; 16 import org.mmbase.module.builders.Properties; 17 import org.mmbase.util.*; 18 19 import org.mmbase.util.logging.Logger; 20 import org.mmbase.util.logging.Logging; 21 22 29 public class MMUsers extends ProcessorModule { 30 31 private static Logger log = Logging.getLoggerInstance(MMUsers.class.getName()); 32 33 private PasswordGeneratorInterface pwgen = new PasswordGenerator (); 34 private MMBase mmbase; 35 private sessionsInterface sessions; 36 private SendMailInterface sendMail; 37 38 private MMObjectBuilder users; private Properties properties; 40 41 private String emailFrom; 43 private String emailReturnPath; 44 private String emailSubject; 45 private String emailBodyBegin; 46 private String emailBodyLogin; 47 private String emailBodyPasswd; 48 private String emailBodyEnd; 49 50 private String error (String msg) { 51 log.error(msg); 52 return "Error-> " + msg; 53 } 54 55 private String cleanupString(String str) { return str; } 56 57 private static String excludedKeys = ";LID;FIRSTNAME;LASTNAME;EMAIL;"; 59 60 private void clearSessionInfo (scanpage sp) { 61 if (sessions==null) sessions = (sessionsInterface)getModule("SESSION"); 62 63 String sid = sp.getSessionName(); 64 sessionInfo info = sessions.getSession (sp, sp.sname); 65 String userid = sessions.getValue (info, "USERNUMBER"); 66 67 if (log.isDebugEnabled()) { 68 log.debug("Clearing session-info of user " + userid); 69 } 70 if (userid!=null) { 71 Hashtable has = getUserProperties (userid); 72 if (has != null) { 73 for (Enumeration e = has.elements (); e.hasMoreElements (); ) { 74 MMObjectNode node = (MMObjectNode)e.nextElement (); 75 String key = node.getStringValue ("key"); 76 if ((key != null) && (excludedKeys.indexOf(";"+key+";")<0)) { 77 if (log.isDebugEnabled()) { 78 log.debug ("Clearing key '" + key + "'"); 79 } 80 sessions.setValue (info, key, ""); 81 } 82 } 83 } 84 sessions.forgetSession( userid ); 85 } 86 deleteSessionSIDs( sid ); 87 } 88 89 101 public String createLogin (String loginname, StringTokenizer tokens, scanpage sp) 102 { 103 String address = getRemainingTokens( tokens ); 104 if (address.equals("")) 105 return error ("CREATELOGIN-" + loginname + " needs an e-mail address"); 106 107 loginname=cleanupString(loginname); 109 address=cleanupString(address); 110 111 String rtn="",res; 112 113 res=doSearchUserNumber(loginname); 115 if (res.length()==0) { 116 res=doSearchUserNumber(address); 117 if (res.length()==0) { 118 119 clearSessionInfo (sp); 125 126 String password = pwgen.getPassword (); 127 int userid = createNewUser (sp, loginname, password, address); 128 129 if (userid < 0) log.error("Couldn't create a new user!"); 130 else sendConfirmationEMail (address, loginname, password); 131 rtn = rtn+userid; 132 } else { 133 rtn="EMAIL"; 134 if (log.isDebugEnabled()) { 135 log.debug("Email address is already in use"); 136 } 137 } 138 } else { 139 rtn="USER"; 140 if (log.isDebugEnabled()) { 141 log.debug("Username is already in use"); 142 } 143 } 144 return rtn; 145 } 146 147 private int createNewUser (scanpage sp, String ln, String pw, String ad) { 148 if (users == null) users = mmbase.getMMObject ("users"); 149 if (sessions==null) sessions = (sessionsInterface)getModule("SESSION"); 150 String sid = sp.getSessionName(); 151 MMObjectNode usernode = users.getNewNode ("system"); 153 if (usernode == null) return -1; 154 usernode.setValue ("description", "created for SID = " + sid); 155 int id = users.insert ("system", usernode); 156 if (log.isServiceEnabled()) log.service("Created new user " + id + ", SID=" + sid); 157 158 sessionInfo info = sessions.getSession (sp, sp.sname); 160 sessions.setValue (info, "USERNUMBER", "" + id); 161 sessions.setValue (info, "PASSWD", pw); 162 sessions.setValue (info, "LID", ln); 163 164 storeValue ("" + id, "SID", sid); 165 storeValue ("" + id, "PASSWD", pw); 166 storeValue ("" + id, "LID", ln); 167 168 return id; 169 } 170 171 private boolean deleteSIDs(String usernumber) { 172 if (properties == null) properties = (Properties)mmbase.getMMObject("properties"); 173 Enumeration e = properties.search("parent=E"+usernumber+" + key=='SID'"); 174 while (e.hasMoreElements()) { 175 MMObjectNode node = (MMObjectNode) e.nextElement(); 176 properties.removeNode(node); 178 } 179 return true; 180 } 181 182 private boolean deleteSessionSIDs(String sessionname) { 183 if (properties == null) properties = (Properties)mmbase.getMMObject("properties"); 184 Enumeration e = properties.search("key=='SID'+ value=='"+sessionname+"'"); 185 while (e.hasMoreElements()) { 186 MMObjectNode node = (MMObjectNode)e.nextElement(); 187 properties.removeNode(node); 189 } 190 return true; 191 } 192 193 public String doSearchUserNumber(String value) { 194 if (properties == null) properties = (Properties)mmbase.getMMObject("properties"); 195 Enumeration e = properties.search("key=='LID' + value=='"+value+"'"); 196 if (e.hasMoreElements()) { 197 MMObjectNode node = (MMObjectNode) e.nextElement(); 198 return node.getStringValue("parent"); 199 } 200 201 e = properties.search("key=='EMAIL' + value=='"+value+"'"); 202 if (e.hasMoreElements()) { 203 MMObjectNode node = (MMObjectNode) e.nextElement(); 204 return node.getStringValue("parent"); 205 } 206 return ""; 207 208 209 } 210 211 private String getRemainingTokens( StringTokenizer tok ) 212 { 213 if (!tok.hasMoreTokens()) 214 return ""; 215 return addRemainingTokens( tok.nextToken(), tok); 216 } 217 218 private String addRemainingTokens( String s, StringTokenizer tok ) 219 { 220 while (tok.hasMoreTokens()) { s += "-"+tok.nextToken(); } 221 return s; 222 } 223 224 225 public void init () { 226 super.init (); 227 mmbase = (MMBase)getModule ("MMBASEROOT"); 228 229 } 230 231 234 private String storeValue (String userid, String key, String value) { 235 if (users == null) users = mmbase.getMMObject("users"); 236 if (properties == null) properties = (Properties)mmbase.getMMObject("properties"); 237 238 MMObjectNode usernode = users.getNode (userid); 239 240 if (usernode != null) { 241 MMObjectNode pnode = usernode.getProperty (key); 242 243 if (pnode == null) { 244 pnode = properties.getNewNode ("system"); 245 pnode.setValue ("parent", Integer.parseInt (userid)); 246 pnode.setValue ("key", key); 247 pnode.setValue ("value", value); 248 pnode.setValue ("ptype", "string"); 249 properties.insert ("system", pnode); 250 usernode.delPropertiesCache(); 251 if (log.isDebugEnabled()) { 252 log.debug ("storeValue() storing '" + value + "' in '" + key + "' for user "+userid); 253 } 254 255 } 256 else 257 { 258 pnode.setValue ("value", value); 259 pnode.setValue ("ptype", "string"); 260 pnode.commit(); 261 if (log.isDebugEnabled()) { 262 log.debug ("storeValue() overwriting '" + value + "' in '" + key + "' for user "+userid); 263 } 264 } 265 } 266 else { 267 return error ("MOD-MMUSERS-" + userid + "-PUT has invalid user-id"); 268 } 269 return ""; 270 } 271 272 273 277 public Vector getList(scanpage sp, StringTagger tagger, String value) throws ParseException{ 278 StringTokenizer tokens = new StringTokenizer (value, "-\n\r"); 279 String tok = tokens.nextToken (); 280 Vector res = new Vector (); 281 282 if (tok.equals ("GETPROPERTY") && tokens.hasMoreTokens ()) { 283 tok = tokens.nextToken (); 284 285 if (tok.equals ("PARENT") && tokens.hasMoreTokens ()) { 286 String userid = tokens.nextToken (); 287 Hashtable props = getUserProperties (userid); 288 Vector fields = tagger.Values ("FIELD"); 289 String wanted = (String )fields.firstElement (); 290 291 res = wanted.equals ("*") 292 ? getListAll (props, tagger) 293 : getListSelection (props, fields, tagger); 294 } 295 else { 296 log.warn("MMUsers can't get property '" + tok + "'"); 297 } 298 } 299 else { 300 log.warn("MMUsers can't list '" + value + "'"); 301 } 302 303 return res; 304 } 305 306 private Vector getListAll (Hashtable props, StringTagger tagger) { 307 Vector v = new Vector (); 308 309 for (Enumeration p = props.elements (); p.hasMoreElements (); ) { 311 MMObjectNode n = (MMObjectNode)p.nextElement (); 312 String key = (String )n.getStringValue ("key"); 313 String type = (String )n.getStringValue ("ptype"); 314 315 v.addElement (key); 316 317 if (type.equals ("string")) v.addElement ((String )n.getStringValue ("value")); 318 else v.addElement ("<NON-PRINTABLE>"); 320 } 321 322 tagger.setValue ("ITEMS", "2"); 324 return v; 325 } 326 327 private Vector getListSelection (Hashtable props, Vector fields, StringTagger tagger) 328 { 329 Vector v = new Vector (); 330 331 for (Enumeration field = fields.elements (); field.hasMoreElements (); ) { 333 String f = (String )field.nextElement (); 334 MMObjectNode node = (MMObjectNode)props.get (f); 335 336 if (node != null) { 337 String type = node.getStringValue ("ptype"); 338 339 if (type.equals ("string")) v.addElement ((String )node.getStringValue("value")); 340 else v.addElement ("<NON-PRINTABLE>"); 342 } 343 } 344 345 tagger.setValue ("ITEMS", "" + fields.size ()); 346 347 return v; 348 } 349 350 353 public String replace (scanpage sp, String command) { 354 StringTokenizer tokens = new StringTokenizer (command, "-\n\r"); 355 356 if (tokens.hasMoreTokens ()) { 357 String param1 = tokens.nextToken (); 358 359 if (tokens.hasMoreTokens ()) { 360 String param2 = tokens.nextToken (); 361 if (param2.equals ("GET")) return replaceGet(param1, tokens); 362 else if (param2.equals ("PUT")) return replacePut(param1, tokens); 363 else if (param1.equals ("SWITCH")) return replaceSwitch(sp, param2); 364 else if (param1.equals ("SEARCHUSERNUMBER")) 365 return doSearchUserNumber(addRemainingTokens(param2, tokens)); 366 else if (param1.equals ("CREATELOGIN")) return createLogin (param2, tokens, sp); 367 368 else return error ("MMUSERS-" + param1 + " has illegal parameter " + param2 + " following"); 370 } 371 else return error ("MMUSERS-" + param1 + " missing parameter"); 372 } 373 else return error ("MMUSERS missing parameter"); 374 } 375 376 private void sendConfirmationEMail (String address, String loginname, String password) 377 { 378 379 emailFrom = getInitParameter("from"); 381 if (emailFrom == null || emailFrom.equals("")) 382 log.debug ("missing init param from"); 383 if (emailFrom.equals("youremail@yourcompany.nl")) 384 log.debug ("from init parameter is still default, please change!!!!"); 385 emailReturnPath = getInitParameter("returnpath"); 386 if (emailReturnPath == null || emailReturnPath.equals("")) 387 log.debug (" missing init param returnpath"); 388 if (emailReturnPath.equals("youremail@yourcompany.nl")) 389 log.debug (" returnpath init parameter is still default, please change!!!!"); 390 emailSubject = getInitParameter("subject"); 391 if (emailSubject == null || emailSubject.equals("")) 392 log.debug ("missing init param subject"); 393 emailBodyBegin = getInitParameter("bodybegin"); 394 if (emailBodyBegin == null || emailBodyBegin.equals("")) 395 log.debug ("missing init param bodybegin"); 396 emailBodyLogin = getInitParameter("bodylogin"); 397 if (emailBodyLogin == null || emailBodyBegin.equals("")) 398 log.debug ("missing init param bodylogin"); 399 emailBodyPasswd = getInitParameter("bodypasswd"); 400 if (emailBodyPasswd == null || emailBodyPasswd.equals("")) 401 log.debug ("missing init param bodypasswd"); 402 emailBodyEnd = getInitParameter("bodyend"); 403 if (emailBodyEnd == null || emailBodyEnd.equals("")) 404 log.debug ("missing init param bodyend"); 405 406 if (log.isServiceEnabled()) { 407 log.service("Sending e-mail to " + address + " concerning login '" + loginname + "' and password '" + password + "'"); 408 } 409 if (sendMail == null) sendMail = (SendMailInterface)getModule("sendmail"); 410 if (sendMail == null) { 411 log.error("MMUsers ERROR CANNOT GET MODULE SendMail!"); 412 log.error("MMUsers could not send mail to " + address + " for login name " + loginname 413 +" with password "+ password); 414 return; 415 } 416 417 Hashtable headers = new Hashtable( 3 ); 418 419 headers.put("From", emailFrom); 420 headers.put("Return-path", emailReturnPath); 421 headers.put("Subject", emailSubject); 422 String body = emailBodyBegin + "\n\n" + emailBodyLogin + loginname + "\n" + emailBodyPasswd + password + "\n\n" + emailBodyEnd; 423 sendMail.sendMail(emailFrom, address, body, headers); 424 425 426 } 427 428 431 private Hashtable getUserProperties (String userid) { 432 if (users == null) users = mmbase.getMMObject ("users"); 434 435 MMObjectNode usernode = (MMObjectNode)users.getNode(userid); 437 438 if (usernode!=null) { 439 Hashtable properties = usernode.getProperties(); 440 return properties; 441 } else { 442 log.warn("MMUsers -> getUserProperties not a valid user = " + userid); 443 return null; 444 } 445 } 446 447 private String replaceGet (String userid, StringTokenizer tokens) { 448 String res = ""; 449 450 if (!tokens.hasMoreTokens()) 451 return error("MOD-MMUSERS-" + userid + "-GET missing KEY param"); 452 String key = tokens.nextToken(); 453 if (users==null) users = mmbase.getMMObject ("users"); 455 MMObjectNode usernode=users.getNode(userid); 457 if (usernode == null) 458 return error("MOD-MMUSERS-" + userid + "-GET: usernode not found"); 459 460 usernode.getProperties(); 461 MMObjectNode pnode=usernode.getProperty(key); 463 if (pnode == null) 464 { 465 if (log.isDebugEnabled()) { 466 log.debug( "MOD-MMUSERS-" + userid + "-GET-"+key+": key not found"); 467 } 468 return ""; 469 } 470 String type = pnode.getStringValue ("ptype"); 471 String value = pnode.getStringValue ("value"); 472 if (type.equals ("string")) res = value; else res = "<NON-PRINTABLE>"; 473 if (log.isDebugEnabled()) { 474 log.debug( "" + userid + " " + res); 475 } 476 return res; 477 } 478 479 private String replacePut (String userid, StringTokenizer tokens) { 480 if (!tokens.hasMoreTokens ()) 481 return error ("MOD-MMUSERS-" + userid + "PUT/ADD missing KEY"); 482 483 String key = tokens.nextToken (); 484 String value = getRemainingTokens( tokens ); 485 return storeValue (userid, key, value); 486 } 487 488 495 private String replaceSwitch(scanpage sp, String newUserID) { 496 if (sessions==null) sessions = (sessionsInterface)getModule("SESSION"); 497 498 String sid = sp.getSessionName(); 500 sessionInfo info = sessions.getSession (sp,sp.sname); 502 String oldUserID = sessions.getValue (info, "USERNUMBER"); 503 504 if (log.isDebugEnabled()) { 505 log.debug("Switch Info from " + oldUserID +" to "+newUserID); 506 } 507 508 if (!newUserID.equals(oldUserID)) 509 clearSessionInfo( sp ); 510 511 deleteSIDs( newUserID ); 513 514 storeValue( newUserID, "SID", sid ); 516 sessions.setValue(info,"USERNUMBER",newUserID); 518 519 Hashtable has2=getUserProperties(newUserID); 520 if (has2 == null) { 521 log.warn("SWITCH: newuser " + newUserID + " has no properties!"); 522 return ""; 523 } 524 for (Enumeration e = has2.elements(); e.hasMoreElements (); ) { 525 sessions.setValueFromNode( info, (MMObjectNode)e.nextElement()); 526 } 527 return ""; 528 } 529 530 532 public String getModuleInfo () { 533 return "Support routines for MMUsers, by Daniel Ockeloen & Arjan Houtman"; 534 } 535 536 public void reload () { 537 mmbase = (MMBase)getModule ("MMBASEROOT"); 538 } 539 540 } 541 542 | Popular Tags |