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 { |
c65695
|
89 |
String kw = user.getPw(); |
70a614
|
90 |
String digesterClassName = getServletContext().getInitParameter(P_DIGESTER); |
35c04d
|
91 |
Digester digester = (Digester) Class.forName(digesterClassName).newInstance(); |
e0ec31
|
92 |
/* |
U |
93 |
MD5 geht nicht mehr, |
|
94 |
vgl. http://stackoverflow.com/questions/39967289/how-to-use-digest-authentication-in-tomcat-8-5 |
|
95 |
*/ |
|
96 |
String digestedPw = digester.digest(kw, Digester.SHA256, null); |
c65695
|
97 |
user.setPw(digestedPw); |
70a614
|
98 |
getDb().insert(user, getMapper(MP_USER)); |
c65695
|
99 |
return user; |
U |
100 |
} |
|
101 |
|
725d10
|
102 |
public List getUserNameList() { |
ff2dff
|
103 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_NAME_LIST), WITHOUT_BLOBS)); |
725d10
|
104 |
} |
U |
105 |
|
72c5c3
|
106 |
public User deleteUser(User user) { |
35c04d
|
107 |
PersistenceManager pm = getDb(); |
4f4b29
|
108 |
Connection c = pm.getConnection(); |
U |
109 |
pm.startTransaction(c); |
35c04d
|
110 |
pm.execute(c, getSql(SQL_DELETE_USER_ROLES), user.getId()); |
70a614
|
111 |
User deletedUser = (User) pm.delete(c, user, getMapper(MP_USER)); |
4f4b29
|
112 |
pm.commit(c); |
U |
113 |
return deletedUser; |
72c5c3
|
114 |
} |
70a614
|
115 |
|
88117b
|
116 |
public String logout() { |
U |
117 |
getRequest().getSession().invalidate(); |
|
118 |
return "logged out"; |
|
119 |
} |
|
120 |
|
be9fa2
|
121 |
/* ------------ Rollen ------------------ */ |
U |
122 |
|
ccefc8
|
123 |
public UserRole grantRole(UserRole role) { |
70a614
|
124 |
getDb().insert(role, getMapper(MP_USER_ROLE)); |
ccefc8
|
125 |
return role; |
be9fa2
|
126 |
} |
U |
127 |
|
ccefc8
|
128 |
public UserRole revokeRole(UserRole role) { |
70a614
|
129 |
getDb().delete(role, getMapper(MP_USER_ROLE)); |
ccefc8
|
130 |
return role; |
be9fa2
|
131 |
} |
U |
132 |
|
|
133 |
public List getRoleNamesGranted() { |
ff2dff
|
134 |
return removeHeadline(getDb().select(getSql(SQL_GET_ROLE_LIST), WITHOUT_BLOBS)); |
be9fa2
|
135 |
} |
U |
136 |
|
c79c12
|
137 |
public List getUserRoleNames(String userId) { |
ff2dff
|
138 |
return removeHeadline(getDb().select(getSql(SQL_GET_USER_ROLES), WITHOUT_BLOBS, userId)); |
f8b605
|
139 |
} |
U |
140 |
|
1fc020
|
141 |
/* ----------- Helfer ---- */ |
U |
142 |
|
ff2dff
|
143 |
private List removeHeadline(List list) { |
U |
144 |
if(list != null && list.size() > 0) { |
|
145 |
list.remove(0); |
|
146 |
} |
|
147 |
return list; |
|
148 |
} |
|
149 |
|
1345f0
|
150 |
} |