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 |
* |
c65695
|
17 |
* @author Ulrich Hilger |
U |
18 |
*/ |
|
19 |
public class UserMgr { |
|
20 |
|
|
21 |
private static final String MD5 = "MD5"; |
|
22 |
|
|
23 |
private static final Record UserMapper = new GenericRecord(User.class); |
87e382
|
24 |
private static final Record UserRoleMapper = new GenericRecord(UserRole.class); |
c65695
|
25 |
|
725d10
|
26 |
public static final String SQL_GET_USER_LIST = "getUserList"; |
U |
27 |
public static final String SQL_GET_USER_NAME_LIST = "getUserNameList"; |
87e382
|
28 |
public static final String SQL_GET_ROLE_LIST = "getRoleList"; |
c79c12
|
29 |
public static final String SQL_GET_USER_ROLES = "getUserRoles"; |
72c5c3
|
30 |
public static final String SQL_DELETE_USER = "deleteUser"; |
4f4b29
|
31 |
public static final String SQL_DELETE_USER_ROLES = "deleteUserRoles"; |
725d10
|
32 |
|
be9fa2
|
33 |
/* ----------- Benutzer -------------- */ |
U |
34 |
|
c65695
|
35 |
public User createUser(User user) { |
U |
36 |
String kw = user.getPw(); |
|
37 |
String digestedPw = RealmBase.Digest(kw, MD5, null); |
|
38 |
user.setPw(digestedPw); |
|
39 |
App.getDatabase().insert(user, UserMapper); |
|
40 |
return user; |
|
41 |
} |
|
42 |
|
725d10
|
43 |
public List getUserNameList() { |
U |
44 |
String sql = App.getSqlStatement(SQL_GET_USER_NAME_LIST); |
|
45 |
List userNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
46 |
return userNames; |
|
47 |
} |
|
48 |
|
72c5c3
|
49 |
public User deleteUser(User user) { |
4f4b29
|
50 |
PersistenceManager pm = App.getDatabase(); |
U |
51 |
Connection c = pm.getConnection(); |
|
52 |
pm.startTransaction(c); |
|
53 |
String sql = App.getSqlStatement(SQL_DELETE_USER_ROLES); |
|
54 |
pm.execute(c, sql, user.getId()); |
|
55 |
User deletedUser = (User) pm.delete(c, user, UserMapper); |
|
56 |
pm.commit(c); |
|
57 |
return deletedUser; |
72c5c3
|
58 |
} |
U |
59 |
|
725d10
|
60 |
/* |
U |
61 |
public List getUserList() { |
|
62 |
String sql = App.getSqlStatement(SQL_GET_USER_LIST); |
|
63 |
List users = App.getDatabase().select(sql, UserMapper, App.WITHOUT_BLOBS); |
|
64 |
return users; |
|
65 |
} |
|
66 |
*/ |
|
67 |
|
be9fa2
|
68 |
/* ------------ Rollen ------------------ */ |
U |
69 |
|
87e382
|
70 |
public UserRole grantRole(String userId, String roleName) { |
U |
71 |
UserRole ur = new UserRole(); |
|
72 |
ur.setRole(roleName); |
|
73 |
ur.setUser(userId); |
|
74 |
App.getDatabase().insert(ur, UserRoleMapper); |
|
75 |
return ur; |
be9fa2
|
76 |
} |
U |
77 |
|
87e382
|
78 |
public UserRole revokeRole(String userId, String roleName) { |
U |
79 |
UserRole ur = new UserRole(); |
|
80 |
ur.setRole(roleName); |
|
81 |
ur.setUser(userId); |
|
82 |
App.getDatabase().delete(ur, UserRoleMapper); |
|
83 |
return ur; |
be9fa2
|
84 |
} |
U |
85 |
|
|
86 |
public List getRoleNamesGranted() { |
87e382
|
87 |
String sql = App.getSqlStatement(SQL_GET_ROLE_LIST); |
U |
88 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS); |
|
89 |
return roleNames; |
be9fa2
|
90 |
} |
U |
91 |
|
c79c12
|
92 |
/** |
U |
93 |
* |
|
94 |
* |
|
95 |
* list.get(recordno).get(fieldno) |
|
96 |
* |
|
97 |
* @param userId ID des Benutzers |
|
98 |
* @return List<List<String>> Rollennamen des Benutzers |
|
99 |
*/ |
|
100 |
public List getUserRoleNames(String userId) { |
|
101 |
String sql = App.getSqlStatement(SQL_GET_USER_ROLES); |
|
102 |
List roleNames = App.getDatabase().select(sql, App.WITHOUT_BLOBS, userId); |
|
103 |
return roleNames; |
|
104 |
} |
|
105 |
|
be9fa2
|
106 |
/* ------------ sonstige Methoden -------------- */ |
U |
107 |
|
f8b605
|
108 |
public String hallo() { |
U |
109 |
return "Hallo Welt"; |
|
110 |
} |
|
111 |
|
c65695
|
112 |
} |