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