Class LocalAcctMgr

java.lang.Object
  extended byLocalAcctMgr
All Implemented Interfaces:
AcctMgrSpec

public class LocalAcctMgr
extends Object
implements AcctMgrSpec

Implements the AcctMgrSpec interface to provide basic account management and persistence functions using local disk space.

This class authenticates a user against the CSH IMAP server. User information is stored in a flat text file. When this class is instantiated, the text file is read into a hashmap that is then used to provide user information. TODO: Surpress flushing to file right after change.

Version:
$Revision: #13 $
Author:
Kevin Thompson [antiduh@csh.rit.edu], Sean M. Graham [grahams@csh.rit.edu]

Field Summary
(package private)  ConfigMgr configMgr
          Where the class gets its runtime configuration data from.
private  String filename
          The file that stores the accounts data.
private  Hashtable hash
          Stores the DrinkUser information.
private static String hostname
          The mail server we use to imap authenticate with.
private  DrinkLoggerSpec log
          Our logger.
private  boolean performAuth
          Whether or not to actually authenticate users.
 
Constructor Summary
LocalAcctMgr(DrinkLoggerSpec inLog, ConfigMgr inConfig)
          Saves the DrinkLogger variable and sets up the hashmap from the accounts text file.
 
Method Summary
 boolean addUser(String username, String password)
          Adds a user to the system.
 boolean addUser(String username, String password, int balance, boolean adminStatus)
          Adds a user to the database of known users using the supplied values to initialize the account.
 boolean decreaseUserBalance(String username, int amount)
          Decrements a users balance.
 Vector dump()
          Returns a vector of DrinkUser objects containing the data for every single last known user.
 int getUserBalance(String username)
          Returns a users balance.
 boolean increaseUserBalance(String username, int amount)
          Increments a users balance.
private  boolean initialize()
          TODO: Cleanup.
 boolean isSystemReady()
          As specified in AcctMgrSpec, this returns whether or not the system is ready and available to process transactions.
 boolean isUserAdmin(String username)
          Looks-up the admin status of a user.
 boolean isValidUser(String username)
          Determines if the username is known to drink.
 boolean isValidUser(String username, String password)
          Validates a user against the CSH IMAP server.
 void purgeDatabase()
          WARNING: USE WITH CAUTION.
 boolean removeUser(String username)
          Removes a user from the hashmap and account file.
private  void saveConfig()
          Saves the hashtable that stores all the user information to the accounts text file.
 boolean setUserAdmin(String username, boolean admin)
          Changes the admin status of a user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

filename

private String filename
The file that stores the accounts data.


hostname

private static String hostname
The mail server we use to imap authenticate with.


log

private DrinkLoggerSpec log
Our logger.


hash

private Hashtable hash
Stores the DrinkUser information.


performAuth

private boolean performAuth
Whether or not to actually authenticate users. If false, then any user will be allowed to log in. This is useful for debugging.


configMgr

ConfigMgr configMgr
Where the class gets its runtime configuration data from.

Constructor Detail

LocalAcctMgr

public LocalAcctMgr(DrinkLoggerSpec inLog,
                    ConfigMgr inConfig)
Saves the DrinkLogger variable and sets up the hashmap from the accounts text file.

Parameters:
inLog - The DrinkLogger to use when printing information
inConfig - The ConfigMgr to get our runtime configuration data from.
Method Detail

initialize

private boolean initialize()
TODO: Cleanup.


isValidUser

public boolean isValidUser(String username,
                           String password)
Validates a user against the CSH IMAP server.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The user to authenticate.
password - The users password.
Returns:
True if the method is able to log in to the CSH IMAP server, false otherwise.

isValidUser

public boolean isValidUser(String username)
Determines if the username is known to drink. Note that if a user logs in once, then they are automatically added.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The username to check.
Returns:
True if the user is known to drink.

getUserBalance

public int getUserBalance(String username)
Returns a users balance. If the user is known to the system, get the users balance from the hashmap, otherwise return 0.

Specified by:
getUserBalance in interface AcctMgrSpec
Parameters:
username - The user whose balance is to be returned.
Returns:
The users balance, in credits (cents).

decreaseUserBalance

public boolean decreaseUserBalance(String username,
                                   int amount)
Decrements a users balance. If the user is known to the system, decrement their balance in the hashmap and then save the hashmap to persistent storage, then return true. If the user is not known or if the balance is less than the amount to be decremented return false.

Specified by:
decreaseUserBalance in interface AcctMgrSpec
Parameters:
username - The user whose account is to be decremented.
amount - The amount to decrement the users account
Returns:
True if the user is known and the user has sufficient funds. Else, return false.
See Also:
AcctMgrSpec.increaseUserBalance(String, int)

increaseUserBalance

public boolean increaseUserBalance(String username,
                                   int amount)
Increments a users balance. If the user is known to the system, amount is added to the users balance, the hashmap is saved and the method returns true. If the user is not known, the method returns false.

Specified by:
increaseUserBalance in interface AcctMgrSpec
Parameters:
username - The username whose account is to be incremented.
amount - The amount to increment the users account
Returns:
True if the user exists and false if the user does not
See Also:
AcctMgrSpec.decreaseUserBalance(String, int)

addUser

public boolean addUser(String username,
                       String password)
Adds a user to the system. Balance defaults to zero and their admin status defaults to false.

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the user to add to the system
password - The users password to add to the system.
Returns:
True if the user is not known to the system, false if the user already exists.
See Also:
isValidUser(String,String)

addUser

public boolean addUser(String username,
                       String password,
                       int balance,
                       boolean adminStatus)
Adds a user to the database of known users using the supplied values to initialize the account.

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the account to add.
password - The password for the new account.
balance - The accounts initial balance.
adminStatus - Whether or not the new user is to be an admin.
Returns:
true if the operation succeeds, false otherwise. The operation will fail if the user already exists.

removeUser

public boolean removeUser(String username)
Removes a user from the hashmap and account file.

Specified by:
removeUser in interface AcctMgrSpec
Parameters:
username - The name of the user to remove
Returns:
True if the user exists, and false otherwise.

isUserAdmin

public boolean isUserAdmin(String username)
Looks-up the admin status of a user.

Specified by:
isUserAdmin in interface AcctMgrSpec
Parameters:
username - The name of the user to look-up
Returns:
True if the user is known to the system and is an admin, false otherwise

setUserAdmin

public boolean setUserAdmin(String username,
                            boolean admin)
Changes the admin status of a user.

Specified by:
setUserAdmin in interface AcctMgrSpec
Parameters:
username - The username to edit.
admin - The admin status to set the user to.
Returns:
True if the user is known to the system, false otherwise.

isSystemReady

public boolean isSystemReady()
As specified in AcctMgrSpec, this returns whether or not the system is ready and available to process transactions. This is always true.

Specified by:
isSystemReady in interface AcctMgrSpec
Returns:
True if the system is ready, false otherwise.

dump

public Vector dump()
Returns a vector of DrinkUser objects containing the data for every single last known user.

Specified by:
dump in interface AcctMgrSpec

purgeDatabase

public void purgeDatabase()
WARNING: USE WITH CAUTION. MAKE SURE YOU HAVE BACKUPS IF YOU USE THIS. Removes all users from the records. No accounts will be recognized after this. All data will be lost.

Specified by:
purgeDatabase in interface AcctMgrSpec

saveConfig

private void saveConfig()
Saves the hashtable that stores all the user information to the accounts text file. This method is generally executed every time something changes so that if the TINI were to unexpectedly be reset, no information would be lost.