[cvs] expresso commit by lhamel: add convenience for getting dbname
from
JCorporate Ltd
jcorp at jcorporate.com
Thu Dec 9 02:05:11 UTC 2004
Log Message:
-----------
add convenience for getting dbname from RequestRegistry
Modified Files:
--------------
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security:
User.java
Revision Data
-------------
Index: User.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java,v
retrieving revision 1.48
retrieving revision 1.49
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java -u -r1.48 -r1.49
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/security/User.java
@@ -81,13 +81,13 @@
import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
import com.jcorporate.expresso.core.dbobj.ValidValue;
import com.jcorporate.expresso.core.logging.LogException;
-import com.jcorporate.expresso.core.misc.Base64;
import com.jcorporate.expresso.core.misc.ByteArrayDataSource;
import com.jcorporate.expresso.core.misc.ConfigManager;
import com.jcorporate.expresso.core.misc.ConfigurationException;
import com.jcorporate.expresso.core.misc.EMailSender;
import com.jcorporate.expresso.core.misc.EventHandler;
import com.jcorporate.expresso.core.misc.StringUtil;
+import com.jcorporate.expresso.core.registry.RequestRegistry;
import com.jcorporate.expresso.kernel.util.FastStringBuffer;
import com.jcorporate.expresso.services.dbobj.UserGroup;
import org.apache.log4j.Logger;
@@ -130,7 +130,7 @@
protected static UserInfo notLoggedInUser = null;
// The DB context that this User resides in
- private String dbName = "default";
+ private String dbName = null;
/**
* The log4j logger.
@@ -428,6 +428,17 @@
* @return java.util.String
*/
public String getDataContext() {
+ if ((dbName == null) || (dbName.trim().equals(""))) {
+ dbName = RequestRegistry.getDataContext();
+ if (dbName == null) {
+ dbName = DBConnection.DEFAULT_DB_CONTEXT_NAME;
+ }
+ try {
+ getUserInfo().setDBName(dbName);
+ } catch (DBException e) {
+ log.error("Cannot set userinfo dbname", e);
+ }
+ }
return dbName;
} /* getDBName() */
@@ -693,36 +704,6 @@
}
/**
- * hashEncodePassword: If passed a plaintext string > 0 bytes, then this
- * function will hash the password, then Base64 encode it and return it
- * as a string.
- * <p/>
- * If the password is zero length, then it will simply return a zero length
- * string also.
- *
- * @param plaintext - the password to process in this manner: <br>
- * returnValue = Base64(SHA-1(plaintext))
- * @return a String representing the password hashencoded
- */
- private String hashEncodePassword(String plaintext)
- throws DBException {
- if (plaintext == null) {
- throw new DBException(thisClass + "hashEncodePassword()" + ": Password Must not be NULL");
- }
- if (plaintext.length() == 0) {
- return plaintext;
- }
- try {
- return Base64.encode(CryptoManager.getInstance().getStringHash().produceHash(plaintext.getBytes()));
- } catch (Exception ex) {
- throw new DBException(thisClass + "hashEncodePassword()" + ":Error hashing Password:" +
- " You may not have installed the" +
- " Cryptography Extensions Properly:", ex);
- }
- } /* hashEncodePassword(String) */
-
-
- /**
* Send this user a notification via e-mail.
*
* @param subject Subject of the e-mail
@@ -771,7 +752,7 @@
try {
EMailSender ems = new EMailSender();
- ems.setDBName(this.getDataContext());
+ ems.setDataContext(getDataContext());
if (htmlFormat) {
ems.setEmailHtmlFormat();
}
@@ -969,7 +950,7 @@
try { // create some properties and get the default Session
EMailSender ems = new EMailSender();
- ems.setDBName(getDataContext());
+ ems.setDataContext(getDataContext());
ems.addFileAttachments(fileNames);
ems.send(sendToUser, subject, message);
} catch (Exception e) {
@@ -1214,17 +1195,19 @@
/**
* determine admin id (usually '3')
- * WARNING: assumes dbname = 'default'
+ * uses db context of RequestRegistry, or 'default' if none found
*
* @return the uid of user "Admin"
* @throws DBException upon error
*/
public static int getAdminId() throws DBException {
- return getAdminId(DBConnection.DEFAULT_DB_CONTEXT_NAME);
+ String dbname = RequestRegistry.getDataContext();
+ if ( dbname == null ) dbname = DBConnection.DEFAULT_DB_CONTEXT_NAME;
+ return getAdminId(dbname);
}
/**
- * Retrieves the User instance of the current user.
+ * Retrieves the User instance of the Admin user.
*
* @param dbName String the data context to check.
* @return User a User instance.
@@ -1243,6 +1226,23 @@
}
/**
+ * Retrieves the User instance of the Admin user.
+ * uses db context of RequestRegistry
+ * @return User a User instance.
+ * @throws DBException if there was no admin user found or
+ * other database errors.
+ */
+ public static User getAdmin() throws DBException {
+ User user = new User();
+ user.setLoginName(ADMIN_USER);
+ if (!user.find()) {
+ throw new DBException("Unable to locate: " + ADMIN_USER);
+ }
+
+ return user;
+ }
+
+ /**
* determine admin id (usually '3')
*
* @param dbname the Database context to use
@@ -1288,7 +1288,7 @@
}
/**
- * utility
+ * utility for translating ID from login name
*
* @param login the login name
* @param myDBname the DataContext to use.
@@ -1307,6 +1307,29 @@
}
/**
+ * utility for translating ID from login name
+ * uses db context of RequestRegistry
+ *
+ * @param login the login name
+ * @return the integer uid of the given login
+ * @throws DBException upon databaes access error or if the uid cannot be found.
+ */
+ public static int getIdFromLogin(String login) throws DBException {
+ User user = new User();
+ String dbname = RequestRegistry.getDataContext();
+ if (dbname == null) {
+ dbname = DBConnection.DEFAULT_DB_CONTEXT_NAME;
+ }
+ user.setDataContext(dbname);
+ user.setLoginName(login);
+ if (!user.find()) {
+ throw new DBException("cannot find user: " + login);
+ }
+
+ return user.getUid();
+ }
+
+ /**
* Pull up the User object associated with the integer id
*
* @param uid the integer user id
@@ -1325,7 +1348,29 @@
}
/**
- * utility
+ * Pull up the User object associated with the integer id
+ * uses db context of RequestRegistry
+ *
+ * @param uid the integer user id
+ * @return a built User object
+ * @throws DBException if the user is unable to be found.
+ */
+ public static User getUserFromId(int uid) throws DBException {
+ User user = new User();
+ String dbname = RequestRegistry.getDataContext();
+ if (dbname == null) {
+ dbname = DBConnection.DEFAULT_DB_CONTEXT_NAME;
+ }
+ user.setDataContext(dbname);
+ user.setUid(uid);
+ if (!user.find()) {
+ throw new DBException("cannot find user: " + uid);
+ }
+ return user;
+ }
+
+ /**
+ * utility for translating login name from ID
*
* @param uid the uid of the user
* @param dataContext the data context to use
@@ -1337,6 +1382,19 @@
return user.getLoginName();
}
+ /**
+ * utility for translating login name from ID
+ * uses db context of RequestRegistry
+ *
+ * @param uid the uid of the user
+ * @return the Login for the given UID
+ * @throws DBException upon database access error or if the given id cannot be found.
+ */
+ public static String getLoginFromId(int uid) throws DBException {
+ User user = User.getUserFromId(uid);
+ return user.getLoginName();
+ }
+
/**
* Given a 'friendly name' get the key value for this object. Example, mappedValue
@@ -1428,7 +1486,7 @@
* @return java.lang.String. Should never be null.
*/
public String getDBName() {
- return this.getDataContext();
+ return getDataContext();
}
/**
More information about the cvs
mailing list