Changeset 3267 for trunk/modules

Show
Ignore:
Timestamp:
08/05/2008 10:44:02 PM (4 months ago)
Author:
Shadowhand
Message:

Fixing issues with Auth, see http://forum.kohanaphp.com/comments.php?DiscussionID=763, this should have been committed after r3253

Location:
trunk/modules/auth
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/auth/controllers/auth_demo.php

    r3183 r3267  
    1111 * @license    http://kohanaphp.com/license.html 
    1212 */ 
    13 class Auth_Demo_Controller extends Controller { 
     13class Auth_Demo_Controller extends Template_Controller { 
    1414 
    1515        // Do not allow to run in production 
    1616        const ALLOW_PRODUCTION = FALSE; 
    1717 
     18        // Use the default Kohana template 
     19        public $template = 'kohana/template'; 
     20 
    1821        public function index() 
    1922        { 
    2023                // Display the install page 
    21                 echo new View('auth/install'); 
     24                $this->template->title   = 'Auth Module Installation'; 
     25                $this->template->content = View::factory('auth/install'); 
    2226        } 
    2327 
    2428        public function create() 
    2529        { 
    26                 $form = new Forge(NULL, 'Create User'); 
     30                $this->template->title = 'Create User'; 
    2731 
     32                $form = new Forge; 
    2833                $form->input('email')->label(TRUE)->rules('required|length[4,32]|valid_email'); 
    2934                $form->input('username')->label(TRUE)->rules('required|length[4,32]'); 
     
    4449                                } 
    4550 
    46                                 if ($user->save() AND $user->add('role', 'login')) 
     51                                if ($user->save() AND $user->add(ORM::factory('role', 'login'))) 
    4752                                { 
    4853                                        Auth::instance()->login($user, $form->password->value); 
     
    5560 
    5661                // Display the form 
    57                 echo $form->render(); 
     62                $this->template->content = $form->render(); 
    5863        } 
    5964 
     
    6267                if (Auth::instance()->logged_in()) 
    6368                { 
    64                         $form = new Forge('auth_demo/logout', 'Log Out'); 
     69                        $this->template->title = 'User Logout'; 
    6570 
     71                        $form = new Forge('auth_demo/logout'); 
    6672                        $form->submit('Logout Now'); 
    6773                } 
    6874                else 
    6975                { 
    70                         $form = new Forge(NULL, 'User Login'); 
     76                        $this->template->title = 'User Login'; 
    7177 
     78                        $form = new Forge; 
    7279                        $form->input('username')->label(TRUE)->rules('required|length[4,32]'); 
    7380                        $form->password('password')->label(TRUE)->rules('required|length[5,40]'); 
     
    7986                                $user = ORM::factory('user', $form->username->value); 
    8087 
    81                                 // Attempt a login 
    8288                                if (Auth::instance()->login($user, $form->password->value)) 
    8389                                { 
    84                                         echo '<h4>Login Success!</h4>'; 
    85                                         echo '<p>Your roles are:</p>'; 
    86                                         echo Kohana::debug($user->roles); 
    87                                         return; 
     90                                        // Login successful, redirect 
     91                                        url::redirect('auth_demo/login'); 
    8892                                } 
    8993                                else 
     
    9599 
    96100                // Display the form 
    97                 echo $form->render(); 
     101                $this->template->content = $form->render(); 
    98102        } 
    99103 
  • trunk/modules/auth/libraries/drivers/Auth/ORM.php

    r3114 r3267  
    3737                // Checks if a user is logged in and valid 
    3838                if ( ! empty($_SESSION['auth_user']) AND is_object($_SESSION['auth_user']) 
    39                         AND ($_SESSION['auth_user'] instanceof User_Model) AND $_SESSION['auth_user']->primary_key_value > 0) 
     39                        AND ($_SESSION['auth_user'] instanceof User_Model) AND $_SESSION['auth_user']->loaded) 
    4040                { 
    4141                        // Everything is okay so far 
     
    6161 
    6262                // If the passwords match, perform a login 
    63                 if ($user->has('role', 'login') AND $user->password === $password) 
     63                if ($user->has(ORM::factory('role', 'login')) AND $user->password === $password) 
    6464                { 
    6565                        if ($remember === TRUE) 
  • trunk/modules/auth/models/auth_user.php

    r3094 r3267  
    66        protected $has_many = array('user_tokens'); 
    77        protected $has_and_belongs_to_many = array('roles'); 
    8  
    9         // User roles 
    10         protected $has_roles; 
    118 
    129        public function __set($key, $value) 
     
    1916 
    2017                parent::__set($key, $value); 
    21         } 
    22  
    23         public function has($object, $id = NULL) 
    24         { 
    25                 if ($object === 'role') 
    26                 { 
    27                         if ( ! $this->loaded) 
    28                                 return FALSE; 
    29  
    30                         if ($this->has_roles === NULL) 
    31                         { 
    32                                 $this->db->select('id', 'name'); 
    33  
    34                                 // Load the roles 
    35                                 $this->has_roles = $this->roles->select_list('id', 'name'); 
    36                         } 
    37  
    38                         if (is_string($id) AND ! ctype_digit($id)) 
    39                         { 
    40                                 return in_array($id, $this->has_roles); 
    41                         } 
    42                         else 
    43                         { 
    44                                 return isset($this->has_roles[$id]); 
    45                         } 
    46                 } 
    47  
    48                 return parent::has($object, $id); 
    4918        } 
    5019 
     
    7544        } 
    7645 
    77         /** 
    78          * Resets roles when results are loaded. 
    79          */ 
    80         protected function load_result($array = FALSE) 
    81         { 
    82                 $result = parent::load_result($array); 
    83  
    84                 if ($array === FALSE) 
    85                 { 
    86                         // Reset roles 
    87                         $this->has_roles = NULL; 
    88                 } 
    89  
    90                 return $result; 
    91         } 
    92  
    9346} // End Auth User Model 
  • trunk/modules/auth/views/auth/install.php

    r2441 r3267  
    1 <p>The following tables must be installed in your database: users, roles, and users_roles. If you have not already installed these tables, please run the following query:</p> 
     1<div class="box"> 
    22 
    3 <pre> 
     3<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> 
     4 
     5<p>After the tables have been installed, <?php echo html::anchor('auth_demo/create', 'create a user') ?>.</p> 
     6 
     7<p><em>This query is MySQL-specific, but should be easy to adapt to an database that supports foreign keys.</em></p> 
     8 
     9<?php echo form::textarea('query', <<<EOF 
    410CREATE TABLE IF NOT EXISTS `roles` ( 
    511  `id` int(11) unsigned NOT NULL auto_increment, 
     
    814  PRIMARY KEY  (`id`), 
    915  UNIQUE KEY `uniq_name` (`name`) 
    10 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 
     16) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
    1117 
    1218INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation'); 
    1319INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.'); 
     20 
     21CREATE 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; 
    1427 
    1528CREATE TABLE IF NOT EXISTS `users` ( 
     
    2336  UNIQUE KEY `uniq_username` (`username`), 
    2437  UNIQUE KEY `uniq_email` (`email`) 
    25 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 
    26  
    27 CREATE TABLE IF NOT EXISTS `users_roles` ( 
    28   `user_id` int(10) unsigned NOT NULL, 
    29   `role_id` int(10) unsigned NOT NULL, 
    30   PRIMARY KEY  (`user_id`,`role_id`) 
    31 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 
     38) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
    3239 
    3340CREATE TABLE IF NOT EXISTS `user_tokens` ( 
     
    3946  `expires` int(10) unsigned NOT NULL, 
    4047  PRIMARY KEY  (`id`), 
    41   UNIQUE KEY `uniq_token` (`token`) 
    42 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8; 
    43 </pre> 
     48  UNIQUE KEY `uniq_token` (`token`), 
     49  KEY `fk_user_id` (`user_id`) 
     50) ENGINE=InnoDB  DEFAULT CHARSET=utf8; 
    4451 
    45 <p>After the tables have been installed, <?php echo html::anchor('auth_demo/create', 'create a user') ?>.</p> 
     52ALTER 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 
     56ALTER TABLE `user_tokens` 
     57  ADD CONSTRAINT `user_tokens_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE; 
     58EOF 
     59, 'style="width:90%;height:30em;padding:0.5em"') ?> 
     60 
     61</div>