09b3ebcfe6a61e009b9daa8a68bab0c578782ac2
[mir.git] / source / mircoders / servlet / ServletModuleUsers.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \r
34 import java.util.Map;\r
35 \r
36 import javax.servlet.http.HttpServletRequest;\r
37 import javax.servlet.http.HttpServletResponse;\r
38 \r
39 import mir.log.LoggerWrapper;\r
40 import mir.module.ModuleException;\r
41 import mir.servlet.*;\r
42 import mir.storage.StorageObjectFailure;\r
43 import mir.util.HTTPRequestParser;\r
44 import mircoders.module.ModuleUsers;\r
45 import mircoders.storage.DatabaseUsers;\r
46 import freemarker.template.SimpleHash;\r
47 \r
48 /*\r
49  *  ServletModuleUsers -\r
50  *  liefert HTML fuer Users\r
51  *\r
52  *\r
53  * @author RK\r
54  */\r
55 \r
56 public class ServletModuleUsers extends ServletModule\r
57 {\r
58   private static ServletModuleUsers instance = new ServletModuleUsers();\r
59   public static ServletModule getInstance() { return instance; }\r
60 \r
61   private ServletModuleUsers() {\r
62     super();\r
63     logger = new LoggerWrapper("ServletModule.Users");\r
64 \r
65     templateListString = configuration.getString("ServletModule.Users.ListTemplate");\r
66     templateObjektString = configuration.getString("ServletModule.Users.ObjektTemplate");\r
67     templateConfirmString = configuration.getString("ServletModule.Users.ConfirmTemplate");\r
68 \r
69     try {\r
70       mainModule = new ModuleUsers(DatabaseUsers.getInstance());\r
71     }\r
72     catch (StorageObjectFailure e) {\r
73       logger.debug("initialization of ServletModuleUsers failed!: " + e.getMessage());\r
74     }\r
75   }\r
76 \r
77   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc\r
78   {\r
79     String idParam = req.getParameter("id");\r
80 \r
81     if (idParam == null)\r
82       throw new ServletModuleExc("ServletModuleUser.edit: invalid call: (id) not specified");\r
83 \r
84     try {\r
85       deliver(req, res, mainModule.getById(idParam), templateObjektString);\r
86     }\r
87     catch (Throwable e) {\r
88       throw new ServletModuleFailure(e);\r
89     }\r
90   }\r
91 \r
92   public void add(HttpServletRequest req, HttpServletResponse res)\r
93       throws ServletModuleExc\r
94   {\r
95     try {\r
96       SimpleHash mergeData = new SimpleHash();\r
97       mergeData.put("new", "1");\r
98       deliver(req, res, mergeData, templateObjektString);\r
99     }\r
100     catch (Throwable e) {\r
101       throw new ServletModuleFailure(e);\r
102     }\r
103   }\r
104 \r
105   public String checkPassword(HTTPRequestParser aRequestParser) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
106   {\r
107     if ( (aRequestParser.getParameter("newpassword") != null &&\r
108           aRequestParser.getParameter("newpassword").length() > 0) ||\r
109         (aRequestParser.getParameter("newpassword2") != null &&\r
110          aRequestParser.getParameter("newpassword2").length() > 0)\r
111         ) {\r
112       String newPassword = aRequestParser.getParameterWithDefault("newpassword", "");\r
113       String newPassword2 = aRequestParser.getParameterWithDefault("newpassword2", "");\r
114 \r
115       if (newPassword.length() == 0 || newPassword2.length() == 0) {\r
116         throw new ServletModuleUserExc("The new password must be entered twice!");\r
117       }\r
118 \r
119       if (!newPassword.equals(newPassword2)) {\r
120         throw new ServletModuleUserExc("New password differes from confirmation");\r
121       }\r
122 \r
123       return newPassword;\r
124     }\r
125     else\r
126       return null;\r
127   }\r
128 \r
129   public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)\r
130       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
131   {\r
132     try {\r
133       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
134       Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
135 \r
136       String newPassword=checkPassword(requestParser);\r
137       if (newPassword!=null)\r
138         withValues.put("password", newPassword);\r
139       else\r
140         throw new ServletModuleUserExc("Password is empty");\r
141 \r
142       String id = mainModule.add(withValues);\r
143       if (requestParser.hasParameter("returnurl"))\r
144         redirect(aResponse, requestParser.getParameter("returnurl"));\r
145       else\r
146         list(aRequest, aResponse);\r
147     }\r
148     catch (Throwable e) {\r
149       throw new ServletModuleFailure(e);\r
150     }\r
151   }\r
152 \r
153   public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
154   {\r
155     try {\r
156       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
157 \r
158       Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
159 \r
160       String newPassword=checkPassword(requestParser);\r
161       if (newPassword!=null)\r
162         withValues.put("password", newPassword);\r
163 \r
164       mainModule.set(withValues);\r
165 \r
166       if (requestParser.hasParameter("returnurl"))\r
167         redirect(aResponse, requestParser.getParameter("returnurl"));\r
168       else\r
169         list(aRequest, aResponse);\r
170     }\r
171     catch (Throwable e) {\r
172       throw new ServletModuleFailure(e);\r
173     }\r
174   }\r
175 \r
176 \r
177 }\r