- Improved user edit support
[mir.git] / source / mircoders / servlet / ServletModuleUsers.java
index 15b455f..b7506df 100755 (executable)
-/*
- * Copyright (C) 2001, 2002  The Mir-coders group
- *
- * This file is part of Mir.
- *
- * Mir is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Mir is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Mir; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- *
- * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
- */
-
-package mircoders.servlet;
-
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import mir.log.LoggerWrapper;
-import mir.module.ModuleException;
-import mir.servlet.ServletModule;
-import mir.servlet.ServletModuleException;
-import mir.storage.StorageObjectFailure;
-import mircoders.module.ModuleUsers;
-import mircoders.storage.DatabaseUsers;
-import freemarker.template.SimpleHash;
-
-/*
- *  ServletModuleUsers -
- *  liefert HTML fuer Users
- *
- *
- * @author RK
- */
-
-public class ServletModuleUsers extends mir.servlet.ServletModule
-{
-  private static ServletModuleUsers instance = new ServletModuleUsers();
-  public static ServletModule getInstance() { return instance; }
-
-  private ServletModuleUsers() {
-    super();
-    logger = new LoggerWrapper("ServletModule.Users");
-
-    templateListString = configuration.getString("ServletModule.Users.ListTemplate");
-    templateObjektString = configuration.getString("ServletModule.Users.ObjektTemplate");
-    templateConfirmString = configuration.getString("ServletModule.Users.ConfirmTemplate");
-
-    try {
-      mainModule = new ModuleUsers(DatabaseUsers.getInstance());
-    }
-    catch (StorageObjectFailure e) {
-      logger.debug("initialization of ServletModuleUsers failed!: " + e.getMessage());
-    }
-  }
-
-  public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
-  {
-    String idParam = req.getParameter("id");
-
-    if (idParam == null)
-      throw new ServletModuleException("ServletModuleUser.edit: invalid call: (id) not specified");
-
-    try {
-      deliver(req, res, mainModule.getById(idParam), templateObjektString);
-    }
-    catch (ModuleException e) {
-      throw new ServletModuleException(e.toString());
-    }
-  }
-
-  public void add(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException
-  {
-    try {
-      SimpleHash mergeData = new SimpleHash();
-      mergeData.put("new", "1");
-      deliver(req, res, mergeData, templateObjektString);
-    }
-    catch (Exception e) { throw new ServletModuleException(e.toString());}
-  }
-
-  public void insert(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException
-  {
-    try {
-      Map withValues = getIntersectingValues(req, mainModule.getStorageObject());
-      String id = mainModule.add(withValues);
-      deliver(req, res, mainModule.getById(id), templateObjektString);
-    }
-    catch (Exception e) { throw new ServletModuleException(e.toString());}
-  }
-}
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.servlet;\r
+\r
+import java.util.Map;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import freemarker.template.SimpleHash;\r
+\r
+import mir.log.LoggerWrapper;\r
+import mir.module.ModuleException;\r
+import mir.servlet.ServletModule;\r
+import mir.servlet.ServletModuleException;\r
+import mir.servlet.ServletModuleUserException;\r
+import mir.storage.StorageObjectFailure;\r
+import mir.util.HTTPRequestParser;\r
+\r
+import mircoders.module.ModuleUsers;\r
+import mircoders.storage.DatabaseUsers;\r
+\r
+/*\r
+ *  ServletModuleUsers -\r
+ *  liefert HTML fuer Users\r
+ *\r
+ *\r
+ * @author RK\r
+ */\r
+\r
+public class ServletModuleUsers extends ServletModule\r
+{\r
+  private static ServletModuleUsers instance = new ServletModuleUsers();\r
+  public static ServletModule getInstance() { return instance; }\r
+\r
+  private ServletModuleUsers() {\r
+    super();\r
+    logger = new LoggerWrapper("ServletModule.Users");\r
+\r
+    templateListString = configuration.getString("ServletModule.Users.ListTemplate");\r
+    templateObjektString = configuration.getString("ServletModule.Users.ObjektTemplate");\r
+    templateConfirmString = configuration.getString("ServletModule.Users.ConfirmTemplate");\r
+\r
+    try {\r
+      mainModule = new ModuleUsers(DatabaseUsers.getInstance());\r
+    }\r
+    catch (StorageObjectFailure e) {\r
+      logger.debug("initialization of ServletModuleUsers failed!: " + e.getMessage());\r
+    }\r
+  }\r
+\r
+  public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException\r
+  {\r
+    String idParam = req.getParameter("id");\r
+\r
+    if (idParam == null)\r
+      throw new ServletModuleException("ServletModuleUser.edit: invalid call: (id) not specified");\r
+\r
+    try {\r
+      deliver(req, res, mainModule.getById(idParam), templateObjektString);\r
+    }\r
+    catch (ModuleException e) {\r
+      throw new ServletModuleException(e.toString());\r
+    }\r
+  }\r
+\r
+  public void add(HttpServletRequest req, HttpServletResponse res)\r
+      throws ServletModuleException\r
+  {\r
+    try {\r
+      SimpleHash mergeData = new SimpleHash();\r
+      mergeData.put("new", "1");\r
+      deliver(req, res, mergeData, templateObjektString);\r
+    }\r
+    catch (Exception e) { throw new ServletModuleException(e.toString());}\r
+  }\r
+\r
+  public String checkPassword(HTTPRequestParser aRequestParser) throws ServletModuleException, ServletModuleUserException\r
+  {\r
+    if ( (aRequestParser.getParameter("newpassword") != null &&\r
+          aRequestParser.getParameter("newpassword").length() > 0) ||\r
+        (aRequestParser.getParameter("newpassword2") != null &&\r
+         aRequestParser.getParameter("newpassword2").length() > 0)\r
+        ) {\r
+      String newPassword = aRequestParser.getParameterWithDefault("newpassword", "");\r
+      String newPassword2 = aRequestParser.getParameterWithDefault("newpassword2", "");\r
+\r
+      if (newPassword.length() == 0 || newPassword2.length() == 0) {\r
+        throw new ServletModuleUserException(\r
+            "The new password must be entered twice!");\r
+      }\r
+\r
+      if (!newPassword.equals(newPassword2)) {\r
+        throw new ServletModuleUserException(\r
+            "New password differes from confirmation");\r
+      }\r
+\r
+      return newPassword;\r
+    }\r
+    else\r
+      return null;\r
+  }\r
+\r
+  public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)\r
+      throws ServletModuleException\r
+  {\r
+    try {\r
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
+      Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
+\r
+      String newPassword=checkPassword(requestParser);\r
+      if (newPassword!=null)\r
+        withValues.put("password", newPassword);\r
+      else\r
+        throw new ServletModuleUserException("Password is empty");\r
+\r
+      String id = mainModule.add(withValues);\r
+      if (requestParser.hasParameter("returnurl"))\r
+        redirect(aResponse, requestParser.getParameter("returnurl"));\r
+      else\r
+        list(aRequest, aResponse);\r
+    }\r
+    catch (Exception e) {\r
+      throw new ServletModuleException(e.toString());\r
+    }\r
+  }\r
+\r
+  public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException, ServletModuleUserException\r
+  {\r
+    try {\r
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
+\r
+      Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
+\r
+      String newPassword=checkPassword(requestParser);\r
+      if (newPassword!=null)\r
+        withValues.put("password", newPassword);\r
+\r
+      mainModule.set(withValues);\r
+\r
+      if (requestParser.hasParameter("returnurl"))\r
+        redirect(aResponse, requestParser.getParameter("returnurl"));\r
+      else\r
+        list(aRequest, aResponse);\r
+    }\r
+    catch (Throwable t) {\r
+      throw new ServletModuleException("ServletModuleUsers: " + t.getMessage());\r
+    }\r
+\r
+  }\r
+\r
+\r
+}\r