1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.interfaces.MySQLConstants;
|
---|
4 | import com.bowman.cardserv.mysql.UserCacheManager;
|
---|
5 | import com.bowman.cardserv.mysql.User;
|
---|
6 | import com.bowman.cardserv.util.ProxyLogger;
|
---|
7 | import com.bowman.cardserv.util.ProxyXmlConfig;
|
---|
8 |
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A simple mysql usermanager to get the user informations directly from a mysql
|
---|
15 | * database without having to load them over external xml sources.
|
---|
16 | * For the database communication it uses the MySQL Connectors/J Driver (mysql-connector-java-5.1.XX-bin.jar)
|
---|
17 | * found on "http://dev.mysql.com/downloads/connector/j/".
|
---|
18 | *
|
---|
19 | * @author DonCarlo
|
---|
20 | * @since 04.12.2010
|
---|
21 | */
|
---|
22 | public class MySQLUserManager extends XmlUserManager {
|
---|
23 |
|
---|
24 | private static final String DEFAULT_DBHOST = "127.0.0.1";
|
---|
25 | private static final int DEFAULT_DBPORT = 3306;
|
---|
26 | private static final String DEFAULT_DBNAME = "cardserverproxy";
|
---|
27 |
|
---|
28 | private ProxyLogger logger = null;
|
---|
29 | private UserCacheManager cacheManager = null;
|
---|
30 |
|
---|
31 | public MySQLUserManager() {
|
---|
32 | logger = ProxyLogger.getLabeledLogger(getClass().getName());
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void configUpdated(ProxyXmlConfig xml) throws ConfigException {
|
---|
36 | try {
|
---|
37 | super.configUpdated(xml);
|
---|
38 | } catch (ConfigException e) {
|
---|
39 | logger.throwing(e);
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (cacheManager != null)
|
---|
43 | cacheManager.interrupt();
|
---|
44 |
|
---|
45 | cacheManager = new UserCacheManager(
|
---|
46 | xml.getSubConfig("mysql-database").getStringValue("dbhost", DEFAULT_DBHOST),
|
---|
47 | xml.getSubConfig("mysql-database").getPortValue("dbport", DEFAULT_DBPORT),
|
---|
48 | xml.getSubConfig("mysql-database").getStringValue("dbname", DEFAULT_DBNAME),
|
---|
49 | xml.getSubConfig("mysql-database").getStringValue("dbuser"),
|
---|
50 | xml.getSubConfig("mysql-database").getStringValue("dbpassword")
|
---|
51 | );
|
---|
52 | }
|
---|
53 |
|
---|
54 | public String[] getUserNames() {
|
---|
55 | List userNames = getMySQLUserNames();
|
---|
56 | // now run through the rest (simple and xml usermanager) and merge them
|
---|
57 | String [] tmp = super.getUserNames();
|
---|
58 | for (int i = 0; i < tmp.length; i++) {
|
---|
59 | if (!userNames.contains(tmp[i])) {
|
---|
60 | userNames.add(tmp[i]);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | Collections.sort(userNames);
|
---|
64 | return (String[])userNames.toArray(new String[userNames.size()]);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public String getPassword(String user) {
|
---|
68 | if(super.getPassword(user) != null) {
|
---|
69 | return super.getPassword(user);
|
---|
70 | } else {
|
---|
71 | return cacheManager.getUser(user).getPassword();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public String getUserName(String user) {
|
---|
76 | if(super.getPassword(user) != null) {
|
---|
77 | return super.getUserName(user);
|
---|
78 | } else {
|
---|
79 | return cacheManager.getUser(user).getUserName();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public boolean authenticate(String user, String pass) {
|
---|
84 | if(super.getPassword(user) != null) {
|
---|
85 | return super.authenticate(user, pass);
|
---|
86 | } else {
|
---|
87 | return (cacheManager.getUser(user) != null) && cacheManager.getUser(user).getPassword().equals(pass);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public int getMaxConnections(String user) {
|
---|
92 | if(super.getPassword(user) != null) {
|
---|
93 | return super.getMaxConnections(user);
|
---|
94 | } else {
|
---|
95 | return cacheManager.getUser(user).getMaxConnections();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public String getIpMask(String user) {
|
---|
100 | if(super.getPassword(user) != null) {
|
---|
101 | return super.getIpMask(user);
|
---|
102 | } else {
|
---|
103 | return cacheManager.getUser(user).getIpMask().equals(new String("")) ? new String("*") : cacheManager.getUser(user).getIpMask();
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public String getEmailAddress(String user) {
|
---|
108 | if(super.getPassword(user) != null) {
|
---|
109 | return super.getEmailAddress(user);
|
---|
110 | } else {
|
---|
111 | return cacheManager.getUser(user).getEmail();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public String getDisplayName(String user) {
|
---|
116 | if(super.getPassword(user) != null) {
|
---|
117 | return super.getDisplayName(user);
|
---|
118 | } else {
|
---|
119 | User us = cacheManager.getUser(user);
|
---|
120 | if (us.getDisplayName().equals(new String())) {
|
---|
121 | return us.getUserName();
|
---|
122 | } else
|
---|
123 | return us.getDisplayName();
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public Set getAllowedProfiles(String user) {
|
---|
128 | if(super.getPassword(user) != null) {
|
---|
129 | return super.getAllowedProfiles(user);
|
---|
130 | } else {
|
---|
131 | return cacheManager.getUser(user).getAllowedProfiles();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | public boolean isEnabled(String user) {
|
---|
136 | if(super.getPassword(user) != null) {
|
---|
137 | return super.isEnabled(user);
|
---|
138 | } else {
|
---|
139 | return cacheManager.getUser(user).isEnabled();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public boolean isAdmin(String user) {
|
---|
144 | if(super.getPassword(user) != null) {
|
---|
145 | return super.isAdmin(user);
|
---|
146 | } else {
|
---|
147 | return cacheManager.getUser(user).isAdmin();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | public boolean exists(String user) {
|
---|
152 | if(super.getPassword(user) != null) {
|
---|
153 | return super.exists(user);
|
---|
154 | } else {
|
---|
155 | return cacheManager.getUser(user) != null;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public boolean isMapExcluded(String user) {
|
---|
160 | if(super.getPassword(user) != null) {
|
---|
161 | return super.isMapExcluded(user);
|
---|
162 | } else {
|
---|
163 | return cacheManager.getUser(user).isMapExcluded();
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | public boolean isDebug(String user) {
|
---|
168 | if(super.getPassword(user) != null) {
|
---|
169 | return super.isDebug(user);
|
---|
170 | } else {
|
---|
171 | return cacheManager.getUser(user).isDebug();
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | public void setDebug(String user, boolean debug) {
|
---|
176 | if(super.getPassword(user) != null) {
|
---|
177 | super.setDebug(user, debug);
|
---|
178 | } else {
|
---|
179 | cacheManager.setDebug(user, debug);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public int getUserCount() {
|
---|
184 | return getUserNames().length;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public Set getAllowedServices(String user, String profile) {
|
---|
188 | if(super.getPassword(user) != null) {
|
---|
189 | return super.getAllowedServices(user, profile);
|
---|
190 | } else {
|
---|
191 | return null;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | public Set getBlockedServices(String user, String profile) {
|
---|
196 | if(super.getPassword(user) != null) {
|
---|
197 | return super.getBlockedServices(user, profile);
|
---|
198 | } else {
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | public Set getAllowedConnectors(String user) {
|
---|
204 | if(super.getPassword(user) != null) {
|
---|
205 | return super.getAllowedConnectors(user);
|
---|
206 | } else {
|
---|
207 | return null;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | public int getAllowedEcmRate(String user) {
|
---|
212 | if(super.getPassword(user) != null) {
|
---|
213 | return super.getAllowedEcmRate(user);
|
---|
214 | } else {
|
---|
215 | return -1;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /* ############################################################################################ */
|
---|
220 | /* add/edit/delete MySQL users */
|
---|
221 | /* ############################################################################################ */
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * returns all usernames which are stored in the database
|
---|
225 | * limited with skipRows and numRows as a List.
|
---|
226 | * @param skipRows
|
---|
227 | * @param numRows
|
---|
228 | * @return usersnames
|
---|
229 | */
|
---|
230 | public List getMySQLUserNames(int skipRows, int numRows) {
|
---|
231 | return cacheManager.getUserNames(skipRows, numRows);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * returns all usernames which are stored in the database as a List.
|
---|
236 | * @return all usersnames
|
---|
237 | */
|
---|
238 | public List getMySQLUserNames() {
|
---|
239 | return getMySQLUserNames(MySQLConstants.DEFAULT_SKIP_ROWS, MySQLConstants.DEFAULT_NUM_ROWS);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Returns the number of total Users in database
|
---|
244 | * @return user count
|
---|
245 | */
|
---|
246 | public int getMySQLUserCount() {
|
---|
247 | return getMySQLUserNames().size();
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Returns user
|
---|
252 | * @param userName
|
---|
253 | * @return User
|
---|
254 | */
|
---|
255 | public User getMySQLUser(String userName) {
|
---|
256 | return cacheManager.getUser(userName);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * tests whether the user exists.
|
---|
261 | * @param userName
|
---|
262 | * @return TRUE, when specified user exists
|
---|
263 | */
|
---|
264 | public boolean existsMySQLUser(String userName) {
|
---|
265 | return cacheManager.getUser(userName) != null;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * add a new user to the database.
|
---|
270 | */
|
---|
271 | public boolean addUser(String username, String password, String displayname, String ipmask,
|
---|
272 | int maxconnections, boolean enabled, boolean debug, boolean admin,
|
---|
273 | String mail, boolean mapexcluded, Set allowedProfileIds) {
|
---|
274 | return cacheManager.addUser(
|
---|
275 | username, password, displayname, ipmask, maxconnections, enabled,
|
---|
276 | debug, admin, mail, mapexcluded, allowedProfileIds
|
---|
277 | );
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * edit an existing user entry by passing the new values.
|
---|
282 | */
|
---|
283 | public boolean editUser(int id, String username, String password, String displayname, String ipmask,
|
---|
284 | int maxconnections, boolean enabled, boolean debug, boolean admin, String mail,
|
---|
285 | boolean mapexcluded, Set allowedProfileIds) {
|
---|
286 | return cacheManager.editUser(id, username, password, displayname, ipmask, maxconnections,
|
---|
287 | enabled, debug, admin, mail, mapexcluded, allowedProfileIds);
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * delete specified user from the MySQL database.
|
---|
292 | * @param id - user to delete.
|
---|
293 | */
|
---|
294 | public boolean deleteUser(String username) {
|
---|
295 | return cacheManager.deleteUser(username);
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * delete all users from the database.
|
---|
300 | * @param id - the user which should not be deleted.
|
---|
301 | * This may be because the user is currently logged in and
|
---|
302 | * therefore shouldn't be deleted.
|
---|
303 | */
|
---|
304 | public boolean deleteAllUsers(String skipUserName) {
|
---|
305 | return cacheManager.deleteAllUsers(skipUserName);
|
---|
306 | }
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Little helper to import a Set of user objects with profilen-ames
|
---|
310 | * instead of profile-ids. Profiles not in DB will be added to DB.
|
---|
311 | * @param users - set of users to import
|
---|
312 | * @return TRUE if import was successfull
|
---|
313 | */
|
---|
314 | public boolean importUsers(Set users) {
|
---|
315 | return cacheManager.importUsers(users);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * tests whether the user with the specified username exists in the MySQL database.
|
---|
320 | * @param id - the user to check
|
---|
321 | * @return TRUE, when the user entry is in database
|
---|
322 | */
|
---|
323 | /**public boolean existsUserInDatabase(String userName) {
|
---|
324 | return cacheManager.getUser(userName) != null;
|
---|
325 | }*/
|
---|
326 |
|
---|
327 | /* ############################################################################################ */
|
---|
328 | /* profiles */
|
---|
329 | /* ############################################################################################ */
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Returns all profiles as a list
|
---|
333 | * @return all profiles
|
---|
334 | */
|
---|
335 | public List getProfiles() {
|
---|
336 | return cacheManager.getProfiles();
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * tests whether the profile exists.
|
---|
341 | * @param id
|
---|
342 | * @return TRUE, when specified profile exists
|
---|
343 | */
|
---|
344 | public boolean existsProfile(int id) {
|
---|
345 | return cacheManager.getProfile(id) != null;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * tests whether the profile exists.
|
---|
350 | * @param profileName
|
---|
351 | * @return TRUE, when specified profile exists
|
---|
352 | */
|
---|
353 | public boolean existsProfile(String profileName) {
|
---|
354 | return cacheManager.getProfile(profileName) != null;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * add a new profile to the mysql database.
|
---|
359 | * @param profileName - profile to add
|
---|
360 | * @return TRUE when adding profile was successfull
|
---|
361 | */
|
---|
362 | public boolean addProfile(String profileName) {
|
---|
363 | return cacheManager.addProfile(profileName);
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * edit an existing profile in the mysql database.
|
---|
368 | * @param id - profile to edit
|
---|
369 | * @param profileName - new profilename
|
---|
370 | * @return TRUE when editing profile was successfull
|
---|
371 | */
|
---|
372 | public boolean editProfile(int id, String profileName) {
|
---|
373 | return cacheManager.editProfile(id, profileName);
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * delete specified profile from the MySQL database.
|
---|
378 | * @param id - profile to delete
|
---|
379 | * @return TRUE when profile was successfully deleted.
|
---|
380 | */
|
---|
381 | public boolean deleteProfile(int id) {
|
---|
382 | return cacheManager.deleteProfile(id);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * delete all profiles from the MySQL database.
|
---|
387 | * @return TRUE when all profiles were successfully deleted.
|
---|
388 | */
|
---|
389 | public boolean deleteAllProfiles() {
|
---|
390 | return cacheManager.deleteAllProfiles();
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* ############################################################################################ */
|
---|
394 | /* database informations */
|
---|
395 | /* ############################################################################################ */
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * the hostname or ip address the MySQL server is reached.
|
---|
399 | * @return host
|
---|
400 | */
|
---|
401 | public String getDatabaseHost() {
|
---|
402 | return cacheManager.getDatabaseHost();
|
---|
403 | }
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * the database name which is used for the tables.
|
---|
407 | * @return database name
|
---|
408 | */
|
---|
409 | public String getDatabaseName() {
|
---|
410 | return cacheManager.getDatabaseName();
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * the port the MySQL database listens on.
|
---|
415 | * @return port
|
---|
416 | */
|
---|
417 | public int getDatabasePort() {
|
---|
418 | return cacheManager.getDatabasePort();
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * the username used to connect to the database.
|
---|
423 | * @return username
|
---|
424 | */
|
---|
425 | public String getDatabaseUser() {
|
---|
426 | return cacheManager.getDatabaseUser();
|
---|
427 | }
|
---|
428 |
|
---|
429 | }
|
---|