commit | author | age
|
c65695
|
1 |
package de.uhilger.um.api; |
U |
2 |
|
|
3 |
import de.uhilger.baselink.GenericRecord; |
4f4b29
|
4 |
import de.uhilger.baselink.PersistenceManager; |
c65695
|
5 |
import de.uhilger.baselink.Record; |
U |
6 |
import de.uhilger.um.App; |
|
7 |
import de.uhilger.um.daten.User; |
87e382
|
8 |
import de.uhilger.um.daten.UserRole; |
4f4b29
|
9 |
import java.sql.Connection; |
725d10
|
10 |
import java.util.List; |
c65695
|
11 |
import org.apache.catalina.realm.RealmBase; |
U |
12 |
|
|
13 |
/** |
87e382
|
14 |
* Klasse zur Verwaltung von Benutzern und Rollen |
U |
15 |
* in einer Datenbank |
59f8b3
|
16 |
* |
627850
|
17 |
* Das SQL findet sich in WEB-INF/sql.properties |
U |
18 |
* |
c65695
|
19 |
* @author Ulrich Hilger |
U |
20 |
*/ |
|
21 |
public class UserMgr { |
|
22 |
|
|
23 |
private static final String MD5 = "MD5"; |
|
24 |
|
|
25 |
private static final Record UserMapper = new GenericRecord(User.class); |
87e382
|
26 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
c65695
|
27 |
|
725d10
|
28 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
U |
29 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
87e382
|
30 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c79c12
|
31 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
72c5c3
|
32 |
public static final String SQL_DELETE_USER = "deleteUser"; |
4f4b29
|
33 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
34 |
|
be9fa2
|
35 |
/* ----------- Benutzer -------------- */ |
U |
36 |
|
c65695
|
37 |
public User createUser(User user) { |
U |
38 |
String kw = user.getPw(); |
|
39 |
String digestedPw = RealmBase.Digest(kw, MD5, null); |
|
40 |
user.setPw(digestedPw); |
|
41 |
App.getDatabase().insert(user, UserMapper); |
|
42 |
return user; |
|
43 |
} |
|
44 |
|
725d10
|
45 |
public List getUserNameList() { |
U |
46 |
String sql = App.getSqlStatement(SQL_GET_USER_NAME_LIST); |
|
47 |
List userNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
48 |
return userNames; |
|
49 |
} |
|
50 |
|
72c5c3
|
51 |
public User deleteUser(User user) { |
4f4b29
|
52 |
PersistenceManager pm = App.getDatabase(); |
U |
53 |
Connection c = pm.getConnection(); |
|
54 |
pm.startTransaction(c); |
|
55 |
String sql = App.getSqlStatement(SQL_DELETE_USER_ROLES); |
|
56 |
pm.execute(c, sql, user.getId()); |
|
57 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
|
58 |
pm.commit(c); |
|
59 |
return deletedUser; |
72c5c3
|
60 |
} |
627850
|
61 |
|
be9fa2
|
62 |
/* ------------ Rollen ------------------ */ |
U |
63 |
|
87e382
|
64 |
public UserRole grantRole(String userId, String roleName) { |
U |
65 |
UserRole ur = new UserRole(); |
|
66 |
ur.setRole(roleName); |
|
67 |
ur.setUser(userId); |
|
68 |
App.getDatabase().insert(ur, UserRoleMapper); |
|
69 |
return ur; |
be9fa2
|
70 |
} |
U |
71 |
|
87e382
|
72 |
public UserRole revokeRole(String userId, String roleName) { |
U |
73 |
UserRole ur = new UserRole(); |
|
74 |
ur.setRole(roleName); |
|
75 |
ur.setUser(userId); |
|
76 |
App.getDatabase().delete(ur, UserRoleMapper); |
|
77 |
return ur; |
be9fa2
|
78 |
} |
U |
79 |
|
|
80 |
public List getRoleNamesGranted() { |
87e382
|
81 |
String sql = App.getSqlStatement(SQL_GET_ROLE_LIST); |
U |
82 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
83 |
return roleNames; |
be9fa2
|
84 |
} |
U |
85 |
|
c79c12
|
86 |
public List getUserRoleNames(String userId) { |
U |
87 |
String sql = App.getSqlStatement(SQL_GET_USER_ROLES); |
|
88 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS, userId); |
|
89 |
return roleNames; |
f8b605
|
90 |
} |
U |
91 |
|
c65695
|
92 |
} |