Changeset 1636
- Timestamp:
- 12/28/2007 12:35:42 PM (13 months ago)
- Location:
- trunk/modules/auth
- Files:
-
- 1 added
- 4 modified
-
config/auth.php (modified) (2 diffs)
-
libraries/Auth.php (modified) (5 diffs)
-
models/user.php (modified) (1 diff)
-
models/user_token.php (added)
-
views/auth/install.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/auth/config/auth.php
r1362 r1636 7 7 * In order to log a user in, a user must have the `login` role. You may create 8 8 * and assign any other role to your users. 9 *10 * Database table schemas:11 * <code sql>12 * CREATE TABLE IF NOT EXISTS `users` (13 * `id` int(11) unsigned NOT NULL auto_increment,14 * `email` varchar(127) NOT NULL,15 * `username` varchar(32) NOT NULL default '',16 * `password` char(50) NOT NULL,17 * `logins` int(10) unsigned NOT NULL default '0',18 * PRIMARY KEY (`id`),19 * UNIQUE KEY `uniq_username` (`username`),20 * UNIQUE KEY `uniq_email` (`email`)21 * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;22 *23 * CREATE TABLE IF NOT EXISTS `roles` (24 * `id` int(11) unsigned NOT NULL auto_increment,25 * `name` varchar(32) NOT NULL,26 * `description` varchar(255) NOT NULL,27 * PRIMARY KEY (`id`),28 * UNIQUE KEY `uniq_name` (`name`)29 * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;30 *31 * INSERT INTO `roles` (`id`, `name`, `description`) VALUES (1, 'login', 'Login access privileges');32 *33 * CREATE TABLE IF NOT EXISTS `users_roles` (34 * `user_id` int(10) unsigned NOT NULL,35 * `role_id` int(10) unsigned NOT NULL,36 * PRIMARY KEY (`user_id`,`role_id`)37 * ) ENGINE=MyISAM DEFAULT CHARSET=utf8;38 * </code>39 9 */ 40 10 … … 49 19 50 20 /** 51 * Defines the hash offsets to 21 * Defines the hash offsets to insert the salt at. The password hash length 22 * will be increased by the total number of offsets. 52 23 */ 53 24 $config['salt_pattern'] = '1, 3, 5, 9, 14, 15, 20, 21, 28, 30'; 25 26 /** 27 * Set the auto-login (remember me) cookie lifetime, in seconds. The default 28 * lifetime is two weeks. 29 */ 30 $config['lifetime'] = 1209600; -
trunk/modules/auth/libraries/Auth.php
r1391 r1636 19 19 20 20 /** 21 * Create an instance of Auth. 22 * 23 * @return object 24 */ 25 public static function factory($config = array()) 26 { 27 return new Auth($config); 28 } 29 30 /** 21 31 * Loads Session and configuration options. 22 32 */ … … 24 34 { 25 35 // Load libraries 26 $this->session = new Session();36 $this->session = Session::instance(); 27 37 28 38 // Append default auth configuration … … 41 51 * Attempt to log in a user by using an ORM object and plain-text password. 42 52 * 43 * @param object user model 44 * @param string plain-text password to check against 45 * @return bool 46 */ 47 public function login($user, $password) 48 { 49 if ( ! is_object($user) OR empty($password)) 53 * @param object user model object 54 * @param string plain-text password to check against 55 * @param bool to allow auto-login, or "remember me" feature 56 * @return bool 57 */ 58 public function login(User_Model $user, $password, $remember = FALSE) 59 { 60 if (empty($password)) 50 61 return FALSE; 51 62 … … 56 67 if ($user->has_role('login') AND $user->password === $password) 57 68 { 58 // Update the number of logins 59 $user->logins += 1; 60 61 // Save the user 62 $user->save(); 63 64 // Store session data 65 $this->session->set(array 66 ( 67 'user_id' => $user->id, 68 'username' => $user->username, 69 'roles' => $user->roles 70 )); 69 if ($remember == TRUE) 70 { 71 // Create a new autologin token 72 $token = new User_Token_Model; 73 74 // Set token data 75 $token->user_id = $user->id; 76 $token->expires = time() + $this->config['lifetime']; 77 $token->save(); 78 79 // Set the autologin cookie 80 cookie::set('autologin', $token->token, $this->config['lifetime']); 81 } 82 83 // Finish the login 84 $this->complete_login($user); 71 85 72 86 return TRUE; 87 } 88 89 return FALSE; 90 } 91 92 /** 93 * Attempt to automatically log a user in by using tokens. 94 * 95 * @return bool 96 */ 97 public function auto_login() 98 { 99 if ($token = cookie::get('autologin')) 100 { 101 // Load the token and user 102 $token = new User_Token_Model($token); 103 $user = new User_Model($token->user_id); 104 105 if ($token->id != 0 AND $user->id != 0) 106 { 107 if ($token->user_agent === sha1(Kohana::$user_agent)) 108 { 109 // Save the token to create a new unique token 110 $token->save(); 111 112 // Set the new token 113 cookie::set('autologin', $token->token, $token->expires - time()); 114 115 // Complete the login with the found data 116 $this->complete_login($user); 117 118 // Automatic login was successful 119 return TRUE; 120 } 121 122 // Token is invalid 123 $token->delete(); 124 } 73 125 } 74 126 … … 179 231 } 180 232 233 /** 234 * Complete the login for a user by incrementing the logins and setting 235 * session data: user_id, username, roles 236 * 237 * @param object user model object 238 * @return void 239 */ 240 protected function complete_login(User_Model $user) 241 { 242 // Update the number of logins 243 $user->logins += 1; 244 245 // Save the user 246 $user->save(); 247 248 // Store session data 249 $this->session->set(array 250 ( 251 'user_id' => $user->id, 252 'username' => $user->username, 253 'roles' => $user->roles 254 )); 255 } 256 181 257 } // End Auth -
trunk/modules/auth/models/user.php
r1534 r1636 4 4 5 5 // Relationships 6 protected $has_many = array('tokens'); 6 7 protected $has_and_belongs_to_many = array('roles'); 7 8 -
trunk/modules/auth/views/auth/install.php
r1297 r1636 29 29 PRIMARY KEY (`user_id`,`role_id`) 30 30 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 31 32 CREATE TABLE IF NOT EXISTS `user_tokens` ( 33 `id` int(11) unsigned NOT NULL auto_increment, 34 `user_id` int(11) unsigned NOT NULL, 35 `user_agent` varchar(40) NOT NULL, 36 `token` varchar(32) NOT NULL, 37 `created` int(10) unsigned NOT NULL, 38 `expires` int(10) unsigned NOT NULL, 39 PRIMARY KEY (`id`), 40 UNIQUE KEY `uniq_token` (`token`) 41 ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 31 42 </pre> 32 43
