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: |
|
45 |
* |
|
46 |
* [noch auspraegen] |
|
47 |
* |
|
48 |
* PersistenceManager |
|
49 |
* SQL-Properties |
|
50 |
* Digester |
|
51 |
* </p> |
627850
|
52 |
* |
6240cd
|
53 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
U |
54 |
* @author Published under the terms and conditions of the |
|
55 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
56 |
* General Public License</a> |
|
57 |
* |
|
58 |
* @version 2, December 27, 2016 |
c65695
|
59 |
*/ |
1fc020
|
60 |
public class UserMgr implements WebKontext { |
U |
61 |
|
|
62 |
private ServletContext ctx; |
c65695
|
63 |
|
U |
64 |
private static final Record UserMapper = new GenericRecord(User.class); |
87e382
|
65 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
1fc020
|
66 |
|
U |
67 |
/** Name, unter dem das Properties-Objekt mit den SQL-Befehlen im ServletContext hinterlegt ist */ |
|
68 |
public static final String UM_SQL_PROPERTIES = "umSqlProperties"; |
|
69 |
|
35c04d
|
70 |
public static final String P_DIGESTER = "digester"; |
U |
71 |
|
1fc020
|
72 |
/** Name, unter dem das Zugriffsobjekt zur Datenbank im ServletContext hinterlegt ist */ |
U |
73 |
public static final String UM_DB = "umDb"; |
c65695
|
74 |
|
35c04d
|
75 |
/** Boolean-Konstante zur Kennzeichnung von Datenbankergebnissen ohne Blobs */ |
U |
76 |
public static final boolean WITHOUT_BLOBS = false; |
|
77 |
|
725d10
|
78 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
U |
79 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
87e382
|
80 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c79c12
|
81 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
72c5c3
|
82 |
public static final String SQL_DELETE_USER = "deleteUser"; |
4f4b29
|
83 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
84 |
|
be9fa2
|
85 |
/* ----------- Benutzer -------------- */ |
U |
86 |
|
35c04d
|
87 |
public User createUser(User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
c65695
|
88 |
String kw = user.getPw(); |
35c04d
|
89 |
String digesterClassName = ctx.getInitParameter(P_DIGESTER); |
U |
90 |
Digester digester = (Digester) Class.forName(digesterClassName).newInstance(); |
|
91 |
String digestedPw = digester.digest(kw, Digester.MD5, null); |
c65695
|
92 |
user.setPw(digestedPw); |
35c04d
|
93 |
getDb().insert(user, UserMapper); |
c65695
|
94 |
return user; |
U |
95 |
} |
|
96 |
|
725d10
|
97 |
public List getUserNameList() { |
35c04d
|
98 |
return getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS); |
725d10
|
99 |
} |
U |
100 |
|
72c5c3
|
101 |
public User deleteUser(User user) { |
35c04d
|
102 |
PersistenceManager pm = getDb(); |
4f4b29
|
103 |
Connection c = pm.getConnection(); |
U |
104 |
pm.startTransaction(c); |
35c04d
|
105 |
pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId()); |
4f4b29
|
106 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
U |
107 |
pm.commit(c); |
|
108 |
return deletedUser; |
72c5c3
|
109 |
} |
627850
|
110 |
|
be9fa2
|
111 |
/* ------------ Rollen ------------------ */ |
U |
112 |
|
87e382
|
113 |
public UserRole grantRole(String userId, String roleName) { |
U |
114 |
UserRole ur = new UserRole(); |
|
115 |
ur.setRole(roleName); |
|
116 |
ur.setUser(userId); |
35c04d
|
117 |
getDb().insert(ur, UserRoleMapper); |
87e382
|
118 |
return ur; |
be9fa2
|
119 |
} |
U |
120 |
|
87e382
|
121 |
public UserRole revokeRole(String userId, String roleName) { |
U |
122 |
UserRole ur = new UserRole(); |
|
123 |
ur.setRole(roleName); |
|
124 |
ur.setUser(userId); |
35c04d
|
125 |
getDb().delete(ur, UserRoleMapper); |
87e382
|
126 |
return ur; |
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 |
} |