Class SecurityComponent
The Security Component creates an easy way to integrate tighter security in your application. It provides methods for various tasks like:
- Restricting which HTTP methods your application accepts.
- Form tampering protection
- Requiring that SSL be used.
- Limiting cross controller communication.
Link: http://book.cakephp.org/3.0/en/controllers/components/security.html
Property Summary
-
$_action protected
string
Holds the current action of the controller
-
$_componentMap protected
array
A component lookup table used to lazy load component objects.
-
$_config protected
array
Runtime config
-
$_configInitialized protected
bool
Whether the config property has already been configured with defaults
-
$_defaultConfig protected
array
Default config
-
$_registry protected
Cake\Controller\ComponentRegistry
Component registry class used to lazy load components.
-
$components public
array
Other Components this component uses.
-
$request public
Cake\Network\Request
Request object
-
$response public
Cake\Network\Response
Response object
-
$session public
Cake\Network\Session
The Session object
Method Summary
-
__construct() public
Constructor
-
__debugInfo() public
Returns an array that can be used to describe the internal state of this object.
-
__get() public
Magic method for lazy loading $components.
-
_authRequired() protected
Check if authentication is required
-
_callback() protected
Calls a controller callback method
-
_configDelete() protected
Delete a single config key
-
_configRead() protected
Read a config variable
-
_configWrite() protected
Write a config variable
-
_requireMethod() protected
Sets the actions that require a $method HTTP request, or empty for all actions
-
_secureRequired() protected
Check if access requires secure connection
-
_validatePost() protected
Validate submitted form
-
blackHole() public
Black-hole an invalid request with a 400 error or custom callback. If SecurityComponent::$blackHoleCallback is specified, it will use this callback by executing the method indicated in $error
-
config() public
Usage
-
configShallow() public
Merge provided config with existing config. Unlike
config()
which does a recursive merge for nested keys, this method does a simple merge. -
generateToken() public
Manually add form tampering prevention token information into the provided request object.
-
implementedEvents() public
Events supported by this component.
-
initialize() public
Constructor hook method.
-
log() public
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
-
requireAuth() public
Sets the actions that require whitelisted form submissions.
-
requireSecure() public
Sets the actions that require a request that is SSL-secured, or empty for all actions
-
startup() public
Component startup. All security checking happens here.
Method Detail
__construct() ¶ public
__construct(Cake\Controller\ComponentRegistry $registry, array $config = [])
Constructor
Parameters
-
Cake\Controller\ComponentRegistry
$registry A ComponentRegistry this component can use to lazy load its components
-
array
$config optional Array of configuration settings.
__debugInfo() ¶ public
__debugInfo(): array
Returns an array that can be used to describe the internal state of this object.
Returns
array
__get() ¶ public
__get(string $name): mixed
Magic method for lazy loading $components.
Parameters
-
string
$name Name of component to get.
Returns
mixed
_authRequired() ¶ protected
_authRequired(Cake\Controller\Controller $controller): bool
Check if authentication is required
Parameters
-
Cake\Controller\Controller
$controller Instantiating controller
Returns
bool
_callback() ¶ protected
_callback(Cake\Controller\Controller $controller, string $method, array $params = []): mixed
Calls a controller callback method
Parameters
-
Cake\Controller\Controller
$controller Controller to run callback on
-
string
$method Method to execute
-
array
$params optional Parameters to send to method
Returns
mixed
Throws
Cake\Network\Exception\BadRequestException
When a the blackholeCallback is not callable.
_configDelete() ¶ protected
_configDelete(string $key): void
Delete a single config key
Parameters
-
string
$key Key to delete.
Returns
void
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
_configRead() ¶ protected
_configRead(string|null $key): mixed
Read a config variable
Parameters
-
string|null
$key Key to read.
Returns
mixed
_configWrite() ¶ protected
_configWrite(string|array $key, mixed $value, bool|string $merge = false): void
Write a config variable
Parameters
-
string|array
$key Key to write to.
-
mixed
$value Value to write.
-
bool|string
$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
void
Throws
Cake\Core\Exception\Exception
if attempting to clobber existing config
_requireMethod() ¶ protected
_requireMethod(string $method, array $actions = []): void
Sets the actions that require a $method HTTP request, or empty for all actions
Parameters
-
string
$method The HTTP method to assign controller actions to
-
array
$actions optional Controller actions to set the required HTTP method to.
Returns
void
_secureRequired() ¶ protected
_secureRequired(Cake\Controller\Controller $controller): bool
Check if access requires secure connection
Parameters
-
Cake\Controller\Controller
$controller Instantiating controller
Returns
bool
_validatePost() ¶ protected
_validatePost(Cake\Controller\Controller $controller): bool
Validate submitted form
Parameters
-
Cake\Controller\Controller
$controller Instantiating controller
Returns
bool
blackHole() ¶ public
blackHole(Cake\Controller\Controller $controller, string $error = ''): mixed
Black-hole an invalid request with a 400 error or custom callback. If SecurityComponent::$blackHoleCallback is specified, it will use this callback by executing the method indicated in $error
Parameters
-
Cake\Controller\Controller
$controller Instantiating controller
-
string
$error optional Error method
Returns
mixed
Throws
Cake\Network\Exception\BadRequestException
See Also
Links
config() ¶ public
config(string|array|null $key = null, mixed|null $value = null, bool $merge = true): mixed
Usage
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']);
Parameters
-
string|array|null
$key optional The key to get/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.
Returns
mixed
Throws
Cake\Core\Exception\Exception
When trying to set a key that is invalid.
configShallow() ¶ public
configShallow(string|array $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->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']);
Parameters
-
string|array
$key The key to set, or a complete array of configs.
-
mixed|null
$value optional The value to set.
Returns
$this
generateToken() ¶ public
generateToken(Cake\Network\Request $request): bool
Manually add form tampering prevention token information into the provided request object.
Parameters
-
Cake\Network\Request
$request The request object to add into.
Returns
bool
implementedEvents() ¶ public
implementedEvents(): array
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.
Returns
array
initialize() ¶ public
initialize(array $config): void
Constructor hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
Parameters
-
array
$config The configuration settings provided to this component.
Returns
void
log() ¶ public
log(mixed $msg, int|string $level = LogLevel::ERROR, string|array $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Parameters
-
mixed
$msg Log message.
-
int|string
$level optional Error level.
-
string|array
$context optional Additional log data relevant to this message.
Returns
bool
requireAuth() ¶ public
requireAuth(string|array $actions): void
Sets the actions that require whitelisted form submissions.
Adding actions with this method will enforce the restrictions set in SecurityComponent::$allowedControllers and SecurityComponent::$allowedActions.
Parameters
-
string|array
$actions Actions list
Returns
void
requireSecure() ¶ public
requireSecure(string|array $actions = null): void
Sets the actions that require a request that is SSL-secured, or empty for all actions
Parameters
-
string|array
$actions optional Actions list
Returns
void
startup() ¶ public
startup(Cake\Event\Event $event): mixed
Component startup. All security checking happens here.
Parameters
-
Cake\Event\Event
$event An Event instance
Returns
mixed
Property Detail
$_configInitialized ¶ protected
Whether the config property has already been configured with defaults
Type
bool
$_defaultConfig ¶ protected
Default config
blackHoleCallback
- The controller method that will be called if this request is black-hole'd.requireSecure
- List of actions that require an SSL-secured connection.requireAuth
- List of actions that require a valid authentication key.allowedControllers
- Controllers from which actions of the current controller are allowed to receive requests.allowedActions
- Actions from which actions of the current controller are allowed to receive requests.unlockedFields
- Form fields to exclude from POST validation. Fields can be unlocked either in the Component, or with FormHelper::unlockField(). Fields that have been unlocked are not required to be part of the POST and hidden unlocked fields do not have their values checked.unlockedActions
- Actions to exclude from POST validation checks. Other checks like requireAuth(), requireSecure() etc. will still be applied.validatePost
- Whether to validate POST data. Set to false to disable for data coming from 3rd party services, etc.
Type
array
$_registry ¶ protected
Component registry class used to lazy load components.
Type
Cake\Controller\ComponentRegistry