Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

Checking Permissions: The ACL Component

Let's use the AclComponent to make sure dwarves and elves can't remove things from the armory. At this point, we should be able to use the AclComponent to make a check between the ACOs and AROs we've created. The basic syntax for making a permissions check is:

Простой текст
  1. $this->Acl->check( $aro, $aco, $action = '*');

Let's give it a try inside a controller action:

Простой текст
  1. function index()
  2. {
  3. //These all return true:
  4. $this->Acl->check('warriors/Aragorn', 'Weapons');
  5. $this->Acl->check('warriors/Aragorn', 'Weapons', 'create');
  6. $this->Acl->check('warriors/Aragorn', 'Weapons', 'read');
  7. $this->Acl->check('warriors/Aragorn', 'Weapons', 'update');
  8. $this->Acl->check('warriors/Aragorn', 'Weapons', 'delete');
  9. //Remember, we can use the model/foreign key syntax
  10. //for our user AROs
  11. $this->Acl->check(array('model' => 'User', 'foreign_key' => 2356), 'Weapons');
  12. //These also return true:
  13. $result = $this->Acl->check('warriors/Legolas', 'Weapons', 'create');
  14. $result = $this->Acl->check('warriors/Gimli', 'Weapons', 'read');
  15. //But these return false:
  16. $result = $this->Acl->check('warriors/Legolas', 'Weapons');
  17. $result = $this->Acl->check('warriors/Gimli', 'Weapons', 'delete');
  18. }

The usage here is demonstrational, but hopefully you can see how checking like this can be used to decide whether or not to allow something to happen, show an error message, or redirect the user to a login.