[Codestriker-commits] CVS update: codestriker codestriker.conf
Brought to you by:
sits
From: <si...@us...> - 2008-09-06 11:12:33
|
User: sits Date: 08/09/06 04:12:31 Modified: bin install.pl . codestriker.conf Added: lib/Codestriker/Model User.pm Log: Initial implementation of the user model object. Also added in a configuration variable $admn_users into codestriker.conf to specify which users are admin. These will be created automatically when install.pl is run. Index: User.pm =================================================================== RCS file: User.pm diff -N User.pm --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ User.pm 6 Sep 2008 11:12:31 -0000 1.1 @@ -0,0 +1,131 @@ +############################################################################### +# Codestriker: Copyright (c) 2001, 2002 David Sitsky. All rights reserved. +# si...@us... +# +# This program is free software; you can redistribute it and modify it under +# the terms of the GPL. + +# Model object for handling user data. + +package Codestriker::Model::User; + +use strict; + +use Codestriker::DB::DBI; + +# Create a User object from an existing record in the database. +sub new { + my ($class, $email) = @_; + my $self = {}; + + $self->{email} = $email; + + # Retrieve the specific user record. + my $dbh = Codestriker::DB::DBI->get_connection(); + eval { + my $select_user = + $dbh->prepare_cached('SELECT password_hash, admin ' . + 'FROM usertable ' . + 'WHERE email = ?'); + $select_user->execute($email); + + my ($password_hash, $admin) = $select_user->fetchrow_array(); + $select_user->finish(); + + $self->{password_hash} = $password_hash; + $self->{admin} = $admin; + }; + my $success = $@ ? 0 : 1; + + Codestriker::DB::DBI->release_connection($dbh, $success); + die $dbh->errstr unless $success; + + # Return the user record found. + bless $self, $class; + return $self; +} + + +# Create a new user into the database with all of the specified properties. +# Return the new password which has been assigned to the user. +sub create { + my ($type, $email, $admin) = @_; + + # Obtain a database connection. + my $dbh = Codestriker::DB::DBI->get_connection(); + + # Create a random password for the new user. + my $new_password = _create_random_password(); + my $password_hash = _hash_password($new_password); + + # Insert the row into the database. + eval { + my $insert_user = + $dbh->prepare_cached('INSERT INTO usertable (email, password_hash, admin) ' . + 'VALUES (?, ?, ?)'); + + $insert_user->execute($email, $password_hash, $admin); + }; + my $success = $@ ? 0 : 1; + + Codestriker::DB::DBI->release_connection($dbh, $success); + die $dbh->errstr unless $success; + + # Return the password that was created. + return $new_password; +} + +# Determine if the specific user already exists. +sub exists { + my ($type, $email) = @_; + + # Obtain a database connection. + my $dbh = Codestriker::DB::DBI->get_connection(); + + my $count = 0; + eval { + my $select_email = + $dbh->prepare_cached('SELECT COUNT(*) FROM usertable ' . + 'WHERE email = ?'); + $select_email->execute($email); + ($count) = $select_email->fetchrow_array(); + $select_email->finish(); + }; + my $success = $@ ? 0 : 1; + + Codestriker::DB::DBI->release_connection($dbh, $success); + die $dbh->errstr unless $success; + + return $count; +} + +# Method for producing a hash from a password. +sub _hash_password { + my ($password) = @_; + + # List of characters that can be used for the salt. + my @salt_characters = ( '.', '/', 'A'..'Z', 'a'..'z', '0' ..'9' ); + + # Generate the salt. Generate an 8 character value in case we are on + # a system which uses MD5 digests (48 bit - 6 * 8). Older systems just + # use the first two characters. + my $salt = ''; + for (my $i = 0; $i < 8; $i++) { + $salt .= $salt_characters[rand(64)]; + } + + # Crypt the password. + my $cryptedpassword = crypt($password, $salt); + + # Return the crypted password. + return $cryptedpassword; + } + +# Method for creating a random password consisting of alphanumeric +# characters. +sub _create_random_password { + my @password_characters = ( 'A'..'Z', 'a'..'z', '0' ..'9' ); + return join("", map{ $password_characters[rand 62] } (1..8)); +} + +1; Index: install.pl =================================================================== RCS file: /cvsroot/codestriker/codestriker/bin/install.pl,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- install.pl 6 Sep 2008 00:31:50 -0000 1.22 +++ install.pl 6 Sep 2008 11:12:31 -0000 1.23 @@ -54,6 +54,7 @@ eval("use Codestriker::FileParser::Parser"); eval("use Codestriker::FileParser::UnknownFormat"); eval("use Codestriker::Model::File"); +eval("use Codestriker::Model::User"); # Set this variables, to avoid compilation warnings below. $Codestriker::COMMENT_SUBMITTED = 0; @@ -572,6 +573,15 @@ indexes => [dbindex(name=>"project_name_idx", column_names=>["name"])]); +# This table records all users which are present in the system. +my $user_table = + table(name => "usertable", + columns => [col(name=>"email", type=>$VARCHAR, length=>200, pk=>1), + col(name=>"password_hash", type=>$VARCHAR, length=>128), + col(name=>"admin", type=>$INT16) + ], + indexes => []); + # Add all of the Codestriker tables into an array. my @tables = (); push @tables, $topic_table; @@ -589,6 +599,7 @@ push @tables, $topicfile_table; push @tables, $delta_table; push @tables, $project_table; +push @tables, $user_table; # Move a table into table_old, create the table with the new definitions, # and create the indexes. @@ -1057,7 +1068,18 @@ print "Failed because of $@\n"; } +# Now create any admin users, if necessary. $dbh->{PrintError} = 1; +if (defined $Codestriker::admin_users) { + foreach my $admin_user (@{ $Codestriker::admin_users }) { + if (!Codestriker::Model::User->exists($admin_user)) { + print "Creating admin user $admin_user...\n"; + Codestriker::Model::User->create($admin_user, 1); + # TODO: consider sending email with password details. + print "Done\n"; + } + } +} # Now generate the contents of the codestriker.pl file, with the appropriate # configuration details set (basically, the location of the lib dir). Index: codestriker.conf =================================================================== RCS file: /cvsroot/codestriker/codestriker/codestriker.conf,v retrieving revision 1.104 retrieving revision 1.105 diff -u -r1.104 -r1.105 --- codestriker.conf 6 Sep 2008 03:31:05 -0000 1.104 +++ codestriker.conf 6 Sep 2008 11:12:31 -0000 1.105 @@ -33,6 +33,15 @@ #$dbpasswd = 'manager'; $dbpasswd = 'cspasswd'; +# Email addresses of admin users for this installation. Admin users +# have unrestricted access to the system. Non-admin users will not +# be able to create/edit/delete projects. If no admin user is defined +# then no login system will be used, and all users will be effectively +# admin users. +#$admin_users = [ 'dav...@gm...' ]; +#$admin_users = [ 'dav...@gm...', 'si...@us...' ]; +$admin_users = []; + # Location of the mailing host. This is used when sending out codestriker # comments. $mailhost = 'localhost'; |