Initial revision

This commit is contained in:
dubtraxis 2003-10-06 16:28:20 +00:00
commit 66d6b5ea75
9 changed files with 1076 additions and 0 deletions

35
build.xml Normal file
View File

@ -0,0 +1,35 @@
<project name="ThdlUsers" default="compile" basedir=".">
<!-- Configure the directory into which the web application is built -->
<property name="build" value="${basedir}/../lex/build"/>
<!-- Configure the directory containing the source files -->
<property name="source" value="${basedir}/src"/>
<!-- Configure the directory containing the config files
This files from this directory should not go in the CVS hierarchy.
-->
<property name="config" value="${basedir}/local-config"/>
<!-- Configure up the classpath -->
<path id="classpath">
<fileset dir="${basedir}/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="compile" description="Compile web application">
<mkdir dir="${build}/WEB-INF/classes"/>
<javac srcdir="${source}/java" destdir="${build}/WEB-INF/classes" classpathref="classpath" debug="on" listfiles="no"/>
</target>
<!-- <target name="test" description="Testing" depends="compile">
<java classname="org.thdl.lex.LexMod" classpath="${build}/WEB-INF/classes/" classpathref="lex-classpath"/>
</target>
-->
</project>

View File

@ -0,0 +1,293 @@
package org.thdl.users;
import java.io.Serializable;
import java.util.*;
/**
* Description of the Class
*
*@author travis
*@created October 2, 2003
*/
public class ThdlUser implements Serializable
{
private int id;
private String firstname;
private String lastname;
private String middlename;
private String email;
private String username;
private String creditAttributionTag;
private String password;
private String passwordHint;
private String roles;
/**
* Sets the roles attribute of the ThdlUser object
*
*@param roles The new roles value
*@since
*/
public void setRoles( String roles )
{
this.roles = roles;
}
/**
* Sets the creditAttributionTag attribute of the ThdlUser object
*
*@param creditAttributionTag The new creditAttributionTag value
*@since
*/
public void setCreditAttributionTag( String creditAttributionTag )
{
this.creditAttributionTag = creditAttributionTag;
}
/**
* Sets the id attribute of the ThdlUser object
*
*@param id The new id value
*@since
*/
public void setId( int id )
{
this.id = id;
}
/**
* Sets the firstname attribute of the ThdlUser object
*
*@param firstname The new firstname value
*@since
*/
public void setFirstname( String firstname )
{
this.firstname = firstname;
}
/**
* Sets the lastname attribute of the ThdlUser object
*
*@param lastname The new lastname value
*@since
*/
public void setLastname( String lastname )
{
this.lastname = lastname;
}
/**
* Sets the middlename attribute of the ThdlUser object
*
*@param middlename The new middlename value
*@since
*/
public void setMiddlename( String middlename )
{
this.middlename = middlename;
}
/**
* Sets the email attribute of the ThdlUser object
*
*@param email The new email value
*@since
*/
public void setEmail( String email )
{
this.email = email;
}
/**
* Sets the username attribute of the ThdlUser object
*
*@param username The new username value
*@since
*/
public void setUsername( String username )
{
this.username = username;
}
/**
* Sets the password attribute of the ThdlUser object
*
*@param password The new password value
*@since
*/
public void setPassword( String password )
{
this.password = password;
}
/**
* Sets the passwordHint attribute of the ThdlUser object
*
*@param passwordHint The new passwordHint value
*@since
*/
public void setPasswordHint( String passwordHint )
{
this.passwordHint = passwordHint;
}
/**
* Gets the roles attribute of the ThdlUser object
*
*@return The roles value
*@since
*/
public String getRoles()
{
if ( null == roles )
{
setRoles( "guest" );
}
return roles;
}
/**
* Gets the creditAttributionTag attribute of the ThdlUser object
*
*@return The creditAttributionTag value
*@since
*/
public String getCreditAttributionTag()
{
return creditAttributionTag;
}
/**
* Gets the id attribute of the ThdlUser object
*
*@return The id value
*@since
*/
public int getId()
{
return id;
}
/**
* Gets the firstname attribute of the ThdlUser object
*
*@return The firstname value
*@since
*/
public String getFirstname()
{
return firstname;
}
/**
* Gets the lastname attribute of the ThdlUser object
*
*@return The lastname value
*@since
*/
public String getLastname()
{
return lastname;
}
/**
* Gets the middlename attribute of the ThdlUser object
*
*@return The middlename value
*@since
*/
public String getMiddlename()
{
return middlename;
}
/**
* Gets the email attribute of the ThdlUser object
*
*@return The email value
*@since
*/
public String getEmail()
{
return email;
}
/**
* Gets the username attribute of the ThdlUser object
*
*@return The username value
*@since
*/
public String getUsername()
{
return username;
}
/**
* Gets the password attribute of the ThdlUser object
*
*@return The password value
*@since
*/
protected String getPassword()
{
return password;
}
/**
* Gets the passwordHint attribute of the ThdlUser object
*
*@return The passwordHint value
*@since
*/
public String getPasswordHint()
{
return passwordHint;
}
//helpers
/**
* Description of the Method
*
*@param userRole Description of Parameter
*@return Description of the Returned Value
*@since
*/
public boolean hasRole( String userRole )
{
boolean boo = false;
StringTokenizer tokens = new StringTokenizer( getRoles(), ":" );
while ( tokens.hasMoreTokens() )
{
if ( userRole.equals( tokens.nextToken() ) )
{
boo = true;
}
}
return boo;
}
}

View File

@ -0,0 +1,46 @@
package org.thdl.users;
/**
* Description of the Class
*
* @author travis
* @created October 6, 2003
*/
public class ThdlUserConstants
{
/**
* Description of the Field
*/
protected final static String USERNAME_REQ_PARAM = "username";
/**
* Description of the Field
*/
protected final static String PASSWORD_REQ_PARAM = "password";
//used by Repository
/**
* Description of the Field
*/
protected final static String DRIVER = "com.mysql.jdbc.Driver";
/**
* Description of the Field
*/
//protected final static String USER = "";
/**
* Description of the Field
*/
//protected final static String PASSWORD = "";
/**
* Description of the Field
*/
//protected final static String URL = "jdbc:mysql://localhost/ThdlUsers";
/**
* Description of the Field
*/
public final static String DATASOURCE_NAME = "java:comp/env/jdbc/thdl-users-datasource";
}

View File

@ -0,0 +1,383 @@
package org.thdl.users;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
/**
* Description of the Class
*
* @author travis
* @created October 6, 2003
*/
public class ThdlUserRepository
{
//attributes
private static ThdlUserRepository instance;
private Connection connection;
/**
* Sets the connection attribute of the ThdlUserRepository object
*
* @param connection The new connection value
*/
private void setConnection( Connection connection )
{
this.connection = connection;
}
//accessors
/**
* Gets the instance attribute of the ThdlUserRepository class
*
* @return The instance value
* @exception ThdlUserRepositoryException Description of the Exception
*/
public static ThdlUserRepository getInstance() throws ThdlUserRepositoryException
{
if ( instance == null )
{
instance = new ThdlUserRepository();
}
return instance;
}
/**
* Gets the connection attribute of the ThdlUserRepository object
*
* @return The connection value
*/
private Connection getConnection()
{
return connection;
}
//helper methods
/**
* Description of the Method
*
* @param user Description of the Parameter
* @return Description of the Return Value
* @exception ThdlUserRepositoryException Description of the Exception
*/
public ThdlUser validate( ThdlUser user ) throws ThdlUserRepositoryException
{
try
{
ThdlUser thdlUser = null;
String sql = "SELECT id, firstname, lastname, middlename, email, username, creditAttributionTag, password, passwordHint "
+ "FROM ThdlUsers "
+ "WHERE ( email = '" + user.getUsername() + "' OR username = '" + user.getUsername() + "' ) "
+ "AND password = PASSWORD('" + user.getPassword() + "')";
Statement stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery( sql );
if ( ThdlUtilities.getResultSetSize( rs ) < 1 )
{
throw new ThdlUserRepositoryException( "Invalid login combination" );
}
else if ( ThdlUtilities.getResultSetSize( rs ) > 1 )
{
throw new ThdlUserRepositoryException( "Login combination returned multiple records." );
}
else if ( ThdlUtilities.getResultSetSize( rs ) == 1 )
{
rs.next();
thdlUser = user;
thdlUser.setId( rs.getInt( "id" ) );
thdlUser.setFirstname( rs.getString( "firstname" ) );
thdlUser.setLastname( rs.getString( "lastname" ) );
thdlUser.setMiddlename( rs.getString( "middlename" ) );
thdlUser.setEmail( rs.getString( "email" ) );
thdlUser.setUsername( rs.getString( "username" ) );
thdlUser.setCreditAttributionTag( rs.getString( "creditAttributionTag" ) );
thdlUser.setPassword( rs.getString( "password" ) );
thdlUser.setPasswordHint( rs.getString( "passwordHint" ) );
}
return thdlUser;
}
catch ( SQLException sqle )
{
throw new ThdlUserRepositoryException( sqle );
}
}
/**
* Description of the Method
*
* @param user Description of the Parameter
* @param application Description of the Parameter
* @return Description of the Return Value
* @exception ThdlUserRepositoryException Description of the Exception
*/
public ThdlUser validate( ThdlUser user, String application ) throws ThdlUserRepositoryException
{
ThdlUser thdlUser = validate( user );
String sql = "SELECT UserRolesForApplication.roles "
+ "FROM UserRolesForApplication, Applications "
+ "LEFT JOIN UserRolesForApplication AS urfa ON urfa.applicationId = Applications.id "
+ "WHERE Applications.application = '" + application + "'"
+ "AND urfa.userId = " + thdlUser.getId() + " ";
try
{
Statement stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery( sql );
if ( ThdlUtilities.getResultSetSize( rs ) == 1 )
{
rs.next();
thdlUser.setRoles( rs.getString( 1 ) );
}
}
catch ( SQLException sqle )
{
throw new ThdlUserRepositoryException( sqle );
}
return thdlUser;
}
/**
* Description of the Method
*
* @param thdlUser Description of the Parameter
* @exception ThdlUserRepositoryException Description of the Exception
*/
public void updateUser( ThdlUser thdlUser ) throws ThdlUserRepositoryException
{
try
{
StringBuffer buffer = new StringBuffer();
buffer.append( "UPDATE ThdlUsers SET firstname = '" );
buffer.append( thdlUser.getFirstname() );
buffer.append( "', lastname = '" );
buffer.append( thdlUser.getLastname() );
buffer.append( "', middlename = '" );
buffer.append( thdlUser.getMiddlename() );
buffer.append( "', email = '" );
buffer.append( thdlUser.getEmail() );
buffer.append( "', username = '" );
buffer.append( thdlUser.getUsername() );
buffer.append( "', creditAttributionTag = '" );
buffer.append( thdlUser.getCreditAttributionTag() );
buffer.append( "', password = PASSWORD('" );
buffer.append( thdlUser.getPassword() );
buffer.append( "'), passwordHint = '" );
buffer.append( thdlUser.getPasswordHint() );
buffer.append( "' WHERE id = " );
buffer.append( thdlUser.getId() );
Statement stmt = getConnection().createStatement();
int updatedRowCount = stmt.executeUpdate( buffer.toString() );
}
catch ( SQLException sqle )
{
throw new ThdlUserRepositoryException( sqle );
}
}
/**
* Description of the Method
*
* @param user Description of the Parameter
* @return Description of the Return Value
* @exception ThdlUserRepositoryException Description of the Exception
*/
public boolean doesNotAlreadyExist( ThdlUser user ) throws ThdlUserRepositoryException
{
boolean insertable = false;
try
{
String sql = "SELECT id FROM ThdlUsers WHERE email = '" + user.getEmail() + "'";
Statement stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery( sql );
if ( ThdlUtilities.getResultSetSize( rs ) > 0 )
{
throw new UserEmailAlreadyExistsException( "A user with this e-mail already exists. " );
}
else
{
insertable = true;
}
sql = "SELECT id FROM ThdlUsers WHERE username = '" + user.getUsername() + "'";
rs = stmt.executeQuery( sql );
if ( ThdlUtilities.getResultSetSize( rs ) > 0 )
{
insertable = true;
throw new UsernameAlreadyExistsException( "This username is already taken. Please choose again." );
}
else
{
insertable = true;
}
}
catch ( SQLException sqle )
{
throw new ThdlUserRepositoryException( sqle );
}
return insertable;
}
/**
* Description of the Method
*
* @param thdlUser Description of the Parameter
* @return Description of the Return Value
* @exception ThdlUserRepositoryException Description of the Exception
*/
public boolean insertUser( ThdlUser thdlUser ) throws ThdlUserRepositoryException
{
try
{
StringBuffer buffer = new StringBuffer();
buffer.append( "Insert INTO ThdlUsers VALUES ( NULL, '" );
buffer.append( thdlUser.getFirstname() );
buffer.append( "', '" );
buffer.append( thdlUser.getLastname() );
buffer.append( "', '" );
buffer.append( thdlUser.getMiddlename() );
buffer.append( "', '" );
buffer.append( thdlUser.getEmail() );
buffer.append( "', '" );
buffer.append( thdlUser.getUsername() );
buffer.append( "', '" );
thdlUser.setCreditAttributionTag( makeCreditTag( thdlUser ) );
buffer.append( thdlUser.getCreditAttributionTag() );
buffer.append( "', PASSWORD('" );
buffer.append( thdlUser.getPassword() );
buffer.append( "'), '" );
buffer.append( thdlUser.getPasswordHint() );
buffer.append( "' ) " );
Statement stmt = getConnection().createStatement();
boolean returnVal = false;
int insertedRowCount = stmt.executeUpdate( buffer.toString() );
if ( insertedRowCount > 0 )
{
ResultSet rs = stmt.executeQuery( "SELECT LAST_INSERT_ID()" );
rs.next();
thdlUser.setId( rs.getInt( 1 ) );
returnVal = true;
}
else
{
throw new ThdlUserRepositoryException( "Insert affected 0 rows. Sql String was '" + buffer.toString() + "'" );
}
return returnVal;
}
catch ( SQLException sqle )
{
throw new ThdlUserRepositoryException( sqle );
}
}
/**
* Description of the Method
*
* @param user Description of the Parameter
* @return Description of the Return Value
* @exception SQLException Description of the Exception
*/
public String makeCreditTag( ThdlUser user ) throws SQLException
{
StringBuffer tag = new StringBuffer();
if ( user.getFirstname().length() > 0 )
{
tag.append( user.getFirstname().substring( 0, 1 ) );
}
if ( user.getMiddlename().length() > 0 )
{
tag.append( user.getMiddlename().substring( 0, 1 ) );
}
if ( user.getLastname().length() > 0 )
{
tag.append( user.getLastname().substring( 0, 1 ) );
}
String sql = "SELECT id FROM ThdlUsers WHERE creditAttributionTag = '" + user.getCreditAttributionTag() + "'";
ResultSet rs = getConnection().createStatement().executeQuery( sql );
int count = ThdlUtilities.getResultSetSize( rs );
if ( count > 0 )
{
tag.append( count + 1 );
}
return tag.toString();
}
//main
/**
* The main program for the ThdlUserRepository class
*
* @param args The command line arguments
*/
public static void main( String[] args )
{
try
{
ThdlUserRepository tur = ThdlUserRepository.getInstance();
ThdlUser user = new ThdlUser();
user.setUsername( args[0] );
user.setPassword( args[1] );
tur.validate( user );
System.out.println( user.getFirstname() );
System.out.println( user.getMiddlename() );
System.out.println( user.getLastname() );
System.out.println( user.getCreditAttributionTag() );
}
catch ( Exception e )
{
e.printStackTrace();
}
}
//constructors
/**
*Constructor for the ThdlUserRepository object
*
* @exception ThdlUserRepositoryException Description of the Exception
*/
private ThdlUserRepository() throws ThdlUserRepositoryException
{
/*
try
{
Class.forName( ThdlUserConstants.DRIVER );
}
catch ( ClassNotFoundException cnfe )
{
throw new ThdlUserRepositoryException( "No Driver Available for: " + ThdlUserConstants.DRIVER );
}
Properties props = new Properties();
props.setProperty( "user", ThdlUserConstants.USER );
props.setProperty( "password", ThdlUserConstants.PASSWORD );
props.setProperty( "useUnicode", "true" );
props.setProperty( "characterEncoding", "UTF-8" );
*/
try
{
/*
setConnection( DriverManager.getConnection( ThdlUserConstants.URL, props ) );
*/
Context context = new InitialContext();
DataSource source = (DataSource) context.lookup( ThdlUserConstants.DATASOURCE_NAME );
setConnection( source.getConnection() );
}
catch ( NamingException ne )
{
throw new ThdlUserRepositoryException( ne );
}
catch ( SQLException se )
{
throw new ThdlUserRepositoryException( se );
}
}
}

View File

@ -0,0 +1,54 @@
package org.thdl.users;
/**
* Description of the Class
*
* @author travis
* @created October 6, 2003
*/
public class ThdlUserRepositoryException extends Exception
{
/**
*Constructor for the ThdlUserRepositoryException object
*/
public ThdlUserRepositoryException()
{
super();
}
/**
*Constructor for the ThdlUserRepositoryException object
*
* @param msg Description of the Parameter
*/
public ThdlUserRepositoryException( String msg )
{
super( msg );
}
/**
*Constructor for the ThdlUserRepositoryException object
*
* @param e Description of the Parameter
*/
public ThdlUserRepositoryException( Exception e )
{
super( e );
}
/**
*Constructor for the ThdlUserRepositoryException object
*
* @param msg Description of the Parameter
* @param e Description of the Parameter
*/
public ThdlUserRepositoryException( String msg, Exception e )
{
super( msg, e );
}
}

View File

@ -0,0 +1,164 @@
package org.thdl.users;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* Description of the Class
*
* @author travis
* @created October 5, 2003
*/
public class ThdlUtilities
{
/**
* Gets the resultSetSize attribute of the ThdlUtilities class
*
* @param rs Description of the Parameter
* @return The resultSetSize value
* @exception java.sql.SQLException Description of the Exception
*/
public static int getResultSetSize( java.sql.ResultSet rs ) throws java.sql.SQLException
{
rs.last();
int i = rs.getRow();
rs.beforeFirst();
return i;
}
/*
public static String formatTimestamp( Timestamp time )
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd 'at' hh:mm:ss a zzz");
Date coDate = new Date( getCreatedOn().getTime() );
String dateString = formatter.format( coDate );
}
*/
/**
* Description of the Method
*
* @param fromString Description of the Parameter
* @return Description of the Return Value
*/
public static String escape( String fromString )
{
HashMap map = new HashMap();
map.put( "'", "\\'" );
/*
map.put("%", "\\%");
map.put("_", "\\_");
map.put("\"", "\\\"");
*/
StringBuffer targetString = new StringBuffer( "" );
if ( null != fromString )
{
StringTokenizer tokens = new StringTokenizer( fromString, "'%_\"", true );
while ( tokens.hasMoreTokens() )
{
String temp = tokens.nextToken();
if ( map.containsKey( temp ) )
{
temp = (String) map.get( temp );
}
targetString.append( temp );
}
}
return targetString.toString();
}
/**
* Description of the Method
*
* @param source Description of the Parameter
* @return Description of the Return Value
*/
public static int[] convertToIntArray( String[] source )
{
if ( null == source )
{
return null;
}
int[] target = new int[source.length];
for ( int i = 0; i < target.length; i++ )
{
target[i] = Integer.parseInt( source[i] );
}
return target;
}
/**
* Description of the Method
*
* @param source Description of the Parameter
* @return Description of the Return Value
*/
public static int[] convertTokensToIntArray( String source )
{
if ( null == source )
{
return null;
}
StringTokenizer sourceTokens = new StringTokenizer( source, ":" );
int[] target = new int[sourceTokens.countTokens()];
for ( int i = 0; sourceTokens.hasMoreTokens(); i++ )
{
target[i] = Integer.parseInt( sourceTokens.nextToken() );
}
return target;
}
/**
* Description of the Method
*
* @param source Description of the Parameter
* @return Description of the Return Value
*/
public static String convertIntArrayToTokens( int[] source )
{
if ( null == source )
{
return null;
}
StringBuffer target = new StringBuffer( "" );
for ( int i = 0; i < source.length; i++ )
{
target.append( Integer.toString( source[i] ) );
if ( i < ( source.length - 1 ) )
{
target.append( ":" );
}
}
return target.toString();
}
/**
* The main program for the ThdlUtilities class
*
* @param args The command line arguments
*/
public static void main( String[] args )
{
String s = "It's stupid to use a % or a _ in a SQL Statement";
System.out.println( ThdlUtilities.escape( s ) );
int[] ia = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println( ThdlUtilities.convertIntArrayToTokens( ia ) );
String str = "9:8:7:6:5";
int[] ia2 = ThdlUtilities.convertTokensToIntArray( str );
String newStr = "";
for ( int i = 0; i < 5; i++ )
{
newStr = newStr + ia2[i] + " ";
}
System.out.println( newStr );
}
}

View File

@ -0,0 +1,31 @@
package org.thdl.users;
/**
* Description of the Class
*
* @author travis
* @created October 6, 2003
*/
public class UserEmailAlreadyExistsException extends ThdlUserRepositoryException
{
/**
*Constructor for the UserEmailAlreadyExistsException object
*/
public UserEmailAlreadyExistsException()
{
super();
}
/**
*Constructor for the UserEmailAlreadyExistsException object
*
* @param msg Description of the Parameter
*/
public UserEmailAlreadyExistsException( String msg )
{
super( msg );
}
}

View File

@ -0,0 +1,31 @@
package org.thdl.users;
/**
* Description of the Class
*
* @author travis
* @created October 6, 2003
*/
public class UsernameAlreadyExistsException extends ThdlUserRepositoryException
{
/**
*Constructor for the UsernameAlreadyExistsException object
*/
public UsernameAlreadyExistsException()
{
super();
}
/**
*Constructor for the UsernameAlreadyExistsException object
*
* @param msg Description of the Parameter
*/
public UsernameAlreadyExistsException( String msg )
{
super( msg );
}
}

View File

@ -0,0 +1,39 @@
CREATE DATABASE ThdlUsers;
USE ThdlUsers;
CREATE TABLE IF NOT EXISTS ThdlUsers
(
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname TEXT,
lastname TEXT,
middlename TEXT,
email TEXT,
username TEXT,
creditAttributionTag TEXT,
password TEXT,
passwordHint TEXT
);
CREATE TABLE IF NOT EXISTS Roles
(
role TEXT NOT NULL,
description TEXT,
PRIMARY KEY ( role(10) )
);
CREATE TABLE IF NOT EXISTS Applications
(
id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
application TEXT,
description TEXT
);
CREATE TABLE IF NOT EXISTS UserRolesForApplication
(
userId MEDIUMINT NOT NULL,
applicationId MEDIUMINT NOT NULL,
roles TEXT,
PRIMARY KEY( userId, applicationId )
)COMMENT="merge colon-delimited role-list with user/app";