commit | author | age
|
6240cd
|
1 |
/* |
U |
2 |
Nutzerverwaltung - A Generic User Manager |
|
3 |
Copyright (c) 2016 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero 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 Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
c65695
|
18 |
package de.uhilger.um.api; |
U |
19 |
|
|
20 |
import de.uhilger.baselink.GenericRecord; |
4f4b29
|
21 |
import de.uhilger.baselink.PersistenceManager; |
c65695
|
22 |
import de.uhilger.baselink.Record; |
U |
23 |
import de.uhilger.um.App; |
6240cd
|
24 |
import de.uhilger.um.Digester; |
c65695
|
25 |
import de.uhilger.um.daten.User; |
87e382
|
26 |
import de.uhilger.um.daten.UserRole; |
4f4b29
|
27 |
import java.sql.Connection; |
725d10
|
28 |
import java.util.List; |
c65695
|
29 |
|
U |
30 |
/** |
87e382
|
31 |
* Klasse zur Verwaltung von Benutzern und Rollen |
6240cd
|
32 |
* in einer Datenbank. Die Datenbankverbindung wird |
U |
33 |
* vom zentralen Anwendungsobjekt bereitgestellt. |
59f8b3
|
34 |
* |
6240cd
|
35 |
* Die SQL-Befehle finden sich in WEB-INF/sql.properties |
627850
|
36 |
* |
6240cd
|
37 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
U |
38 |
* @author Published under the terms and conditions of the |
|
39 |
* <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero |
|
40 |
* General Public License</a> |
|
41 |
* |
|
42 |
* @version 2, December 27, 2016 |
c65695
|
43 |
*/ |
U |
44 |
public class UserMgr { |
|
45 |
|
|
46 |
private static final Record UserMapper = new GenericRecord(User.class); |
87e382
|
47 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
c65695
|
48 |
|
725d10
|
49 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
U |
50 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
87e382
|
51 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c79c12
|
52 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
72c5c3
|
53 |
public static final String SQL_DELETE_USER = "deleteUser"; |
4f4b29
|
54 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
55 |
|
be9fa2
|
56 |
/* ----------- Benutzer -------------- */ |
U |
57 |
|
c65695
|
58 |
public User createUser(User user) { |
U |
59 |
String kw = user.getPw(); |
6240cd
|
60 |
String digestedPw = App.getDigester().digest(kw, Digester.MD5, null); |
c65695
|
61 |
user.setPw(digestedPw); |
U |
62 |
App.getDatabase().insert(user, UserMapper); |
|
63 |
return user; |
|
64 |
} |
|
65 |
|
725d10
|
66 |
public List getUserNameList() { |
U |
67 |
String sql = App.getSqlStatement(SQL_GET_USER_NAME_LIST); |
|
68 |
List userNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
69 |
return userNames; |
|
70 |
} |
|
71 |
|
72c5c3
|
72 |
public User deleteUser(User user) { |
4f4b29
|
73 |
PersistenceManager pm = App.getDatabase(); |
U |
74 |
Connection c = pm.getConnection(); |
|
75 |
pm.startTransaction(c); |
|
76 |
String sql = App.getSqlStatement(SQL_DELETE_USER_ROLES); |
|
77 |
pm.execute(c, sql, user.getId()); |
|
78 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
|
79 |
pm.commit(c); |
|
80 |
return deletedUser; |
72c5c3
|
81 |
} |
627850
|
82 |
|
be9fa2
|
83 |
/* ------------ Rollen ------------------ */ |
U |
84 |
|
87e382
|
85 |
public UserRole grantRole(String userId, String roleName) { |
U |
86 |
UserRole ur = new UserRole(); |
|
87 |
ur.setRole(roleName); |
|
88 |
ur.setUser(userId); |
|
89 |
App.getDatabase().insert(ur, UserRoleMapper); |
|
90 |
return ur; |
be9fa2
|
91 |
} |
U |
92 |
|
87e382
|
93 |
public UserRole revokeRole(String userId, String roleName) { |
U |
94 |
UserRole ur = new UserRole(); |
|
95 |
ur.setRole(roleName); |
|
96 |
ur.setUser(userId); |
|
97 |
App.getDatabase().delete(ur, UserRoleMapper); |
|
98 |
return ur; |
be9fa2
|
99 |
} |
U |
100 |
|
|
101 |
public List getRoleNamesGranted() { |
87e382
|
102 |
String sql = App.getSqlStatement(SQL_GET_ROLE_LIST); |
U |
103 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
104 |
return roleNames; |
be9fa2
|
105 |
} |
U |
106 |
|
c79c12
|
107 |
public List getUserRoleNames(String userId) { |
U |
108 |
String sql = App.getSqlStatement(SQL_GET_USER_ROLES); |
|
109 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS, userId); |
|
110 |
return roleNames; |
f8b605
|
111 |
} |
U |
112 |
|
c65695
|
113 |
} |