Changeset 3403

Show
Ignore:
Timestamp:
08/30/2008 09:58:40 PM (3 months ago)
Author:
Shadowhand
Message:

Updated Auth:

  • Moved auth_demo controller to auth
  • Removed Forge usage in favor of Validation and form helper
  • Added auth/delete to delete user accounts; needs confirmation and admin checking
  • Made installation automatic using the default Database instance with queries
  • Added Model_User::validate() as a ORM validation example, for use with auth/create
  • Updated view layout and styles
Location:
trunk/modules/auth
Files:
9 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/auth/classes/controller/auth.php

    r3387 r3403  
    1919        public $template = 'kohana/template'; 
    2020 
     21        // Currently logged in user 
     22        protected $user; 
     23 
     24        public function __construct() 
     25        { 
     26                parent::__construct(); 
     27 
     28                // Load sessions, to support logins 
     29                $this->session = Session::instance(); 
     30 
     31                if (Auth::instance()->logged_in()) 
     32                { 
     33                        // Set the current user 
     34                        $this->user = $_SESSION['auth_user']; 
     35                } 
     36        } 
     37 
    2138        public function index() 
    2239        { 
    2340                // Display the install page 
    2441                $this->template->title   = 'Auth Module Installation'; 
    25                 $this->template->content = View::factory('auth/install'); 
     42                $this->template->content = View::factory('auth/install') 
     43                        ->bind('sql', $sql) 
     44                        ->bind('result', $result); 
     45 
     46                // Load installation SQL 
     47                $sql = View::factory('auth/install', NULL, 'sql')->render(); 
     48 
     49                // Load validation 
     50                $post = Validation::factory($_POST) 
     51                        ->pre_filter('trim') 
     52                        ->add_rules('query', 'required'); 
     53 
     54                if ($post->validate()) 
     55                { 
     56                        try 
     57                        { 
     58                                // Run the query 
     59                                Database::instance()->query($post['query']); 
     60 
     61                                // Go to the creation page 
     62                                url::redirect('auth/installed'); 
     63                        } 
     64                        catch (Kohana_Database_Exception $e) 
     65                        { 
     66                                // Set the result to the exception 
     67                                $result = $e; 
     68                        } 
     69                } 
     70        } 
     71 
     72        public function installed() 
     73        { 
     74                if (request::referrer() !== 'auth') 
     75                { 
     76                        // Do not allow non-referrered requests 
     77                        url::redirect('auth'); 
     78                } 
     79 
     80                $this->template->title = 'Installation Sucessful!'; 
     81                $this->template->content = View::factory('auth/installed'); 
    2682        } 
    2783 
     
    2985        { 
    3086                $this->template->title = 'Create User'; 
     87                $this->template->content = View::factory('auth/create_user') 
     88                        ->bind('post', $post) 
     89                        ->bind('errors', $errors); 
    3190 
    32                 $form = new Forge; 
    33                 $form->input('email')->label(TRUE)->rules('required|length[4,32]|valid_email'); 
    34                 $form->input('username')->label(TRUE)->rules('required|length[4,32]'); 
    35                 $form->password('password')->label(TRUE)->rules('required|length[5,40]'); 
    36                 $form->submit('Create New User'); 
     91                // Will be converted into a Validation object 
     92                $post = $_POST; 
    3793 
    38                 if ($form->validate()) 
     94                // Create a new user 
     95                $user = ORM::factory('user'); 
     96 
     97                if ($user->validate($post, TRUE)) 
    3998                { 
    40                         // Create new user 
    41                         $user = ORM::factory('user'); 
     99                        // Give the user login privileges 
     100                        $user->add(ORM::factory('role', 'login')); 
    42101 
    43                         if ( ! $user->username_exists($form->username->value)) 
    44                         { 
    45                                 foreach ($form->as_array() as $key => $val) 
    46                                 { 
    47                                         // Set user data 
    48                                         $user->$key = $val; 
    49                                 } 
     102                        // Log in now 
     103                        Auth::instance()->login($user, $post['password']); 
    50104 
    51                                 if ($user->save() AND $user->add(ORM::factory('role', 'login'))) 
    52                                 { 
    53                                         Auth::instance()->login($user, $form->password->value); 
    54  
    55                                         // Redirect to the login page 
    56                                         url::redirect('auth_demo/login'); 
    57                                 } 
    58                         } 
     105                        // Redirect to the logged_in page 
     106                        url::redirect('auth/logged_in'); 
    59107                } 
    60108 
    61                 // Display the form 
    62                 $this->template->content = $form->render(); 
     109                $errors = $post->errors('form_user'); 
     110        } 
     111 
     112        public function edit($id = NULL) 
     113        { 
     114 
     115        } 
     116 
     117        public function delete($id = NULL) 
     118        { 
     119                $user = ORM::factory('user', $id); 
     120 
     121                // If the user does not exist, redirect 
     122                $user->loaded or url::redirect('auth/logged_in'); 
     123 
     124                if (is_object($this->user) AND $user->id === $this->user->id) 
     125                { 
     126                        // Log the user out, their account will no longer exist 
     127                        Auth::instance()->logout(); 
     128                } 
     129 
     130                // Delete the user 
     131                $user->delete(); 
     132 
     133                url::redirect('auth/logged_in'); 
     134        } 
     135 
     136        public function logged_in() 
     137        { 
     138                if ( ! is_object($this->user)) 
     139                { 
     140                        // No user is currently logged in 
     141                        url::redirect('auth/login'); 
     142                } 
     143 
     144                $this->template->title = 'User Properties'; 
     145                $this->template->content = View::factory('auth/user_info') 
     146                        ->bind('user', $this->user); 
    63147        } 
    64148 
    65149        public function login() 
    66150        { 
    67                 if (Auth::instance()->logged_in()) 
     151                $this->template->title = 'Login'; 
     152                $this->template->content = View::factory('auth/login') 
     153                        ->bind('post', $post) 
     154                        ->bind('errors', $errors); 
     155 
     156                $post = Validation::factory($_POST) 
     157                        ->pre_filter('trim') 
     158                        ->add_rules('username', 'required', 'length[4,127]') 
     159                        ->add_rules('password', 'required'); 
     160 
     161                if ($post->validate()) 
    68162                { 
    69                         $this->template->title = 'User Logout'; 
     163                        $user = ORM::factory('user', $post['username']); 
    70164 
    71                         $form = new Forge('auth_demo/logout'); 
    72                         $form->submit('Logout Now'); 
    73                 } 
    74                 else 
    75                 { 
    76                         $this->template->title = 'User Login'; 
    77  
    78                         $form = new Forge; 
    79                         $form->input('username')->label(TRUE)->rules('required|length[4,32]'); 
    80                         $form->password('password')->label(TRUE)->rules('required|length[5,40]'); 
    81                         $form->submit('Attempt Login'); 
    82  
    83                         if ($form->validate()) 
     165                        if ( ! $user->loaded) 
    84166                        { 
    85                                 // Load the user 
    86                                 $user = ORM::factory('user', $form->username->value); 
    87  
    88                                 if (Auth::instance()->login($user, $form->password->value)) 
    89                                 { 
    90                                         // Login successful, redirect 
    91                                         url::redirect('auth_demo/login'); 
    92                                 } 
    93                                 else 
    94                                 { 
    95                                         $form->password->add_error('login_failed', 'Invalid username or password.'); 
    96                                 } 
     167                                // The user could not be located 
     168                                $post->add_error('username', 'not_found'); 
     169                        } 
     170                        elseif (Auth::instance()->login($user, $post['password'])) 
     171                        { 
     172                                // Successful login 
     173                                url::redirect('auth/logged_in'); 
     174                        } 
     175                        else 
     176                        { 
     177                                // Incorrect password 
     178                                $post->add_error('password', 'incorrect'); 
    97179                        } 
    98180                } 
    99181 
    100                 // Display the form 
    101                 $this->template->content = $form->render(); 
     182                $errors = $post->errors('form_login'); 
    102183        } 
    103184 
    104185        public function logout() 
    105186        { 
    106                 // Force a complete logout 
    107                 Auth::instance()->logout(TRUE); 
     187                Auth::instance()->logout(); 
    108188 
    109                 // Redirect back to the login page 
    110                 url::redirect('auth_demo/login'); 
     189                url::redirect('auth/login'); 
    111190        } 
    112191 
  • trunk/modules/auth/classes/model/auth/user.php

    r3387 r3403  
    66        protected $has_many = array('user_tokens'); 
    77        protected $has_and_belongs_to_many = array('roles'); 
     8 
     9        // Columns to ignore 
     10        protected $ignored_columns = array('password_confirm'); 
    811 
    912        public function __set($key, $value) 
     
    1922 
    2023        /** 
    21          * Tests if a username exists in the database. 
     24         * Validates and optionally saves a new user record from an array. 
     25         * 
     26         * @param  array    values to check 
     27         * @param  boolean  save the record when validation succeeds 
     28         * @return boolean 
     29         */ 
     30        public function validate( array & $array, $save = FALSE) 
     31        { 
     32                $array = Validation::factory($array) 
     33                        ->pre_filter('trim') 
     34                        ->add_rules('email', 'required', 'length[4,127]', 'valid::email') 
     35                        ->add_rules('username', 'required', 'length[4,32]', 'chars[a-zA-Z0-9_.]', array($this, 'username_available')) 
     36                        ->add_rules('password', 'required', 'length[5,42]') 
     37                        ->add_rules('password_confirm', 'matches[password]'); 
     38 
     39                return parent::validate($array, $save); 
     40        } 
     41 
     42        /** 
     43         * Tests if a username exists in the database. This can be used as a 
     44         * Valdidation rule. 
    2245         * 
    2346         * @param   mixed    id to check 
    2447         * @return  boolean 
    2548         */ 
    26         public function username_exists($id) 
     49        public function username_available($id) 
    2750        { 
    28                 return (bool) $this->db 
     51                return ! $this->db 
    2952                        ->where($this->unique_key($id), $id) 
    3053                        ->count_records($this->table_name); 
  • trunk/modules/auth/views/auth/install.php

    r3267 r3403  
    11<div class="box"> 
     2 
     3<p class="intro">This demo will walk you through installing the Auth module using the ORM driver.</p> 
    24 
    35<p>The following tables must be installed in your database: <code>users</code>, <code>roles</code>, <code>roles_users</code>, and <code>user_tokens</code>. If you have not already installed these tables, please run the installation query below.</p> 
    46 
    5 <p>After the tables have been installed, <?php echo html::anchor('auth_demo/create', 'create a user') ?>.</p> 
     7<p>After the tables have been installed, you will be able to <?php echo html::anchor('auth/create', 'create a user') ?>.</p> 
    68 
    79<p><em>This query is MySQL-specific, but should be easy to adapt to an database that supports foreign keys.</em></p> 
    810 
    9 <?php echo form::textarea('query', <<<EOF 
    10 CREATE TABLE IF NOT EXISTS `roles` ( 
    11   `id` int(11) unsigned NOT NULL auto_increment, 
    12   `name` varchar(32) NOT NULL, 
    13   `description` varchar(255) NOT NULL, 
    14   PRIMARY KEY  (`id`), 
    15   UNIQUE KEY `uniq_name` (`name`) 
    16 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
     11<?php echo form::open('auth') ?> 
    1712 
    18 INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation'); 
    19 INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.'); 
     13<?php if (is_object($result) AND $result instanceof Exception): ?> 
     14<ul class="errors"> 
     15<li><?php echo $result->getMessage() ?></li> 
     16</ul> 
     17<?php endif ?> 
    2018 
    21 CREATE TABLE IF NOT EXISTS `roles_users` ( 
    22   `user_id` int(10) unsigned NOT NULL, 
    23   `role_id` int(10) unsigned NOT NULL, 
    24   PRIMARY KEY  (`user_id`,`role_id`), 
    25   KEY `fk_role_id` (`role_id`) 
    26 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
     19<fieldset> 
     20<label><span>Installation SQL</span><?php echo form::textarea(array('name' => 'query', 'style' => 'height:30em'), $sql) ?></label> 
     21</fieldset> 
    2722 
    28 CREATE TABLE IF NOT EXISTS `users` ( 
    29   `id` int(11) unsigned NOT NULL auto_increment, 
    30   `email` varchar(127) NOT NULL, 
    31   `username` varchar(32) NOT NULL default '', 
    32   `password` char(50) NOT NULL, 
    33   `logins` int(10) unsigned NOT NULL default '0', 
    34   `last_login` int(10) unsigned, 
    35   PRIMARY KEY  (`id`), 
    36   UNIQUE KEY `uniq_username` (`username`), 
    37   UNIQUE KEY `uniq_email` (`email`) 
    38 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
     23<fieldset class="submit"><?php echo form::button(NULL, 'Run Query') ?></fieldset> 
    3924 
    40 CREATE TABLE IF NOT EXISTS `user_tokens` ( 
    41   `id` int(11) unsigned NOT NULL auto_increment, 
    42   `user_id` int(11) unsigned NOT NULL, 
    43   `user_agent` varchar(40) NOT NULL, 
    44   `token` varchar(32) NOT NULL, 
    45   `created` int(10) unsigned NOT NULL, 
    46   `expires` int(10) unsigned NOT NULL, 
    47   PRIMARY KEY  (`id`), 
    48   UNIQUE KEY `uniq_token` (`token`), 
    49   KEY `fk_user_id` (`user_id`) 
    50 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
    51  
    52 ALTER TABLE `roles_users` 
    53   ADD CONSTRAINT `roles_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, 
    54   ADD CONSTRAINT `roles_users_ibfk_2` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE; 
    55  
    56 ALTER TABLE `user_tokens` 
    57   ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE; 
    58 EOF 
    59 , 'style="width:90%;height:30em;padding:0.5em"') ?> 
     25<?php echo form::close() ?> 
    6026 
    6127</div>