org.locomotive.loco
Class User

java.lang.Object
  |
  +--org.locomotive.loco.User

public class User
extends java.lang.Object
implements java.io.Serializable, javax.servlet.http.HttpSessionBindingListener

The User object encapsulates information stored for a user in the Locomotive User Management system. It is passed to modules as a member of the the HandlerData Object. The user can be "anonymous", or can be a specific user whose content is persistantly stored in the User Database. Modules wishing to store additional module-specific data based on users should use the userid field as a key. If the current session is non-anonymous (aka "authenticated"), the User object will contain pre-loaded user specific data. Given that it will be loaded every hit, the User object only contains the most important data, for performance reasons.

See Also:
Serialized Form

Field Summary
static int ANONYMOUS_ID
           
static java.lang.String ANONYMOUS_NAME
           
static int LOCO_FLAGS_CAN_CHANGE_LOCO_VARS
           
static int LOCO_FLAGS_CAN_EDIT_USER_PREFS
           
static int LOCO_FLAGS_CAN_SHUT_DOWN_LOCO
           
static int LOCO_FLAGS_CAN_VIEW_SERVER_STATUS
           
static int LOCO_FLAGS_CAN_VIEW_USER_PREFS
           
static int LOCO_FLAGS_DEFAULT
           
static int MIN_ID
           
static int NULL_USERID
           
 
Constructor Summary
User()
          create an anonymous User
User(java.sql.Connection conn, int uid)
          loads persistant User data from the user database.
User(java.sql.Connection conn, int uid, java.lang.String un, java.lang.String pword, int locoflg)
          creates User and persistantly stores User's data.
User(java.sql.Connection conn, int uid, java.lang.String un, java.lang.String pword, java.lang.String fn, java.lang.String ln, int flg, int locoflg)
          Deprecated. thanks to schema changes for the loco_user table. fn, ln, flg are all now ignored.
User(java.sql.Connection conn, java.lang.String un)
          loads persistant User data from the user database.
User(java.sql.Connection conn, java.lang.String un, boolean sensitiveToCase)
          loads persistant User data from the user database.
User(int uid, java.lang.String un, java.lang.String pword, int locoflg)
          used internally by loadUsers (Connection, int[]) to create the heap of individual User objects.
 
Method Summary
 boolean canChangeLocoVars()
           
 boolean canEditUserPrefs()
           
 boolean canShutDownLoco()
           
 boolean canViewServerStatus()
           
 boolean canViewUserPrefs()
           
 boolean equals(java.lang.Object o)
          overrides Object.equals for User specific checking
static boolean exists(java.sql.Connection conn, int id)
          returns true if a user with this userid exists in the user database
static boolean exists(java.sql.Connection conn, java.lang.String un)
          returns true if a user in the user database has the same name
static boolean existsAnyCase(java.sql.Connection conn, java.lang.String un)
          returns true if a user in the user database has the same name, regardless of case.
static java.lang.String generateNewPassword(int seedmodifier)
          Generates a character sequence of length 8 suitable for a password (though probably not easy to remember)
 int getAnonymousUserid()
          gets the userid of the Anonymous User
 java.lang.String getAnonymousUsername()
          gets the username of the Anonymous User
 int getLocoFlags()
          gets the value for this User's Loco-specific flags
 java.lang.String getPassword()
          returns the value of the User's password Note that later this might be made package accessable only
 int getUserid()
          gets the userid of this User
 java.lang.String getUsername()
          gets username of this User
static java.lang.String getUsername(java.sql.Connection conn, int userid)
          Given a userid, return the username
static java.util.Vector getUsernames(java.sql.Connection conn, java.util.Vector userids)
          Given a vector of Integer-ized userids, look up each username for them.
 boolean hasPassword(java.lang.String pwtry)
          tests to see if the stored password is equivalent to the input
 boolean hasPasswordAnyCase(java.lang.String pwtry)
          tests to see if the stored password is equivalent to the input in any case (i.e.
protected static void initialize()
          initializes the config object.
 boolean isAnonymous()
          checks whether this user is an anonymous user.
static java.lang.String[] loadAllUserNames(java.sql.Connection conn)
          Returns an array of all the user names in the database.
static User[] loadUsers(java.sql.Connection conn, int[] userids)
          Searches the user database and returns a list of Users, based upon userid.
 void save(java.sql.Connection conn)
          Updates the User data in the database if changes have been made, silently ignored on anonymous users
 void saveLoginTime(java.sql.Connection conn)
          Updates the user.last_login field in the database.
 void setLocoFlags(int newlocoflags)
          sets the loco_flags value for this User
 void setPassword(java.lang.String pw)
          sets the password value for this User
 void setUsername(java.lang.String un)
          sets the username value for this User Note: this may be made package friendly only in the future
 java.lang.String toString()
          Returns the userid and username
 void valueBound(javax.servlet.http.HttpSessionBindingEvent hsbe)
          If the name of the event is 'locomotive.user', this will go ahead and associate this user with this session in the database.
 void valueUnbound(javax.servlet.http.HttpSessionBindingEvent hsbe)
          We don't really need to do anything here
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOCO_FLAGS_DEFAULT

public static final int LOCO_FLAGS_DEFAULT

LOCO_FLAGS_CAN_SHUT_DOWN_LOCO

public static final int LOCO_FLAGS_CAN_SHUT_DOWN_LOCO

LOCO_FLAGS_CAN_VIEW_SERVER_STATUS

public static final int LOCO_FLAGS_CAN_VIEW_SERVER_STATUS

LOCO_FLAGS_CAN_CHANGE_LOCO_VARS

public static final int LOCO_FLAGS_CAN_CHANGE_LOCO_VARS

LOCO_FLAGS_CAN_VIEW_USER_PREFS

public static final int LOCO_FLAGS_CAN_VIEW_USER_PREFS

LOCO_FLAGS_CAN_EDIT_USER_PREFS

public static final int LOCO_FLAGS_CAN_EDIT_USER_PREFS

NULL_USERID

public static final int NULL_USERID

ANONYMOUS_ID

public static final int ANONYMOUS_ID

MIN_ID

public static final int MIN_ID

ANONYMOUS_NAME

public static final java.lang.String ANONYMOUS_NAME
Constructor Detail

User

public User()
create an anonymous User

User

public User(java.sql.Connection conn,
            int uid)
     throws LoadUserException,
            UserNotFoundException
loads persistant User data from the user database.
Parameters:
conn - a JDBC connection with access to the Loco user database
uid - the userid of the user
Throws:
LoadUserException - if there is a database access error
UserNotFoundException - if a user of the specified userid cannot be found

User

public User(java.sql.Connection conn,
            java.lang.String un)
     throws LoadUserException,
            UserNotFoundException
loads persistant User data from the user database.
Parameters:
conn - a JDBC connection with access to the Loco user database
un - the username of the user
Throws:
LoadUserException - if there is a database access error
UserNotFoundException - if a user of the specified userid cannot be found

User

public User(java.sql.Connection conn,
            java.lang.String un,
            boolean sensitiveToCase)
     throws LoadUserException,
            UserNotFoundException
loads persistant User data from the user database.
Parameters:
conn - a JDBC connection with access to the Loco user database
un - the username of the user
sensitiveToCase - whether or not to pay attention to case
Throws:
LoadUserException - if there is a database access error
UserNotFoundException - if a user of the specified userid cannot be found

User

public User(java.sql.Connection conn,
            int uid,
            java.lang.String un,
            java.lang.String pword,
            java.lang.String fn,
            java.lang.String ln,
            int flg,
            int locoflg)
     throws LoadUserException
Deprecated. thanks to schema changes for the loco_user table. fn, ln, flg are all now ignored.

creates User and persistantly stores User's data. Will fail if uid is not unique (uid <= 0 will cause system to pick a userid). Will also fail if un (intended username) is already used.
Parameters:
conn - a JDBC connection with access to the Loco user database
uid - the userid, system will pick if <= 0
un - username of the new user
pword - password of the new user
fn - firstname of the new user
ln - lastname of the new user
flg - flags of the new user
locoflg - flags of the new user for Loco-specific functions
Throws:
LoadUserException - if there is a database storage error

User

public User(java.sql.Connection conn,
            int uid,
            java.lang.String un,
            java.lang.String pword,
            int locoflg)
     throws LoadUserException
creates User and persistantly stores User's data. Will fail if uid is not unique (uid <= 0 will cause system to pick a userid). Will also fail if un (intended username) is already used.
Parameters:
conn - a JDBC connection with access to the Loco user database
uid - the userid, system will pick if <= 0
un - username of the new user
pword - password of the new user
locoflg - flags of the new user for Loco-specific functions
Throws:
LoadUserException - if there is a database storage error

User

public User(int uid,
            java.lang.String un,
            java.lang.String pword,
            int locoflg)
used internally by loadUsers (Connection, int[]) to create the heap of individual User objects. DW change visibility from private to public so can be used by UserStore
Method Detail

initialize

protected static void initialize()
initializes the config object. Run this if the config object has been reloaded.

getUserid

public int getUserid()
gets the userid of this User

getAnonymousUserid

public int getAnonymousUserid()
gets the userid of the Anonymous User

getUsername

public java.lang.String getUsername()
gets username of this User

getAnonymousUsername

public java.lang.String getAnonymousUsername()
gets the username of the Anonymous User

getPassword

public java.lang.String getPassword()
returns the value of the User's password Note that later this might be made package accessable only

getLocoFlags

public int getLocoFlags()
gets the value for this User's Loco-specific flags

setLocoFlags

public void setLocoFlags(int newlocoflags)
sets the loco_flags value for this User
Parameters:
newlocoflags - the new value for loco_flags

setUsername

public void setUsername(java.lang.String un)
sets the username value for this User Note: this may be made package friendly only in the future
Parameters:
un - the new username value

setPassword

public void setPassword(java.lang.String pw)
sets the password value for this User
Parameters:
pw - the new value for the User's password

hasPassword

public boolean hasPassword(java.lang.String pwtry)
tests to see if the stored password is equivalent to the input
Parameters:
pwtry - the password to compare for a match

hasPasswordAnyCase

public boolean hasPasswordAnyCase(java.lang.String pwtry)
tests to see if the stored password is equivalent to the input in any case (i.e. lower(password) == lower(try))
Parameters:
pwtry - the password to compare for a match

isAnonymous

public boolean isAnonymous()
checks whether this user is an anonymous user.

canShutDownLoco

public boolean canShutDownLoco()

canViewServerStatus

public boolean canViewServerStatus()

canChangeLocoVars

public boolean canChangeLocoVars()

canViewUserPrefs

public boolean canViewUserPrefs()

canEditUserPrefs

public boolean canEditUserPrefs()

toString

public java.lang.String toString()
Returns the userid and username
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
overrides Object.equals for User specific checking
Overrides:
equals in class java.lang.Object

valueBound

public void valueBound(javax.servlet.http.HttpSessionBindingEvent hsbe)
If the name of the event is 'locomotive.user', this will go ahead and associate this user with this session in the database. We'll try to cast the HttpSession to a org.locomotive.loco.Session, and if that fails, we'll do nothing.
Specified by:
valueBound in interface javax.servlet.http.HttpSessionBindingListener

valueUnbound

public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent hsbe)
We don't really need to do anything here
Specified by:
valueUnbound in interface javax.servlet.http.HttpSessionBindingListener

exists

public static boolean exists(java.sql.Connection conn,
                             int id)
                      throws LoadUserException
returns true if a user with this userid exists in the user database
Parameters:
conn - a JDBC connection with access to the Loco user database
id - the userid of the user to look for
Throws:
LoadUserException - if there is a database access error

exists

public static boolean exists(java.sql.Connection conn,
                             java.lang.String un)
                      throws LoadUserException
returns true if a user in the user database has the same name
Parameters:
conn - a JDBC connection with access to the Loco user database
un - the username of the user to look for
Throws:
LoadUserException - if there is a database access error

existsAnyCase

public static boolean existsAnyCase(java.sql.Connection conn,
                                    java.lang.String un)
                             throws LoadUserException
returns true if a user in the user database has the same name, regardless of case. e.g. user "bob" exists, even though the userbase only contains a user "Bob"
Parameters:
conn - a JDBC connection with access to the Loco user database
un - the username of the user to look for
Throws:
LoadUserException - if there is a database access error

saveLoginTime

public void saveLoginTime(java.sql.Connection conn)
                   throws LoadUserException
Updates the user.last_login field in the database. silently ignored on anonymous users
Parameters:
conn - an active JDBC connection
Throws:
LoadUserException - if there is a database storage error

save

public void save(java.sql.Connection conn)
          throws LoadUserException
Updates the User data in the database if changes have been made, silently ignored on anonymous users
Parameters:
conn - an active JDBC connection
Throws:
LoadUserException - if there is a database storage error

loadUsers

public static User[] loadUsers(java.sql.Connection conn,
                               int[] userids)
                        throws LoadUserException
Searches the user database and returns a list of Users, based upon userid.
Parameters:
conn - a JDBC connection
userids - a list of userids to retrieve
Throws:
LoadUserException - if there is a database storage error

loadAllUserNames

public static java.lang.String[] loadAllUserNames(java.sql.Connection conn)
                                           throws LoadUserException
Returns an array of all the user names in the database.
Parameters:
conn - a JDBC connection
Throws:
LoadUserException - if there is a database storage error

getUsernames

public static java.util.Vector getUsernames(java.sql.Connection conn,
                                            java.util.Vector userids)
                                     throws java.sql.SQLException
Given a vector of Integer-ized userids, look up each username for them.

getUsername

public static java.lang.String getUsername(java.sql.Connection conn,
                                           int userid)
                                    throws java.sql.SQLException
Given a userid, return the username

generateNewPassword

public static java.lang.String generateNewPassword(int seedmodifier)
Generates a character sequence of length 8 suitable for a password (though probably not easy to remember)
Parameters:
seedmodifier - seed used to assist random character generation