ControllerAuthorize
extends BaseAuthorize
in package
An authorization adapter for AuthComponent. Provides the ability to authorize using a controller callback. Your controller's isAuthorized() method should return a boolean to indicate whether the user is authorized.
public function isAuthorized($user)
{
if ($this->request->getParam('admin')) {
return $user['role'] === 'admin';
}
return !empty($user);
}
The above is simple implementation that would only authorize users of the 'admin' role to access admin routing.
Tags
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_Controller : Controller
- Controller for the request.
- $_defaultConfig : array<string, mixed>
- Default config for authorize objects.
- $_registry : ComponentRegistry
- ComponentRegistry instance for getting more components.
Methods
- __construct() : mixed
- Constructor
- authorize() : bool
- Checks user authorization using a controller callback.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- controller() : Controller
- Get/set the controller this authorize object will be working with. Also checks that isAuthorized is implemented.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- setConfig() : $this
- Sets the config.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_Controller
Controller for the request.
protected
Controller
$_Controller
$_defaultConfig
Default config for authorize objects.
protected
array<string, mixed>
$_defaultConfig
= []
$_registry
ComponentRegistry instance for getting more components.
protected
ComponentRegistry
$_registry
Methods
__construct()
Constructor
public
__construct(ComponentRegistry $registry[, array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $registry : ComponentRegistry
-
The controller for this request.
- $config : array<string|int, mixed> = []
-
An array of config. This class does not use any config.
Tags
authorize()
Checks user authorization using a controller callback.
public
authorize(ArrayAccess|array<string|int, mixed> $user, ServerRequest $request) : bool
Parameters
- $user : ArrayAccess|array<string|int, mixed>
-
Active user data
- $request : ServerRequest
-
Request instance.
Tags
Return values
boolconfigShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
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']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thiscontroller()
Get/set the controller this authorize object will be working with. Also checks that isAuthorized is implemented.
public
controller([Controller|null $controller = null ]) : Controller
Parameters
- $controller : Controller|null = null
-
null to get, a controller to set.
Return values
ControllergetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
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');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
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']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$this_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.