From 1271afd1d82153f5a5c446aa334bbd935d3452f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 6 Feb 2012 00:28:07 +0100 Subject: [PATCH] "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. --- arse | 2 +- includes/t-basics/controllers.inc.php | 18 ++++++++++++++- includes/t-basics/dao_users.inc.php | 2 +- includes/t-basics/package.inc.php | 2 ++ includes/t-basics/pages.inc.php | 11 +++++++++ includes/t-users/users.inc.php | 32 +++++++++++++++++++++++---- 6 files changed, 60 insertions(+), 7 deletions(-) diff --git a/arse b/arse index f80d739..62a4d0a 160000 --- a/arse +++ b/arse @@ -1 +1 @@ -Subproject commit f80d739f5d7b6241c154e2e38e0b1d5b6325142b +Subproject commit 62a4d0a39166df5bcb9c9a89431b32ec6fab8acb diff --git a/includes/t-basics/controllers.inc.php b/includes/t-basics/controllers.inc.php index 532e98e..275f9e5 100644 --- a/includes/t-basics/controllers.inc.php +++ b/includes/t-basics/controllers.inc.php @@ -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 ); + } +} diff --git a/includes/t-basics/dao_users.inc.php b/includes/t-basics/dao_users.inc.php index 4b98ef6..2977002 100644 --- a/includes/t-basics/dao_users.inc.php +++ b/includes/t-basics/dao_users.inc.php @@ -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; } } diff --git a/includes/t-basics/package.inc.php b/includes/t-basics/package.inc.php index 3f51227..baa20f5 100644 --- a/includes/t-basics/package.inc.php +++ b/includes/t-basics/package.inc.php @@ -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'; diff --git a/includes/t-basics/pages.inc.php b/includes/t-basics/pages.inc.php index 7809136..cf7257c 100644 --- a/includes/t-basics/pages.inc.php +++ b/includes/t-basics/pages.inc.php @@ -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' ) ); + } +} diff --git a/includes/t-users/users.inc.php b/includes/t-users/users.inc.php index 2188b7b..b137a7f 100644 --- a/includes/t-users/users.inc.php +++ b/includes/t-users/users.inc.php @@ -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: