2008-05-30 11:40:08 +00:00
< ? php
/**
* Authentication component
*
* Manages user logins and permissions .
*
* PHP versions 4 and 5
*
2008-10-30 17:30:26 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// www . cakephp . org )
2009-09-14 03:42:25 +00:00
* Copyright 2005 - 2009 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2009-09-14 03:42:25 +00:00
* @ copyright Copyright 2005 - 2009 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
2008-10-30 17:30:26 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . libs . controller . components
* @ since CakePHP ( tm ) v 0.10 . 0.1076
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2008-05-30 11:40:08 +00:00
*/
2009-09-14 03:42:25 +00:00
App :: import ( 'Core' , array ( 'Router' , 'Security' ), false );
2008-05-30 11:40:08 +00:00
/**
* Authentication control component class
*
* Binds access control with user authentication and session management .
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . cake . libs . controller . components
2008-05-30 11:40:08 +00:00
*/
class AuthComponent extends Object {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Maintains current user login state .
*
* @ var boolean
* @ access private
*/
var $_loggedIn = false ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Other components utilized by AuthComponent
*
* @ var array
* @ access public
*/
var $components = array ( 'Session' , 'RequestHandler' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* A reference to the object used for authentication
*
* @ var object
* @ access public
*/
var $authenticate = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The name of the component to use for Authorization or set this to
* 'controller' will validate against Controller :: isAuthorized ()
* 'actions' will validate Controller :: action against an AclComponent :: check ()
* 'crud' will validate mapActions against an AclComponent :: check ()
2008-12-18 14:31:53 +00:00
* array ( 'model' => 'name' ); will validate mapActions against model $name :: isAuthorized ( user , controller , mapAction )
2008-05-30 11:40:08 +00:00
* 'object' will validate Controller :: action against object :: isAuthorized ( user , controller , action )
*
* @ var mixed
* @ access public
*/
var $authorize = false ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The name of an optional view element to render when an Ajax request is made
* with an invalid or expired session
*
* @ var string
* @ access public
*/
var $ajaxLogin = null ;
2009-07-24 19:18:37 +00:00
2009-08-13 15:18:46 +00:00
/**
* The name of the layout element used on Session :: setFlash
*
* @ var string
* @ access public
*/
var $flashLayout = 'default' ;
2008-05-30 11:40:08 +00:00
/**
* The name of the model that represents users which will be authenticated . Defaults to 'User' .
*
* @ var string
* @ access public
*/
var $userModel = 'User' ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Additional query conditions to use when looking up and authenticating users ,
* i . e . array ( 'User.is_active' => 1 ) .
*
* @ var array
* @ access public
*/
var $userScope = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Allows you to specify non - default login name and password fields used in
* $userModel , i . e . array ( 'username' => 'login_name' , 'password' => 'passwd' ) .
*
* @ var array
* @ access public
*/
var $fields = array ( 'username' => 'username' , 'password' => 'password' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The session key name where the record of the current user is stored . If
* unspecified , it will be " Auth. { $userModel name } " .
*
* @ var string
* @ access public
*/
var $sessionKey = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* If using action - based access control , this defines how the paths to action
* ACO nodes is computed . If , for example , all controller nodes are nested
* under an ACO node named 'Controllers' , $actionPath should be set to
* " Controllers/ " .
*
* @ var string
* @ access public
*/
var $actionPath = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* A URL ( defined as a string or array ) to the controller action that handles
* logins .
*
* @ var mixed
* @ access public
*/
var $loginAction = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Normally , if a user is redirected to the $loginAction page , the location they
* were redirected from will be stored in the session so that they can be
* redirected back after a successful login . If this session value is not
* set , the user will be redirected to the page specified in $loginRedirect .
*
* @ var mixed
* @ access public
*/
var $loginRedirect = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The the default action to redirect to after the user is logged out . While AuthComponent does
* not handle post - logout redirection , a redirect URL will be returned from AuthComponent :: logout () .
* Defaults to AuthComponent :: $loginAction .
*
* @ var mixed
* @ access public
* @ see AuthComponent :: $loginAction
* @ see AuthComponent :: logout ()
*/
var $logoutRedirect = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The name of model or model object , or any other object has an isAuthorized method .
*
* @ var string
* @ access public
*/
var $object = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* 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 .
*
* @ var string
* @ access public
*/
var $loginError = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Error to display when user attempts to access an object or action to which they do not have
* acccess .
*
* @ var string
* @ access public
*/
var $authError = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Determines whether AuthComponent will automatically redirect and exit if login is successful .
*
* @ var boolean
* @ access public
*/
var $autoRedirect = true ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Controller actions for which user validation is not required .
*
* @ var array
* @ access public
* @ see AuthComponent :: allow ()
*/
var $allowedActions = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Maps actions to CRUD operations . Used for controller - based validation ( $validate = 'controller' ) .
*
* @ var array
* @ access public
* @ see AuthComponent :: mapActions ()
*/
var $actionMap = array (
'index' => 'read' ,
'add' => 'create' ,
'edit' => 'update' ,
'view' => 'read' ,
'remove' => 'delete'
);
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Form data from Controller :: $data
*
* @ var array
* @ access public
*/
var $data = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Parameter data from Controller :: $params
*
* @ var array
* @ access public
*/
var $params = array ();
2009-07-24 19:18:37 +00:00
2008-10-15 02:52:19 +00:00
/**
* Method list for bound controller
*
* @ var array
* @ access protected
*/
var $_methods = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Initializes AuthComponent for use in the controller
*
* @ param object $controller A reference to the instantiating controller object
2008-09-25 16:49:56 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access public
*/
function initialize ( & $controller ) {
$this -> params = $controller -> params ;
$crud = array ( 'create' , 'read' , 'update' , 'delete' );
$this -> actionMap = array_merge ( $this -> actionMap , array_combine ( $crud , $crud ));
2008-11-11 16:34:05 +00:00
$this -> _methods = $controller -> methods ;
2008-05-30 11:40:08 +00:00
$admin = Configure :: read ( 'Routing.admin' );
if ( ! empty ( $admin )) {
$this -> actionMap = array_merge ( $this -> actionMap , array (
$admin . '_index' => 'read' ,
$admin . '_add' => 'create' ,
$admin . '_edit' => 'update' ,
$admin . '_view' => 'read' ,
$admin . '_remove' => 'delete' ,
$admin . '_create' => 'create' ,
$admin . '_read' => 'read' ,
$admin . '_update' => 'update' ,
$admin . '_delete' => 'delete'
));
}
if ( Configure :: read () > 0 ) {
App :: import ( 'Debugger' );
Debugger :: checkSessionKey ();
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Main execution method . Handles redirecting of invalid users , and processing
* of login form data .
*
* @ param object $controller A reference to the instantiating controller object
2008-09-25 16:49:56 +00:00
* @ return boolean
2008-05-30 11:40:08 +00:00
* @ access public
*/
function startup ( & $controller ) {
2009-02-06 23:23:12 +00:00
$methods = array_flip ( $controller -> methods );
2009-07-03 00:20:54 +00:00
$action = strtolower ( $controller -> params [ 'action' ]);
$allowedActions = array_map ( 'strtolower' , $this -> allowedActions );
2009-06-30 00:25:09 +00:00
2008-11-11 16:34:05 +00:00
$isErrorOrTests = (
strtolower ( $controller -> name ) == 'cakeerror' ||
2009-02-06 23:23:12 +00:00
( strtolower ( $controller -> name ) == 'tests' && Configure :: read () > 0 )
2008-10-15 02:52:19 +00:00
);
2008-11-11 16:34:05 +00:00
if ( $isErrorOrTests ) {
2008-10-15 02:52:19 +00:00
return true ;
2008-05-30 11:40:08 +00:00
}
2009-02-06 23:23:12 +00:00
$isMissingAction = (
$controller -> scaffold === false &&
2009-07-03 00:20:54 +00:00
! isset ( $methods [ $action ])
2009-02-06 23:23:12 +00:00
);
if ( $isMissingAction ) {
return true ;
}
2008-05-30 11:40:08 +00:00
if ( ! $this -> __setDefaults ()) {
return false ;
}
$this -> data = $controller -> data = $this -> hashPasswords ( $controller -> data );
$url = '' ;
2008-10-15 02:52:19 +00:00
2009-01-06 03:20:11 +00:00
if ( isset ( $controller -> params [ 'url' ][ 'url' ])) {
2008-05-30 11:40:08 +00:00
$url = $controller -> params [ 'url' ][ 'url' ];
2008-07-31 19:00:12 +00:00
}
2008-05-30 11:40:08 +00:00
$url = Router :: normalize ( $url );
$loginAction = Router :: normalize ( $this -> loginAction );
2009-02-06 23:23:12 +00:00
2008-09-20 20:18:16 +00:00
$isAllowed = (
$this -> allowedActions == array ( '*' ) ||
2009-07-03 00:20:54 +00:00
in_array ( $action , $allowedActions )
2008-09-20 20:18:16 +00:00
);
2008-05-31 12:36:38 +00:00
2008-09-20 20:18:16 +00:00
if ( $loginAction != $url && $isAllowed ) {
2008-10-15 02:52:19 +00:00
return true ;
2008-05-30 11:40:08 +00:00
}
if ( $loginAction == $url ) {
2009-08-13 14:39:59 +00:00
$model =& $this -> getModel ();
if ( empty ( $controller -> data ) || ! isset ( $controller -> data [ $model -> alias ])) {
2008-05-30 11:40:08 +00:00
if ( ! $this -> Session -> check ( 'Auth.redirect' ) && env ( 'HTTP_REFERER' )) {
2008-11-14 09:07:22 +00:00
$this -> Session -> write ( 'Auth.redirect' , $controller -> referer ( null , true ));
2008-05-30 11:40:08 +00:00
}
return false ;
}
2009-08-13 14:39:59 +00:00
$isValid = ! empty ( $controller -> data [ $model -> alias ][ $this -> fields [ 'username' ]]) &&
! empty ( $controller -> data [ $model -> alias ][ $this -> fields [ 'password' ]]);
2008-05-30 11:40:08 +00:00
2009-01-14 05:21:26 +00:00
if ( $isValid ) {
2009-08-13 14:39:59 +00:00
$username = $controller -> data [ $model -> alias ][ $this -> fields [ 'username' ]];
$password = $controller -> data [ $model -> alias ][ $this -> fields [ 'password' ]];
2009-01-14 05:21:26 +00:00
$data = array (
2009-08-13 14:39:59 +00:00
$model -> alias . '.' . $this -> fields [ 'username' ] => $username ,
$model -> alias . '.' . $this -> fields [ 'password' ] => $password
2009-01-14 05:21:26 +00:00
);
if ( $this -> login ( $data )) {
if ( $this -> autoRedirect ) {
$controller -> redirect ( $this -> redirect (), null , true );
}
return true ;
2008-05-30 11:40:08 +00:00
}
}
2009-01-14 05:21:26 +00:00
2009-08-13 15:18:46 +00:00
$this -> Session -> setFlash ( $this -> loginError , $this -> flashLayout , array (), 'auth' );
2009-08-13 14:39:59 +00:00
$controller -> data [ $model -> alias ][ $this -> fields [ 'password' ]] = null ;
2008-05-30 11:40:08 +00:00
return false ;
} else {
if ( ! $this -> user ()) {
if ( ! $this -> RequestHandler -> isAjax ()) {
2009-08-13 15:18:46 +00:00
$this -> Session -> setFlash ( $this -> authError , $this -> flashLayout , array (), 'auth' );
2009-04-25 02:16:05 +00:00
if ( ! empty ( $controller -> params [ 'url' ]) && count ( $controller -> params [ 'url' ]) >= 2 ) {
$query = $controller -> params [ 'url' ];
2009-06-01 00:01:58 +00:00
unset ( $query [ 'url' ], $query [ 'ext' ]);
2009-04-25 02:16:05 +00:00
$url .= Router :: queryString ( $query , array ());
}
2008-05-30 11:40:08 +00:00
$this -> Session -> write ( 'Auth.redirect' , $url );
2008-09-12 02:10:55 +00:00
$controller -> redirect ( $loginAction );
2008-05-30 11:40:08 +00:00
return false ;
} elseif ( ! empty ( $this -> ajaxLogin )) {
$controller -> viewPath = 'elements' ;
2008-09-20 20:18:16 +00:00
echo $controller -> render ( $this -> ajaxLogin , $this -> RequestHandler -> ajaxLayout );
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
return false ;
2008-09-12 02:10:55 +00:00
} else {
$controller -> redirect ( null , 403 );
2008-05-30 11:40:08 +00:00
}
}
}
2008-05-31 12:36:38 +00:00
if ( ! $this -> authorize ) {
return true ;
}
2008-05-30 11:40:08 +00:00
2008-05-31 12:36:38 +00:00
extract ( $this -> __authType ());
switch ( $type ) {
case 'controller' :
$this -> object =& $controller ;
break ;
case 'crud' :
case 'actions' :
if ( isset ( $controller -> Acl )) {
$this -> Acl =& $controller -> Acl ;
} else {
2008-10-15 02:52:19 +00:00
$err = 'Could not find AclComponent. Please include Acl in ' ;
$err .= 'Controller::$components.' ;
trigger_error ( __ ( $err , true ), E_USER_WARNING );
2008-05-31 12:36:38 +00:00
}
break ;
case 'model' :
if ( ! isset ( $object )) {
2008-10-15 02:52:19 +00:00
$hasModel = (
isset ( $controller -> { $controller -> modelClass }) &&
is_object ( $controller -> { $controller -> modelClass })
);
$isUses = (
! empty ( $controller -> uses ) && isset ( $controller -> { $controller -> uses [ 0 ]}) &&
is_object ( $controller -> { $controller -> uses [ 0 ]})
);
if ( $hasModel ) {
2008-05-31 12:36:38 +00:00
$object = $controller -> modelClass ;
2008-10-15 02:52:19 +00:00
} elseif ( $isUses ) {
2008-05-31 12:36:38 +00:00
$object = $controller -> uses [ 0 ];
}
}
$type = array ( 'model' => $object );
break ;
}
2008-05-30 11:40:08 +00:00
2008-05-31 12:36:38 +00:00
if ( $this -> isAuthorized ( $type )) {
2008-05-30 11:40:08 +00:00
return true ;
}
2008-05-31 12:36:38 +00:00
2009-08-13 15:18:46 +00:00
$this -> Session -> setFlash ( $this -> authError , $this -> flashLayout , array (), 'auth' );
2008-05-31 12:36:38 +00:00
$controller -> redirect ( $controller -> referer (), null , true );
return false ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Attempts to introspect the correct values for object properties including
* $userModel and $sessionKey .
*
* @ param object $controller A reference to the instantiating controller object
2008-09-25 16:49:56 +00:00
* @ return boolean
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __setDefaults () {
if ( empty ( $this -> userModel )) {
trigger_error ( __ ( " Could not find \$ userModel. Please set AuthComponent:: \$ userModel in beforeFilter(). " , true ), E_USER_WARNING );
return false ;
}
2008-05-31 12:36:38 +00:00
$defaults = array (
'loginAction' => Router :: normalize ( array (
'controller' => Inflector :: underscore ( Inflector :: pluralize ( $this -> userModel )),
'action' => 'login'
)),
'sessionKey' => 'Auth.' . $this -> userModel ,
'logoutRedirect' => $this -> loginAction ,
'loginError' => __ ( 'Login failed. Invalid username or password.' , true ),
'authError' => __ ( 'You are not authorized to access that location.' , true )
);
foreach ( $defaults as $key => $value ) {
if ( empty ( $this -> { $key })) {
$this -> { $key } = $value ;
}
2008-05-30 11:40:08 +00:00
}
return true ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2008-10-15 02:52:19 +00:00
* Determines whether the given user is authorized to perform an action . The type of
* authorization used is based on the value of AuthComponent :: $authorize or the
* passed $type param .
2008-05-30 11:40:08 +00:00
*
* Types :
2008-10-15 02:52:19 +00:00
* 'controller' will validate against Controller :: isAuthorized () if controller instance is
* passed in $object
2008-05-30 11:40:08 +00:00
* 'actions' will validate Controller :: action against an AclComponent :: check ()
* 'crud' will validate mapActions against an AclComponent :: check ()
2008-10-15 02:52:19 +00:00
* array ( 'model' => 'name' ); will validate mapActions against model
2008-12-18 14:31:53 +00:00
* $name :: isAuthorized ( user , controller , mapAction )
2008-10-15 02:52:19 +00:00
* 'object' will validate Controller :: action against
* object :: isAuthorized ( user , controller , action )
2008-05-30 11:40:08 +00:00
*
* @ param string $type Type of authorization
* @ param mixed $object object , model object , or model name
* @ param mixed $user The user to check the authorization of
* @ return boolean True if $user is authorized , otherwise false
* @ access public
*/
function isAuthorized ( $type = null , $object = null , $user = null ) {
if ( empty ( $user ) && ! $this -> user ()) {
return false ;
} elseif ( empty ( $user )) {
$user = $this -> user ();
}
extract ( $this -> __authType ( $type ));
if ( ! $object ) {
$object = $this -> object ;
}
$valid = false ;
switch ( $type ) {
case 'controller' :
$valid = $object -> isAuthorized ();
break ;
case 'actions' :
$valid = $this -> Acl -> check ( $user , $this -> action ());
break ;
case 'crud' :
$this -> mapActions ();
if ( ! isset ( $this -> actionMap [ $this -> params [ 'action' ]])) {
2008-10-15 02:52:19 +00:00
$err = 'Auth::startup() - Attempted access of un-mapped action "%1$s" in' ;
$err .= ' controller "%2$s"' ;
trigger_error (
sprintf ( __ ( $err , true ), $this -> params [ 'action' ], $this -> params [ 'controller' ]),
E_USER_WARNING
);
2008-05-30 11:40:08 +00:00
} else {
2008-10-15 02:52:19 +00:00
$valid = $this -> Acl -> check (
$user ,
$this -> action ( ':controller' ),
$this -> actionMap [ $this -> params [ 'action' ]]
);
2008-05-30 11:40:08 +00:00
}
break ;
case 'model' :
$this -> mapActions ();
$action = $this -> params [ 'action' ];
2008-10-23 00:10:44 +00:00
if ( isset ( $this -> actionMap [ $action ])) {
2008-05-30 11:40:08 +00:00
$action = $this -> actionMap [ $action ];
}
if ( is_string ( $object )) {
$object = $this -> getModel ( $object );
}
case 'object' :
if ( ! isset ( $action )) {
$action = $this -> action ( ':action' );
}
if ( empty ( $object )) {
trigger_error ( sprintf ( __ ( 'Could not find %s. Set AuthComponent::$object in beforeFilter() or pass a valid object' , true ), get_class ( $object )), E_USER_WARNING );
return ;
}
if ( method_exists ( $object , 'isAuthorized' )) {
$valid = $object -> isAuthorized ( $user , $this -> action ( ':controller' ), $action );
2008-10-23 00:10:44 +00:00
} elseif ( $object ) {
2008-05-30 11:40:08 +00:00
trigger_error ( sprintf ( __ ( '%s::isAuthorized() is not defined.' , true ), get_class ( $object )), E_USER_WARNING );
}
break ;
case null :
case false :
return true ;
break ;
default :
trigger_error ( __ ( 'Auth::isAuthorized() - $authorize is set to an incorrect value. Allowed settings are: "actions", "crud", "model" or null.' , true ), E_USER_WARNING );
break ;
}
return $valid ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Get authorization type
*
* @ param string $auth Type of authorization
* @ return array Associative array with : type , object
* @ access private
*/
function __authType ( $auth = null ) {
if ( $auth == null ) {
$auth = $this -> authorize ;
}
$object = null ;
if ( is_array ( $auth )) {
$type = key ( $auth );
$object = $auth [ $type ];
} else {
$type = $auth ;
return compact ( 'type' );
}
return compact ( 'type' , 'object' );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Takes a list of actions in the current controller for which authentication is not required , or
* no parameters to allow all actions .
*
2009-08-13 16:08:30 +00:00
* @ param mixed $action Controller action name or array of actions
2008-05-30 11:40:08 +00:00
* @ param string $action Controller action name
* @ param string ... etc .
2008-09-25 16:49:56 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access public
*/
function allow () {
$args = func_get_args ();
2008-10-15 02:52:19 +00:00
if ( empty ( $args ) || $args == array ( '*' )) {
$this -> allowedActions = $this -> _methods ;
2008-05-30 11:40:08 +00:00
} else {
if ( isset ( $args [ 0 ]) && is_array ( $args [ 0 ])) {
$args = $args [ 0 ];
}
2009-09-21 16:09:06 +00:00
$this -> allowedActions = array_merge ( $this -> allowedActions , array_map ( 'strtolower' , $args ));
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Removes items from the list of allowed actions .
*
2009-08-13 16:08:30 +00:00
* @ param mixed $action Controller action name or array of actions
2008-05-30 11:40:08 +00:00
* @ param string $action Controller action name
* @ param string ... etc .
2008-09-25 16:49:56 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ see AuthComponent :: allow ()
* @ access public
*/
function deny () {
$args = func_get_args ();
2009-08-13 16:08:30 +00:00
if ( isset ( $args [ 0 ]) && is_array ( $args [ 0 ])) {
$args = $args [ 0 ];
}
2008-05-30 11:40:08 +00:00
foreach ( $args as $arg ) {
2009-08-31 00:41:39 +00:00
$i = array_search ( strtolower ( $arg ), $this -> allowedActions );
2008-05-30 11:40:08 +00:00
if ( is_int ( $i )) {
unset ( $this -> allowedActions [ $i ]);
}
}
$this -> allowedActions = array_values ( $this -> allowedActions );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Maps action names to CRUD operations . Used for controller - based authentication .
*
* @ param array $map Actions to map
2008-09-25 16:49:56 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access public
*/
function mapActions ( $map = array ()) {
$crud = array ( 'create' , 'read' , 'update' , 'delete' );
foreach ( $map as $action => $type ) {
if ( in_array ( $action , $crud ) && is_array ( $type )) {
foreach ( $type as $typedAction ) {
$this -> actionMap [ $typedAction ] = $action ;
}
} else {
$this -> actionMap [ $action ] = $type ;
}
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Manually log - in a user with the given parameter data . The $data provided can be any data
* structure used to identify a user in AuthComponent :: identify () . If $data is empty or not
* specified , POST data from Controller :: $data will be used automatically .
*
* After ( if ) login is successful , the user record is written to the session key specified in
* AuthComponent :: $sessionKey .
*
* @ param mixed $data User object
* @ return boolean True on login success , false on failure
* @ access public
*/
function login ( $data = null ) {
$this -> __setDefaults ();
$this -> _loggedIn = false ;
if ( empty ( $data )) {
$data = $this -> data ;
}
if ( $user = $this -> identify ( $data )) {
$this -> Session -> write ( $this -> sessionKey , $user );
$this -> _loggedIn = true ;
}
return $this -> _loggedIn ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Logs a user out , and returns the login action to redirect to .
*
* @ param mixed $url Optional URL to redirect the user to after logout
* @ return string AuthComponent :: $loginAction
* @ see AuthComponent :: $loginAction
* @ access public
*/
function logout () {
$this -> __setDefaults ();
2009-09-11 10:58:23 +00:00
$this -> Session -> delete ( $this -> sessionKey );
$this -> Session -> delete ( 'Auth.redirect' );
2008-05-30 11:40:08 +00:00
$this -> _loggedIn = false ;
return Router :: normalize ( $this -> logoutRedirect );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Get the current user from the session .
*
2008-09-25 16:49:56 +00:00
* @ param string $key field to retrive . Leave null to get entire User record
* @ return mixed User record . or null if no user is logged in .
2008-05-30 11:40:08 +00:00
* @ access public
*/
function user ( $key = null ) {
$this -> __setDefaults ();
if ( ! $this -> Session -> check ( $this -> sessionKey )) {
return null ;
}
if ( $key == null ) {
2009-08-13 14:39:59 +00:00
$model =& $this -> getModel ();
return array ( $model -> alias => $this -> Session -> read ( $this -> sessionKey ));
2008-05-30 11:40:08 +00:00
} else {
$user = $this -> Session -> read ( $this -> sessionKey );
if ( isset ( $user [ $key ])) {
return $user [ $key ];
}
2008-09-08 20:33:48 +00:00
return null ;
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* If no parameter is passed , gets the authentication redirect URL .
*
* @ param mixed $url Optional URL to write as the login redirect URL .
* @ return string Redirect URL
* @ access public
*/
function redirect ( $url = null ) {
if ( ! is_null ( $url )) {
2008-12-24 05:40:05 +00:00
$redir = $url ;
$this -> Session -> write ( 'Auth.redirect' , $redir );
2008-12-17 07:30:55 +00:00
} elseif ( $this -> Session -> check ( 'Auth.redirect' )) {
2008-05-30 11:40:08 +00:00
$redir = $this -> Session -> read ( 'Auth.redirect' );
$this -> Session -> delete ( 'Auth.redirect' );
if ( Router :: normalize ( $redir ) == Router :: normalize ( $this -> loginAction )) {
$redir = $this -> loginRedirect ;
}
} else {
$redir = $this -> loginRedirect ;
}
return Router :: normalize ( $redir );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Validates a user against an abstract object .
*
* @ param mixed $object The object to validate the user against .
* @ param mixed $user Optional . The identity of the user to be validated .
* Uses the current user session if none specified . For
* valid forms of identifying users , see
* AuthComponent :: identify () .
* @ param string $action Optional . The action to validate against .
* @ see AuthComponent :: identify ()
* @ return boolean True if the user validates , false otherwise .
* @ access public
*/
function validate ( $object , $user = null , $action = null ) {
if ( empty ( $user )) {
$user = $this -> user ();
}
if ( empty ( $user )) {
return false ;
}
return $this -> Acl -> check ( $user , $object , $action );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Returns the path to the ACO node bound to a controller / action .
*
* @ param string $action Optional . The controller / action path to validate the
* user against . The current request action is used if
* none is specified .
* @ return boolean ACO node path
* @ access public
*/
2009-09-14 04:29:33 +00:00
function action ( $action = ':plugin/:controller/:action' ) {
$plugin = empty ( $this -> params [ 'plugin' ]) ? null : Inflector :: camelize ( $this -> params [ 'plugin' ]) . '/' ;
2008-05-30 11:40:08 +00:00
return str_replace (
2009-09-14 04:29:33 +00:00
array ( ':controller' , ':action' , ':plugin/' ),
array ( Inflector :: camelize ( $this -> params [ 'controller' ]), $this -> params [ 'action' ], $plugin ),
2008-05-30 11:40:08 +00:00
$this -> actionPath . $action
);
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Returns a reference to the model object specified , and attempts
* to load it if it is not found .
*
* @ param string $name Model name ( defaults to AuthComponent :: $userModel )
* @ return object A reference to a model object
* @ access public
*/
function & getModel ( $name = null ) {
$model = null ;
if ( ! $name ) {
$name = $this -> userModel ;
}
if ( PHP5 ) {
$model = ClassRegistry :: init ( $name );
} else {
$model =& ClassRegistry :: init ( $name );
}
if ( empty ( $model )) {
trigger_error ( __ ( 'Auth::getModel() - Model is not set or could not be found' , true ), E_USER_WARNING );
return null ;
}
return $model ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Identifies a user based on specific criteria .
*
* @ param mixed $user Optional . The identity of the user to be validated .
* Uses the current user session if none specified .
* @ param array $conditions Optional . Additional conditions to a find .
* @ return array User record data , or null , if the user could not be identified .
* @ access public
*/
function identify ( $user = null , $conditions = null ) {
if ( $conditions === false ) {
$conditions = null ;
} elseif ( is_array ( $conditions )) {
$conditions = array_merge (( array ) $this -> userScope , $conditions );
} else {
$conditions = $this -> userScope ;
}
2009-08-13 14:39:59 +00:00
$model =& $this -> getModel ();
2008-05-30 11:40:08 +00:00
if ( empty ( $user )) {
$user = $this -> user ();
if ( empty ( $user )) {
return null ;
}
} elseif ( is_object ( $user ) && is_a ( $user , 'Model' )) {
if ( ! $user -> exists ()) {
return null ;
}
$user = $user -> read ();
2009-08-13 14:39:59 +00:00
$user = $user [ $model -> alias ];
} elseif ( is_array ( $user ) && isset ( $user [ $model -> alias ])) {
$user = $user [ $model -> alias ];
2008-05-30 11:40:08 +00:00
}
2009-08-13 14:39:59 +00:00
if ( is_array ( $user ) && ( isset ( $user [ $this -> fields [ 'username' ]]) || isset ( $user [ $model -> alias . '.' . $this -> fields [ 'username' ]]))) {
2008-05-30 11:40:08 +00:00
if ( isset ( $user [ $this -> fields [ 'username' ]]) && ! empty ( $user [ $this -> fields [ 'username' ]]) && ! empty ( $user [ $this -> fields [ 'password' ]])) {
if ( trim ( $user [ $this -> fields [ 'username' ]]) == '=' || trim ( $user [ $this -> fields [ 'password' ]]) == '=' ) {
return false ;
}
$find = array (
2009-08-13 14:39:59 +00:00
$model -> alias . '.' . $this -> fields [ 'username' ] => $user [ $this -> fields [ 'username' ]],
$model -> alias . '.' . $this -> fields [ 'password' ] => $user [ $this -> fields [ 'password' ]]
2008-05-30 11:40:08 +00:00
);
2009-08-13 14:39:59 +00:00
} elseif ( isset ( $user [ $model -> alias . '.' . $this -> fields [ 'username' ]]) && ! empty ( $user [ $model -> alias . '.' . $this -> fields [ 'username' ]])) {
if ( trim ( $user [ $model -> alias . '.' . $this -> fields [ 'username' ]]) == '=' || trim ( $user [ $model -> alias . '.' . $this -> fields [ 'password' ]]) == '=' ) {
2008-05-30 11:40:08 +00:00
return false ;
}
$find = array (
2009-08-13 14:39:59 +00:00
$model -> alias . '.' . $this -> fields [ 'username' ] => $user [ $model -> alias . '.' . $this -> fields [ 'username' ]],
$model -> alias . '.' . $this -> fields [ 'password' ] => $user [ $model -> alias . '.' . $this -> fields [ 'password' ]]
2008-05-30 11:40:08 +00:00
);
} else {
return false ;
}
2009-10-02 19:36:07 +00:00
$data = $model -> find ( 'first' , array (
'conditions' => array_merge ( $find , $conditions ),
'recursive' => 0
));
2009-08-13 14:39:59 +00:00
if ( empty ( $data ) || empty ( $data [ $model -> alias ])) {
2008-05-30 11:40:08 +00:00
return null ;
}
2009-01-14 05:21:26 +00:00
} elseif ( ! empty ( $user ) && is_string ( $user )) {
2009-10-02 19:36:07 +00:00
$data = $model -> find ( 'first' , array (
'conditions' => array_merge ( array ( $model -> escapeField () => $user ), $conditions ),
));
2009-08-13 14:39:59 +00:00
if ( empty ( $data ) || empty ( $data [ $model -> alias ])) {
2008-05-30 11:40:08 +00:00
return null ;
}
}
2008-07-31 19:00:12 +00:00
if ( ! empty ( $data )) {
2009-08-13 14:39:59 +00:00
if ( ! empty ( $data [ $model -> alias ][ $this -> fields [ 'password' ]])) {
unset ( $data [ $model -> alias ][ $this -> fields [ 'password' ]]);
2008-05-30 11:40:08 +00:00
}
2009-08-13 14:39:59 +00:00
return $data [ $model -> alias ];
2008-05-30 11:40:08 +00:00
}
2008-09-08 20:33:48 +00:00
return null ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Hash any passwords found in $data using $userModel and $fields [ 'password' ]
*
* @ param array $data Set of data to look for passwords
* @ return array Data with passwords hashed
* @ access public
*/
function hashPasswords ( $data ) {
if ( is_object ( $this -> authenticate ) && method_exists ( $this -> authenticate , 'hashPasswords' )) {
return $this -> authenticate -> hashPasswords ( $data );
}
2009-08-13 14:39:59 +00:00
$model =& $this -> getModel ();
if ( is_array ( $data ) && isset ( $data [ $model -> alias ])) {
if ( isset ( $data [ $model -> alias ][ $this -> fields [ 'username' ]]) && isset ( $data [ $model -> alias ][ $this -> fields [ 'password' ]])) {
$data [ $model -> alias ][ $this -> fields [ 'password' ]] = $this -> password ( $data [ $model -> alias ][ $this -> fields [ 'password' ]]);
2008-05-30 11:40:08 +00:00
}
}
return $data ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Hash a password with the application 's salt value (as defined with Configure::write(' Security . salt ' );
*
* @ param string $password Password to hash
* @ return string Hashed password
* @ access public
*/
function password ( $password ) {
return Security :: hash ( $password , null , true );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Component shutdown . If user is logged in , wipe out redirect .
*
* @ param object $controller Instantiating controller
* @ access public
*/
function shutdown ( & $controller ) {
if ( $this -> _loggedIn ) {
2009-09-11 10:58:23 +00:00
$this -> Session -> delete ( 'Auth.redirect' );
2008-05-30 11:40:08 +00:00
}
}
}
?>