SolarPHP Basic User Authentication Example

Here's another short tutorial for solar php in which I'm going to try to explain how to achieve a simple authentication system in SolarPHP.

User Roles and ACL's will follow in another tutorial.

Let's get started:
I'm using the SQL adapter for the authentication system. I'm assuming you're already have the config file setup for connecting to the sql server.

The config file is as follows:

  1. // Specify our authentication adapter
  2. $config['Solar_Auth']['adapter']= 'Solar_Auth_Adapter_Sql';
  3.  
  4. // authentication adapter information
  5. $config['Solar_Auth_Adapter_Sql'] = array(
  6.     'table'     => 'users',
  7.     'uid_col'   => 'id',
  8.     'email_col' => 'email',
  9.     'passwd_col'=> 'password',
  10.     'handle_col'=> 'username',
  11.     'source_handle' => 'username',
  12.     'source_passwd' => 'password',
  13.     'process_login' => 'login',
  14.     'process_logout'=> 'logout',
  15. );
  16.  
  17. $config['Solar']['registry_set']['user'] = 'Solar_User';

The code is mostly self-explanatory with the following clarification:
source_handle and source_passwd represent the post keys on which the authentication happens.
The process_login represents the type of process sent by the form, based on which the authentication happens.

Also, notice the last line that sets the Solar_User meta class in our registry. Solar_User is a wrapper for the auth / role / access classes.

  1. class Webland_App_User extends Webland_Controller_Page
  2. {
  3.     /**
  4.      *
  5.      * The default action when no action is specified.
  6.      *
  7.      * @var string
  8.      *
  9.      */
  10.     protected $_action_default = 'login';
  11.    
  12.     protected $_model;
  13.     protected $_user;
  14.    
  15.     public $form;
  16.    
  17.     protected function _setup()
  18.     {
  19.         parent::_setup();
  20.         $this->_user  = Solar_Registry::get('user');
  21.     }
  22.    
  23.     /**
  24.      *
  25.      * Generic login action.
  26.      *
  27.      * @return void
  28.      *
  29.      */
  30.     public function actionLogin()
  31.     {
  32.         //check to see if the user is authenticated, if so redirect him somewhere
  33.         if ($this->_user->auth->isValid()) {
  34.             $uri = "/{$this->_controller}/details";
  35.             $this->_redirectNoCache($uri);  
  36.         }
  37.        
  38.         $this->form = Solar::factory('Solar_Form');        
  39.        
  40.         $this->form->setElements(array(
  41.             'username' => array(
  42.                 'type'   => 'text',
  43.                 'label'  => 'LABEL_LOGIN_FORM_USERNAME',
  44.                 'require'=> true,
  45.                 'valid'  => array(
  46.                     array('notBlank','Please enter a username!'),
  47.                 ),
  48.             ),
  49.             'password' => array(
  50.                 'type'   => 'password',
  51.                 'label'  => 'LABEL_LOGIN_FORM_PASSWORD',
  52.                 'require'=> true,
  53.                 'valid'  => array(
  54.                     array('notBlank','Please enter a password!'),
  55.                 ),
  56.             ),
  57.         ));
  58.        
  59.         // did the user click the save button?
  60.         if ($this->_isProcess('login')) {
  61.             $this->_user->auth->processLogin();
  62.             $this->form->populate();
  63.            
  64.             if ($this->form->validate() && $this->_user->auth->isValid()) {
  65.                 print 'login valid';        
  66.             } else {
  67.                 print 'login invalid';
  68.             }
  69.         }  
  70.     }
  71.    
  72.     public function actionLogout()
  73.     {
  74.         $this->_view = null;
  75.         $this->_user->auth->processLogout();
  76.         $uri = "/{$this->_controller}/login";
  77.         $this->_redirectNoCache($uri);  
  78.     }
  79. }
  1. <h3>Login Form</h3>
  2. <?php
  3.     echo $this->form()
  4.               ->auto($this->form)
  5.               ->addProcess('login')
  6.               ->fetch();
  7. ?>

We're loading the Solar_User from the registry and setting it to the $_user variable in the _setup() method.

actionLogin is pretty straightforward as well:
- we're checking to see if the user is already logged in using the isValid() function, and if so redirecting him to another action
- we're creating a login form loading the Solar_Form using the factory
- the process for the login form is set in the view (i'm open for cleaner solutions)
- we're checking to see if the process request is login, if so we're calling the processLogin() function to start the authentication check.
- if the form is valid (no missing required fields) and the isValid function returns true (because the user is logged in now) we're displaying a login valid message (or better yet, we should redirect him somewhere).
- else we're displaying login invalid and the "magic" behind the scenes notifies the user about the required fields.

For suggestions for improvements use the comments box below :).

Add new comment