Class Phalcon\Acl\Adapter\Memory

extends class Phalcon\Acl\Adapter

Manages ACL lists in memory

  1. <?php
  2. $acl = new \Phalcon\Acl\Adapter\Memory();
  3. $acl->setDefaultAction(Phalcon\Acl::DENY);
  4. //Register roles
  5. $roles = array(
  6. 'users' => new \Phalcon\Acl\Role('Users'),
  7. 'guests' => new \Phalcon\Acl\Role('Guests')
  8. );
  9. foreach ($roles as $role) {
  10. $acl->addRole($role);
  11. }
  12. //Private area resources
  13. $privateResources = array(
  14. 'companies' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
  15. 'products' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
  16. 'invoices' => array('index', 'profile')
  17. );
  18. foreach ($privateResources as $resource => $actions) {
  19. $acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
  20. }
  21. //Public area resources
  22. $publicResources = array(
  23. 'index' => array('index'),
  24. 'about' => array('index'),
  25. 'session' => array('index', 'register', 'start', 'end'),
  26. 'contact' => array('index', 'send')
  27. );
  28. foreach ($publicResources as $resource => $actions) {
  29. $acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
  30. }
  31. //Grant access to public areas to both users and guests
  32. foreach ($roles as $role){
  33. foreach ($publicResources as $resource => $actions) {
  34. $acl->allow($role->getName(), $resource, '*');
  35. }
  36. }
  37. //Grant access to private area to role Users
  38. foreach ($privateResources as $resource => $actions) {
  39. foreach ($actions as $action) {
  40. $acl->allow('Users', $resource, $action);
  41. }
  42. }

Methods

public __construct ()

Phalcon\Acl\Adapter\Memory constructor

public boolean addRole (unknown $role, [unknown $accessInherits])

Adds a role to the ACL list. Second parameter allows inheriting access data from other existing role Example:

  1. <?php
  2. $acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultant');
  3. $acl->addRole('administrator', 'consultant');

public addInherit (unknown $roleName, unknown $roleToInherit)

Do a role inherit from another existing role

public boolean isRole (unknown $roleName)

Check whether role exist in the roles list

public boolean isResource (unknown $resourceName)

Check whether resource exist in the resources list

public boolean addResource (unknown $resourceValue, unknown $accessList)

Adds a resource to the ACL list Access names can be a particular action, by example search, update, delete, etc or a list of them Example:

  1. <?php
  2. //Add a resource to the the list allowing access to an action
  3. $acl->addResource(new Phalcon\Acl\Resource('customers'), 'search');
  4. $acl->addResource('customers', 'search');
  5. //Add a resource with an access list
  6. $acl->addResource(new Phalcon\Acl\Resource('customers'), array('create', 'search'));
  7. $acl->addResource('customers', array('create', 'search'));

public boolean addResourceAccess (unknown $resourceName, unknown $accessList)

Adds access to resources

public dropResourceAccess (unknown $resourceName, unknown $accessList)

Removes an access from a resource

protected _allowOrDeny (unknown $roleName, unknown $resourceName, unknown $access, unknown $action)

Checks if a role has access to a resource

public allow (unknown $roleName, unknown $resourceName, unknown $access)

Allow access to a role on a resource You can use ‘*’ as wildcard Example:

  1. <?php
  2. //Allow access to guests to search on customers
  3. $acl->allow('guests', 'customers', 'search');
  4. //Allow access to guests to search or create on customers
  5. $acl->allow('guests', 'customers', array('search', 'create'));
  6. //Allow access to any role to browse on products
  7. $acl->allow('*', 'products', 'browse');
  8. //Allow access to any role to browse on any resource
  9. $acl->allow('*', '*', 'browse');

public boolean deny (unknown $roleName, unknown $resourceName, unknown $access)

Deny access to a role on a resource You can use ‘*’ as wildcard Example:

  1. <?php
  2. //Deny access to guests to search on customers
  3. $acl->deny('guests', 'customers', 'search');
  4. //Deny access to guests to search or create on customers
  5. $acl->deny('guests', 'customers', array('search', 'create'));
  6. //Deny access to any role to browse on products
  7. $acl->deny('*', 'products', 'browse');
  8. //Deny access to any role to browse on any resource
  9. $acl->deny('*', '*', 'browse');

public boolean isAllowed (unknown $roleName, unknown $resourceName, unknown $access)

Check whether a role is allowed to access an action from a resource

  1. <?php
  2. //Does andres have access to the customers resource to create?
  3. $acl->isAllowed('andres', 'Products', 'create');
  4. //Do guests have access to any resource to edit?
  5. $acl->isAllowed('guests', '*', 'edit');

public Phalcon\Acl\Role [] getRoles ()

Return an array with every role registered in the list

public Phalcon\Acl\Resource [] getResources ()

Return an array with every resource registered in the list

public getActiveRole () inherited from Phalcon\Acl\Adapter

Role which the list is checking if it’s allowed to certain resource/access

public getActiveResource () inherited from Phalcon\Acl\Adapter

Resource which the list is checking if some role can access it

public getActiveAccess () inherited from Phalcon\Acl\Adapter

Active access which the list is checking if some role can access it

public setEventsManager (unknown $eventsManager) inherited from Phalcon\Acl\Adapter

Sets the events manager

public Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\Acl\Adapter

Returns the internal event manager

public setDefaultAction (unknown $defaultAccess) inherited from Phalcon\Acl\Adapter

Sets the default access level (Phalcon\Acl::ALLOW or Phalcon\Acl::DENY)

public int getDefaultAction () inherited from Phalcon\Acl\Adapter

Returns the default ACL access level