Authentication control component class.
Binds access control with user authentication and session management.
string
'all'
Constant for 'all'
string
'redirect'
The query string key used for remembering the referred page when getting redirected to login.
Cake\Controller\Component\FlashComponent
Cake\Controller\Component\RequestHandlerComponent
arrayCake\Auth\BaseAuthenticate>
Objects that will be used for authentication checks.
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
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.
arrayCake\Auth\BaseAuthorize>
Objects that will be used for authorization checks.
array<string, array>
A component lookup table used to lazy load component objects.
array<string, mixed>
Runtime config
bool
Whether the config property has already been configured with defaults
array<string, mixed>
Default config
string
Default class name for new event objects.
Cake\Event\EventManagerInterface|null
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Cake\Controller\ComponentRegistry
Component registry class used to lazy load components.
Cake\Auth\Storage\StorageInterface|null
Storage object.
array<string>
Controller actions for which user validation is not required.
array
Other components utilized by AuthComponent
Constructor
Returns an array that can be used to describe the internal state of this object.
Magic accessor for backward compatibility for property $sessionKey
.
Magic setter for backward compatibility for property $sessionKey
.
Deletes a single config key.
Reads a config key.
Writes a config key.
Returns the URL to redirect back to or / if not possible.
Similar to AuthComponent::user() except if user is not found in configured storage, connected authentication objects will have their getUser() methods called.
Checks whether current action is accessible without authentication.
Normalizes config loginAction
and checks if current request URL is same as login action.
Returns the URL of the login action to redirect to.
Sets defaults for configs.
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.
Handle unauthorized access attempt
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.
Merge provided config with existing config. Unlike config()
which does a recursive merge for nested keys, this method does a simple merge.
Loads the configured authentication objects.
Loads the authorization objects configured.
Removes items from the list of allowed/no authentication required actions.
Wrapper for creating and dispatching events.
Set a flash message. Uses the Flash component with values from flash
config.
Getter for authenticate objects. Will return a particular authenticate object.
Getter for authorize objects. Will return a particular authorize object.
Returns the config.
Returns the config for this specific key.
Get the controller this component is bound to.
Returns the Cake\Event\EventManager manager instance for this object.
Use the configured authentication adapters, and attempt to identify the user by credentials contained in $request.
Events supported by this component.
Initialize properties.
Check if the provided user is authorized for the request.
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Log a user out.
Get the URL a user should be redirected to upon login.
Sets the config.
Returns the Cake\Event\EventManagerInterface instance for this object.
Set provided user info to storage as logged in user.
Callback for Controller.startup event.
Get/set user record storage object.
Get the current user from storage.
__construct(Cake\Controller\ComponentRegistry $registry, array<string, mixed> $config = [])
Constructor
Cake\Controller\ComponentRegistry
$registry A component registry this component can use to lazy load its components.
array<string, mixed>
$config optional Array of configuration settings.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>
__get(string $name): mixed
Magic accessor for backward compatibility for property $sessionKey
.
string
$name Property name
mixed
__set(string $name, mixed $value): void
Magic setter for backward compatibility for property $sessionKey
.
string
$name Property name.
mixed
$value Value to set.
void
_configDelete(string $key): void
Deletes a single config key.
string
$key Key to delete.
void
Cake\Core\Exception\CakeException
_configRead(string|null $key): mixed
Reads a config key.
string|null
$key Key to read.
mixed
_configWrite(array<string, mixed>|string $key, mixed $value, string|bool $merge = false): void
Writes a config key.
array<string, mixed>|string
$key Key to write to.
mixed
$value Value to write.
string|bool
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
void
Cake\Core\Exception\CakeException
_getUrlToRedirectBackTo(): string
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.
string
_getUser(): bool
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.
bool
_isAllowed(Cake\Controller\Controller $controller): bool
Checks whether current action is accessible without authentication.
Cake\Controller\Controller
$controller A reference to the instantiating controller object
bool
_isLoginAction(Cake\Controller\Controller $controller): bool
Normalizes config loginAction
and checks if current request URL is same as login action.
Cake\Controller\Controller
$controller A reference to the controller object.
bool
_loginActionRedirectUrl(): array|string
Returns the URL of the login action to redirect to.
This includes the redirect query string if applicable.
array|string
_setDefaults(): void
Sets defaults for configs.
void
_unauthenticated(Cake\Controller\Controller $controller): Cake\Http\Response|null
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.
Cake\Controller\Controller
$controller A reference to the controller object.
Cake\Http\Response|null
Cake\Core\Exception\CakeException
_unauthorized(Cake\Controller\Controller $controller): Cake\Http\Response|null
Handle unauthorized access attempt
Cake\Controller\Controller
$controller A reference to the controller object
Cake\Http\Response|null
Cake\Http\Exception\ForbiddenException
allow(array<string>|string|null $actions = null): void
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();
array<string>|string|null
$actions optional Controller action name or array of actions
void
authCheck(Cake\Event\EventInterface $event): Cake\Http\Response|null
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\EventInterface
$event Event instance.
Cake\Http\Response|null
ReflectionException
authenticationProvider(): Cake\Auth\BaseAuthenticate|null
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|null
authorizationProvider(): Cake\Auth\BaseAuthorize|null
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|null
configShallow(array<string, mixed>|string $key, mixed|null $value = null): $this
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']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
$this
constructAuthenticate(): array<string, object>|null
Loads the configured authentication objects.
array<string, object>|null
Cake\Core\Exception\CakeException
constructAuthorize(): array|null
Loads the authorization objects configured.
array|null
Cake\Core\Exception\CakeException
deny(array<string>|string|null $actions = null): void
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
array<string>|string|null
$actions optional Controller action name or array of actions
void
dispatchEvent(string $name, array|null $data = null, object|null $subject = null): Cake\Event\EventInterface
Wrapper for creating and dispatching events.
Returns a dispatched event.
string
$name Name of the event.
array|null
$data optional Any value you wish to be transported with this event to it can be read by listeners.
object|null
$subject optional The object that this event applies to ($this by default).
Cake\Event\EventInterface
flash(string|false $message): void
Set a flash message. Uses the Flash component with values from flash
config.
string|false
$message The message to set. False to skip.
void
getAuthenticate(string $alias): Cake\Auth\BaseAuthenticate|null
Getter for authenticate objects. Will return a particular authenticate object.
string
$alias Alias for the authenticate object
Cake\Auth\BaseAuthenticate|null
getAuthorize(string $alias): Cake\Auth\BaseAuthorize|null
Getter for authorize objects. Will return a particular authorize object.
string
$alias Alias for the authorize object
Cake\Auth\BaseAuthorize|null
getConfig(string|null $key = null, mixed $default = null): mixed
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');
string|null
$key optional The key to get or null for the whole config.
mixed
$default optional The return value when the key does not exist.
mixed
getConfigOrFail(string $key): mixed
Returns the config for this specific key.
The config value for this key must exist, it can never be null.
string
$key The key to get.
mixed
InvalidArgumentException
getController(): Cake\Controller\Controller
Get the controller this component is bound to.
Cake\Controller\Controller
getEventManager(): Cake\Event\EventManagerInterface
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\EventManagerInterface
identify(): array|false
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.
array|false
implementedEvents(): array<string, mixed>
Events supported by this component.
Uses Conventions to map controller events to standard component callback method names. By defining one of the callback methods a component is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want components to listen to non-standard events.
array<string, mixed>
initialize(array<string, mixed> $config): void
Initialize properties.
Implement this method to avoid having to overwrite the constructor and call parent.
array<string, mixed>
$config The config data.
void
isAuthorized(ArrayAccess|array|null $user = null, Cake\Http\ServerRequest|null $request = null): bool
Check if the provided user is authorized for the request.
Uses the configured Authorization adapters to check whether 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.
ArrayAccess|array|null
$user optional The user to check the authorization of. If empty the user fetched from storage will be used.
Cake\Http\ServerRequest|null
$request optional The request to authenticate for. If empty, the current request will be used.
bool
log(string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
string
$message Log message.
string|int
$level optional Error level.
array|string
$context optional Additional log data relevant to this message.
bool
logout(): string
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.
string
redirectUrl(array|string|null $url = null): string
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.array|string|null
$url optional Optional URL to write as the login redirect URL.
string
setConfig(array<string, mixed>|string $key, mixed|null $value = null, bool $merge = true): $this
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']);
array<string, mixed>|string
$key The key to set, or a complete array of configs.
mixed|null
$value optional The value to set.
bool
$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
$this
Cake\Core\Exception\CakeException
setEventManager(Cake\Event\EventManagerInterface $eventManager): $this
Returns the Cake\Event\EventManagerInterface 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\EventManagerInterface
$eventManager the eventManager to set
$this
setUser(ArrayAccess|array $user): void
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().
ArrayAccess|array
$user User data.
void
startup(Cake\Event\EventInterface $event): Cake\Http\Response|null
Callback for Controller.startup event.
Cake\Event\EventInterface
$event Event instance.
Cake\Http\Response|null
storage(Cake\Auth\Storage\StorageInterface|null $storage = null): Cake\Auth\Storage\StorageInterface|null
Get/set user record storage object.
Cake\Auth\Storage\StorageInterface|null
$storage optional Sets provided object as storage or if null returns configured storage object.
Cake\Auth\Storage\StorageInterface|null
user(string|null $key = null): mixed|null
Get the current user from storage.
string|null
$key optional Field to retrieve. Leave null to get entire User record.
mixed|null
Cake\Controller\Component\FlashComponent
Cake\Controller\Component\RequestHandlerComponent
Objects that will be used for authentication checks.
arrayCake\Auth\BaseAuthenticate>
The instance of the Authenticate provider that was used for successfully logging in the current user after calling login()
in the same request
Cake\Auth\BaseAuthenticate|null
The instance of the Authorize provider that was used to grant access to the current user to the URL they are requesting.
Cake\Auth\BaseAuthorize|null
Objects that will be used for authorization checks.
arrayCake\Auth\BaseAuthorize>
A component lookup table used to lazy load component objects.
array<string, array>
Runtime config
array<string, mixed>
Whether the config property has already been configured with defaults
bool
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' ]);
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.
For default value 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.
array<string, mixed>
Default class name for new event objects.
string
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Cake\Event\EventManagerInterface|null
Component registry class used to lazy load components.
Cake\Controller\ComponentRegistry
Storage object.
Cake\Auth\Storage\StorageInterface|null
Controller actions for which user validation is not required.
array<string>
Other components utilized by AuthComponent
array
© 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/4.4/class-Cake.Controller.Component.AuthComponent.html