/*
* Copyright(c) 2007, StarLight Systems
* All rights reserved.
*/
package com.starlight.access.user;
import com.starlight.access.LoadException;
import com.starlight.access.SaveException;
/**
*
*/
public final class UserSupport {
private static UserSupportDriver driver = null;
private static User active_user = null;
public static void init( UserSupportDriver driver ) {
if ( UserSupport.driver != null ) {
throw new IllegalStateException( "Already initialized" );
}
UserSupport.driver = driver;
}
public static User getActiveUser() {
return active_user;
}
/**
* Attempt to log in a user.
*/
public static User login( String username, char[] password, String server,
boolean dev_mode ) throws LoadException {
checkInit();
// Make sure we're not already logged in
if ( active_user != null ) {
throw new IllegalStateException( "Already logged in: " + active_user );
}
active_user = driver.login( username, password, server, dev_mode );
return active_user;
}
/**
* Logs out the current user.
*/
public static void logout() {
active_user = null;
}
/**
* Change the active user's password.
*
* @return False is password verification failed.
*/
public static boolean changePassword( char[] old_password, char[] new_password )
throws SaveException {
checkInit();
checkLoggedIn();
try {
if ( !driver.verifyPassword( active_user, old_password ) ) {
return false;
}
}
catch( LoadException ex ) {
throw new SaveException( ex );
}
driver.changePassword( active_user, new_password );
return true;
}
/**
* Change a user's (not the active user's) password. The active user must be an
* administrator.
*/
public static boolean changeUserPassword( User user, char[] new_user_password )
throws SaveException {
checkInit();
checkLoggedIn();
driver.changePassword( user, new_user_password );
return true;
}
/**
* Load a user.
*/
public static User loadUser( int id ) throws LoadException {
return driver.loadUser( id );
}
/**
* Load all users.
*/
public static User[] loadUsers() throws LoadException {
return driver.loadUsers();
}
private static void checkInit() {
if ( driver != null ) return;
throw new IllegalStateException( "UserSupport has not been initialized" );
}
private static void checkLoggedIn() {
if ( active_user != null ) return;
throw new IllegalStateException( "No user is currently logged in." );
}
}