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; |
61cf48
|
27 |
import de.uhilger.um.daten.UserData; |
87e382
|
28 |
import de.uhilger.um.daten.UserRole; |
4f4b29
|
29 |
import java.sql.Connection; |
725d10
|
30 |
import java.util.List; |
1fc020
|
31 |
import java.util.Properties; |
61cf48
|
32 |
import java.util.logging.Logger; |
1fc020
|
33 |
import javax.servlet.ServletContext; |
c65695
|
34 |
|
U |
35 |
/** |
1fc020
|
36 |
* <p>Die Methoden der Klasse UserMgr sind |
U |
37 |
* die 'Fassade', die über Transit als Programmschnittstelle |
|
38 |
* (Application Programming Interface, API) |
|
39 |
* nach außen bereitgestellt wird. UserMgr dient weitgehend |
|
40 |
* als 'Durchreiche' für die Funktionalität anderer Klassen.</p> |
59f8b3
|
41 |
* |
1fc020
|
42 |
* <p> |
U |
43 |
* Der UserMgr erhält die benötigten Methoden aus dem ServletContext. |
|
44 |
* Auf diese Weise sind keine statischen Abhängigkeiten in den Code |
|
45 |
* gewandert. Die folgenden Elemente werden vom UserMgr im ServletContext |
|
46 |
* erwartet: |
c43d2a
|
47 |
* <ul> |
U |
48 |
* <li>PersistenceManager</li> |
|
49 |
* <li>SQL-Properties</li> |
|
50 |
* <li>Digester</li> |
1fc020
|
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 |
|
61cf48
|
62 |
private static final Logger logger = Logger.getLogger(UserMgr.class.getName()); |
U |
63 |
|
c43d2a
|
64 |
/** Zeiger zum Servlet-Kontext dieser Anwendung */ |
1fc020
|
65 |
private ServletContext ctx; |
U |
66 |
|
|
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 |
|
c43d2a
|
70 |
/** Name, unter dem das Digester-Objekt im ServletContext hinterlegt ist */ |
35c04d
|
71 |
public static final String P_DIGESTER = "digester"; |
U |
72 |
|
1fc020
|
73 |
/** Name, unter dem das Zugriffsobjekt zur Datenbank im ServletContext hinterlegt ist */ |
U |
74 |
public static final String UM_DB = "umDb"; |
c65695
|
75 |
|
35c04d
|
76 |
/** Boolean-Konstante zur Kennzeichnung von Datenbankergebnissen ohne Blobs */ |
U |
77 |
public static final boolean WITHOUT_BLOBS = false; |
|
78 |
|
c43d2a
|
79 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer */ |
725d10
|
80 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
c43d2a
|
81 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer-Namen */ |
725d10
|
82 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
c43d2a
|
83 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen */ |
87e382
|
84 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c43d2a
|
85 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen eines Benutzers */ |
c79c12
|
86 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
c43d2a
|
87 |
/** Referenz zum SQL-Befehl zum Loeschen aller Rollen eines Nutzers */ |
4f4b29
|
88 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
89 |
|
c43d2a
|
90 |
/** Mapper-Objekt fuer Benutzer */ |
U |
91 |
private static final Record UserMapper = new GenericRecord(User.class); |
61cf48
|
92 |
/** Mapper-Objekt fuer Benutzerdaten */ |
ff2dff
|
93 |
//private static final Record UserDataMapper = new GenericRecord(UserData.class); |
c43d2a
|
94 |
/** Mapper-Objekt fuer Benutzerrollen */ |
U |
95 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
|
96 |
|
be9fa2
|
97 |
/* ----------- Benutzer -------------- */ |
U |
98 |
|
35c04d
|
99 |
public User createUser(User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
c65695
|
100 |
String kw = user.getPw(); |
35c04d
|
101 |
String digesterClassName = ctx.getInitParameter(P_DIGESTER); |
U |
102 |
Digester digester = (Digester) Class.forName(digesterClassName).newInstance(); |
e0ec31
|
103 |
/* |
U |
104 |
MD5 geht nicht mehr, |
|
105 |
vgl. http://stackoverflow.com/questions/39967289/how-to-use-digest-authentication-in-tomcat-8-5 |
|
106 |
*/ |
|
107 |
String digestedPw = digester.digest(kw, Digester.SHA256, null); |
c65695
|
108 |
user.setPw(digestedPw); |
35c04d
|
109 |
getDb().insert(user, UserMapper); |
c65695
|
110 |
return user; |
U |
111 |
} |
|
112 |
|
725d10
|
113 |
public List getUserNameList() { |
ff2dff
|
114 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS)); |
725d10
|
115 |
} |
U |
116 |
|
72c5c3
|
117 |
public User deleteUser(User user) { |
35c04d
|
118 |
PersistenceManager pm = getDb(); |
4f4b29
|
119 |
Connection c = pm.getConnection(); |
U |
120 |
pm.startTransaction(c); |
35c04d
|
121 |
pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId()); |
4f4b29
|
122 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
U |
123 |
pm.commit(c); |
|
124 |
return deletedUser; |
72c5c3
|
125 |
} |
627850
|
126 |
|
be9fa2
|
127 |
/* ------------ Rollen ------------------ */ |
U |
128 |
|
ccefc8
|
129 |
public UserRole grantRole(UserRole role) { |
U |
130 |
getDb().insert(role, UserRoleMapper); |
|
131 |
return role; |
be9fa2
|
132 |
} |
U |
133 |
|
ccefc8
|
134 |
public UserRole revokeRole(UserRole role) { |
U |
135 |
getDb().delete(role, UserRoleMapper); |
|
136 |
return role; |
be9fa2
|
137 |
} |
U |
138 |
|
|
139 |
public List getRoleNamesGranted() { |
ff2dff
|
140 |
return removeHeadline(getDb().select(getSql(SQL_GET_ROLE_LIST), WITHOUT_BLOBS)); |
be9fa2
|
141 |
} |
U |
142 |
|
c79c12
|
143 |
public List getUserRoleNames(String userId) { |
ff2dff
|
144 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_ROLES), WITHOUT_BLOBS, userId)); |
f8b605
|
145 |
} |
U |
146 |
|
1fc020
|
147 |
/* ----------- Helfer ---- */ |
U |
148 |
|
ff2dff
|
149 |
private List removeHeadline(List list) { |
U |
150 |
if(list != null && list.size() > 0) { |
|
151 |
list.remove(0); |
|
152 |
} |
|
153 |
return list; |
|
154 |
} |
|
155 |
|
1fc020
|
156 |
/** |
U |
157 |
* Ein benanntes SQL-Kommando ermitteln |
|
158 |
* @param id Name des gewuenschten SQL-Kommandos |
|
159 |
* @return das SQL-Kommando mit der in id angegebenen Bezeichnung |
|
160 |
*/ |
35c04d
|
161 |
private String getSql(String id) { |
1fc020
|
162 |
Properties sql = (Properties) ctx.getAttribute(UserMgr.UM_SQL_PROPERTIES); |
U |
163 |
return sql.getProperty(id); |
|
164 |
} |
|
165 |
|
35c04d
|
166 |
private PersistenceManager getDb() { |
U |
167 |
return (PersistenceManager) ctx.getAttribute(UM_DB); |
|
168 |
} |
|
169 |
|
1fc020
|
170 |
/* ------------- Implementierung WebKontext ------------- */ |
U |
171 |
|
|
172 |
@Override |
|
173 |
public ServletContext getServletContext() { |
|
174 |
return ctx; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public void setServletContext(ServletContext servletContext) { |
|
179 |
this.ctx = servletContext; |
|
180 |
} |
|
181 |
|
|
182 |
|
c65695
|
183 |
} |