"Installer" added

A very basic installer will appear when accessing the home page if there
are no users in the database. It allows the initial user to be created.
This commit is contained in:
Emmanuel BENOîT 2012-02-06 00:28:07 +01:00
parent 071577168a
commit 1271afd1d8
6 changed files with 60 additions and 7 deletions

2
arse

@ -1 +1 @@
Subproject commit f80d739f5d7b6241c154e2e38e0b1d5b6325142b
Subproject commit 62a4d0a39166df5bcb9c9a89431b32ec6fab8acb

View file

@ -10,8 +10,10 @@ class Ctrl_HomePage
session_start( );
if ( array_key_exists( 'uid' , $_SESSION ) ) {
return 'items';
} else {
} elseif ( Loader::DAO( 'users' )->hasUsers( ) ) {
return 'login';
} else {
return 'install';
}
}
@ -119,3 +121,17 @@ class Ctrl_LoggedOut
}
}
class Ctrl_Install
extends Controller
{
public function handle( Page $page )
{
if ( Loader::DAO( 'users' )->hasUsers( ) ) {
return 'login';
}
return Loader::Ctrl( 'users_add_form' , true );
}
}

View file

@ -74,6 +74,6 @@ class Dao_Users
public function hasUsers( )
{
$result = $this->query( 'SELECT COUNT(*) AS n_users FROM users' )->execute( );
return $result[0]->n_users;
return $result[0]->n_users > 0;
}
}

View file

@ -11,6 +11,7 @@ $package[ 'extras' ][] = 'AuthenticatedPage';
$package[ 'ctrls' ][] = 'check_session';
$package[ 'ctrls' ][] = 'home_page';
$package[ 'ctrls' ][] = 'install';
$package[ 'ctrls' ][] = 'log_in';
$package[ 'ctrls' ][] = 'log_in_form';
$package[ 'ctrls' ][] = 'logged_out';
@ -19,5 +20,6 @@ $package[ 'ctrls' ][] = 'logout';
$package[ 'pages' ][] = 'tasks_home';
$package[ 'pages' ][] = 'tasks_login';
$package[ 'pages' ][] = 'tasks_logout';
$package[ 'pages' ][] = 'tasks_install';
$package[ 'daos' ][] = 'users';

View file

@ -66,3 +66,14 @@ class Page_TasksLogout
$this->addController( Loader::Ctrl( 'logout' ) );
}
}
class Page_TasksInstall
extends Page_Basic
{
public function __construct( )
{
parent::__construct( );
$this->addController( Loader::Ctrl( 'install' ) );
}
}

View file

@ -34,9 +34,16 @@ class Ctrl_UsersList
class Ctrl_UsersAddForm
extends Controller
{
private $initial;
public function __construct( $initial = false )
{
$this->initial = $initial;
}
public function handle( Page $page )
{
return Loader::Create( 'Form' , 'Create user' , 'user-add' )
$form = Loader::Create( 'Form' , 'Create user' , 'user-add' )
->addField( Loader::Create( 'Field' , 'email' , 'text' )
->setDescription( 'E-mail address:' )
->setValidator( Loader::Create( 'Validator_Email' , 'Invalid address.' ) ) )
@ -45,9 +52,16 @@ class Ctrl_UsersAddForm
->setValidator( Loader::Create( 'Validator_StringLength' , 'This password' , 8 ) ) )
->addField( Loader::Create( 'Field' , 'pass2' , 'password' )
->setDescription( 'Confirm password:' ) )
->setURL( 'users' )
->addController( Loader::Ctrl( 'users_add' ) )
->controller( );
->addController( Loader::Ctrl( 'users_add' , $this->initial ) );
if ( $this->initial ) {
$form->setSuccessURL( 'home' );
$page->setTitle( 'Initial user' );
} else {
$form->setURL( 'users' );
}
return $form->controller( );
}
}
@ -57,6 +71,12 @@ class Ctrl_UsersAdd
implements FormAware
{
private $form;
private $initial;
public function __construct( $initial )
{
$this->initial = $initial;
}
public function setForm( Form $form )
{
@ -80,6 +100,10 @@ class Ctrl_UsersAdd
switch ( $error ) {
case 0:
if ( $this->initial ) {
session_start( );
$_SESSION[ 'uid' ] = Loader::DAO( 'users' )->getUser( $email->value( ) )->user_id;
}
return true;
case 1: