commit | author | age
|
6240cd
|
1 |
/* |
51c9e3
|
2 |
* Nutzerverwaltung - User and role management in your browser |
U |
3 |
* Copyright (C) 2011-2016 Ulrich Hilger, http://uhilger.de |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
|
8 |
* (at your option) any later version. |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program. If not, see http://www.gnu.org/licenses/ |
6240cd
|
17 |
*/ |
51c9e3
|
18 |
|
c65695
|
19 |
package de.uhilger.um.api; |
U |
20 |
|
|
21 |
import de.uhilger.baselink.GenericRecord; |
4f4b29
|
22 |
import de.uhilger.baselink.PersistenceManager; |
c65695
|
23 |
import de.uhilger.baselink.Record; |
1fc020
|
24 |
import de.uhilger.transit.web.WebKontext; |
6240cd
|
25 |
import de.uhilger.um.Digester; |
c65695
|
26 |
import de.uhilger.um.daten.User; |
87e382
|
27 |
import de.uhilger.um.daten.UserRole; |
4f4b29
|
28 |
import java.sql.Connection; |
725d10
|
29 |
import java.util.List; |
1fc020
|
30 |
import java.util.Properties; |
U |
31 |
import javax.servlet.ServletContext; |
c65695
|
32 |
|
U |
33 |
/** |
1fc020
|
34 |
* <p>Die Methoden der Klasse UserMgr sind |
U |
35 |
* die 'Fassade', die über Transit als Programmschnittstelle |
|
36 |
* (Application Programming Interface, API) |
|
37 |
* nach außen bereitgestellt wird. UserMgr dient weitgehend |
|
38 |
* als 'Durchreiche' für die Funktionalität anderer Klassen.</p> |
59f8b3
|
39 |
* |
1fc020
|
40 |
* <p> |
U |
41 |
* Der UserMgr erhält die benötigten Methoden aus dem ServletContext. |
|
42 |
* Auf diese Weise sind keine statischen Abhängigkeiten in den Code |
|
43 |
* gewandert. Die folgenden Elemente werden vom UserMgr im ServletContext |
|
44 |
* erwartet: |
c43d2a
|
45 |
* <ul> |
U |
46 |
* <li>PersistenceManager</li> |
|
47 |
* <li>SQL-Properties</li> |
|
48 |
* <li>Digester</li> |
1fc020
|
49 |
* </p> |
627850
|
50 |
* |
6240cd
|
51 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
U |
52 |
* @author Published under the terms and conditions of the |
|
53 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
54 |
* General Public License</a> |
|
55 |
* |
|
56 |
* @version 2, December 27, 2016 |
c65695
|
57 |
*/ |
1fc020
|
58 |
public class UserMgr implements WebKontext { |
U |
59 |
|
c43d2a
|
60 |
/** Zeiger zum Servlet-Kontext dieser Anwendung */ |
1fc020
|
61 |
private ServletContext ctx; |
U |
62 |
|
|
63 |
/** Name, unter dem das Properties-Objekt mit den SQL-Befehlen im ServletContext hinterlegt ist */ |
|
64 |
public static final String UM_SQL_PROPERTIES = "umSqlProperties"; |
|
65 |
|
c43d2a
|
66 |
/** Name, unter dem das Digester-Objekt im ServletContext hinterlegt ist */ |
35c04d
|
67 |
public static final String P_DIGESTER = "digester"; |
U |
68 |
|
1fc020
|
69 |
/** Name, unter dem das Zugriffsobjekt zur Datenbank im ServletContext hinterlegt ist */ |
U |
70 |
public static final String UM_DB = "umDb"; |
c65695
|
71 |
|
35c04d
|
72 |
/** Boolean-Konstante zur Kennzeichnung von Datenbankergebnissen ohne Blobs */ |
U |
73 |
public static final boolean WITHOUT_BLOBS = false; |
|
74 |
|
c43d2a
|
75 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer */ |
725d10
|
76 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
c43d2a
|
77 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer-Namen */ |
725d10
|
78 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
c43d2a
|
79 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen */ |
87e382
|
80 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c43d2a
|
81 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen eines Benutzers */ |
c79c12
|
82 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
c43d2a
|
83 |
/** Referenz zum SQL-Befehl zum Loeschen aller Rollen eines Nutzers */ |
4f4b29
|
84 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
85 |
|
c43d2a
|
86 |
/** Mapper-Objekt fuer Benutzer */ |
U |
87 |
private static final Record UserMapper = new GenericRecord(User.class); |
|
88 |
/** Mapper-Objekt fuer Benutzerrollen */ |
|
89 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
|
90 |
|
be9fa2
|
91 |
/* ----------- Benutzer -------------- */ |
U |
92 |
|
35c04d
|
93 |
public User createUser(User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
c65695
|
94 |
String kw = user.getPw(); |
35c04d
|
95 |
String digesterClassName = ctx.getInitParameter(P_DIGESTER); |
U |
96 |
Digester digester = (Digester) Class.forName(digesterClassName).newInstance(); |
|
97 |
String digestedPw = digester.digest(kw, Digester.MD5, null); |
c65695
|
98 |
user.setPw(digestedPw); |
35c04d
|
99 |
getDb().insert(user, UserMapper); |
c65695
|
100 |
return user; |
U |
101 |
} |
|
102 |
|
725d10
|
103 |
public List getUserNameList() { |
35c04d
|
104 |
return getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS); |
725d10
|
105 |
} |
U |
106 |
|
72c5c3
|
107 |
public User deleteUser(User user) { |
35c04d
|
108 |
PersistenceManager pm = getDb(); |
4f4b29
|
109 |
Connection c = pm.getConnection(); |
U |
110 |
pm.startTransaction(c); |
35c04d
|
111 |
pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId()); |
4f4b29
|
112 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
U |
113 |
pm.commit(c); |
|
114 |
return deletedUser; |
72c5c3
|
115 |
} |
627850
|
116 |
|
be9fa2
|
117 |
/* ------------ Rollen ------------------ */ |
U |
118 |
|
ccefc8
|
119 |
public UserRole grantRole(UserRole role) { |
U |
120 |
getDb().insert(role, UserRoleMapper); |
|
121 |
return role; |
be9fa2
|
122 |
} |
U |
123 |
|
ccefc8
|
124 |
public UserRole revokeRole(UserRole role) { |
U |
125 |
getDb().delete(role, UserRoleMapper); |
|
126 |
return role; |
be9fa2
|
127 |
} |
U |
128 |
|
|
129 |
public List getRoleNamesGranted() { |
35c04d
|
130 |
return getDb().select(getSql(SQL_GET_ROLE_LIST), WITHOUT_BLOBS); |
be9fa2
|
131 |
} |
U |
132 |
|
c79c12
|
133 |
public List getUserRoleNames(String userId) { |
35c04d
|
134 |
return getDb().select(getSql(SQL_GET_USER_ROLES), WITHOUT_BLOBS, userId); |
f8b605
|
135 |
} |
U |
136 |
|
1fc020
|
137 |
/* ----------- Helfer ---- */ |
U |
138 |
|
|
139 |
/** |
|
140 |
* Ein benanntes SQL-Kommando ermitteln |
|
141 |
* @param id Name des gewuenschten SQL-Kommandos |
|
142 |
* @return das SQL-Kommando mit der in id angegebenen Bezeichnung |
|
143 |
*/ |
35c04d
|
144 |
private String getSql(String id) { |
1fc020
|
145 |
Properties sql = (Properties) ctx.getAttribute(UserMgr.UM_SQL_PROPERTIES); |
U |
146 |
return sql.getProperty(id); |
|
147 |
} |
|
148 |
|
35c04d
|
149 |
private PersistenceManager getDb() { |
U |
150 |
return (PersistenceManager) ctx.getAttribute(UM_DB); |
|
151 |
} |
|
152 |
|
1fc020
|
153 |
/* ------------- Implementierung WebKontext ------------- */ |
U |
154 |
|
|
155 |
@Override |
|
156 |
public ServletContext getServletContext() { |
|
157 |
return ctx; |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public void setServletContext(ServletContext servletContext) { |
|
162 |
this.ctx = servletContext; |
|
163 |
} |
|
164 |
|
|
165 |
|
c65695
|
166 |
} |