Kohana v3.3.0
This commit is contained in:
31
modules/orm/classes/Model/Auth/Role.php
Normal file
31
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
modules/orm/classes/Model/Auth/User.php
Normal file
204
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
|
77
modules/orm/classes/Model/Auth/User/Token.php
Normal file
77
modules/orm/classes/Model/Auth/User/Token.php
Normal file
@@ -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
modules/orm/classes/Model/Role.php
Normal file
7
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
modules/orm/classes/Model/User.php
Normal file
7
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
modules/orm/classes/Model/User/Token.php
Normal file
7
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
|
Reference in New Issue
Block a user