com.ecyrd.jspwiki.auth.user
Class AbstractUserDatabase

java.lang.Object
  extended by com.ecyrd.jspwiki.auth.user.AbstractUserDatabase
All Implemented Interfaces:
UserDatabase
Direct Known Subclasses:
JDBCUserDatabase, UserManager.DummyUserDatabase, XMLUserDatabase

public abstract class AbstractUserDatabase
extends Object
implements UserDatabase

Abstract UserDatabase class that provides convenience methods for finding profiles, building Principal collections and hashing passwords.

Since:
2.3
Version:
$Revision: 1.4 $ $Date: 2006/01/11 03:46:21 $
Author:
Andrew R. Jaquith

Field Summary
protected static org.apache.log4j.Logger log
           
protected static String PROP_SHARED_WITH_CONTAINER
           
protected static String SHA_PREFIX
           
 
Constructor Summary
AbstractUserDatabase()
           
 
Method Summary
abstract  void commit()
          Persists the current state of the user database to back-end storage.
 UserProfile find(String index)
          Looks up and returns the first UserProfilein the user database that whose login name, full name, or wiki name matches the supplied string.
abstract  UserProfile findByEmail(String index)
          Looks up and returns the first UserProfile in the user database that matches a profile having a given e-mail address.
abstract  UserProfile findByFullName(String index)
          Looks up and returns the first UserProfile in the user database that matches a profile having a given full name.
abstract  UserProfile findByLoginName(String index)
          Looks up and returns the first UserProfile in the user database that matches a profile having a given login name.
abstract  UserProfile findByWikiName(String index)
          Looks up and returns the first UserProfile in the user database that matches a profile having a given wiki name.
protected  String getHash(String text)
          Private method that calculates the SHA-1 hash of a given String
 Principal[] getPrincipals(String identifier)
          Looks up the Principals representing a user from the user database.
abstract  void initialize(WikiEngine engine, Properties props)
          Initializes the user database based on values from a Properties object.
 UserProfile newProfile()
          Factory method that instantiates a new DefaultUserProfile.
abstract  void save(UserProfile profile)
           Saves a UserProfileto the user database, overwriting the existing profile if it exists.
 boolean validatePassword(String loginName, String password)
          Validates the password for a given user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.ecyrd.jspwiki.auth.user.UserDatabase
deleteByLoginName, getWikiNames, isSharedWithContainer
 

Field Detail

log

protected static final org.apache.log4j.Logger log

SHA_PREFIX

protected static final String SHA_PREFIX
See Also:
Constant Field Values

PROP_SHARED_WITH_CONTAINER

protected static final String PROP_SHARED_WITH_CONTAINER
See Also:
Constant Field Values
Constructor Detail

AbstractUserDatabase

public AbstractUserDatabase()
Method Detail

commit

public abstract void commit()
                     throws WikiSecurityException
Description copied from interface: UserDatabase
Persists the current state of the user database to back-end storage. This method is intended to be atomic; results cannot be partially committed. If the commit fails, it should roll back its state appropriately. Implementing classes that persist to the file system may wish to make this method synchronized.

Specified by:
commit in interface UserDatabase
Throws:
WikiSecurityException
See Also:
UserDatabase.commit()

find

public UserProfile find(String index)
                 throws NoSuchPrincipalException
Looks up and returns the first UserProfilein the user database that whose login name, full name, or wiki name matches the supplied string. This method provides a "forgiving" search algorithm for resolving principal names when the exact profile attribute that supplied the name is unknown.

Specified by:
find in interface UserDatabase
Parameters:
index - the login name, full name, or wiki name
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.find(java.lang.String)

findByEmail

public abstract UserProfile findByEmail(String index)
                                 throws NoSuchPrincipalException
Description copied from interface: UserDatabase
Looks up and returns the first UserProfile in the user database that matches a profile having a given e-mail address. If the user database does not contain a user with a matching attribute, throws a NoSuchPrincipalException.

Specified by:
findByEmail in interface UserDatabase
Parameters:
index - the e-mail address of the desired user profile
Returns:
the user profile
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.findByEmail(java.lang.String)

findByFullName

public abstract UserProfile findByFullName(String index)
                                    throws NoSuchPrincipalException
Description copied from interface: UserDatabase
Looks up and returns the first UserProfile in the user database that matches a profile having a given full name. If the user database does not contain a user with a matching attribute, throws a NoSuchPrincipalException.

Specified by:
findByFullName in interface UserDatabase
Parameters:
index - the fill name of the desired user profile
Returns:
the user profile
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.findByFullName(java.lang.String)

findByLoginName

public abstract UserProfile findByLoginName(String index)
                                     throws NoSuchPrincipalException
Description copied from interface: UserDatabase
Looks up and returns the first UserProfile in the user database that matches a profile having a given login name. If the user database does not contain a user with a matching attribute, throws a NoSuchPrincipalException.

Specified by:
findByLoginName in interface UserDatabase
Parameters:
index - the login name of the desired user profile
Returns:
the user profile
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.findByLoginName(java.lang.String)

findByWikiName

public abstract UserProfile findByWikiName(String index)
                                    throws NoSuchPrincipalException
Description copied from interface: UserDatabase
Looks up and returns the first UserProfile in the user database that matches a profile having a given wiki name. If the user database does not contain a user with a matching attribute, throws a NoSuchPrincipalException.

Specified by:
findByWikiName in interface UserDatabase
Parameters:
index - the wiki name of the desired user profile
Returns:
the user profile
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.findByWikiName(java.lang.String)

getPrincipals

public Principal[] getPrincipals(String identifier)
                          throws NoSuchPrincipalException

Looks up the Principals representing a user from the user database. These are defined as a set of WikiPrincipals manufactured from the login name, full name, and wiki name. If the user database does not contain a user with the supplied identifier, throws a NoSuchPrincipalException.

When this method creates WikiPrincipals, the Principal containing the user's full name is marked as containing the common name (see WikiPrincipal.WikiPrincipal(String, String)).

Specified by:
getPrincipals in interface UserDatabase
Parameters:
identifier - the name of the principal to retrieve; this corresponds to value returned by the user profile's UserProfile.getLoginName()method.
Returns:
the array of Principals representing the user
Throws:
NoSuchPrincipalException
See Also:
UserDatabase.getPrincipals(java.lang.String)

initialize

public abstract void initialize(WikiEngine engine,
                                Properties props)
                         throws NoRequiredPropertyException
Description copied from interface: UserDatabase
Initializes the user database based on values from a Properties object.

Specified by:
initialize in interface UserDatabase
Throws:
NoRequiredPropertyException
See Also:
UserDatabase.initialize(com.ecyrd.jspwiki.WikiEngine, java.util.Properties)

newProfile

public UserProfile newProfile()
Factory method that instantiates a new DefaultUserProfile.

Specified by:
newProfile in interface UserDatabase
See Also:
UserDatabase.newProfile()

save

public abstract void save(UserProfile profile)
                   throws WikiSecurityException
Description copied from interface: UserDatabase

Saves a UserProfileto the user database, overwriting the existing profile if it exists. The user name under which the profile should be saved is returned by the supplied profile's UserProfile.getLoginName() method.

The database implementation is responsible for detecting potential duplicate user profiles; specifically, the login name, wiki name, and full name must be unique. The implementation is not required to check for validity of passwords or e-mail addresses. Special case: if the profile already exists and the password is null, it should retain its previous value, rather than being set to null.

Implementations are required to time-stamp the creation or modification fields of the UserProfile./p>

Calling classes should generally call AuthenticationManager.refreshCredentials(WikiSession) after calling this method to ensure that Principals are reloaded into the current WikiSession's Subject.

Specified by:
save in interface UserDatabase
Parameters:
profile - the user profile to save
Throws:
WikiSecurityException - if the profile cannot be saved
See Also:
UserDatabase.save(com.ecyrd.jspwiki.auth.user.UserProfile)

validatePassword

public boolean validatePassword(String loginName,
                                String password)
Validates the password for a given user. If the user does not exist in the user database, this method always returns false. If the user exists, the supplied password is compared to the stored password. Note that if the stored password's value starts with {SHA}, the supplied password is hashed prior to the comparison.

Specified by:
validatePassword in interface UserDatabase
Parameters:
loginName - the user's login name
password - the user's password (obtained from user input, e.g., a web form)
Returns:
true if the supplied user password matches the stored password
See Also:
UserDatabase.validatePassword(java.lang.String, java.lang.String)

getHash

protected String getHash(String text)
Private method that calculates the SHA-1 hash of a given String

Parameters:
text - the text to hash
Returns:
the result hash