commit | author | age
|
6240cd
|
1 |
/* |
51c9e3
|
2 |
* Nutzerverwaltung - User and role management in your browser |
e28ac1
|
3 |
* Copyright (C) 2011-2017 Ulrich Hilger, http://uhilger.de |
51c9e3
|
4 |
* |
U |
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 |
|
4f4b29
|
21 |
import de.uhilger.baselink.PersistenceManager; |
6240cd
|
22 |
import de.uhilger.um.Digester; |
c65695
|
23 |
import de.uhilger.um.daten.User; |
87e382
|
24 |
import de.uhilger.um.daten.UserRole; |
4f4b29
|
25 |
import java.sql.Connection; |
725d10
|
26 |
import java.util.List; |
61cf48
|
27 |
import java.util.logging.Logger; |
c65695
|
28 |
|
U |
29 |
/** |
1fc020
|
30 |
* <p>Die Methoden der Klasse UserMgr sind |
U |
31 |
* die 'Fassade', die über Transit als Programmschnittstelle |
|
32 |
* (Application Programming Interface, API) |
|
33 |
* nach außen bereitgestellt wird. UserMgr dient weitgehend |
|
34 |
* als 'Durchreiche' für die Funktionalität anderer Klassen.</p> |
59f8b3
|
35 |
* |
1fc020
|
36 |
* <p> |
U |
37 |
* Der UserMgr erhält die benötigten Methoden aus dem ServletContext. |
|
38 |
* Auf diese Weise sind keine statischen Abhängigkeiten in den Code |
|
39 |
* gewandert. Die folgenden Elemente werden vom UserMgr im ServletContext |
|
40 |
* erwartet: |
c43d2a
|
41 |
* <ul> |
U |
42 |
* <li>PersistenceManager</li> |
|
43 |
* <li>SQL-Properties</li> |
|
44 |
* <li>Digester</li> |
1fc020
|
45 |
* </p> |
627850
|
46 |
* |
6240cd
|
47 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
U |
48 |
* @author Published under the terms and conditions of the |
|
49 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
50 |
* General Public License</a> |
|
51 |
* |
|
52 |
* @version 2, December 27, 2016 |
c65695
|
53 |
*/ |
70a614
|
54 |
public class UserMgr extends Api /*implements WebKontext, RequestKontext*/ { |
1fc020
|
55 |
|
61cf48
|
56 |
private static final Logger logger = Logger.getLogger(UserMgr.class.getName()); |
U |
57 |
|
1fc020
|
58 |
/** Name, unter dem das Properties-Objekt mit den SQL-Befehlen im ServletContext hinterlegt ist */ |
U |
59 |
public static final String UM_SQL_PROPERTIES = "umSqlProperties"; |
|
60 |
|
c43d2a
|
61 |
/** Name, unter dem das Digester-Objekt im ServletContext hinterlegt ist */ |
35c04d
|
62 |
public static final String P_DIGESTER = "digester"; |
U |
63 |
|
1fc020
|
64 |
/** Name, unter dem das Zugriffsobjekt zur Datenbank im ServletContext hinterlegt ist */ |
U |
65 |
public static final String UM_DB = "umDb"; |
c65695
|
66 |
|
35c04d
|
67 |
/** Boolean-Konstante zur Kennzeichnung von Datenbankergebnissen ohne Blobs */ |
U |
68 |
public static final boolean WITHOUT_BLOBS = false; |
|
69 |
|
c43d2a
|
70 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer */ |
725d10
|
71 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
c43d2a
|
72 |
/** Referenz zum SQL-Befehl zur Ermittlung der Benutzer-Namen */ |
725d10
|
73 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
c43d2a
|
74 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen */ |
87e382
|
75 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c43d2a
|
76 |
/** Referenz zum SQL-Befehl zur Ermittlung der Rollen eines Benutzers */ |
c79c12
|
77 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
c43d2a
|
78 |
/** Referenz zum SQL-Befehl zum Loeschen aller Rollen eines Nutzers */ |
4f4b29
|
79 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
70a614
|
80 |
public static final String SQL_GET_USER_DATA = "getUserData"; |
U |
81 |
|
|
82 |
public static final String MP_USER = "userMapper"; |
|
83 |
public static final String MP_USER_DATA = "userDataMapper"; |
|
84 |
public static final String MP_USER_ROLE = "userRoleMapper"; |
c43d2a
|
85 |
|
be9fa2
|
86 |
/* ----------- Benutzer -------------- */ |
U |
87 |
|
35c04d
|
88 |
public User createUser(User user) throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
8318da
|
89 |
|
U |
90 |
/* |
|
91 |
Irgendetwas wurde am Tomcat-Mechanismus fuer verschluesslete Kennworte |
|
92 |
geaendert. Es muss ein eigener Mechanismus implementiert werden. |
|
93 |
Bis da hin wird das Kennwort im Klartext in der Datenbank gespeichert. |
|
94 |
*/ |
|
95 |
|
|
96 |
|
|
97 |
|
c65695
|
98 |
String kw = user.getPw(); |
70a614
|
99 |
String digesterClassName = getServletContext().getInitParameter(P_DIGESTER); |
35c04d
|
100 |
Digester digester = (Digester) Class.forName(digesterClassName).newInstance(); |
8318da
|
101 |
|
e0ec31
|
102 |
/* |
U |
103 |
MD5 geht nicht mehr, |
|
104 |
vgl. http://stackoverflow.com/questions/39967289/how-to-use-digest-authentication-in-tomcat-8-5 |
|
105 |
*/ |
8318da
|
106 |
|
e0ec31
|
107 |
String digestedPw = digester.digest(kw, Digester.SHA256, null); |
c65695
|
108 |
user.setPw(digestedPw); |
8318da
|
109 |
|
70a614
|
110 |
getDb().insert(user, getMapper(MP_USER)); |
c65695
|
111 |
return user; |
U |
112 |
} |
|
113 |
|
725d10
|
114 |
public List getUserNameList() { |
ff2dff
|
115 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS)); |
725d10
|
116 |
} |
U |
117 |
|
72c5c3
|
118 |
public User deleteUser(User user) { |
35c04d
|
119 |
PersistenceManager pm = getDb(); |
4f4b29
|
120 |
Connection c = pm.getConnection(); |
U |
121 |
pm.startTransaction(c); |
35c04d
|
122 |
pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId()); |
70a614
|
123 |
User deletedUser = (User) pm.delete(c, user, getMapper(MP_USER)); |
4f4b29
|
124 |
pm.commit(c); |
U |
125 |
return deletedUser; |
72c5c3
|
126 |
} |
70a614
|
127 |
|
88117b
|
128 |
public String logout() { |
U |
129 |
getRequest().getSession().invalidate(); |
|
130 |
return "logged out"; |
|
131 |
} |
|
132 |
|
be9fa2
|
133 |
/* ------------ Rollen ------------------ */ |
U |
134 |
|
ccefc8
|
135 |
public UserRole grantRole(UserRole role) { |
70a614
|
136 |
getDb().insert(role, getMapper(MP_USER_ROLE)); |
ccefc8
|
137 |
return role; |
be9fa2
|
138 |
} |
U |
139 |
|
ccefc8
|
140 |
public UserRole revokeRole(UserRole role) { |
70a614
|
141 |
getDb().delete(role, getMapper(MP_USER_ROLE)); |
ccefc8
|
142 |
return role; |
be9fa2
|
143 |
} |
U |
144 |
|
|
145 |
public List getRoleNamesGranted() { |
ff2dff
|
146 |
return removeHeadline(getDb().select(getSql(SQL_GET_ROLE_LIST), WITHOUT_BLOBS)); |
be9fa2
|
147 |
} |
U |
148 |
|
c79c12
|
149 |
public List getUserRoleNames(String userId) { |
ff2dff
|
150 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_ROLES), WITHOUT_BLOBS, userId)); |
f8b605
|
151 |
} |
U |
152 |
|
1fc020
|
153 |
/* ----------- Helfer ---- */ |
U |
154 |
|
ff2dff
|
155 |
private List removeHeadline(List list) { |
U |
156 |
if(list != null && list.size() > 0) { |
|
157 |
list.remove(0); |
|
158 |
} |
|
159 |
return list; |
|
160 |
} |
|
161 |
|
1345f0
|
162 |
} |