Upgrade to 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 { }
|
@@ -1,10 +1,10 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* ORM Auth driver.
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2011 Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_Auth_ORM extends Auth {
|
||||
@@ -32,7 +32,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
if (is_array($role))
|
||||
{
|
||||
// Get all the roles
|
||||
$roles = ORM::factory('role')
|
||||
$roles = ORM::factory('Role')
|
||||
->where('name', 'IN', $role)
|
||||
->find_all()
|
||||
->as_array(NULL, 'id');
|
||||
@@ -46,7 +46,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
if ( ! is_object($role))
|
||||
{
|
||||
// Load the role
|
||||
$roles = ORM::factory('role', array('name' => $role));
|
||||
$roles = ORM::factory('Role', array('name' => $role));
|
||||
|
||||
if ( ! $roles->loaded())
|
||||
return FALSE;
|
||||
@@ -60,9 +60,9 @@ class Kohana_Auth_ORM extends Auth {
|
||||
/**
|
||||
* Logs a user in.
|
||||
*
|
||||
* @param string username
|
||||
* @param string password
|
||||
* @param boolean enable autologin
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @param boolean $remember enable autologin
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _login($user, $password, $remember)
|
||||
@@ -72,24 +72,30 @@ class Kohana_Auth_ORM extends Auth {
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('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 ($user->has('roles', ORM::factory('Role', array('name' => 'login'))) AND $user->password === $password)
|
||||
{
|
||||
if ($remember === TRUE)
|
||||
{
|
||||
// Token data
|
||||
$data = array(
|
||||
'user_id' => $user->id,
|
||||
'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')
|
||||
$token = ORM::factory('User_Token')
|
||||
->values($data)
|
||||
->create();
|
||||
|
||||
@@ -110,8 +116,8 @@ class Kohana_Auth_ORM extends Auth {
|
||||
/**
|
||||
* Forces a user to be logged in, without specifying a password.
|
||||
*
|
||||
* @param mixed username string, or user ORM object
|
||||
* @param boolean mark the session as forced
|
||||
* @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)
|
||||
@@ -121,7 +127,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('user');
|
||||
$user = ORM::factory('User');
|
||||
$user->where($user->unique_key($username), '=', $username)->find();
|
||||
}
|
||||
|
||||
@@ -145,7 +151,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
if ($token = Cookie::get('authautologin'))
|
||||
{
|
||||
// Load the token and user
|
||||
$token = ORM::factory('user_token', array('token' => $token));
|
||||
$token = ORM::factory('User_Token', array('token' => $token));
|
||||
|
||||
if ($token->loaded() AND $token->user->loaded())
|
||||
{
|
||||
@@ -174,18 +180,20 @@ class Kohana_Auth_ORM extends Auth {
|
||||
|
||||
/**
|
||||
* Gets the currently logged in user from the session (with auto_login check).
|
||||
* Returns FALSE if no user is currently logged in.
|
||||
* 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)
|
||||
if ($user === $default)
|
||||
{
|
||||
// check for "remembered" login
|
||||
$user = $this->auto_login();
|
||||
if (($user = $this->auto_login()) === FALSE)
|
||||
return $default;
|
||||
}
|
||||
|
||||
return $user;
|
||||
@@ -194,8 +202,8 @@ class Kohana_Auth_ORM extends Auth {
|
||||
/**
|
||||
* Log a user out and remove any autologin cookies.
|
||||
*
|
||||
* @param boolean completely destroy the session
|
||||
* @param boolean remove all tokens for user
|
||||
* @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)
|
||||
@@ -209,11 +217,17 @@ class Kohana_Auth_ORM extends Auth {
|
||||
Cookie::delete('authautologin');
|
||||
|
||||
// Clear the autologin token from the database
|
||||
$token = ORM::factory('user_token', array('token' => $token));
|
||||
$token = ORM::factory('User_Token', array('token' => $token));
|
||||
|
||||
if ($token->loaded() AND $logout_all)
|
||||
{
|
||||
ORM::factory('user_token')->where('user_id', '=', $token->user_id)->delete_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())
|
||||
{
|
||||
@@ -227,7 +241,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
/**
|
||||
* Get the stored password for a username.
|
||||
*
|
||||
* @param mixed username string, or user ORM object
|
||||
* @param mixed $user username string, or user ORM object
|
||||
* @return string
|
||||
*/
|
||||
public function password($user)
|
||||
@@ -237,7 +251,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
$username = $user;
|
||||
|
||||
// Load the user
|
||||
$user = ORM::factory('user');
|
||||
$user = ORM::factory('User');
|
||||
$user->where($user->unique_key($username), '=', $username)->find();
|
||||
}
|
||||
|
||||
@@ -248,7 +262,7 @@ class Kohana_Auth_ORM extends Auth {
|
||||
* Complete the login for a user by incrementing the logins and setting
|
||||
* session data: user_id, username, roles.
|
||||
*
|
||||
* @param object user ORM object
|
||||
* @param object $user user ORM object
|
||||
* @return void
|
||||
*/
|
||||
protected function complete_login($user)
|
||||
@@ -274,4 +288,4 @@ class Kohana_Auth_ORM extends Auth {
|
||||
return ($this->hash($password) === $user->password);
|
||||
}
|
||||
|
||||
} // End Auth ORM
|
||||
} // End Auth ORM
|
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,10 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* ORM Validation exceptions.
|
||||
*
|
||||
* @package Kohana/ORM
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2010 Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
@@ -16,27 +16,28 @@ class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
protected $_objects = array();
|
||||
|
||||
/**
|
||||
* The _object_name property of the main ORM model this exception was created for
|
||||
* The alias of the main ORM model this exception was created for
|
||||
* @var string
|
||||
*/
|
||||
protected $_object_name = NULL;
|
||||
protected $_alias = NULL;
|
||||
|
||||
/**
|
||||
* Constructs a new exception for the specified model
|
||||
*
|
||||
* @param string $object_name The _object_name of the model this exception is for
|
||||
* @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($object_name, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0)
|
||||
public function __construct($alias, Validation $object, $message = 'Failed to validate array', array $values = NULL, $code = 0, Exception $previous = NULL)
|
||||
{
|
||||
$this->_object_name = $object_name;
|
||||
$this->_alias = $alias;
|
||||
$this->_objects['_object'] = $object;
|
||||
$this->_objects['_has_many'] = FALSE;
|
||||
|
||||
parent::__construct($message, $values, $code);
|
||||
parent::__construct($message, $values, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,6 +63,9 @@ class Kohana_ORM_Validation_Exception extends Kohana_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
|
||||
@@ -84,13 +88,17 @@ class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
* Merges an ORM_Validation_Exception object into the current exception
|
||||
* Useful when you want to combine errors into one array
|
||||
*
|
||||
* @param string $alias The relationship alias from the model
|
||||
* @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($alias, ORM_Validation_Exception $object, $has_many = FALSE)
|
||||
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
|
||||
@@ -122,48 +130,46 @@ class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
*/
|
||||
public function errors($directory = NULL, $translate = TRUE)
|
||||
{
|
||||
if ($directory !== NULL)
|
||||
{
|
||||
// Everything starts at $directory/$object_name
|
||||
$directory .= '/'.$this->_object_name;
|
||||
}
|
||||
|
||||
return $this->generate_errors($this->_objects, $directory, $translate);
|
||||
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(array $array, $directory, $translate)
|
||||
protected function generate_errors($alias, array $array, $directory, $translate)
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
foreach ($array as $alias => $object)
|
||||
foreach ($array as $key => $object)
|
||||
{
|
||||
if ($directory === NULL)
|
||||
{
|
||||
// Return the raw errors
|
||||
$file = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
$file = trim($directory.'/'.$alias, '/');
|
||||
}
|
||||
|
||||
if (is_array($object))
|
||||
{
|
||||
// Recursively fill the errors array
|
||||
$errors[$alias] = $this->generate_errors($object, $file, $translate);
|
||||
$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);
|
||||
}
|
||||
else
|
||||
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($directory, $translate);
|
||||
$errors += $object->errors($file, $translate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,4 +185,14 @@ class Kohana_ORM_Validation_Exception extends Kohana_Exception {
|
||||
{
|
||||
return $this->_objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the protected _alias property from this exception
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function alias()
|
||||
{
|
||||
return $this->_alias;
|
||||
}
|
||||
} // End Kohana_ORM_Validation_Exception
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth role
|
||||
*
|
||||
@@ -10,7 +10,9 @@
|
||||
class Model_Auth_Role extends ORM {
|
||||
|
||||
// Relationships
|
||||
protected $_has_many = array('users' => array('through' => 'roles_users'));
|
||||
protected $_has_many = array(
|
||||
'users' => array('model' => 'User','through' => 'roles_users'),
|
||||
);
|
||||
|
||||
public function rules()
|
||||
{
|
||||
@@ -26,4 +28,4 @@ class Model_Auth_Role extends ORM {
|
||||
);
|
||||
}
|
||||
|
||||
} // End Auth Role Model
|
||||
} // End Auth Role Model
|
@@ -1,10 +1,10 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth user
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2011 Kohana Team
|
||||
* @copyright (c) 2007-2012 Kohana Team
|
||||
* @license http://kohanaframework.org/license
|
||||
*/
|
||||
class Model_Auth_User extends ORM {
|
||||
@@ -15,8 +15,8 @@ class Model_Auth_User extends ORM {
|
||||
* @var array Relationhips
|
||||
*/
|
||||
protected $_has_many = array(
|
||||
'user_tokens' => array('model' => 'user_token'),
|
||||
'roles' => array('model' => 'role', 'through' => 'roles_users'),
|
||||
'user_tokens' => array('model' => 'User_Token'),
|
||||
'roles' => array('model' => 'Role', 'through' => 'roles_users'),
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -32,20 +32,16 @@ class Model_Auth_User extends ORM {
|
||||
return array(
|
||||
'username' => array(
|
||||
array('not_empty'),
|
||||
array('min_length', array(':value', 4)),
|
||||
array('max_length', array(':value', 32)),
|
||||
array('regex', array(':value', '/^[-\pL\pN_.]++$/uD')),
|
||||
array(array($this, 'username_available'), array(':validation', ':field')),
|
||||
array(array($this, 'unique'), array('username', ':value')),
|
||||
),
|
||||
'password' => array(
|
||||
array('not_empty'),
|
||||
),
|
||||
'email' => array(
|
||||
array('not_empty'),
|
||||
array('min_length', array(':value', 4)),
|
||||
array('max_length', array(':value', 127)),
|
||||
array('email'),
|
||||
array(array($this, 'email_available'), array(':validation', ':field')),
|
||||
array(array($this, 'unique'), array('email', ':value')),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -99,38 +95,6 @@ class Model_Auth_User extends ORM {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the reverse of unique_key_exists() by triggering error if username exists.
|
||||
* Validation callback.
|
||||
*
|
||||
* @param Validation Validation object
|
||||
* @param string Field name
|
||||
* @return void
|
||||
*/
|
||||
public function username_available(Validation $validation, $field)
|
||||
{
|
||||
if ($this->unique_key_exists($validation[$field], 'username'))
|
||||
{
|
||||
$validation->error($field, 'username_available', array($validation[$field]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the reverse of unique_key_exists() by triggering error if email exists.
|
||||
* Validation callback.
|
||||
*
|
||||
* @param Validation Validation object
|
||||
* @param string Field name
|
||||
* @return void
|
||||
*/
|
||||
public function email_available(Validation $validation, $field)
|
||||
{
|
||||
if ($this->unique_key_exists($validation[$field], 'email'))
|
||||
{
|
||||
$validation->error($field, 'email_available', array($validation[$field]));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if a unique key value exists in the database.
|
||||
*
|
||||
@@ -146,7 +110,7 @@ class Model_Auth_User extends ORM {
|
||||
$field = $this->unique_key($value);
|
||||
}
|
||||
|
||||
return (bool) DB::select(array('COUNT("*")', 'total_count'))
|
||||
return (bool) DB::select(array(DB::expr('COUNT(*)'), 'total_count'))
|
||||
->from($this->_table_name)
|
||||
->where($field, '=', $value)
|
||||
->where($this->_primary_key, '!=', $this->pk())
|
||||
@@ -183,7 +147,7 @@ class Model_Auth_User extends ORM {
|
||||
*
|
||||
* Example usage:
|
||||
* ~~~
|
||||
* $user = ORM::factory('user')->create_user($_POST, array(
|
||||
* $user = ORM::factory('User')->create_user($_POST, array(
|
||||
* 'username',
|
||||
* 'password',
|
||||
* 'email',
|
||||
@@ -210,7 +174,7 @@ class Model_Auth_User extends ORM {
|
||||
*
|
||||
* Example usage:
|
||||
* ~~~
|
||||
* $user = ORM::factory('user')
|
||||
* $user = ORM::factory('User')
|
||||
* ->where('username', '=', 'kiall')
|
||||
* ->find()
|
||||
* ->update_user($_POST, array(
|
||||
@@ -237,4 +201,4 @@ class Model_Auth_User extends ORM {
|
||||
return $this->values($values, $expected)->update($extra_validation);
|
||||
}
|
||||
|
||||
} // End Auth User Model
|
||||
} // End Auth User Model
|
@@ -1,16 +1,23 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
/**
|
||||
* Default auth user toke
|
||||
*
|
||||
* @package Kohana/Auth
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2007-2011 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());
|
||||
protected $_belongs_to = array(
|
||||
'user' => array('model' => 'User'),
|
||||
);
|
||||
|
||||
protected $_created_column = array(
|
||||
'column' => 'created',
|
||||
'format' => TRUE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Handles garbage collection and deleting of expired objects.
|
||||
@@ -62,9 +69,9 @@ class Model_Auth_User_Token extends ORM {
|
||||
{
|
||||
$token = sha1(uniqid(Text::random('alnum', 32), TRUE));
|
||||
}
|
||||
while(ORM::factory('user_token', array('token' => $token))->loaded());
|
||||
while (ORM::factory('User_Token', array('token' => $token))->loaded());
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
} // End Auth User Token Model
|
||||
} // End Auth User Token Model
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_Role extends Model_Auth_Role {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_User extends Model_Auth_User {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
<?php defined('SYSPATH') OR die('No direct access allowed.');
|
||||
|
||||
class Model_User_Token extends Model_Auth_User_Token {
|
||||
|
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 {}
|
@@ -1,4 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
<?php defined('SYSPATH') OR die('No direct script access.');
|
||||
/**
|
||||
* ORM Validation exceptions.
|
||||
*
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
class Auth_ORM extends Kohana_Auth_ORM { }
|
@@ -1,3 +0,0 @@
|
||||
<?php defined('SYSPATH') or die('No direct script access.');
|
||||
|
||||
class ORM extends Kohana_ORM {}
|
Reference in New Issue
Block a user