001: package com.jcorporate.expresso.core.security;
002:
003: import com.jcorporate.expresso.core.db.DBException;
004: import com.jcorporate.expresso.core.misc.ConfigManager;
005: import com.jcorporate.expresso.core.misc.Base64;
006:
007: import java.util.Enumeration;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.Vector;
011:
012: /**
013: * A User Info implementation that is Map based instead of database object
014: * based. Although this isn't usually used in Expresso, a few cases, where
015: * we need a SuperUser, for example when database access doesn't exist. It is
016: * not considered something to use on a broad basis, more as a workaround
017: * for some special startup conditions.
018: *
019: * @author Michael Rimov
020: */
021: public class MapBasedUserInfo implements UserInfo {
022: /**
023: * Map of a map of user info objects.
024: */
025: private static Map dataContexts;
026:
027: /**
028: * Current data context.
029: */
030: private String dataContext;
031:
032: private String email;
033:
034: private String emailAuthCode;
035:
036: private String emailValCode;
037:
038: private String accountStatus;
039:
040: private String createDate;
041:
042: private String loginName;
043:
044: private String password;
045:
046: private String primaryGroup;
047:
048: private boolean regComplete;
049:
050: private String registrationDomain;
051:
052: private int uid;
053:
054: private String updateDate;
055:
056: static {
057: synchronized (MapBasedUserInfo.class) {
058: dataContexts = new HashMap();
059: for (Enumeration e = ConfigManager.getAllConfigKeys(); e
060: .hasMoreElements();) {
061: String contextName = (String) e.nextElement();
062: dataContexts.put(contextName, new HashMap());
063: }
064: }
065: }
066:
067: /**
068: * Default constructor.
069: */
070: public MapBasedUserInfo() {
071: }
072:
073: /**
074: * @throws DBException If the add fails
075: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
076: * method
077: */
078: public synchronized void add() throws DBException {
079: synchronized (MapBasedUserInfo.class) {
080: Map contextMap = (Map) dataContexts.get(this
081: .getDataContext());
082: contextMap.put(new Integer(this .getUid()), this );
083: }
084: }
085:
086: /**
087: * @throws DBException If the delete fails
088: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
089: * method
090: */
091: public synchronized void delete() throws DBException {
092: synchronized (MapBasedUserInfo.class) {
093: Map contextMap = (Map) dataContexts.get(this
094: .getDataContext());
095: contextMap.remove(new Integer(this .getUid()));
096: }
097: }
098:
099: /**
100: * @return true if the user is found
101: * @throws DBException If the find fails
102: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
103: * method
104: */
105: public synchronized boolean find() throws DBException {
106: return false;
107: }
108:
109: /**
110: * Retrieve the current account status.
111: *
112: * @return java.lang.String
113: * @throws DBException If the find fails
114: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
115: * method
116: */
117: public synchronized String getAccountStatus() throws DBException {
118: return accountStatus;
119: }
120:
121: /**
122: * Unsupported operation
123: *
124: * @return java.util.Vector
125: * @throws DBException If there is an error during the retrieval
126: */
127: public Vector getAllUsers() throws DBException {
128: throw new java.lang.UnsupportedOperationException(
129: "Not Supported");
130: }
131:
132: /**
133: * @return java.lang.String
134: * @throws DBException If there is an error during the retrieval
135: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
136: * method
137: */
138: public synchronized String getCreateDate() throws DBException {
139: return createDate;
140: }
141:
142: /**
143: * @return String
144: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
145: * method
146: */
147: public synchronized String getDBName() {
148: if (dataContext == null) {
149: return "default";
150: }
151: return dataContext;
152: }
153:
154: /**
155: * @return String
156: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
157: * method
158: */
159: public synchronized String getDataContext() {
160: if (dataContext == null) {
161: return "default";
162: }
163: return dataContext;
164: }
165:
166: /**
167: * @return java.lang.String
168: * @throws DBException If there is an error during the retrieval
169: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
170: * method
171: */
172: public synchronized String getEmail() throws DBException {
173: return email;
174: }
175:
176: /**
177: * @return java.lang.String
178: * @throws DBException If there is an error during the retrieval
179: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
180: * method
181: */
182: public synchronized String getEmailAuthCode() throws DBException {
183: return emailAuthCode;
184: }
185:
186: /**
187: * @return java.lang.String
188: * @throws DBException If the find fails
189: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
190: * method
191: */
192: public synchronized String getEmailValCode() throws DBException {
193: return emailValCode;
194: }
195:
196: /**
197: * Not Implemented
198: *
199: * @param fieldName The field to retrieve
200: * @return Vector Group names that this user belongs to
201: * @throws DBException If an error occurs when the group info is read
202: * @throws java.lang.UnsupportedOperationException
203: * This is not Implemented
204: * @deprecated Use the direct getLoginName, getEmail, getPassword, etc.
205: */
206: public String getField(String fieldName) throws DBException {
207: throw new java.lang.UnsupportedOperationException(
208: "Not Supported");
209: }
210:
211: /**
212: * Currently unsupported. Returns a blank Vector
213: *
214: * @return Vector Group names that this user belongs to
215: * @throws DBException If an error occurs when the group info is read
216: */
217: public Vector getGroups() throws DBException {
218: return new Vector();
219: }
220:
221: /**
222: * @return java.lang.String
223: * @throws DBException If there is an error during the retrieval
224: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
225: * method
226: */
227: public synchronized String getLoginName() throws DBException {
228: return loginName;
229: }
230:
231: /**
232: * @return java.lang.String
233: * @throws DBException If there is an error during the retrieval
234: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
235: * method
236: */
237: public synchronized String getPassword() throws DBException {
238: return password;
239: }
240:
241: /**
242: * @return name of the primary group of this user; null if no group is
243: * found
244: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
245: * method
246: */
247: public synchronized String getPrimaryGroup() throws DBException {
248: return primaryGroup;
249: }
250:
251: /**
252: * this returns an appropriately hashed password.
253: * @param password to be hashed
254: * @return appropriately hashed password.
255: */
256: public String hashEncodePassword(String password)
257: throws DBException {
258: if (password == null) {
259: throw new DBException("Password Must not be NULL");
260: }
261: if (password.length() == 0) {
262: return password;
263: }
264: try {
265: return Base64.encode(CryptoManager.getInstance()
266: .getStringHash().produceHash(password.getBytes()));
267: } catch (Exception ex) {
268: throw new DBException("Error hashing Password:"
269: + " You may not have installed the"
270: + " Cryptography Extensions Properly:", ex);
271: }
272: }
273:
274: /**
275: * @return java.lang.String
276: * @throws DBException If there is an error during the retrieval
277: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
278: * method
279: */
280: public synchronized boolean getRegComplete() throws DBException {
281: return regComplete;
282: }
283:
284: /**
285: * @return java.lang.String
286: * @throws DBException If the underlying User implementation throws the
287: * same
288: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
289: * method
290: */
291: public synchronized String getRegistrationDomain()
292: throws DBException {
293: return registrationDomain;
294: }
295:
296: /**
297: * @return java.lang.String
298: * @throws DBException If there is an error during the retrieval
299: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
300: * method
301: */
302: public synchronized int getUid() throws DBException {
303: return uid;
304: }
305:
306: /**
307: * @return java.lang.String
308: * @throws DBException If there is an error during the retrieval
309: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
310: * method
311: */
312: public synchronized String getUpdateDate() throws DBException {
313: return updateDate;
314: }
315:
316: /**
317: * @return java.lang.String
318: * @throws DBException If there is an error during the retrieval
319: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
320: * method
321: */
322: public synchronized String getUserName() throws DBException {
323: return loginName;
324: }
325:
326: /**
327: * Unsupported Operation
328: *
329: * @return A ValidValue vector describing what can be looked up.
330: */
331: public Vector getValues() throws DBException {
332: return null;
333: }
334:
335: /**
336: * Checks if the given password equals what we have on file.
337: *
338: * @param testPassword The string to test if it's a correct password
339: * @return true if the testPassword equals the password on file.
340: * @throws DBException If an error occurs when the group info is read
341: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
342: * method
343: */
344: public synchronized boolean passwordEquals(String testPassword)
345: throws DBException {
346: if (password == null) {
347: if (testPassword == null) {
348: return true;
349: } else {
350: return true;
351: }
352: }
353: return password.equals(testPassword);
354: }
355:
356: /**
357: * @throws DBException If the retrieve fails
358: */
359: public synchronized void retrieve() throws DBException {
360: MapBasedUserInfo returnValue = null;
361: synchronized (MapBasedUserInfo.class) {
362: Map contextMap = (Map) dataContexts.get(this
363: .getDataContext());
364: returnValue = (MapBasedUserInfo) contextMap
365: .get(new Integer(this .getUid()));
366: }
367:
368: if (returnValue == null) {
369: throw new DBException("Unable to locate uid: "
370: + this .getUid());
371: }
372:
373: this .setEmail(returnValue.getEmail());
374: this .setAccountStatus(returnValue.getAccountStatus());
375: this .setEmailValCode(this .getEmailValCode());
376: this .setLoginName(this .getLoginName());
377: this .setPassword(this .getPassword());
378: this .setRegComplete(this .getRegComplete());
379: this .setRegistrationDomain(this .getRegistrationDomain());
380: }
381:
382: /**
383: * Not Implemented
384: */
385: public void sendAuthEmail() throws DBException {
386: throw new java.lang.UnsupportedOperationException(
387: "Not supported");
388: }
389:
390: /**
391: * Not Implemented
392: */
393: public void sendFollowUpEmail() throws DBException {
394: throw new java.lang.UnsupportedOperationException(
395: "Not supported");
396: }
397:
398: /**
399: * @param accountStatus java.lang.String
400: * @throws DBException If there is an error
401: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
402: * method
403: */
404: public synchronized void setAccountStatus(String accountStatus)
405: throws DBException {
406: this .accountStatus = accountStatus;
407: }
408:
409: /**
410: * @param newDBName java.lang.String
411: * @throws DBException If there is an error
412: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
413: * method
414: */
415: public synchronized void setDBName(String newDBName)
416: throws DBException {
417: dataContext = newDBName;
418: }
419:
420: /**
421: * @param email java.lang.String
422: * @throws DBException If there is an error
423: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
424: * method
425: */
426: public synchronized void setEmail(String email) throws DBException {
427: this .email = email;
428: }
429:
430: /**
431: * Sets the code required for auth.
432: *
433: * @param code java.lang.String
434: * @throws DBException If there is an error
435: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
436: * method
437: */
438: public synchronized void setEmailValCode(String code)
439: throws DBException {
440: this .emailValCode = code;
441: }
442:
443: /**
444: * @param loginName java.lang.String
445: * @throws DBException If there is an error
446: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
447: * method
448: */
449: public synchronized void setLoginName(String loginName)
450: throws DBException {
451: this .loginName = loginName;
452: }
453:
454: /**
455: * @param password java.lang.String
456: * @throws DBException If there is an error
457: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
458: * method
459: */
460: public synchronized void setPassword(String password)
461: throws DBException {
462: this .password = password;
463: }
464:
465: /**
466: * @param status java.lang.String
467: * @throws DBException If there is an error
468: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
469: * method
470: */
471: public synchronized void setRegComplete(boolean status)
472: throws DBException {
473: this .regComplete = status;
474: }
475:
476: /**
477: * @param id java.lang.String
478: * @throws DBException If there is an error
479: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
480: * method
481: */
482: public synchronized void setRegistrationDomain(String id)
483: throws DBException {
484: this .registrationDomain = id;
485: }
486:
487: /**
488: * @param uid The uid of the user
489: * @throws DBException If there is an error
490: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
491: * method
492: */
493: public synchronized void setUid(int uid) throws DBException {
494: this .uid = uid;
495: }
496:
497: /**
498: * @param name java.lang.String
499: * @throws DBException If there is an error
500: * @todo Implement this com.jcorporate.expresso.core.security.UserInfo
501: * method
502: */
503: public synchronized void setUserName(String name)
504: throws DBException {
505: this .loginName = name;
506: }
507:
508: /**
509: * There should be no copying and detaching from the datasource. This
510: * does nothing.
511: *
512: * @throws DBException If the add fails
513: */
514: public void update() throws DBException {
515: return;
516: }
517: }
|