Authentication control component class.
Binds access control with user authentication and session management.
string
ALL 'all' string
QUERY_STRING_REDIRECT 'redirect' $_authenticateObjects protected $_authenticationProvider protected The instance of the Authenticate provider that was used for successfully logging in the current user after calling login() in the same request
$_authorizationProvider protected The instance of the Authorize provider that was used to grant access to the current user to the URL they are requesting.
$_authorizeObjects protected $_defaultConfig protected array$_storage protected $allowedActions public string[]$components public array$request public $response public $session public $Flash public $RequestHandler public $sessionKey. $sessionKey. Similar to AuthComponent::user() except if user is not found in configured storage, connected authentication objects will have their getUser() methods called.
loginAction and checks if current request URL is same as login action. Handles unauthenticated access attempt. First the unauthenticated() method of the last authenticator in the chain will be called. The authenticator can handle sending response or redirection as appropriate and return true to indicate no further action is necessary. If authenticator returns null this method redirects user to login action. If it's an AJAX request and config ajaxLogin is specified that element is rendered else a 403 HTTP status code is returned.
Takes a list of actions in the current controller for which authentication is not required, or no parameters to allow all actions.
Main execution method, handles initial authentication check and redirection of invalid users.
If login was called during this request and the user was successfully authenticated, this function will return the instance of the authentication object that was used for logging the user in.
If there was any authorization processing for the current request, this function will return the instance of the Authorization object that granted access to the user to the current address.
flash config. Use the configured authentication adapters, and attempt to identify the user by credentials contained in $request.
__get( string $name )
Magic accessor for backward compatibility for property $sessionKey.
$name Cake\Controller\Component::__get() __set( string $name , mixed $value )
Magic setter for backward compatibility for property $sessionKey.
$name $value _getUrlToRedirectBackTo( )
Returns the URL to redirect back to or / if not possible.
This method takes the referrer into account if the request is not of type GET.
_getUser( )
Similar to AuthComponent::user() except if user is not found in configured storage, connected authentication objects will have their getUser() methods called.
This lets stateless authentication methods function correctly.
_isAllowed( Cake\Controller\Controller $controller )
Checks whether current action is accessible without authentication.
Cake\Controller\Controller $controller A reference to the instantiating controller object
_isLoginAction( Cake\Controller\Controller $controller )
Normalizes config loginAction and checks if current request URL is same as login action.
Cake\Controller\Controller $controller _loginActionRedirectUrl( )
Returns the URL of the login action to redirect to.
This includes the redirect query string if applicable.
_unauthenticated( Cake\Controller\Controller $controller )
Handles unauthenticated access attempt. First the unauthenticated() method of the last authenticator in the chain will be called. The authenticator can handle sending response or redirection as appropriate and return true to indicate no further action is necessary. If authenticator returns null this method redirects user to login action. If it's an AJAX request and config ajaxLogin is specified that element is rendered else a 403 HTTP status code is returned.
Cake\Controller\Controller $controller Cake\Http\Response|nullNull if current action is login action else response object returned by authenticate object or Controller::redirect().
Cake\Core\Exception\Exception_unauthorized( Cake\Controller\Controller $controller )
Handle unauthorized access attempt
Cake\Controller\Controller $controller Cake\Http\ResponseCake\Http\Exception\ForbiddenExceptionallow( string|string[]|null $actions = null )
Takes a list of actions in the current controller for which authentication is not required, or no parameters to allow all actions.
You can use allow with either an array or a simple string.
$this->Auth->allow('view');
$this->Auth->allow(['edit', 'add']); or to allow all actions
$this->Auth->allow();
$actions optional null authCheck( Cake\Event\Event $event )
Main execution method, handles initial authentication check and redirection of invalid users.
The auth check is done when event name is same as the one configured in checkAuthIn config.
Cake\Event\Event $event Cake\Http\Response|nullauthenticationProvider( )
If login was called during this request and the user was successfully authenticated, this function will return the instance of the authentication object that was used for logging the user in.
Cake\Auth\BaseAuthenticate|nullauthorizationProvider( )
If there was any authorization processing for the current request, this function will return the instance of the Authorization object that granted access to the user to the current address.
Cake\Auth\BaseAuthorize|nullconstructAuthenticate( )
Loads the configured authentication objects.
Cake\Core\Exception\ExceptionconstructAuthorize( )
Loads the authorization objects configured.
Cake\Core\Exception\Exceptiondeny( string|string[]|null $actions = null )
Removes items from the list of allowed/no authentication required actions.
You can use deny with either an array or a simple string.
$this->Auth->deny('view');
$this->Auth->deny(['edit', 'add']); or
$this->Auth->deny();
to remove all items from the allowed list
$actions optional null flash( string $message )
Set a flash message. Uses the Flash component with values from flash config.
$message getAuthenticate( string $alias )
Getter for authenticate objects. Will return a particular authenticate object.
$alias Cake\Auth\BaseAuthenticate|nullgetAuthorize( string $alias )
Getter for authorize objects. Will return a particular authorize object.
$alias Cake\Auth\BaseAuthorize|nullidentify( )
Use the configured authentication adapters, and attempt to identify the user by credentials contained in $request.
Triggers Auth.afterIdentify event which the authenticate classes can listen to.
implementedEvents( )
Events supported by this component.
Cake\Controller\Component::implementedEvents() initialize( array $config )
Initialize properties.
$config Cake\Controller\Component::initialize() isAuthorized( array|ArrayAccess|null $user = null , Cake\Http\ServerRequest $request = null )
Check if the provided user is authorized for the request.
Uses the configured Authorization adapters to check whether or not a user is authorized. Each adapter will be checked in sequence, if any of them return true, then the user will be authorized for the request.
$user optional null The user to check the authorization of. If empty the user fetched from storage will be used.
Cake\Http\ServerRequest $request optional null The request to authenticate for. If empty, the current request will be used.
logout( )
Log a user out.
Returns the logout action to redirect to. Triggers the Auth.logout event which the authenticate classes can listen for and perform custom logout logic.
logoutRedirectredirectUrl( string|array|null $url = null )
Get the URL a user should be redirected to upon login.
Pass a URL in to set the destination a user should be redirected to upon logging in.
If no parameter is passed, gets the authentication redirect URL. The URL returned is as per following rules:
loginRedirect, the loginRedirect value is returned.loginRedirect, / is returned.$url optional null setUser( array|ArrayAccess $user )
Set provided user info to storage as logged in user.
The storage class is configured using storage config key or passing instance to AuthComponent::storage().
$user startup( Cake\Event\Event $event )
Callback for Controller.startup event.
Cake\Event\Event $event Cake\Http\Response|nullstorage( Cake\Auth\Storage\StorageInterface $storage = null )
Get/set user record storage object.
Cake\Auth\Storage\StorageInterface $storage optional null Sets provided object as storage or if null returns configured storage object.
Cake\Auth\Storage\StorageInterface|nulluser( string|null $key = null )
Get the current user from storage.
$key optional null __construct( Cake\Controller\ComponentRegistry $registry , array $config = [] )
Constructor
Cake\Controller\ComponentRegistry $registry $config optional [] __debugInfo( )
Returns an array that can be used to describe the internal state of this object.
getController( )
Get the controller this component is bound to.
Cake\Controller\ControllerdispatchEvent( string $name , array|null $data = null , object|null $subject = null )
Wrapper for creating and dispatching events.
Returns a dispatched event.
$name $data optional null Any value you wish to be transported with this event to it can be read by listeners.
$subject optional null The object that this event applies to ($this by default).
Cake\Event\EventeventManager( Cake\Event\EventManager $eventManager = null )
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Cake\Event\EventManager $eventManager optional null Cake\Event\EventManagergetEventManager( )
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Cake\Event\EventManagersetEventManager( Cake\Event\EventManager $eventManager )
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Cake\Event\EventManager $eventManager _configDelete( string $key )
Deletes a single config key.
$key Cake\Core\Exception\Exception_configRead( string|null $key )
Reads a config key.
$key _configWrite( string|array $key , mixed $value , boolean|string $merge = false )
Writes a config key.
$key $value $merge optional false True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Cake\Core\Exception\Exceptionconfig( string|array|null $key = null , mixed|null $value = null , boolean $merge = true )
Gets/Sets the config.
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key'); Reading a nested value:
$this->config('some.nested.key'); Setting a specific value:
$this->config('key', $value); Setting a nested value:
$this->config('some.nested.key', $value); Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
$key optional null $value optional null $merge optional true Cake\Core\Exception\ExceptionconfigShallow( string|array $key , mixed|null $value = null )
Merge provided config with existing config. Unlike config() which does a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->configShallow('key', $value); Setting a nested value:
$this->configShallow('some.nested.key', $value); Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
$key $value optional null getConfig( string|null $key = null , mixed $default = null )
Returns the config.
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key'); Reading a nested value:
$this->getConfig('some.nested.key'); Reading with default value:
$this->getConfig('some-key', 'default-value'); $key optional null $default optional null setConfig( string|array $key , mixed|null $value = null , boolean $merge = true )
Sets the config.
Setting a specific value:
$this->setConfig('key', $value); Setting a nested value:
$this->setConfig('some.nested.key', $value); Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
$key $value optional null $merge optional true Cake\Core\Exception\Exceptionlog( mixed $msg , integer|string $level = LogLevel::ERROR , string|array $context = [] )
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
$msg $level optional LogLevel::ERROR $context optional [] protected Cake\Auth\BaseAuthenticate[]
Objects that will be used for authentication checks.
[]
protected Cake\Auth\BaseAuthenticate|null
The instance of the Authenticate provider that was used for successfully logging in the current user after calling login() in the same request
protected Cake\Auth\BaseAuthorize|null
The instance of the Authorize provider that was used to grant access to the current user to the URL they are requesting.
protected Cake\Auth\BaseAuthorize[]
Objects that will be used for authorization checks.
[]
protected array
Default config
authenticate - An array of authentication objects to use for authenticating users. You can configure multiple adapters and they will be checked sequentially when users are identified.
$this->Auth->setConfig('authenticate', [
'Form' => [
'userModel' => 'Users.Users'
]
]); Using the class name without 'Authenticate' as the key, you can pass in an array of config for each authentication object. Additionally you can define config that should be set to all authentications objects using the 'all' key:
$this->Auth->setConfig('authenticate', [
AuthComponent::ALL => [
'userModel' => 'Users.Users',
'scope' => ['Users.active' => 1]
],
'Form',
'Basic'
]);
authorize - An array of authorization objects to use for authorizing users. You can configure multiple adapters and they will be checked sequentially when authorization checks are done.
$this->Auth->setConfig('authorize', [
'Crud' => [
'actionPath' => 'controllers/'
]
]); Using the class name without 'Authorize' as the key, you can pass in an array of config for each authorization object. Additionally you can define config that should be set to all authorization objects using the AuthComponent::ALL key:
$this->Auth->setConfig('authorize', [
AuthComponent::ALL => [
'actionPath' => 'controllers/'
],
'Crud',
'CustomAuth'
]);
~~ajaxLogin~~ - The name of an optional view element to render when an Ajax request is made with an invalid or expired session. This option is deprecated since 3.3.6. Your client side code should instead check for 403 status code and show appropriate login form.
flash - Settings to use when Auth needs to do a flash message with FlashComponent::set(). Available keys are:
key - The message domain to use for flashes generated by this component, defaults to 'auth'.element - Flash element to use, defaults to 'default'.params - The array of additional params to use, defaults to ['class' => 'error']loginAction - A URL (defined as a string or array) to the controller action that handles logins. Defaults to /users/login.
loginRedirect - 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, redirectUrl() method will return the URL specified in loginRedirect.
logoutRedirect - 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 loginAction.
authError - Error to display when user attempts to access an object or action to which they do not have access.
unauthorizedRedirect - Controls handling of unauthorized access.
true unauthorized user is redirected to the referrer URL or $loginRedirect or '/'.ForbiddenException exception is thrown instead of redirecting.storage - Storage class to use for persisting user record. When using stateless authenticator you should set this to 'Memory'. Defaults to 'Session'.
checkAuthIn - Name of event for which initial auth checks should be done. Defaults to 'Controller.startup'. You can set it to 'Controller.initialize' if you want the check to be done before controller's beforeFilter() is run.
[
'authenticate' => null,
'authorize' => null,
'ajaxLogin' => null,
'flash' => null,
'loginAction' => null,
'loginRedirect' => null,
'logoutRedirect' => null,
'authError' => null,
'unauthorizedRedirect' => true,
'storage' => 'Session',
'checkAuthIn' => 'Controller.startup'
] public string[]
Controller actions for which user validation is not required.
[]
public array
Other components utilized by AuthComponent
['RequestHandler', 'Flash']
public Cake\Http\Session
Instance of the Session object
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.8/class-Cake.Controller.Component.AuthComponent.html