Added KH 3.3.0
This commit is contained in:
3
includes/kohana/modules/orm/classes/Auth/ORM.php
Normal file
3
includes/kohana/modules/orm/classes/Auth/ORM.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Auth_ORM extends Kohana_Auth_ORM { }
|
291
includes/kohana/modules/orm/classes/Kohana/Auth/ORM.php
Normal file
291
includes/kohana/modules/orm/classes/Kohana/Auth/ORM.php
Normal file
@@ -0,0 +1,291 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* ORM Auth driver.
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Auth_ORM extends Auth {
|
||||
|
||||
/**
|
||||
* Checks if a session is active.
|
||||
*
|
||||
* @param mixed $role Role name string, role ORM object, or array with role names
|
||||
* @return boolean
|
||||
*/
|
||||
public function logged_in($role = NULL)
|
||||
{
|
||||
// Get the user from the session
|
||||
$user = $this->get_user();
|
||||
|
||||
if ( ! $user)
|
||||
return FALSE;
|
||||
|
||||
if ($user instanceof Model_User AND $user->loaded())
|
||||
{
|
||||
// If we don't have a roll no further checking is needed
|
||||
if ( ! $role)
|
||||
return TRUE;
|
||||
|
||||
if (is_array($role))
|
||||
{
|
||||
// Get all the roles
|
||||
$roles = ORM::factory('Role')
|
||||
->where('name', 'IN', $role)
|
||||
->find_all()
|
||||
->as_array(NULL, 'id');
|
||||
|
||||
// Make sure all the roles are valid ones
|
||||
if (count($roles) !== count($role))
|
||||
return FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ! is_object($role))
|
||||
{
|
||||
// Load the role
|
||||
$roles = ORM::factory('Role', array('name' => $role));
|
||||
|
||||
if ( ! $roles->loaded())
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return $user->has('roles', $roles);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a user in.
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param boolean $remember enable autologin
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _login($user, $password, $remember)
|
||||
{
|
||||
if ( ! is_object($user))
|
||||
{
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('User');
|
||||
$user->where($user->unique_key($username), '=', $username)->find();
|
||||
}
|
||||
|
||||
if (is_string($password))
|
||||
{
|
||||
// Create a hashed password
|
||||
$password = $this->hash($password);
|
||||
}
|
||||
|
||||
// If the passwords match, perform a login
|
||||
if ($user->has('roles', ORM::factory('Role', array('name' => 'login'))) AND $user->password === $password)
|
||||
{
|
||||
if ($remember === TRUE)
|
||||
{
|
||||
// Token data
|
||||
$data = array(
|
||||
'user_id' => $user->pk(),
|
||||
'expires' => time() + $this->_config['lifetime'],
|
||||
'user_agent' => sha1(Request::$user_agent),
|
||||
);
|
||||
|
||||
// Create a new autologin token
|
||||
$token = ORM::factory('User_Token')
|
||||
->values($data)
|
||||
->create();
|
||||
|
||||
// Set the autologin cookie
|
||||
Cookie::set('authautologin', $token->token, $this->_config['lifetime']);
|
||||
}
|
||||
|
||||
// Finish the login
|
||||
$this->complete_login($user);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Login failed
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces a user to be logged in, without specifying a password.
|
||||
*
|
||||
* @param mixed $user username string, or user ORM object
|
||||
* @param boolean $mark_session_as_forced mark the session as forced
|
||||
* @return boolean
|
||||
*/
|
||||
public function force_login($user, $mark_session_as_forced = FALSE)
|
||||
{
|
||||
if ( ! is_object($user))
|
||||
{
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('User');
|
||||
$user->where($user->unique_key($username), '=', $username)->find();
|
||||
}
|
||||
|
||||
if ($mark_session_as_forced === TRUE)
|
||||
{
|
||||
// Mark the session as forced, to prevent users from changing account information
|
||||
$this->_session->set('auth_forced', TRUE);
|
||||
}
|
||||
|
||||
// Run the standard completion
|
||||
$this->complete_login($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a user in, based on the authautologin cookie.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function auto_login()
|
||||
{
|
||||
if ($token = Cookie::get('authautologin'))
|
||||
{
|
||||
// Load the token and user
|
||||
$token = ORM::factory('User_Token', array('token' => $token));
|
||||
|
||||
if ($token->loaded() AND $token->user->loaded())
|
||||
{
|
||||
if ($token->user_agent === sha1(Request::$user_agent))
|
||||
{
|
||||
// Save the token to create a new unique token
|
||||
$token->save();
|
||||
|
||||
// Set the new token
|
||||
Cookie::set('authautologin', $token->token, $token->expires - time());
|
||||
|
||||
// Complete the login with the found data
|
||||
$this->complete_login($token->user);
|
||||
|
||||
// Automatic login was successful
|
||||
return $token->user;
|
||||
}
|
||||
|
||||
// Token is invalid
|
||||
$token->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently logged in user from the session (with auto_login check).
|
||||
* Returns $default if no user is currently logged in.
|
||||
*
|
||||
* @param mixed $default to return in case user isn't logged in
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_user($default = NULL)
|
||||
{
|
||||
$user = parent::get_user($default);
|
||||
|
||||
if ($user === $default)
|
||||
{
|
||||
// check for "remembered" login
|
||||
if (($user = $this->auto_login()) === FALSE)
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log a user out and remove any autologin cookies.
|
||||
*
|
||||
* @param boolean $destroy completely destroy the session
|
||||
* @param boolean $logout_all remove all tokens for user
|
||||
* @return boolean
|
||||
*/
|
||||
public function logout($destroy = FALSE, $logout_all = FALSE)
|
||||
{
|
||||
// Set by force_login()
|
||||
$this->_session->delete('auth_forced');
|
||||
|
||||
if ($token = Cookie::get('authautologin'))
|
||||
{
|
||||
// Delete the autologin cookie to prevent re-login
|
||||
Cookie::delete('authautologin');
|
||||
|
||||
// Clear the autologin token from the database
|
||||
$token = ORM::factory('User_Token', array('token' => $token));
|
||||
|
||||
if ($token->loaded() AND $logout_all)
|
||||
{
|
||||
// Delete all user tokens. This isn't the most elegant solution but does the job
|
||||
$tokens = ORM::factory('User_Token')->where('user_id','=',$token->user_id)->find_all();
|
||||
|
||||
foreach ($tokens as $_token)
|
||||
{
|
||||
$_token->delete();
|
||||
}
|
||||
}
|
||||
elseif ($token->loaded())
|
||||
{
|
||||
$token->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return parent::logout($destroy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the stored password for a username.
|
||||
*
|
||||
* @param mixed $user username string, or user ORM object
|
||||
* @return string
|
||||
*/
|
||||
public function password($user)
|
||||
{
|
||||
if ( ! is_object($user))
|
||||
{
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('User');
|
||||
$user->where($user->unique_key($username), '=', $username)->find();
|
||||
}
|
||||
|
||||
return $user->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete the login for a user by incrementing the logins and setting
|
||||
* session data: user_id, username, roles.
|
||||
*
|
||||
* @param object $user user ORM object
|
||||
* @return void
|
||||
*/
|
||||
protected function complete_login($user)
|
||||
{
|
||||
$user->complete_login();
|
||||
|
||||
return parent::complete_login($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare password with original (hashed). Works for current (logged in) user
|
||||
*
|
||||
* @param string $password
|
||||
* @return boolean
|
||||
*/
|
||||
public function check_password($password)
|
||||
{
|
||||
$user = $this->get_user();
|
||||
|
||||
if ( ! $user)
|
||||
return FALSE;
|
||||
|
||||
return ($this->hash($password) === $user->password);
|
||||
}
|
||||
|
||||
} // End Auth ORM
|
2354
includes/kohana/modules/orm/classes/Kohana/ORM.php
Normal file
2354
includes/kohana/modules/orm/classes/Kohana/ORM.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,198 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* ORM Validation exceptions.
|
||||
*
|
||||
* @package Kohana/ORM
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
|
||||
/**
|
||||
* Array of validation objects
|
||||
* @var array
|
||||
*/
|
||||
protected $_objects = array();
|
||||
|
||||
/**
|
||||
* The alias of the main ORM model this exception was created for
|
||||
* @var string
|
||||
*/
|
||||
protected $_alias = NULL;
|
||||
|
||||
/**
|
||||
* Constructs a new exception for the specified model
|
||||
*
|
||||
* @param string $alias The alias to use when looking for error messages
|
||||
* @param Validation $object The Validation object of the model
|
||||
* @param string $message The error message
|
||||
* @param array $values The array of values for the error message
|
||||
* @param integer $code The error code for the exception
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0, Exception $previous = NULL)
|
||||
{
|
||||
$this->_alias = $alias;
|
||||
$this->_objects['_object'] = $object;
|
||||
$this->_objects['_has_many'] = FALSE;
|
||||
|
||||
parent::__construct($message, $values, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Validation object to this exception
|
||||
*
|
||||
* // The following will add a validation object for a profile model
|
||||
* // inside the exception for a user model.
|
||||
* $e->add_object('profile', $validation);
|
||||
* // The errors array will now look something like this
|
||||
* // array
|
||||
* // (
|
||||
* // 'username' => 'This field is required',
|
||||
* // 'profile' => array
|
||||
* // (
|
||||
* // 'first_name' => 'This field is required',
|
||||
* // ),
|
||||
* // );
|
||||
*
|
||||
* @param string $alias The relationship alias from the model
|
||||
* @param Validation $object The Validation object to merge
|
||||
* @param mixed $has_many The array key to use if this exception can be merged multiple times
|
||||
* @return ORM_Validation_Exception
|
||||
*/
|
||||
public function add_object($alias, Validation $object, $has_many = FALSE)
|
||||
{
|
||||
// We will need this when generating errors
|
||||
$this->_objects[$alias]['_has_many'] = ($has_many !== FALSE);
|
||||
|
||||
if ($has_many === TRUE)
|
||||
{
|
||||
// This is most likely a has_many relationship
|
||||
$this->_objects[$alias][]['_object'] = $object;
|
||||
}
|
||||
elseif ($has_many)
|
||||
{
|
||||
// This is most likely a has_many relationship
|
||||
$this->_objects[$alias][$has_many]['_object'] = $object;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->_objects[$alias]['_object'] = $object;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges an ORM_Validation_Exception object into the current exception
|
||||
* Useful when you want to combine errors into one array
|
||||
*
|
||||
* @param ORM_Validation_Exception $object The exception to merge
|
||||
* @param mixed $has_many The array key to use if this exception can be merged multiple times
|
||||
* @return ORM_Validation_Exception
|
||||
*/
|
||||
public function merge(ORM_Validation_Exception $object, $has_many = FALSE)
|
||||
{
|
||||
$alias = $object->alias();
|
||||
|
||||
// We will need this when generating errors
|
||||
$this->_objects[$alias]['_has_many'] = ($has_many !== FALSE);
|
||||
|
||||
if ($has_many === TRUE)
|
||||
{
|
||||
// This is most likely a has_many relationship
|
||||
$this->_objects[$alias][] = $object->objects();
|
||||
}
|
||||
elseif ($has_many)
|
||||
{
|
||||
// This is most likely a has_many relationship
|
||||
$this->_objects[$alias][$has_many] = $object->objects();
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->_objects[$alias] = $object->objects();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a merged array of the errors from all the Validation objects in this exception
|
||||
*
|
||||
* // Will load Model_User errors from messages/orm-validation/user.php
|
||||
* $e->errors('orm-validation');
|
||||
*
|
||||
* @param string $directory Directory to load error messages from
|
||||
* @param mixed $translate Translate the message
|
||||
* @return array
|
||||
* @see generate_errors()
|
||||
*/
|
||||
public function errors($directory = NULL, $translate = TRUE)
|
||||
{
|
||||
return $this->generate_errors($this->_alias, $this->_objects, $directory, $translate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursive method to fetch all the errors in this exception
|
||||
*
|
||||
* @param string $alias Alias to use for messages file
|
||||
* @param array $array Array of Validation objects to get errors from
|
||||
* @param string $directory Directory to load error messages from
|
||||
* @param mixed $translate Translate the message
|
||||
* @return array
|
||||
*/
|
||||
protected function generate_errors($alias, array $array, $directory, $translate)
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
foreach ($array as $key => $object)
|
||||
{
|
||||
if (is_array($object))
|
||||
{
|
||||
$errors[$key] = ($key === '_external')
|
||||
// Search for errors in $alias/_external.php
|
||||
? $this->generate_errors($alias.'/'.$key, $object, $directory, $translate)
|
||||
// Regular models get their own file not nested within $alias
|
||||
: $this->generate_errors($key, $object, $directory, $translate);
|
||||
}
|
||||
elseif ($object instanceof Validation)
|
||||
{
|
||||
if ($directory === NULL)
|
||||
{
|
||||
// Return the raw errors
|
||||
$file = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
$file = trim($directory.'/'.$alias, '/');
|
||||
}
|
||||
|
||||
// Merge in this array of errors
|
||||
$errors += $object->errors($file, $translate);
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the protected _objects property from this exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function objects()
|
||||
{
|
||||
return $this->_objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the protected _alias property from this exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function alias()
|
||||
{
|
||||
return $this->_alias;
|
||||
}
|
||||
} // End Kohana_ORM_Validation_Exception
|
31
includes/kohana/modules/orm/classes/Model/Auth/Role.php
Normal file
31
includes/kohana/modules/orm/classes/Model/Auth/Role.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth role
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license.html
|
||||
*/
|
||||
class Model_Auth_Role extends ORM {
|
||||
|
||||
// Relationships
|
||||
protected $_has_many = array(
|
||||
'users' => array('model' => 'User','through' => 'roles_users'),
|
||||
);
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return array(
|
||||
'name' => array(
|
||||
array('not_empty'),
|
||||
array('min_length', array(':value', 4)),
|
||||
array('max_length', array(':value', 32)),
|
||||
),
|
||||
'description' => array(
|
||||
array('max_length', array(':value', 255)),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
} // End Auth Role Model
|
204
includes/kohana/modules/orm/classes/Model/Auth/User.php
Normal file
204
includes/kohana/modules/orm/classes/Model/Auth/User.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth user
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Model_Auth_User extends ORM {
|
||||
|
||||
/**
|
||||
* A user has many tokens and roles
|
||||
*
|
||||
* @var array Relationhips
|
||||
*/
|
||||
protected $_has_many = array(
|
||||
'user_tokens' => array('model' => 'User_Token'),
|
||||
'roles' => array('model' => 'Role', 'through' => 'roles_users'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Rules for the user model. Because the password is _always_ a hash
|
||||
* when it's set,you need to run an additional not_empty rule in your controller
|
||||
* to make sure you didn't hash an empty string. The password rules
|
||||
* should be enforced outside the model or with a model helper method.
|
||||
*
|
||||
* @return array Rules
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return array(
|
||||
'username' => array(
|
||||
array('not_empty'),
|
||||
array('max_length', array(':value', 32)),
|
||||
array(array($this, 'unique'), array('username', ':value')),
|
||||
),
|
||||
'password' => array(
|
||||
array('not_empty'),
|
||||
),
|
||||
'email' => array(
|
||||
array('not_empty'),
|
||||
array('email'),
|
||||
array(array($this, 'unique'), array('email', ':value')),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters to run when data is set in this model. The password filter
|
||||
* automatically hashes the password when it's set in the model.
|
||||
*
|
||||
* @return array Filters
|
||||
*/
|
||||
public function filters()
|
||||
{
|
||||
return array(
|
||||
'password' => array(
|
||||
array(array(Auth::instance(), 'hash'))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Labels for fields in this model
|
||||
*
|
||||
* @return array Labels
|
||||
*/
|
||||
public function labels()
|
||||
{
|
||||
return array(
|
||||
'username' => 'username',
|
||||
'email' => 'email address',
|
||||
'password' => 'password',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Complete the login for a user by incrementing the logins and saving login timestamp
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function complete_login()
|
||||
{
|
||||
if ($this->_loaded)
|
||||
{
|
||||
// Update the number of logins
|
||||
$this->logins = new Database_Expression('logins + 1');
|
||||
|
||||
// Set the last login date
|
||||
$this->last_login = time();
|
||||
|
||||
// Save the user
|
||||
$this->update();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a unique key value exists in the database.
|
||||
*
|
||||
* @param mixed the value to test
|
||||
* @param string field name
|
||||
* @return boolean
|
||||
*/
|
||||
public function unique_key_exists($value, $field = NULL)
|
||||
{
|
||||
if ($field === NULL)
|
||||
{
|
||||
// Automatically determine field by looking at the value
|
||||
$field = $this->unique_key($value);
|
||||
}
|
||||
|
||||
return (bool) DB::select(array(DB::expr('COUNT(*)'), 'total_count'))
|
||||
->from($this->_table_name)
|
||||
->where($field, '=', $value)
|
||||
->where($this->_primary_key, '!=', $this->pk())
|
||||
->execute($this->_db)
|
||||
->get('total_count');
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows a model use both email and username as unique identifiers for login
|
||||
*
|
||||
* @param string unique value
|
||||
* @return string field name
|
||||
*/
|
||||
public function unique_key($value)
|
||||
{
|
||||
return Valid::email($value) ? 'email' : 'username';
|
||||
}
|
||||
|
||||
/**
|
||||
* Password validation for plain passwords.
|
||||
*
|
||||
* @param array $values
|
||||
* @return Validation
|
||||
*/
|
||||
public static function get_password_validation($values)
|
||||
{
|
||||
return Validation::factory($values)
|
||||
->rule('password', 'min_length', array(':value', 8))
|
||||
->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user
|
||||
*
|
||||
* Example usage:
|
||||
* ~~~
|
||||
* $user = ORM::factory('User')->create_user($_POST, array(
|
||||
* 'username',
|
||||
* 'password',
|
||||
* 'email',
|
||||
* );
|
||||
* ~~~
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $expected
|
||||
* @throws ORM_Validation_Exception
|
||||
*/
|
||||
public function create_user($values, $expected)
|
||||
{
|
||||
// Validation for passwords
|
||||
$extra_validation = Model_User::get_password_validation($values)
|
||||
->rule('password', 'not_empty');
|
||||
|
||||
return $this->values($values, $expected)->create($extra_validation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing user
|
||||
*
|
||||
* [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
|
||||
*
|
||||
* Example usage:
|
||||
* ~~~
|
||||
* $user = ORM::factory('User')
|
||||
* ->where('username', '=', 'kiall')
|
||||
* ->find()
|
||||
* ->update_user($_POST, array(
|
||||
* 'username',
|
||||
* 'password',
|
||||
* 'email',
|
||||
* );
|
||||
* ~~~
|
||||
*
|
||||
* @param array $values
|
||||
* @param array $expected
|
||||
* @throws ORM_Validation_Exception
|
||||
*/
|
||||
public function update_user($values, $expected = NULL)
|
||||
{
|
||||
if (empty($values['password']))
|
||||
{
|
||||
unset($values['password'], $values['password_confirm']);
|
||||
}
|
||||
|
||||
// Validation for passwords
|
||||
$extra_validation = Model_User::get_password_validation($values);
|
||||
|
||||
return $this->values($values, $expected)->update($extra_validation);
|
||||
}
|
||||
|
||||
} // End Auth User Model
|
@@ -0,0 +1,77 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth user toke
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Model_Auth_User_Token extends ORM {
|
||||
|
||||
// Relationships
|
||||
protected $_belongs_to = array(
|
||||
'user' => array('model' => 'User'),
|
||||
);
|
||||
|
||||
protected $_created_column = array(
|
||||
'column' => 'created',
|
||||
'format' => TRUE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Handles garbage collection and deleting of expired objects.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($id = NULL)
|
||||
{
|
||||
parent::__construct($id);
|
||||
|
||||
if (mt_rand(1, 100) === 1)
|
||||
{
|
||||
// Do garbage collection
|
||||
$this->delete_expired();
|
||||
}
|
||||
|
||||
if ($this->expires < time() AND $this->_loaded)
|
||||
{
|
||||
// This object has expired
|
||||
$this->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all expired tokens.
|
||||
*
|
||||
* @return ORM
|
||||
*/
|
||||
public function delete_expired()
|
||||
{
|
||||
// Delete all expired tokens
|
||||
DB::delete($this->_table_name)
|
||||
->where('expires', '<', time())
|
||||
->execute($this->_db);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function create(Validation $validation = NULL)
|
||||
{
|
||||
$this->token = $this->create_token();
|
||||
|
||||
return parent::create($validation);
|
||||
}
|
||||
|
||||
protected function create_token()
|
||||
{
|
||||
do
|
||||
{
|
||||
$token = sha1(uniqid(Text::random('alnum', 32), TRUE));
|
||||
}
|
||||
while (ORM::factory('User_Token', array('token' => $token))->loaded());
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
} // End Auth User Token Model
|
7
includes/kohana/modules/orm/classes/Model/Role.php
Normal file
7
includes/kohana/modules/orm/classes/Model/Role.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_Role extends Model_Auth_Role {
|
||||
|
||||
// This class can be replaced or extended
|
||||
|
||||
} // End Role Model
|
7
includes/kohana/modules/orm/classes/Model/User.php
Normal file
7
includes/kohana/modules/orm/classes/Model/User.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_User extends Model_Auth_User {
|
||||
|
||||
// This class can be replaced or extended
|
||||
|
||||
} // End User Model
|
7
includes/kohana/modules/orm/classes/Model/User/Token.php
Normal file
7
includes/kohana/modules/orm/classes/Model/User/Token.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_User_Token extends Model_Auth_User_Token {
|
||||
|
||||
// This class can be replaced or extended
|
||||
|
||||
} // End User Token Model
|
3
includes/kohana/modules/orm/classes/ORM.php
Normal file
3
includes/kohana/modules/orm/classes/ORM.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
|
||||
class ORM extends Kohana_ORM {}
|
@@ -0,0 +1,10 @@
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* ORM Validation exceptions.
|
||||
*
|
||||
* @package Kohana/ORM
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license
|
||||
*/
|
||||
class ORM_Validation_Exception extends Kohana_ORM_Validation_Exception {}
|
Reference in New Issue
Block a user