User display name field

Added a "display name" field, which can be NULL. The field can be set
from the user creation form, and is displayed in the list.
This commit is contained in:
Emmanuel BENOîT 2012-02-06 08:36:01 +01:00
parent 1271afd1d8
commit b37265d0eb
4 changed files with 45 additions and 14 deletions

View file

@ -56,6 +56,7 @@ GRANT SELECT,INSERT,UPDATE,DELETE ON items TO :webapp_user;
CREATE TABLE users ( CREATE TABLE users (
user_id INT NOT NULL DEFAULT NEXTVAL('users_user_id_seq'::TEXT), user_id INT NOT NULL DEFAULT NEXTVAL('users_user_id_seq'::TEXT),
user_email VARCHAR(256) NOT NULL, user_email VARCHAR(256) NOT NULL,
user_display_name VARCHAR(256) ,
user_salt CHAR(8) NOT NULL, user_salt CHAR(8) NOT NULL,
user_iterations INT NOT NULL, user_iterations INT NOT NULL,
user_hash CHAR(40) NOT NULL, user_hash CHAR(40) NOT NULL,

View file

@ -2,14 +2,18 @@
-- Create a new user -- Create a new user
-- --
CREATE OR REPLACE FUNCTION users_add( _email TEXT , _salt TEXT , _iters INT , _hash TEXT ) CREATE OR REPLACE FUNCTION users_add( _email TEXT , _salt TEXT , _iters INT , _hash TEXT , _name TEXT )
RETURNS INT RETURNS INT
LANGUAGE PLPGSQL LANGUAGE PLPGSQL
STRICT VOLATILE SECURITY INVOKER STRICT VOLATILE SECURITY INVOKER
AS $users_add$ AS $users_add$
BEGIN BEGIN
INSERT INTO users ( user_email , user_salt , user_iterations , user_hash ) IF _name = '' THEN
VALUES ( _email , _salt , _iters , _hash ); _name := NULL;
END IF;
INSERT INTO users ( user_email , user_salt , user_iterations , user_hash , user_display_name )
VALUES ( _email , _salt , _iters , _hash , _name );
RETURN 0; RETURN 0;
EXCEPTION EXCEPTION
WHEN unique_violation THEN WHEN unique_violation THEN
@ -17,5 +21,5 @@ EXCEPTION
END; END;
$users_add$; $users_add$;
REVOKE EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT ) FROM PUBLIC; REVOKE EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT , TEXT ) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT) TO :webapp_user; GRANT EXECUTE ON FUNCTION users_add( TEXT , TEXT , INT , TEXT , TEXT) TO :webapp_user;

View file

@ -18,7 +18,10 @@ class Dao_Users
public function getUsers( ) public function getUsers( )
{ {
return $this->query( 'SELECT user_id , user_email FROM users ORDER BY LOWER( user_email )' )->execute( ); return $this->query(
'SELECT user_id , user_display_name , user_email '
. 'FROM users '
. 'ORDER BY LOWER( user_email )' )->execute( );
} }
@ -48,7 +51,7 @@ class Dao_Users
} }
public function addUser( $email , $password ) public function addUser( $email , $password , $name )
{ {
$iterations = rand( 130 , 160 ); $iterations = rand( 130 , 160 );
@ -65,8 +68,8 @@ class Dao_Users
$hash = $this->hashPassword( $password , $salt , $iterations ); $hash = $this->hashPassword( $password , $salt , $iterations );
$result = $this->query( 'SELECT users_add( $1 , $2 , $3 , $4 ) AS error' ) $result = $this->query( 'SELECT users_add( $1 , $2 , $3 , $4 , $5 ) AS error' )
->execute( $email , $salt , $iterations , $hash ); ->execute( $email , $salt , $iterations , $hash , $name );
return $result[ 0 ]->error; return $result[ 0 ]->error;
} }

View file

@ -52,6 +52,11 @@ class Ctrl_UsersAddForm
->setValidator( Loader::Create( 'Validator_StringLength' , 'This password' , 8 ) ) ) ->setValidator( Loader::Create( 'Validator_StringLength' , 'This password' , 8 ) ) )
->addField( Loader::Create( 'Field' , 'pass2' , 'password' ) ->addField( Loader::Create( 'Field' , 'pass2' , 'password' )
->setDescription( 'Confirm password:' ) ) ->setDescription( 'Confirm password:' ) )
->addField( Loader::Create( 'Field' , 'display-name' , 'text' )
->setDescription( 'Display name:' )
->setMandatory( false )
->setValidator( Loader::Create( 'Validator_StringLength' , 'This display name',
5 , 256 , true ) ) )
->addController( Loader::Ctrl( 'users_add' , $this->initial ) ); ->addController( Loader::Ctrl( 'users_add' , $this->initial ) );
if ( $this->initial ) { if ( $this->initial ) {
@ -94,8 +99,9 @@ class Ctrl_UsersAdd
} }
$email = $this->form->field( 'email' ); $email = $this->form->field( 'email' );
$name = $this->form->field( 'display-name' );
$error = Loader::DAO( 'users' )->addUser( $email->value( ) , $error = Loader::DAO( 'users' )->addUser( $email->value( ) ,
$p1->value( ) ); $p1->value( ) , $name->value( ) );
switch ( $error ) { switch ( $error ) {
@ -137,14 +143,31 @@ class View_UsersList
->appendElement( HTML::make( 'tr' ) ->appendElement( HTML::make( 'tr' )
->setAttribute( 'class' , 'header' ) ->setAttribute( 'class' , 'header' )
->appendElement( HTML::make( 'th' ) ->appendElement( HTML::make( 'th' )
->appendText( 'E-mail address' ) ) ); ->appendText( 'E-mail address' ) )
->appendElement( HTML::make( 'th' )
->appendText( 'Display name' ) ) );
foreach ( $this->users as $user ) { foreach ( $this->users as $user ) {
$table->appendElement( HTML::make( 'tr' ) $table->appendElement( $this->makeUserRow( $user ) );
->appendElement( HTML::make( 'td' )
->appendText( $user->user_email ) ) );
} }
return $table; return $table;
} }
private function makeUserRow( $user )
{
$row = HTML::make( 'tr' )
->appendElement( HTML::make( 'td' )
->appendText( $user->user_email ) );
$nameColumn = HTML::make( 'td' );
if ( $user->user_display_name !== null ) {
$nameColumn->appendText( $user->user_display_name );
} else {
$nameColumn->appendElement( HTML::make( 'em' )->appendText( 'N/A' ) );
}
$row->appendElement( $nameColumn );
return $row;
}
} }