📄 dbuser.java
字号:
/**
* $RCSfile: DbUser.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:50:59 $
*
* New Jive from Jdon.com.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/
package com.jivesoftware.forum.database;
import com.jivesoftware.forum.*;
import com.jivesoftware.util.*;
import java.util.*;
import java.text.*;
import java.sql.*;
import java.security.*;
/**
* Database implementation of the User interface. Additionally, it filters all
* HTML tags from fields before returning them for security purposes.<p>
*
* The DbUser keeps track of a user's reward points. Optionally, you
* can specify a initial number of reward points to assign a newly created user
* by setting the Jive property <tt>rewards.initialPoints</tt>. If no value is
* set, the default will be 0 points.
*/
public class DbUser implements User, Cacheable {
/** DATABASE QUERIES **/
private static final String LOAD_PROPERTIES =
"SELECT name, propValue FROM jiveUserProp WHERE userID=?";
private static final String DELETE_PROPERTY =
"DELETE FROM jiveUserProp WHERE userID=? AND name=?";
private static final String DELETE_PROPERTIES =
"DELETE FROM jiveUserProp WHERE userID=?";
private static final String INSERT_PROPERTY =
"INSERT INTO jiveUserProp(userID,name,propValue) VALUES(?,?,?)";
private static final String LOAD_USER_BY_USERNAME =
"SELECT userID,username,passwordHash,name,nameVisible,email,emailVisible,"+
"rewardPoints,creationDate,modifiedDate FROM jiveUser WHERE username=?";
private static final String LOAD_USER_BY_ID =
"SELECT userID,username,passwordHash,name,nameVisible,email,emailVisible,"+
"rewardPoints,creationDate,modifiedDate FROM jiveUser WHERE userID=?";
private static final String INSERT_USER =
"INSERT INTO jiveUser(userID,username,passwordHash,name,nameVisible," +
"email,emailVisible,rewardPoints, creationDate,modifiedDate) " +
"VALUES(?,?,?,?,?,?,?,?,?,?)";
private static final String SAVE_USER =
"UPDATE jiveUser SET passwordHash=?,name=?,nameVisible=?,email=?," +
"emailVisible=?,rewardPoints=?,creationDate=?,modifiedDate=? WHERE " +
"userID=?";
private static final String SAVE_REWARD_POINTS =
"UPDATE jiveUser SET rewardPoints=? WHERE userID=?";
/**
* Controls whether extended properties should be lazily loaded (not loaded
* until requested). If the properties are infrequently used, this provides
* a great speedup in initial object loading time. However, if your
* application does use extended properties all the time, you may wish to
* turn lazy loading off, as it's actually faster in total db lookup time
* to load everything at once.
*/
private static final boolean LAZY_PROP_LOADING = true;
private static final ForumPermissions USER_ADMIN_PERMS =
new ForumPermissions(false,false,false,true,false,false,false,false,false);
private static int initialRewardPoints;
static {
// Load the default reward points to assign new users. Default is 0.
initialRewardPoints = 0;
// Load a user defined value if specified.
try {
String points = JiveGlobals.getJiveProperty("rewards.initialPoints");
if (points != null) {
initialRewardPoints = Integer.parseInt(points);
}
}
catch(Exception e) { }
}
/**
* User id of -2 means no user id has been set yet. -1 is reserved for
* "anonymous user" and 0 is reserved for "all users".
*/
private long id = -2;
private String username = null;
private String passwordHash;
private String name = "";
private boolean nameVisible = true;
private String email;
private boolean emailVisible = true;
private int rewardPoints;
private java.util.Date creationDate;
private java.util.Date modifiedDate;
private Map properties;
private DbForumFactory factory;
/**
* Create a new DbUser with all required fields.
*
* @param username the username for the user.
* @param password a password for the user.
* @param email the email address for the user.
*/
protected DbUser(String username, String password, String email,
DbForumFactory factory)
{
this.id = SequenceManager.nextID(JiveGlobals.USER);
this.username = username;
// Compute hash of password.
this.passwordHash = StringUtils.hash(password);
this.email = email;
this.factory = factory;
// Set the reward points to the default.
this.rewardPoints = initialRewardPoints;
long now = System.currentTimeMillis();
creationDate = new java.util.Date(now);
modifiedDate = new java.util.Date(now);
properties = new Hashtable();
insertIntoDb();
}
/**
* Create a new DbUser with all required AND optional fields. Using this
* constructor can result in significantly fewer database operations than
* calling the other create constructor and then using setter methods.
*
* @param username the username for the user.
* @param password a password for the user.
* @param email the email address for the user.
*/
protected DbUser(String username, String password, String name, String email,
boolean nameVisisble, boolean emailVisible, Map properties,
DbForumFactory factory)
{
this.id = SequenceManager.nextID(JiveGlobals.USER);
this.username = username;
// Compute hash of password.
this.passwordHash = StringUtils.hash(password);
this.email = email;
if (name != null) {
this.name = name;
}
this.nameVisible = nameVisible;
this.emailVisible = emailVisible;
this.rewardPoints = initialRewardPoints;
this.factory = factory;
long now = System.currentTimeMillis();
creationDate = new java.util.Date(now);
modifiedDate = new java.util.Date(now);
if (properties != null) {
// Make sure that the none of the properties are null values.
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
String pName = (String)iter.next();
if (pName == null || "".equals(pName)) {
throw new NullPointerException(
"Cannot set property with empty or null value."
);
}
}
iter = properties.values().iterator();
while (iter.hasNext()) {
String pValue = (String)iter.next();
if (pValue == null || "".equals(pValue)) {
throw new NullPointerException(
"Cannot set property with empty or null value."
);
}
}
// No null found, set the properties.
this.properties = properties;
}
else {
this.properties = new Hashtable();
}
insertIntoDb();
}
/**
* Load a DbUser object specified by userID.
*
* @param userID the userID of the user to load.
*/
protected DbUser(long userID, DbForumFactory factory)
throws UserNotFoundException
{
if (userID < 1) {
throw new UserNotFoundException();
}
this.id = userID;
this.factory = factory;
loadFromDb();
}
/**
* Load a DbUser object specified by username.
*
* @param username the username of the user to load.
*/
protected DbUser(String username, DbForumFactory factory)
throws UserNotFoundException
{
if (username == null) {
throw new UserNotFoundException();
}
this.username = username;
this.factory = factory;
loadFromDb();
}
//FROM THE USER INTERFACE//
public long getID() {
return id;
}
public String getUsername() {
return StringUtils.escapeHTMLTags(username);
}
public String getName() {
return StringUtils.escapeHTMLTags(name);
}
public void setName(String name) throws UnauthorizedException {
this.name = name;
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public boolean isNameVisible() {
return nameVisible;
}
public void setNameVisible(boolean visible) throws UnauthorizedException {
this.nameVisible = visible;
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public void setPassword(String password) throws UnauthorizedException {
// Compute hash of password.
this.passwordHash = StringUtils.hash(password);
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public String getPasswordHash() throws UnauthorizedException {
return passwordHash;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public String getEmail() {
return StringUtils.escapeHTMLTags(email);
}
public void setEmail(String email) throws UnauthorizedException {
this.email = email;
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public boolean isEmailVisible() {
return emailVisible;
}
public void setEmailVisible(boolean visible) throws UnauthorizedException {
this.emailVisible = visible;
// Update modified date
modifiedDate.setTime(System.currentTimeMillis());
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public java.util.Date getCreationDate() {
return creationDate;
}
public void setCreationDate(java.util.Date creationDate)
throws UnauthorizedException
{
this.creationDate = creationDate;
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public java.util.Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(java.util.Date modifiedDate)
throws UnauthorizedException
{
this.modifiedDate = modifiedDate;
saveToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public String getProperty(String name) {
if (LAZY_PROP_LOADING && properties == null) {
loadPropertiesFromDb();
}
return StringUtils.escapeHTMLTags((String)properties.get(name));
}
public void setProperty(String name, String value) {
if (LAZY_PROP_LOADING && properties == null) {
loadPropertiesFromDb();
}
// Make sure the property name and value aren't null.
if (name == null || value == null || "".equals(name) || "".equals(value)) {
throw new NullPointerException("Cannot set property with empty or null value.");
}
properties.put(name, value);
savePropertiesToDb();
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
}
public void deleteProperty(String name) {
if (LAZY_PROP_LOADING) {
if (properties == null) {
loadPropertiesFromDb();
}
}
properties.remove(name);
deletePropertyFromDb(name);
// Remove user from cache
factory.cacheManager.userCache.remove(this.id);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -