Changeset 3267 for trunk/modules
- Timestamp:
- 08/05/2008 10:44:02 PM (4 months ago)
- Location:
- trunk/modules/auth
- Files:
-
- 4 modified
-
controllers/auth_demo.php (modified) (6 diffs)
-
libraries/drivers/Auth/ORM.php (modified) (2 diffs)
-
models/auth_user.php (modified) (3 diffs)
-
views/auth/install.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/auth/controllers/auth_demo.php
r3183 r3267 11 11 * @license http://kohanaphp.com/license.html 12 12 */ 13 class Auth_Demo_Controller extends Controller {13 class Auth_Demo_Controller extends Template_Controller { 14 14 15 15 // Do not allow to run in production 16 16 const ALLOW_PRODUCTION = FALSE; 17 17 18 // Use the default Kohana template 19 public $template = 'kohana/template'; 20 18 21 public function index() 19 22 { 20 23 // 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'); 22 26 } 23 27 24 28 public function create() 25 29 { 26 $ form = new Forge(NULL, 'Create User');30 $this->template->title = 'Create User'; 27 31 32 $form = new Forge; 28 33 $form->input('email')->label(TRUE)->rules('required|length[4,32]|valid_email'); 29 34 $form->input('username')->label(TRUE)->rules('required|length[4,32]'); … … 44 49 } 45 50 46 if ($user->save() AND $user->add( 'role', 'login'))51 if ($user->save() AND $user->add(ORM::factory('role', 'login'))) 47 52 { 48 53 Auth::instance()->login($user, $form->password->value); … … 55 60 56 61 // Display the form 57 echo$form->render();62 $this->template->content = $form->render(); 58 63 } 59 64 … … 62 67 if (Auth::instance()->logged_in()) 63 68 { 64 $ form = new Forge('auth_demo/logout', 'Log Out');69 $this->template->title = 'User Logout'; 65 70 71 $form = new Forge('auth_demo/logout'); 66 72 $form->submit('Logout Now'); 67 73 } 68 74 else 69 75 { 70 $ form = new Forge(NULL, 'User Login');76 $this->template->title = 'User Login'; 71 77 78 $form = new Forge; 72 79 $form->input('username')->label(TRUE)->rules('required|length[4,32]'); 73 80 $form->password('password')->label(TRUE)->rules('required|length[5,40]'); … … 79 86 $user = ORM::factory('user', $form->username->value); 80 87 81 // Attempt a login82 88 if (Auth::instance()->login($user, $form->password->value)) 83 89 { 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'); 88 92 } 89 93 else … … 95 99 96 100 // Display the form 97 echo$form->render();101 $this->template->content = $form->render(); 98 102 } 99 103 -
trunk/modules/auth/libraries/drivers/Auth/ORM.php
r3114 r3267 37 37 // Checks if a user is logged in and valid 38 38 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) 40 40 { 41 41 // Everything is okay so far … … 61 61 62 62 // 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) 64 64 { 65 65 if ($remember === TRUE) -
trunk/modules/auth/models/auth_user.php
r3094 r3267 6 6 protected $has_many = array('user_tokens'); 7 7 protected $has_and_belongs_to_many = array('roles'); 8 9 // User roles10 protected $has_roles;11 8 12 9 public function __set($key, $value) … … 19 16 20 17 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 roles35 $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 else43 {44 return isset($this->has_roles[$id]);45 }46 }47 48 return parent::has($object, $id);49 18 } 50 19 … … 75 44 } 76 45 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 roles87 $this->has_roles = NULL;88 }89 90 return $result;91 }92 93 46 } // 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"> 2 2 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 4 10 CREATE TABLE IF NOT EXISTS `roles` ( 5 11 `id` int(11) unsigned NOT NULL auto_increment, … … 8 14 PRIMARY KEY (`id`), 9 15 UNIQUE KEY `uniq_name` (`name`) 10 ) ENGINE= MyISAMDEFAULT CHARSET=utf8;16 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 11 17 12 18 INSERT INTO `roles` (`id`, `name`, `description`) VALUES(1, 'login', 'Login privileges, granted after account confirmation'); 13 19 INSERT INTO `roles` (`id`, `name`, `description`) VALUES(2, 'admin', 'Administrative user, has access to everything.'); 20 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; 14 27 15 28 CREATE TABLE IF NOT EXISTS `users` ( … … 23 36 UNIQUE KEY `uniq_username` (`username`), 24 37 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; 32 39 33 40 CREATE TABLE IF NOT EXISTS `user_tokens` ( … … 39 46 `expires` int(10) unsigned NOT NULL, 40 47 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; 44 51 45 <p>After the tables have been installed, <?php echo html::anchor('auth_demo/create', 'create a user') ?>.</p> 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"') ?> 60 61 </div>
