\User\UtilityAcoManager

A simple class for handling plugin's ACOs.

Usage:

$manager = new AcoManager('PluginName');

You must indicate the plugin to manage.

Summary

Methods
Properties
Constants
__construct()
add()
remove()
buildAcos()
paths()
cache()
dropCache()
No public properties found
No constants found
_controllerMethods()
_parseAco()
_routerPrefixes()
$_pluginName
$_cache
N/A
No private methods found
No private properties found
N/A

Properties

$_pluginName

$_pluginName : string

Name of the plugin being managed.

Type

string

$_cache

$_cache : array

Used internally.

Type

array

Methods

__construct()

__construct(string $pluginName)

Constructor.

Parameters

string $pluginName

The plugin being managed

Throws

\Cake\Error\FatalErrorException

When no plugin name is given.

add()

add(string $path, array $roles) : boolean

Grants permissions to all users within $roles over the given $aco.

ACO path format:

  • ControllerName/: Maps to \\Controller\ControllerName::index()
  • ControllerName: Same.
  • ControllerName/action_name: Maps to \\Controller\ControllerName::action_name()
  • Prefix/ControllerName/action_name: Maps to \\Controller\Prefix\ControllerName::action_name()

Parameters

string $path

ACO path as described above

array $roles

List of user roles to grant access to. If not given, $path cannot be used by anyone but "administrators"

Returns

boolean —

True on success

remove()

remove(string $path) : boolean

Removes the given ACO and its permissions.

Parameters

string $path

ACO path e.g. ControllerName/action_name

Returns

boolean —

True on success, false if path was not found

buildAcos()

buildAcos(string $for, boolean $sync) : boolean

This method should never be used unless you know what are you doing.

Populates the "acos" DB with information of every installed plugin, or for the given plugin. It will automatically extracts plugin's controllers and actions for creating a tree structure as follow:

  • PluginName
    • Admin
    • PrivateController
      • index
      • private_action
    • ControllerName
    • index
    • another_action

After tree is created you should be able to change permissions using User's permissions section in backend.

Parameters

string $for

Optional, build ACOs for the given plugin, or all plugins if not given

boolean $sync

Whether to sync the tree or not. When syncing all invalid ACO entries will be removed from the tree, also new ones will be added. When syn is set to false only new ACO entries will be added, any invalid entry will remain in the tree. Defaults to false

Returns

boolean —

True on success, false otherwise

paths()

paths(string $for) : array

Gets a list of existing ACO paths for the given plugin, or the entire list if no plugin is given.

Parameters

string $for

Optional plugin name. e.g. Taxonomy

Returns

array —

All registered ACO paths

cache()

cache(null|string $key, mixed $value) : mixed

Reads, writes or search internal class's cache.

Usages:

  • When reading if no cache key is found NULL will be returned. e.g. $null = static::cache('invalid-key');
  • When writing, this method return the value that was written. e.g. $value = static::cache('key', 'value');
  • Set both arguments to NULL to read the whole cache content at the moment. e.g. $allCache = static::cache()
  • Set key to null and value to anything to find the first key holding the given value. e.g. $key = static::cache(null, 'search key for this value'), if no key for the given value is found NULL will be returned.

Examples:

Writing cache:

static::cache('user_name', 'John');
// returns 'John'

static::cache('user_last', 'Locke');
// returns 'Locke'

Reading cache:

static::cache('user_name');
// returns: John

static::cache('unexisting_key');
// returns: null

static::cache();
// Reads the entire cache
// returns: ['user_name' => 'John', 'user_last' => 'Locke']

Searching keys:

static::cache(null, 'Locke');
// returns: user_last

static::cache(null, 'Unexisting Value');
// returns: null

Parameters

null|string $key

Cache key to read or write, set both $key and $value to get the whole cache information

mixed $value

Values to write into the given $key, or null indicates reading from cache

Returns

mixed

dropCache()

dropCache(string|null $key) : void

Drops the entire cache or a specific key.

Usage:

static::dropCache('user_cache'); // removes "user_cache" only
static::dropCache(); // removes every key

Parameters

string|null $key

Cache key to clear, if NULL the entire cache will be erased.

_controllerMethods()

_controllerMethods(string $className) : array

Extracts method names of the given controller class.

Parameters

string $className

Fully qualified name

Returns

array —

List of method names

_parseAco()

_parseAco(string $aco, boolean $string) : boolean|array|string

Sanitizes the given ACO path.

This methods can return an array with the following keys if $string option is set to false:

  • plugin: The name of the plugin being managed by this class.
  • prefix: ACO prefix, for example Admin for controller within /Controller/Admin/ it may be empty, if not prefix is found.
  • controller: Controller name. e.g.: MySuperController
  • action: Controller's action. e.g.: mini_action, index by default

For example:

`Admin/Users/`

Returns:

  • plugin: YourPlugin
  • prefix: Admin
  • controller: Users
  • action: index

Where "YourPlugin" is the plugin name passed to this class's constructor.

Parameters

string $aco

An ACO path to parse

boolean $string

Indicates if it should return a string format path (/Controller/action)

Returns

boolean|array|string —

An array as described above or false if an invalid $aco was given

_routerPrefixes()

_routerPrefixes() : array

Gets a CamelizedList of all existing router prefixes.

Returns

array