mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
updating auth, acl and tests
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5410 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
e746fedf45
commit
3769fa796d
6 changed files with 233 additions and 154 deletions
|
@ -232,9 +232,7 @@ class Dispatcher extends Object {
|
||||||
$controller->namedArgs = true;
|
$controller->namedArgs = true;
|
||||||
}
|
}
|
||||||
if (!empty($controller->params['pass'])) {
|
if (!empty($controller->params['pass'])) {
|
||||||
$controller->passed_args =& $controller->params['pass'];
|
|
||||||
$controller->passedArgs =& $controller->params['pass'];
|
$controller->passedArgs =& $controller->params['pass'];
|
||||||
|
|
||||||
if ($controller->namedArgs === true) {
|
if ($controller->namedArgs === true) {
|
||||||
$controller->namedArgs = array();
|
$controller->namedArgs = array();
|
||||||
$c = count($controller->passedArgs);
|
$c = count($controller->passedArgs);
|
||||||
|
@ -265,9 +263,7 @@ class Dispatcher extends Object {
|
||||||
$controller->namedArgs = am($namedArgs, $controller->namedArgs);
|
$controller->namedArgs = am($namedArgs, $controller->namedArgs);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$controller->passed_args = null;
|
|
||||||
$controller->passedArgs = null;
|
$controller->passedArgs = null;
|
||||||
/* set default namedArgs if they exist*/
|
|
||||||
if ($controller->namedArgs === true) {
|
if ($controller->namedArgs === true) {
|
||||||
$controller->passedArgs = array();
|
$controller->passedArgs = array();
|
||||||
$controller->namedArgs = array();
|
$controller->namedArgs = array();
|
||||||
|
@ -292,6 +288,7 @@ class Dispatcher extends Object {
|
||||||
$controller->layout = $params['layout'];
|
$controller->layout = $params['layout'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (array('components', 'helpers') as $var) {
|
foreach (array('components', 'helpers') as $var) {
|
||||||
if (isset($params[$var]) && !empty($params[$var]) && is_array($controller->{$var})) {
|
if (isset($params[$var]) && !empty($params[$var]) && is_array($controller->{$var})) {
|
||||||
$diff = array_diff($params[$var], $controller->{$var});
|
$diff = array_diff($params[$var], $controller->{$var});
|
||||||
|
@ -302,7 +299,6 @@ class Dispatcher extends Object {
|
||||||
if (!is_null($controller->webservices)) {
|
if (!is_null($controller->webservices)) {
|
||||||
array_push($controller->components, $controller->webservices);
|
array_push($controller->components, $controller->webservices);
|
||||||
array_push($controller->helpers, $controller->webservices);
|
array_push($controller->helpers, $controller->webservices);
|
||||||
$component =& new Component($controller);
|
|
||||||
}
|
}
|
||||||
Router::setRequestInfo(array($params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot, 'passedArgs' => $controller->passedArgs, 'argSeparator' => $controller->argSeparator, 'namedArgs' => $controller->namedArgs, 'webservices' => $controller->webservices)));
|
Router::setRequestInfo(array($params, array('base' => $this->base, 'here' => $this->here, 'webroot' => $this->webroot, 'passedArgs' => $controller->passedArgs, 'argSeparator' => $controller->argSeparator, 'namedArgs' => $controller->namedArgs, 'webservices' => $controller->webservices)));
|
||||||
$controller->_initComponents();
|
$controller->_initComponents();
|
||||||
|
|
|
@ -26,8 +26,6 @@
|
||||||
* @lastmodified $Date$
|
* @lastmodified $Date$
|
||||||
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||||
*/
|
*/
|
||||||
uses('model' . DS . 'db_acl');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Access Control List factory class.
|
* Access Control List factory class.
|
||||||
*
|
*
|
||||||
|
@ -39,11 +37,13 @@ uses('model' . DS . 'db_acl');
|
||||||
class AclComponent extends Object {
|
class AclComponent extends Object {
|
||||||
|
|
||||||
var $_instance = null;
|
var $_instance = null;
|
||||||
|
|
||||||
|
var $name = ACL_CLASSNAME;
|
||||||
/**
|
/**
|
||||||
* Constructor. Will return an instance of the correct ACL class.
|
* Constructor. Will return an instance of the correct ACL class.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function __construct() {
|
function startup(&$controller) {
|
||||||
$this->getACL();
|
$this->getACL();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -53,8 +53,7 @@ class AclComponent extends Object {
|
||||||
*/
|
*/
|
||||||
function &getACL() {
|
function &getACL() {
|
||||||
if ($this->_instance == null) {
|
if ($this->_instance == null) {
|
||||||
$classname = ACL_CLASSNAME;
|
$this->_instance =& new $this->name();
|
||||||
$this->_instance = new $classname;
|
|
||||||
$this->_instance->initialize($this);
|
$this->_instance->initialize($this);
|
||||||
}
|
}
|
||||||
return $this->_instance;
|
return $this->_instance;
|
||||||
|
@ -216,6 +215,7 @@ class DB_ACL extends AclBase {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function __construct() {
|
function __construct() {
|
||||||
|
uses('model' . DS . 'db_acl');
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->Aro =& new Aro();
|
$this->Aro =& new Aro();
|
||||||
$this->Aco =& new Aco();
|
$this->Aco =& new Aco();
|
||||||
|
@ -419,8 +419,6 @@ class DB_ACL extends AclBase {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function getAclLink($aro, $aco) {
|
function getAclLink($aro, $aco) {
|
||||||
$Link = new Permission();
|
|
||||||
|
|
||||||
$obj = array();
|
$obj = array();
|
||||||
$obj['Aro'] = $this->Aro->node($aro);
|
$obj['Aro'] = $this->Aro->node($aro);
|
||||||
$obj['Aco'] = $this->Aco->node($aco);
|
$obj['Aco'] = $this->Aco->node($aco);
|
||||||
|
@ -475,7 +473,7 @@ class INI_ACL extends AclBase {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object).
|
* Main ACL check function. Checks to see if the ARO (access request object) has access to the ACO (access control object).
|
||||||
* Looks at the acl.ini.php file for permissions (see instructions in/config/acl.ini.php).
|
* Looks at the acl.ini.php file for permissions (see instructions in /config/acl.ini.php).
|
||||||
*
|
*
|
||||||
* @param string $aro
|
* @param string $aro
|
||||||
* @param string $aco
|
* @param string $aco
|
||||||
|
@ -487,58 +485,45 @@ class INI_ACL extends AclBase {
|
||||||
}
|
}
|
||||||
$aclConfig = $this->config;
|
$aclConfig = $this->config;
|
||||||
|
|
||||||
//First, if the user is specifically denied, then DENY
|
|
||||||
if (isset($aclConfig[$aro]['deny'])) {
|
if (isset($aclConfig[$aro]['deny'])) {
|
||||||
$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
|
$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
|
||||||
|
|
||||||
if (array_search($aco, $userDenies)) {
|
if (array_search($aco, $userDenies)) {
|
||||||
//echo "User Denied!";
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Second, if the user is specifically allowed, then ALLOW
|
|
||||||
if (isset($aclConfig[$aro]['allow'])) {
|
if (isset($aclConfig[$aro]['allow'])) {
|
||||||
$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
|
$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));
|
||||||
|
|
||||||
if (array_search($aco, $userAllows)) {
|
if (array_search($aco, $userAllows)) {
|
||||||
//echo "User Allowed!";
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check group permissions
|
|
||||||
if (isset($aclConfig[$aro]['groups'])) {
|
if (isset($aclConfig[$aro]['groups'])) {
|
||||||
$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
|
$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));
|
||||||
|
|
||||||
foreach ($userGroups as $group) {
|
foreach ($userGroups as $group) {
|
||||||
//If such a group exists,
|
|
||||||
if (array_key_exists($group, $aclConfig)) {
|
if (array_key_exists($group, $aclConfig)) {
|
||||||
//If the group is specifically denied, then DENY
|
|
||||||
if (isset($aclConfig[$group]['deny'])) {
|
if (isset($aclConfig[$group]['deny'])) {
|
||||||
$groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
|
$groupDenies=$this->arrayTrim(explode(",", $aclConfig[$group]['deny']));
|
||||||
|
|
||||||
if (array_search($aco, $groupDenies)) {
|
if (array_search($aco, $groupDenies)) {
|
||||||
//echo("Group Denied!");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the group is specifically allowed, then ALLOW
|
|
||||||
if (isset($aclConfig[$group]['allow'])) {
|
if (isset($aclConfig[$group]['allow'])) {
|
||||||
$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
|
$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));
|
||||||
|
|
||||||
if (array_search($aco, $groupAllows)) {
|
if (array_search($aco, $groupAllows)) {
|
||||||
//echo("Group Allowed!");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Default, DENY
|
|
||||||
//echo("DEFAULT: DENY.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -577,7 +562,6 @@ class INI_ACL extends AclBase {
|
||||||
$iniSetting[$sectionName][strtolower(trim($dataLine))]='';
|
$iniSetting[$sectionName][strtolower(trim($dataLine))]='';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,16 @@ class AuthComponent extends Object {
|
||||||
* @var array
|
* @var array
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
var $components = array('Session', 'Acl', 'RequestHandler');
|
var $components = array('Session', 'RequestHandler');
|
||||||
|
/**
|
||||||
|
* The name of the component to use for Authorization or set this to 'controller'
|
||||||
|
* and the Controller::isAuthorized() method will be used
|
||||||
|
* The component used for Authorization should have a "check" method
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $authorize = 'Acl';
|
||||||
/**
|
/**
|
||||||
* The name of an optional view element to render when an Ajax request is made
|
* The name of an optional view element to render when an Ajax request is made
|
||||||
* with an invalid or expired session
|
* with an invalid or expired session
|
||||||
|
@ -62,13 +71,6 @@ class AuthComponent extends Object {
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
var $userModel = 'User';
|
var $userModel = 'User';
|
||||||
/**
|
|
||||||
* The name of the model that represents objects which users can be authorized for against.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
* @access public
|
|
||||||
*/
|
|
||||||
var $objectModel = null;
|
|
||||||
/**
|
/**
|
||||||
* Additional query conditions to use when looking up and authenticating users,
|
* Additional query conditions to use when looking up and authenticating users,
|
||||||
* i.e. array('User.is_active' => 1).
|
* i.e. array('User.is_active' => 1).
|
||||||
|
@ -133,14 +135,13 @@ class AuthComponent extends Object {
|
||||||
*/
|
*/
|
||||||
var $logoutRedirect = null;
|
var $logoutRedirect = null;
|
||||||
/**
|
/**
|
||||||
* The type of automatic ACL validation to perform, where 'actions' validates
|
* The name of the model that represents objects which users can be authorized for against.
|
||||||
* the controller action of the current request, 'objects' validates against
|
|
||||||
* model objects accessed, and null prevents automatic validation.
|
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
var $validate = 'actions';
|
var $objectModel = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error to display when user login fails. For security purposes, only one error is used for all
|
* Error to display when user login fails. For security purposes, only one error is used for all
|
||||||
* login failures, so as not to expose information on why the login failed.
|
* login failures, so as not to expose information on why the login failed.
|
||||||
|
@ -249,9 +250,11 @@ class AuthComponent extends Object {
|
||||||
if (low($controller->name) == 'app' || (low($controller->name) == 'tests' && Configure::read() > 0)) {
|
if (low($controller->name) == 'app' || (low($controller->name) == 'tests' && Configure::read() > 0)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$this->__setDefaults()) {
|
if (!$this->__setDefaults()) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data = $controller->data = $this->hashPasswords($controller->data);
|
$this->data = $controller->data = $this->hashPasswords($controller->data);
|
||||||
|
|
||||||
if ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions)) {
|
if ($this->allowedActions == array('*') || in_array($controller->action, $this->allowedActions)) {
|
||||||
|
@ -269,35 +272,65 @@ class AuthComponent extends Object {
|
||||||
if (!$this->Session->check('Auth.redirect')) {
|
if (!$this->Session->check('Auth.redirect')) {
|
||||||
$this->Session->write('Auth.redirect', $controller->referer());
|
$this->Session->write('Auth.redirect', $controller->referer());
|
||||||
}
|
}
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
$data = array(
|
$data = array(
|
||||||
$this->userModel . '.' . $this->fields['username'] => '= ' . $controller->data[$this->userModel][$this->fields['username']],
|
$this->userModel . '.' . $this->fields['username'] => '= ' . $controller->data[$this->userModel][$this->fields['username']],
|
||||||
$this->userModel . '.' . $this->fields['password'] => '= ' . $controller->data[$this->userModel][$this->fields['password']]
|
$this->userModel . '.' . $this->fields['password'] => '= ' . $controller->data[$this->userModel][$this->fields['password']]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->login($data) && $this->autoRedirect) {
|
if ($this->login($data) && $this->autoRedirect) {
|
||||||
$controller->redirect($this->redirect(), null, true);
|
$controller->redirect($this->redirect(), null, true);
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
$this->Session->setFlash($this->loginError, 'default', array(), 'Auth.login');
|
$this->Session->setFlash($this->loginError, 'default', array(), 'Auth.login');
|
||||||
unset($controller->data[$this->userModel][$this->fields['password']]);
|
unset($controller->data[$this->userModel][$this->fields['password']]);
|
||||||
}
|
}
|
||||||
return;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (!$this->user()) {
|
if (!$this->user()) {
|
||||||
if (!$this->RequestHandler->isAjax()) {
|
if (!$this->RequestHandler->isAjax()) {
|
||||||
$this->Session->write('Auth.redirect', $url);
|
$this->Session->write('Auth.redirect', $url);
|
||||||
$controller->redirect($this->_normalizeURL($this->loginAction));
|
$controller->redirect($this->_normalizeURL($this->loginAction), null, true);
|
||||||
|
return false;
|
||||||
} elseif (!empty($this->ajaxLogin)) {
|
} elseif (!empty($this->ajaxLogin)) {
|
||||||
$controller->viewPath = 'elements';
|
$controller->viewPath = 'elements';
|
||||||
$controller->render($this->ajaxLogin, 'ajax');
|
$controller->render($this->ajaxLogin, 'ajax');
|
||||||
}
|
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$this->isAuthorized($controller)) {
|
}
|
||||||
|
extract($this->__authType($this->authorize));
|
||||||
|
if($type !== 'controller') {
|
||||||
|
if(isset($controller->Acl)) {
|
||||||
|
$this->Acl =& $controller->Acl;
|
||||||
|
if($type == 'model') {
|
||||||
|
if(!isset($object)) {
|
||||||
|
if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) {
|
||||||
|
$object = $controller->modelClass;
|
||||||
|
} elseif (!empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]})) {
|
||||||
|
$object = $controller->uses[0];
|
||||||
|
} else {
|
||||||
|
$object = $this->objectModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($this->isAuthorized($type, null, $object)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
trigger_error(__('Could not find AclComponent. Please include Acl in Controller::$components.', true), E_USER_WARNING);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (method_exists($controller, 'isAuthorized')) {
|
||||||
|
if($controller->isAuthorized()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
$this->Session->setFlash($this->authError);
|
$this->Session->setFlash($this->authError);
|
||||||
$controller->redirect($controller->referer(), null, true);
|
$controller->redirect($controller->referer(), null, true);
|
||||||
}
|
return false;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Attempts to introspect the correct values for object properties including
|
* Attempts to introspect the correct values for object properties including
|
||||||
|
@ -313,7 +346,7 @@ class AuthComponent extends Object {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (empty($this->loginAction)) {
|
if (empty($this->loginAction)) {
|
||||||
$this->loginAction = Inflector::underscore(Inflector::pluralize($this->userModel)) . '/login';
|
$this->loginAction = Router::url(array('controller'=> Inflector::underscore(Inflector::pluralize($this->userModel)), 'action'=>'login'));
|
||||||
}
|
}
|
||||||
if (empty($this->sessionKey)) {
|
if (empty($this->sessionKey)) {
|
||||||
$this->sessionKey = 'Auth.' . $this->userModel;
|
$this->sessionKey = 'Auth.' . $this->userModel;
|
||||||
|
@ -333,27 +366,17 @@ class AuthComponent extends Object {
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return boolean True if $user is authorized, otherwise false
|
* @return boolean True if $user is authorized, otherwise false
|
||||||
*/
|
*/
|
||||||
function isAuthorized(&$controller, $type = null, $user = null) {
|
function isAuthorized($type = null, $user = null, $object = null) {
|
||||||
if (empty($user) && !$this->user()) {
|
if (empty($user) && !$this->user()) {
|
||||||
return false;
|
return false;
|
||||||
} elseif (empty($user)) {
|
} elseif (empty($user)) {
|
||||||
$user = $this->user();
|
$user = $this->user();
|
||||||
}
|
}
|
||||||
|
|
||||||
extract($this->__authType($type));
|
extract($this->__authType($type));
|
||||||
|
|
||||||
if (($type == 'objects' || $type == 'association')) {
|
if(!$object) {
|
||||||
if (!isset($this->params['id'])) {
|
$object = $this->objectModel;
|
||||||
return;
|
|
||||||
} elseif (empty($this->objectModel)) {
|
|
||||||
if (isset($controller->{$controller->modelClass}) && is_object($controller->{$controller->modelClass})) {
|
|
||||||
$this->objectModel = $controller->modelClass;
|
|
||||||
} elseif (!empty($controller->uses) && isset($controller->{$controller->uses[0]}) && is_object($controller->{$controller->uses[0]})) {
|
|
||||||
$this->objectModel = $controller->uses[0];
|
|
||||||
} else {
|
|
||||||
trigger_error(__('Could not find $objectModel. Please set AuthComponent::$objectModel in beforeFilter().', true), E_USER_WARNING);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$valid = false;
|
$valid = false;
|
||||||
|
@ -361,13 +384,7 @@ class AuthComponent extends Object {
|
||||||
case 'actions':
|
case 'actions':
|
||||||
$valid = $this->Acl->check($user, $this->action());
|
$valid = $this->Acl->check($user, $this->action());
|
||||||
break;
|
break;
|
||||||
case 'objects':
|
case 'crud':
|
||||||
|
|
||||||
break;
|
|
||||||
case 'association':
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'controller':
|
|
||||||
$this->mapActions();
|
$this->mapActions();
|
||||||
if (!isset($this->actionMap[$this->params['action']])) {
|
if (!isset($this->actionMap[$this->params['action']])) {
|
||||||
trigger_error('Auth::startup() - Attempted access of un-mapped action "' . $this->params['action'] . '" in controller "' . $this->params['controller'] . '"', E_USER_WARNING);
|
trigger_error('Auth::startup() - Attempted access of un-mapped action "' . $this->params['action'] . '" in controller "' . $this->params['controller'] . '"', E_USER_WARNING);
|
||||||
|
@ -375,12 +392,26 @@ class AuthComponent extends Object {
|
||||||
$valid = $this->Acl->check($user, $this->action(':controller'), $this->actionMap[$this->params['action']]);
|
$valid = $this->Acl->check($user, $this->action(':controller'), $this->actionMap[$this->params['action']]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'model':
|
||||||
|
if(empty($object)) {
|
||||||
|
trigger_error(__('Could not find $this->objectModel. Please set AuthComponent::$objectModel in beforeFilter().', true), E_USER_WARNING);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$model = $this->getModel($object);
|
||||||
|
if (method_exists($model, 'isAuthorized')) {
|
||||||
|
if($model->isAuthorized()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
trigger_error(__($object.'::isAuthorized() is not defined.', true), E_USER_WARNING);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case null:
|
case null:
|
||||||
case false:
|
case false:
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
trigger_error(__('Auth::startup() - $validate is set to an incorrect value. Allowed settings are: "controller", "actions", "objects", "association" or null.', true), E_USER_WARNING);
|
trigger_error(__('Auth::startup() - $authorize is set to an incorrect value. Allowed settings are: "actions", "crud", "model" or null.', true), E_USER_WARNING);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return $valid;
|
return $valid;
|
||||||
|
@ -393,21 +424,19 @@ class AuthComponent extends Object {
|
||||||
*/
|
*/
|
||||||
function __authType($auth = null) {
|
function __authType($auth = null) {
|
||||||
if (empty($auth)) {
|
if (empty($auth)) {
|
||||||
$auth = $this->validate;
|
$auth = $this->authorize;
|
||||||
}
|
}
|
||||||
$object = $assoc = null;
|
$object = null;
|
||||||
|
|
||||||
if (is_array($auth)) {
|
if (is_array($auth)) {
|
||||||
$type = key($auth);
|
$type = key($auth);
|
||||||
$object = $auth[$type];
|
$object = $auth[$type];
|
||||||
|
|
||||||
if (isset($auth[0])) {
|
if (isset($auth[0])) {
|
||||||
$assoc = $auth[0];
|
$assoc = $auth[0];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$type = $auth;
|
$type = $auth;
|
||||||
}
|
}
|
||||||
return compact('type', 'object', 'assoc');
|
return compact('type', 'object');
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Takes a list of actions in the current controller for which authentication is not required, or
|
* Takes a list of actions in the current controller for which authentication is not required, or
|
||||||
|
@ -518,6 +547,7 @@ class AuthComponent extends Object {
|
||||||
if (!$this->Session->check($this->sessionKey)) {
|
if (!$this->Session->check($this->sessionKey)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key == null) {
|
if ($key == null) {
|
||||||
return array($this->userModel => $this->Session->read($this->sessionKey));
|
return array($this->userModel => $this->Session->read($this->sessionKey));
|
||||||
} else {
|
} else {
|
||||||
|
@ -566,7 +596,7 @@ class AuthComponent extends Object {
|
||||||
*/
|
*/
|
||||||
function validate($object, $user = null, $action = null) {
|
function validate($object, $user = null, $action = null) {
|
||||||
if (empty($user)) {
|
if (empty($user)) {
|
||||||
$this->getUserModel();
|
$this->getModel();
|
||||||
$user = $this->user();
|
$user = $this->user();
|
||||||
}
|
}
|
||||||
if (empty($user)) {
|
if (empty($user)) {
|
||||||
|
@ -597,32 +627,34 @@ class AuthComponent extends Object {
|
||||||
* @access public
|
* @access public
|
||||||
* @return object A reference to a model object.
|
* @return object A reference to a model object.
|
||||||
*/
|
*/
|
||||||
function &getUserModel() {
|
function &getModel($name = null) {
|
||||||
$user = null;
|
$model = null;
|
||||||
|
if(!$name) {
|
||||||
if (!ClassRegistry::isKeySet($this->userModel)) {
|
$name = $this->userModel;
|
||||||
if (!loadModel(Inflector::underscore($this->userModel))) {
|
}
|
||||||
trigger_error(__('Auth::getUserModel() - $userModel is not set or could not be found', true) . $this->userModel, E_USER_WARNING);
|
if (!ClassRegistry::isKeySet($name)) {
|
||||||
|
if (!loadModel(Inflector::underscore($name))) {
|
||||||
|
trigger_error(__('Auth::getModel() - $userModel is not set or could not be found', true) . $name, E_USER_WARNING);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
$model = $this->userModel;
|
$model = new $name();
|
||||||
$user = new $model();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($user)) {
|
if (empty($model)) {
|
||||||
if (PHP5) {
|
if (PHP5) {
|
||||||
$user = ClassRegistry::getObject($this->userModel);
|
$model = ClassRegistry::getObject($name);
|
||||||
} else {
|
} else {
|
||||||
$user =& ClassRegistry::getObject($this->userModel);
|
$model =& ClassRegistry::getObject($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($user)) {
|
if (empty($model)) {
|
||||||
trigger_error(__('Auth::getUserModel() - $userModel is not set or could not be found', true) . $this->userModel, E_USER_WARNING);
|
trigger_error(__('Auth::getModel() - $name is not set or could not be found', true) . $name, E_USER_WARNING);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $user;
|
|
||||||
|
return $model;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Identifies a user based on specific criteria.
|
* Identifies a user based on specific criteria.
|
||||||
|
@ -667,7 +699,8 @@ class AuthComponent extends Object {
|
||||||
$this->fields['password'] => $user[$this->userModel . '.' . $this->fields['password']]
|
$this->fields['password'] => $user[$this->userModel . '.' . $this->fields['password']]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$model =& $this->getUserModel();
|
$model =& $this->getModel();
|
||||||
|
|
||||||
$data = $model->find(am($find, $this->userScope), null, null, -1);
|
$data = $model->find(am($find, $this->userScope), null, null, -1);
|
||||||
|
|
||||||
if (empty($data) || empty($data[$this->userModel])) {
|
if (empty($data) || empty($data[$this->userModel])) {
|
||||||
|
@ -675,7 +708,7 @@ class AuthComponent extends Object {
|
||||||
}
|
}
|
||||||
} elseif (is_numeric($user)) {
|
} elseif (is_numeric($user)) {
|
||||||
// Assume it's a user's ID
|
// Assume it's a user's ID
|
||||||
$model =& $this->getUserModel();
|
$model =& $this->getModel();
|
||||||
$data = $model->find(am(array($model->escapeField() => $user), $this->userScope));
|
$data = $model->find(am(array($model->escapeField() => $user), $this->userScope));
|
||||||
|
|
||||||
if (empty($data) || empty($data[$this->userModel])) {
|
if (empty($data) || empty($data[$this->userModel])) {
|
||||||
|
@ -701,7 +734,7 @@ class AuthComponent extends Object {
|
||||||
function hashPasswords($data) {
|
function hashPasswords($data) {
|
||||||
if (isset($data[$this->userModel])) {
|
if (isset($data[$this->userModel])) {
|
||||||
if (!empty($data[$this->userModel][$this->fields['username']]) && !empty($data[$this->userModel][$this->fields['password']])) {
|
if (!empty($data[$this->userModel][$this->fields['username']]) && !empty($data[$this->userModel][$this->fields['password']])) {
|
||||||
$model =& $this->getUserModel();
|
$model =& $this->getModel();
|
||||||
$data[$this->userModel][$this->fields['password']] = $this->password($data[$this->userModel][$this->fields['password']]);
|
$data[$this->userModel][$this->fields['password']] = $this->password($data[$this->userModel][$this->fields['password']]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -735,9 +768,13 @@ class AuthComponent extends Object {
|
||||||
function _normalizeURL($url = '/') {
|
function _normalizeURL($url = '/') {
|
||||||
if (is_array($url)) {
|
if (is_array($url)) {
|
||||||
$url = Router::url($url);
|
$url = Router::url($url);
|
||||||
|
}
|
||||||
|
|
||||||
$paths = Router::getPaths();
|
$paths = Router::getPaths();
|
||||||
|
if(stristr($url, $paths['base'])) {
|
||||||
$url = r($paths['base'], '', $url);
|
$url = r($paths['base'], '', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = '/' . $url . '/';
|
$url = '/' . $url . '/';
|
||||||
|
|
||||||
while (strpos($url, '//') !== false) {
|
while (strpos($url, '//') !== false) {
|
||||||
|
|
|
@ -563,6 +563,15 @@ class Controller extends Object {
|
||||||
unset($args[0]);
|
unset($args[0]);
|
||||||
call_user_func_array(array(&$this, $action), $args);
|
call_user_func_array(array(&$this, $action), $args);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* contoroller callback to tie into Auth component.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function isAuthorized() {
|
||||||
|
trigger_error(__($this->name.'::isAuthorized() is not defined.', true), E_USER_WARNING);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Returns number of errors in a submitted FORM.
|
* Returns number of errors in a submitted FORM.
|
||||||
*
|
*
|
||||||
|
|
|
@ -36,7 +36,7 @@ class AuthUser extends CakeTestModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
function bindNode($object) {
|
function bindNode($object) {
|
||||||
return 'Roles/User';
|
return 'Roles/Admin';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,13 +47,11 @@ class AuthTestController extends Controller {
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
$this->params = Router::parse('/auth_test');
|
$this->params = Router::parse('/auth_test');
|
||||||
Router::setRequestInfo(array($this->params, array('base' => '/', 'here' => '/', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array(), 'webservices' => null)));
|
Router::setRequestInfo(array($this->params, array('base' => null, 'here' => '/', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array(), 'webservices' => null)));
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
function beforeFilter() {
|
function beforeFilter() {
|
||||||
$this->Auth->userModel = 'AuthUser';
|
|
||||||
$this->Auth->logoutAction = 'login';
|
|
||||||
$this->Auth->allow('logout');
|
$this->Auth->allow('logout');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +67,7 @@ class AuthTestController extends Controller {
|
||||||
|
|
||||||
function redirect() {
|
function redirect() {
|
||||||
return false;
|
return false;
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
function isAuthorized() {
|
function isAuthorized() {
|
||||||
|
@ -90,43 +89,95 @@ class AuthTest extends CakeTestCase {
|
||||||
function testIt(){
|
function testIt(){
|
||||||
$this->assertTrue(true);
|
$this->assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testNoAuth() {
|
||||||
|
$this->assertFalse($this->Controller->Auth->isAuthorized());
|
||||||
|
}
|
||||||
|
|
||||||
|
function testLogin() {
|
||||||
|
$this->AuthUser =& new AuthUser();
|
||||||
|
$user['id'] = 1;
|
||||||
|
$user['username'] = 'mariano';
|
||||||
|
$user['password'] = Security::hash(CAKE_SESSION_STRING . 'cake');
|
||||||
|
$this->AuthUser->save($user, false);
|
||||||
|
|
||||||
|
$authUser = $this->AuthUser->find();
|
||||||
|
|
||||||
|
$this->Controller->data['AuthUser']['username'] = $authUser['AuthUser']['username'];
|
||||||
|
$this->Controller->data['AuthUser']['password'] = 'cake';
|
||||||
|
|
||||||
|
$this->Controller->params['url']['url'] = 'auth_test/login';
|
||||||
|
|
||||||
|
$this->Controller->Auth->initialize($this->Controller);
|
||||||
|
|
||||||
|
$this->Controller->Auth->loginAction = 'auth_test/login';
|
||||||
|
$this->Controller->Auth->userModel = 'AuthUser';
|
||||||
|
|
||||||
|
$this->Controller->Auth->startup($this->Controller);
|
||||||
|
$user = $this->Controller->Auth->user();
|
||||||
|
$this->assertEqual($user, array('AuthUser'=>array('id'=>1, 'username'=>'mariano', 'created'=> '2007-03-17 01:16:23', 'updated'=> date('Y-m-d H:i:s'))));
|
||||||
|
$this->Controller->Session->del('Auth');
|
||||||
|
}
|
||||||
|
|
||||||
function testAuthController(){
|
function testAuthController(){
|
||||||
$this->AuthUser =& new AuthUser();
|
$this->AuthUser =& new AuthUser();
|
||||||
$user = $this->AuthUser->find();
|
$user = $this->AuthUser->find();
|
||||||
$this->Controller->Session->write('Auth', $user);
|
$this->Controller->Session->write('Auth', $user);
|
||||||
|
$this->Controller->Auth->userModel = 'AuthUser';
|
||||||
$this->Controller->Auth->authorize = 'controller';
|
$this->Controller->Auth->authorize = 'controller';
|
||||||
$this->Controller->Auth->startup($this->Controller);
|
$result = $this->Controller->Auth->startup($this->Controller);
|
||||||
$this->assertTrue(true);
|
$this->assertTrue($result);
|
||||||
|
$this->Controller->Session->del('Auth');
|
||||||
}
|
}
|
||||||
function testNoAuth() {
|
|
||||||
$this->assertFalse($this->Controller->Auth->isAuthorized($this->Controller));
|
function testAuthWithDB_ACL() {
|
||||||
}
|
|
||||||
/*
|
|
||||||
function testUserData() {
|
|
||||||
$this->AuthUser =& new AuthUser();
|
$this->AuthUser =& new AuthUser();
|
||||||
foreach ($this->AuthUser->findAll() as $key => $result) {
|
$user = $this->AuthUser->find();
|
||||||
$result['User']['password'] = Security::hash(CAKE_SESSION_STRING . $result['User']['password']);
|
$this->Controller->Session->write('Auth', $user);
|
||||||
$this->AuthUser->save($result, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
$authUser = $this->AuthUser->read();
|
$this->Controller->params['controller'] = 'auth_test';
|
||||||
$this->Controller->data['User']['username'] = $authUser['User']['username'];
|
$this->Controller->params['action'] = 'add';
|
||||||
$this->Controller->data['User']['password'] = $authUser['User']['password'];
|
|
||||||
|
$this->Controller->Acl->startup($this->Controller);
|
||||||
|
|
||||||
|
$this->Controller->Acl->Aro->id = null;
|
||||||
|
$this->Controller->Acl->Aro->create(array('alias'=>'Roles'));
|
||||||
|
$this->Controller->Acl->Aro->save();
|
||||||
|
$this->Controller->Acl->Aro->create(array('alias'=>'Admin'));
|
||||||
|
$this->Controller->Acl->Aro->save();
|
||||||
|
$this->Controller->Acl->Aro->create(array('model'=>'AuthUser', 'foreign_key'=>'1', 'alias'=> 'mariano'));
|
||||||
|
$this->Controller->Acl->Aro->save();
|
||||||
|
$this->Controller->Acl->Aro->setParent(1, 2);
|
||||||
|
$this->Controller->Acl->Aro->setParent(2, 3);
|
||||||
|
|
||||||
|
$this->Controller->Acl->Aco->create(array('alias'=>'Root'));
|
||||||
|
$this->Controller->Acl->Aco->save();
|
||||||
|
$this->Controller->Acl->Aco->create(array('alias'=>'AuthTest'));
|
||||||
|
$this->Controller->Acl->Aco->save();
|
||||||
|
$this->Controller->Acl->Aco->setParent(1, 2);
|
||||||
|
|
||||||
|
$this->Controller->Acl->allow('Roles/Admin', 'Root');
|
||||||
|
$this->Controller->Acl->allow('Roles/Admin', 'Root/AuthTest');
|
||||||
|
|
||||||
|
$this->Controller->Auth->initialize($this->Controller);
|
||||||
|
|
||||||
|
$this->Controller->Auth->userModel = 'AuthUser';
|
||||||
|
$this->Controller->Auth->authorize = 'crud';
|
||||||
|
$this->Controller->Auth->actionPath = 'Root/';
|
||||||
|
|
||||||
$this->Controller->Auth->authorize = 'Acl';
|
|
||||||
$this->Controller->Auth->startup($this->Controller);
|
$this->Controller->Auth->startup($this->Controller);
|
||||||
|
|
||||||
$this->Controller->Auth->params['controller'] = 'auth_test';
|
|
||||||
$this->Controller->Auth->params['action'] = 'add';
|
$this->assertTrue($this->Controller->Auth->isAuthorized());
|
||||||
pr($this->Controller->Auth);
|
|
||||||
$this->Controller->Auth->Acl->create(1, null, 'chartjes');
|
|
||||||
$this->Controller->Auth->Acl->create(0, null, 'Users');
|
$this->Controller->Session->del('Auth');
|
||||||
$this->Controller->Auth->Acl->setParent('Users', 1);
|
$this->Controller->Acl->Aro->execute('truncate users;');
|
||||||
$this->Controller->Auth->Acl->create(0, null, '/Home/home');
|
$this->Controller->Acl->Aro->execute('truncate aros;');
|
||||||
$this->Controller->Auth->Acl->allow('Users', 'Home/home');
|
$this->Controller->Acl->Aro->execute('truncate acos;');
|
||||||
$this->assertTrue($this->Controller->Auth->isAuthorized($this->Controller, 'controller'));
|
$this->Controller->Acl->Aro->execute('truncate aros_acos;');
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
function tearDown() {
|
function tearDown() {
|
||||||
unset($this->Controller, $this->AuthUser);
|
unset($this->Controller, $this->AuthUser);
|
||||||
}
|
}
|
||||||
|
|
12
cake/tests/fixtures/auth_user_fixture.php
vendored
12
cake/tests/fixtures/auth_user_fixture.php
vendored
|
@ -36,16 +36,18 @@ class AuthUserFixture extends CakeTestFixture {
|
||||||
var $name = 'AuthUser';
|
var $name = 'AuthUser';
|
||||||
var $fields = array(
|
var $fields = array(
|
||||||
'id' => array('type' => 'integer', 'key' => 'primary'),
|
'id' => array('type' => 'integer', 'key' => 'primary'),
|
||||||
'user' => array('type' => 'string', 'null' => false),
|
'username' => array('type' => 'string', 'null' => false),
|
||||||
'password' => array('type' => 'string', 'null' => false),
|
'password' => array('type' => 'string', 'null' => false),
|
||||||
'created' => 'datetime',
|
'created' => 'datetime',
|
||||||
'updated' => 'datetime'
|
'updated' => 'datetime'
|
||||||
);
|
);
|
||||||
var $records = array(
|
var $records = array(
|
||||||
array('id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
|
array('id' => 1, 'username' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
|
||||||
array('id' => 2, 'user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
|
array('id' => 2, 'username' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:18:23', 'updated' => '2007-03-17 01:20:31'),
|
||||||
array('id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
|
array('id' => 3, 'username' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'),
|
||||||
array('id' => 4, 'user' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'),
|
array('id' => 4, 'username' => 'garrett', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'),
|
||||||
|
array('id' => 5, 'username' => 'chartjes', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:22:23', 'updated' => '2007-03-17 01:24:31'),
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue