8c97894e39bcb2437766c5527310b7c903514069
[mir.git] / source / mircoders / module / ModuleUsers.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.module;\r
33 \r
34 import java.util.HashMap;\r
35 import java.util.Map;\r
36 \r
37 import mir.entity.EntityList;\r
38 import mir.log.LoggerWrapper;\r
39 import mir.module.AbstractModule;\r
40 import mir.module.ModuleExc;\r
41 import mir.module.ModuleFailure;\r
42 import mir.storage.StorageObject;\r
43 import mir.util.JDBCStringRoutines;\r
44 import mircoders.entity.EntityUsers;\r
45 import mircoders.global.MirGlobal;\r
46 \r
47 \r
48 /*\r
49  *  Users Module -\r
50  *\r
51  *\r
52  * @author RK\r
53  */\r
54 \r
55 public class ModuleUsers extends AbstractModule\r
56 {\r
57   static LoggerWrapper logger = new LoggerWrapper("Module.Users");\r
58 \r
59   public ModuleUsers(StorageObject theStorage)\r
60   {\r
61     if (theStorage == null)\r
62       logger.warn("ModuleUsers(): StorageObject was null!");\r
63 \r
64     this.theStorage = theStorage;\r
65   }\r
66 \r
67   /**\r
68    * Authenticate and lookup a user\r
69    *\r
70    * @param user              The user to lookup\r
71    * @param password          The password\r
72    * @return                  The authenticated user, or <code>null</code> if the user\r
73    *                          doesn't exist, or the supplied password is invalid.\r
74    * @throws ModuleException\r
75    */\r
76 \r
77   public EntityUsers getUserForLogin(String user, String password) throws ModuleExc, ModuleFailure {\r
78     try {\r
79       String whereString =\r
80           "login='" + JDBCStringRoutines.escapeStringLiteral(user) + "' " +\r
81           "and password='" + JDBCStringRoutines.escapeStringLiteral(\r
82           MirGlobal.localizer().adminInterface().makePasswordDigest(password)) +\r
83           "' " +\r
84           "and is_admin='1'";\r
85 \r
86       EntityList userList = getByWhereClause(whereString, -1);\r
87 \r
88       if (userList != null && userList.getCount() == 1)\r
89         return (EntityUsers) userList.elementAt(0);\r
90       else\r
91         return null;\r
92     }\r
93     catch (Throwable t) {\r
94       throw new ModuleFailure(t);\r
95     }\r
96   }\r
97 \r
98   private Map digestPassword(Map aValues) throws ModuleExc, ModuleFailure {\r
99     Map result = aValues;\r
100 \r
101     try {\r
102       if (aValues.containsKey("password")) {\r
103         result = new HashMap();\r
104         result.putAll(aValues);\r
105         result.put("password",\r
106             MirGlobal.localizer().adminInterface().\r
107             makePasswordDigest( (String) aValues.get("password")));\r
108       }\r
109     }\r
110     catch (Throwable t) {\r
111       throw new ModuleFailure("ModuleUsers.add: " + t.getMessage(), t);\r
112     }\r
113 \r
114     return result;\r
115   }\r
116 \r
117   public String add (Map theValues) throws ModuleExc, ModuleFailure {\r
118     try {\r
119       return super.add(digestPassword(theValues));\r
120     }\r
121     catch (Throwable t) {\r
122       throw new ModuleFailure(t);\r
123     }\r
124   }\r
125 \r
126   /**\r
127    * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren\r
128    * @param theValues Hash mit Spalte/Wert-Paaren\r
129    * @return Id des eingef?gten Objekts\r
130    * @exception ModuleException\r
131    */\r
132   public String set (Map theValues) throws ModuleExc, ModuleFailure {\r
133     try {\r
134       return super.set(digestPassword(theValues));\r
135     }\r
136     catch (Throwable t) {\r
137       throw new ModuleFailure(t);\r
138     }\r
139   }\r
140 }