The ACL and auth in cakephp

Source: Internet
Author: User
Tags acos
ACL and Auth in cakephp

Since learning cakePHP , has not finished understanding the ACL, but also checked a lot of information, but the use of it is smattering, ACL should be a more difficult to understand cakephp, these days again read the manual, found that the ACL does not believe in the difficult, But it's better than I thought. Welcome to reprint and visit my website http://www.batterylaptops.co.uk
Now let me say that the specific usage of it should be noticed in the process of using the problem
Pre-preparation work:
It is best to be able to configure the bake, so that the bake name is valid, although this is not necessary, but there is this command-line tool we will be more convenient
Import the following SQL statement in your database

  1. CREATE TABLE Users (
  2. ? ? ID INT (one) not NULL auto_increment PRIMARY KEY,
  3. ? ?? ??? Username VARCHAR (255) is not NULL UNIQUE,
  4. ? ? Password CHAR (+) not NULL,
  5. ? ? group_id INT (one) is not NULL,
  6. ? ? Created DATETIME,
  7. ? ? Modified DATETIME
  8. );


  9. CREATE TABLE groups (
  10. ? ? ID INT (one) not NULL auto_increment PRIMARY KEY,
  11. ? ? Name VARCHAR (+) is not NULL,
  12. ? ? Created DATETIME,
  13. ? ? Modified DATETIME
  14. );


  15. CREATE TABLE Posts (
  16. ? ? ID INT (one) not NULL auto_increment PRIMARY KEY,
  17. ? ? USER_ID INT (one) is not NULL,
  18. ? ? Title VARCHAR (255) Not NULL,
  19. ? ? Body TEXT,
  20. ? ? Created DATETIME,
  21. ? ? Modified DATETIME
  22. );

  23. CREATE TABLE Widgets (
  24. ? ? ID INT (one) not NULL auto_increment PRIMARY KEY,
  25. ? ? Name VARCHAR (+) is not NULL,
  26. ? ? Part_no VARCHAR (12),
  27. ? ? Quantity INT (11)
  28. );

Here we use the Cake bake all command tool to quickly generate models, controllers, and views (here I'll explain in detail how to use the bake command in cakephp)


Next, prepare to use Auth component certification
First we open the users_controller.php in the Userscontroller class to add login logout action

    1. function Login () {
    2. ? ? Auth Magic
    3. }

    4. function logout () {
    5. ? ? Leave empty for now.
    6. }
    7. Then create the view file APP/VIEWS/USERS/LOGIN.CTP
    8. $session->flash (' auth ');
    9. echo $form->create (' User ', Array (' action ' = ' login ');
    10. echo $form->inputs (Array (
    11. ? ?? ???' Legend ' = ' + __ (' Login ', true),
    12. ? ?? ???' Username ',
    13. ? ?? ???' Password
    14. ));
    15. echo $form->end (' Login ');

    16. ?>

Next we need to modify AppController (/app/app_controller.php), if you do not have app_controller.php file under the app directory, you can create a

    1. Class AppController extends Controller {
    2. ? ? var $components = array (' ACL ', ' Auth ');

    3. ? ? function Beforefilter () {
    4. ? ?? ??? Configure authcomponent
    5. ? ?? ??? $this->auth->authorize = ' actions ';
    6. ? ?? ??? $this->auth->loginaction = Array (' controller ' = ' users ', ' action ' = ' login ');
    7. ? ?? ??? $this->auth->logoutredirect = Array (' controller ' = ' users ', ' action ' = ' login ');
    8. ? ?? ??? $this->auth->loginredirect = Array (' controller ' = ' posts ', ' action ' = ' add ');
    9. ? ? }
    10. }
    11. ?>

Next we need to modify the Groupscontroller and Userscontroller, the two files directory everyone should know where it is.
Add the following code to the two controllers separately

    1. function Beforefilter () {
    2. ? ? Parent::beforefilter ();
    3. ? ? $this->auth->allowedactions = Array (' * ');
    4. }

In fact, this code is meant to allow users to access all the user and group action, of course, this is to be changed back

Next we're going to initialize the ACL table
Because now we have only four tables in our database, and we haven't imported ACL tables.
We use the following statement to import the ACL table into the database
On the command line, enter cake schema run Create Dbacl

We follow the prompts to import the table

Next we need to modify the user and group model
First we open the user.php in the Model directory and add the following code (in fact you can replace it with the following code)

    1. var $name = ' User ';
    2. var $belongsTo = array (' Group ');
    3. var $actsAs = array (' Acl ' = ' requester ');

    4. function ParentNode () {
    5. ? ? if (! $this->id && Empty ($this->data)) {
    6. ? ?? ??? return null;
    7. ? ? }
    8. ? ? $data = $this->data;
    9. ? ? if (Empty ($this->data)) {
    10. ? ?? ??? $data = $this->read ();
    11. ? ? }
    12. ? ? if (! $data [' User '] [' group_id ']) {
    13. ? ?? ??? return null;
    14. ? ? } else {
    15. ? ?? ??? Return Array (' Group ' = = Array (' id ' = = $data [' User '] [' group_id ']);
    16. ? ? }
    17. }

Copy Code

Then modify the group model

    1. var $actsAs = array (' Acl ' = = Array (' requester '));

    2. function ParentNode () {
    3. ? ? return null;
    4. }

Well, to this point we need to temporarily stop, now in the browser open the corresponding user and group page, add user and group, such as my, I opened http://localhost/cakephp/groups Add Group, open HTTP// localhost/cakephp/users/Add users, here we add three groups and three users

After adding it, you can open the phpMyAdmin and look at the Aros table to see if there are any more records.

It's amazing, isn't it? Haha, this is the charm of the frame, so far the table related to the ACL, only the records exist in the Aros table

We also have to modify the Aros table records when we modify each of the user users later
All we should do is add the following code to the user model

    1. /**? ?
    2. * After Save callback
    3. *
    4. * Update The ARO for the user.
    5. *
    6. * @access Public
    7. * @return void
    8. */
    9. function Aftersave ($created) {
    10. ? ?? ??? if (! $created) {
    11. ? ?? ?? ?? ? $parent = $this->parentnode ();
    12. ? ?? ?? ?? ? $parent = $this->node ($parent);
    13. ? ?? ?? ?? ? $node = $this->node ();
    14. ? ?? ?? ?? ? $aro = $node [0];
    15. ? ?? ?? ?? ? $aro [' Aro '] [' parent_id '] = $parent [0][' Aro '] [' id '];
    16. ? ?? ?? ?? ? $this->aro->save ($aro);
    17. ? ?? ???}
    18. }

Here we have ARO created, and then we're going to create a good aco.
We should know that the nature of the ACL is used to define when an ARO can access an ACO component, and it becomes easy to understand.
For simplicity we use the command line to execute the cake ACL create ACO root controllers

You can now open the Acors table with phpMyAdmin, you should see that there is already a record in the table, this record is the result of just executing this command, of course, we do not have to use the command line tool.

Next we need to modify the next AppController, in the inside of the Beforefilter method we need to add a line $this->auth->actionpath = ' controllers/';

Here are the main points, there may be many controllers and methods in our app project, we need to add each action to the Acors table for permission control, of course, if you are not afraid of trouble can be a manual add, but I think most programmers are still very lazy, All we need here to use the Auto build tool

Here we add the following code in the users_controller.php, of course you can also put in the other controller
I'll add the following code to the users_controller.php

  1. function Build_acl () {
  2. ? ?? ?? ?? ?? ? if (! Configure::read (' Debug ')) {
  3. ? ?? ?? ?? ?? ?? ?? ?? ? return $this->_stop ();
  4. ? ?? ?? ?? ?? ? }
  5. ? ?? ?? ?? ?? ? $log = Array ();

  6. ? ?? ?? ?? ?? ? $aco =& $this->acl->aco;
  7. ? ?? ?? ?? ?? ? $root = $aco->node (' controllers ');
  8. ? ?? ?? ?? ?? ? if (! $root) {
  9. ? ?? ?? ?? ?? ?? ?? ?? ? $aco->create (' parent_id ' = null, ' model ' = null, ' Alias ' = ' controllers '));
  10. ? ?? ?? ?? ?? ?? ?? ?? ? $root = $aco->save ();
  11. ? ?? ?? ?? ?? ?? ?? ?? ? $root [' Aco '] [' id '] = $aco->id;
  12. ? ?? ?? ?? ?? ?? ?? ?? ? $log [] = ' Created Aco node for controllers ';
  13. ? ?? ?? ?? ?? ? } else {
  14. ? ?? ?? ?? ?? ?? ?? ?? ? $root = $root [0];
  15. ? ?? ?? ?? ?? ? }? ?

  16. ? ?? ?? ?? ?? ? App::import (' Core ', ' File ');
  17. ? ?? ?? ?? ?? ? $Controllers = configure::listobjects (' controller ');
  18. ? ?? ?? ?? ?? ? $appIndex = Array_search (' App ', $Controllers);
  19. ? ?? ?? ?? ?? ? if ($appIndex!== false) {
  20. ? ?? ?? ?? ?? ?? ?? ?? Unset ($Controllers [$appIndex]);
  21. ? ?? ?? ?? ?? ? }
  22. ? ?? ?? ?? ?? ? $baseMethods = Get_class_methods (' Controller ');
  23. ? ?? ?? ?? ?? ? $baseMethods [] = ' buildacl ';

  24. ? ?? ?? ?? ?? ? $Plugins = $this->_getplugincontrollernames ();
  25. ? ?? ?? ?? ?? ? $Controllers = Array_merge ($Controllers, $Plugins);

  26. ? ?? ?? ?? ?? ? Look at all controller in App/controllers
  27. ? ?? ?? ?? ?? ? foreach ($Controllers as $ctrlName) {
  28. ? ?? ?? ?? ?? ?? ?? ?? ? $methods = $this->_getclassmethods ($this->_getplugincontrollerpath ($ctrlName));

  29. ? ?? ?? ?? ?? ?? ?? ?? ?//Do all Plugins first
  30. ? ?? ?? ?? ?? ?? ?? ?? ? if ($this->_isplugin ($ctrlName)) {
  31. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $pluginNode = $aco->node (' controllers/'. $this->_getpluginname ($ctrlName));
  32. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if (! $pluginNode) {
  33. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $aco->create (' parent_id ' + $root [' aco '] [' id '], ' model ' = null, ' Alias ' + $this->_getpluginname ($ctrlName)));
  34. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $pluginNode = $aco->save ();
  35. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $pluginNode [' Aco '] [' id '] = $aco->id;
  36. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $log [] = ' Created Aco node for '. $this->_getpluginname ($ctrlName). ' Plugin ';
  37. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  38. ? ?? ?? ?? ?? ?? ?? ?? ?}
  39. ? ?? ?? ?? ?? ?? ?? ?? ?//Find/make Controller node
  40. ? ?? ?? ?? ?? ?? ?? ?? ? $controllerNode = $aco->node (' controllers/'. $ctrlName);
  41. ? ?? ?? ?? ?? ?? ?? ?? ? if (! $controllerNode) {
  42. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if ($this->_isplugin ($ctrlName)) {
  43. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $pluginNode = $aco->node (' controllers/'. $this->_getpluginname ($ctrlName));
  44. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $aco->create (' parent_id ' = ' $pluginNode [' 0 '] [' aco '] [' id '], ' model ' = null, ' Alias ' = $this->_ Getplugincontrollername ($ctrlName)));
  45. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $controllerNode = $aco->save ();
  46. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $controllerNode [' Aco '] [' id '] = $aco->id;
  47. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $log [] = ' Created Aco node for '. $this->_getplugincontrollername ($ctrlName). ' ' . $this->_getpluginname ($ctrlName). ' Plugin Controller ';
  48. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???} else {
  49. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $aco->create (' parent_id ' + $root [' aco '] [' id '], ' model ' = null, ' Alias ' = $ctrlName));
  50. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $controllerNode = $aco->save ();
  51. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $controllerNode [' Aco '] [' id '] = $aco->id;
  52. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $log [] = ' Created Aco node for '. $ctrlName;
  53. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  54. ? ?? ?? ?? ?? ?? ?? ?? ?} else {
  55. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $controllerNode = $controllerNode [0];
  56. ? ?? ?? ?? ?? ?? ?? ?? ?}

  57. ? ?? ?? ?? ?? ?? ?? ?? ?//clean the methods. To remove those in Controller and private actions.
  58. ? ?? ?? ?? ?? ?? ?? ?? ? foreach ($methods as $k = = $method) {
  59. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if (Strpos ($method, ' _ ', 0) = = = 0) {
  60. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Unset ($methods [$k]);
  61. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Continue
  62. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  63. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if (In_array ($method, $baseMethods)) {
  64. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Unset ($methods [$k]);
  65. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Continue
  66. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  67. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $methodNode = $aco->node (' controllers/'. $ctrlName. ' /'. $method);
  68. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if (! $methodNode) {
  69. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $aco->create (' parent_id ' + $controllerNode [' aco '] [' id '], ' model ' = null, ' Alias ' = $method));
  70. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $methodNode = $aco->save ();
  71. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $log [] = ' Created Aco node for '. $method;
  72. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  73. ? ?? ?? ?? ?? ?? ?? ?? ?}
  74. ? ?? ?? ?? ?? ? }
  75. ? ?? ?? ?? ?? ? if (count ($log) >0) {
  76. ? ?? ?? ?? ?? ?? ?? ?? ? debug ($log);
  77. ? ?? ?? ?? ?? ? }
  78. ? ?? ???}

  79. ? ?? ??? function _getclassmethods ($ctrlName = null) {
  80. ? ?? ?? ?? ?? ? App::import (' Controller ', $ctrlName);
  81. ? ?? ?? ?? ?? ? if (strlen (Strstr ($ctrlName, '. ')) > 0) {
  82. ? ?? ?? ?? ?? ?? ?? ?? ?//Plugin ' s controller
  83. ? ?? ?? ?? ?? ?? ?? ?? ? $num = Strpos ($ctrlName, '. ');
  84. ? ?? ?? ?? ?? ?? ?? ?? ? $ctrlName = substr ($ctrlName, $num + 1);
  85. ? ?? ?? ?? ?? ? }
  86. ? ?? ?? ?? ?? ? $ctrlclass = $ctrlName. ' Controller ';
  87. ? ?? ?? ?? ?? ? $methods = Get_class_methods ($ctrlclass);

  88. ? ?? ?? ?? ?? ? ADD Scaffold Defaults If scaffolds is being used
  89. ? ?? ?? ?? ?? ? $properties = Get_class_vars ($ctrlclass);
  90. ? ?? ?? ?? ?? ? if (array_key_exists (' scaffold ', $properties)) {
  91. ? ?? ?? ?? ?? ?? ?? ?? if ($properties [' scaffold '] = = ' admin ') {
  92. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $methods = Array_merge ($methods, Array (' Admin_add ', ' admin_edit ', ' admin_index ', ' admin_view ', ' admin_delete '));
  93. ? ?? ?? ?? ?? ?? ?? ?? ?} else {
  94. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $methods = Array_merge ($methods, Array (' Add ', ' edit ', ' index ', ' View ', ' delete '));
  95. ? ?? ?? ?? ?? ?? ?? ?? ?}
  96. ? ?? ?? ?? ?? ? }
  97. ? ?? ?? ?? ?? ? return $methods;
  98. ? ?? ???}

  99. ? ?? ??? function _isplugin ($ctrlName = null) {
  100. ? ?? ?? ?? ?? ? $arr = String::tokenize ($ctrlName, '/');
  101. ? ?? ?? ?? ?? ? if (count ($arr) > 1) {
  102. ? ?? ?? ?? ?? ?? ?? ?? ? return true;
  103. ? ?? ?? ?? ?? ? } else {
  104. ? ?? ?? ?? ?? ?? ?? ?? ? return false;
  105. ? ?? ?? ?? ?? ? }
  106. ? ?? ???}

  107. ? ?? ??? function _getplugincontrollerpath ($ctrlName = null) {
  108. ? ?? ?? ?? ?? ? $arr = String::tokenize ($ctrlName, '/');
  109. ? ?? ?? ?? ?? ? if (count ($arr) = = 2) {
  110. ? ?? ?? ?? ?? ?? ?? ?? return $arr [0]. '.' . $arr [1];
  111. ? ?? ?? ?? ?? ? } else {
  112. ? ?? ?? ?? ?? ?? ?? ?? return $arr [0];
  113. ? ?? ?? ?? ?? ? }
  114. ? ?? ???}

  115. ? ?? ??? function _getpluginname ($ctrlName = null) {
  116. ? ?? ?? ?? ?? ? $arr = String::tokenize ($ctrlName, '/');
  117. ? ?? ?? ?? ?? ? if (count ($arr) = = 2) {
  118. ? ?? ?? ?? ?? ?? ?? ?? return $arr [0];
  119. ? ?? ?? ?? ?? ? } else {
  120. ? ?? ?? ?? ?? ?? ?? ?? ? return false;
  121. ? ?? ?? ?? ?? ? }
  122. ? ?? ???}

  123. ? ?? ??? function _getplugincontrollername ($ctrlName = null) {
  124. ? ?? ?? ?? ?? ? $arr = String::tokenize ($ctrlName, '/');
  125. ? ?? ?? ?? ?? ? if (count ($arr) = = 2) {
  126. ? ?? ?? ?? ?? ?? ?? ?? return $arr [1];
  127. ? ?? ?? ?? ?? ? } else {
  128. ? ?? ?? ?? ?? ?? ?? ?? ? return false;
  129. ? ?? ?? ?? ?? ? }
  130. ? ?? ???}

  131. /**
  132. * Get the names of the plugin controllers ...
  133. *
  134. * This function would get an array of the plugin controller names, and
  135. * Also makes sure the controllers is available for us to get the
  136. * Method names by doing a app::import for each plugin controller.
  137. *
  138. * @return array of plugin names.
  139. *
  140. */
  141. ? ?? ??? function _getplugincontrollernames () {
  142. ? ?? ?? ?? ?? ? App::import (' Core ', ' File ', ' Folder ');
  143. ? ?? ?? ?? ?? ? $paths = Configure::getinstance ();
  144. ? ?? ?? ?? ?? ? $folder =& new Folder ();
  145. ? ?? ?? ?? ?? ? $folder->CD (APP. ' Plugins ');

  146. ? ?? ?? ?? ?? ? Get the list of plugins
  147. ? ?? ?? ?? ?? ? $Plugins = $folder->read ();
  148. ? ?? ?? ?? ?? ? $Plugins = $Plugins [0];
  149. ? ?? ?? ?? ?? ? $arr = Array ();

  150. ? ?? ?? ?? ?? ? Loop through the plugins
  151. ? ?? ?? ?? ?? ? foreach ($Plugins as $pluginName) {
  152. ? ?? ?? ?? ?? ?? ?? ?? ?//change directory to the plugin
  153. ? ?? ?? ?? ?? ?? ?? ?? ? $didCD = $folder->cd (APP. ' Plugins '. Ds. $pluginName. Ds. ' Controllers ');
  154. ? ?? ?? ?? ?? ?? ?? ?? ?//Get A list of the files that has a file name that ends
  155. ? ?? ?? ?? ?? ?? ?? ?? ?//With controller.php
  156. ? ?? ?? ?? ?? ?? ?? ?? ? $files = $folder->findrecursive ('. *_controller\.php ');

  157. ? ?? ?? ?? ?? ?? ?? ?? ?//Loop through the controllers we found in the plugins directory
  158. ? ?? ?? ?? ?? ?? ?? ?? ? foreach ($files as $fileName) {
  159. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? Get the base file name
  160. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $file = basename ($fileName);

  161. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? Get the controller name
  162. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? $file = Inflector::camelize (substr ($file, 0, strlen ($file)-strlen (' _controller.php '));
  163. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??? if (!preg_match ('/^ '). Inflector::humanize ($pluginName). ' app/', $file)) {
  164. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? if (! App::import (' Controller ', $pluginName. '. $file)) {
  165. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? Debug (' Error importing '. $file. ' For plugin '. $pluginName);
  166. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? } else {
  167. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//Now prepend the Plugin name ...
  168. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//This is required to allow us to fetch the method names.
  169. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? $arr [] = Inflector::humanize ($pluginName). "/" . $file;
  170. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? }
  171. ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???}
  172. ? ?? ?? ?? ?? ?? ?? ?? ?}
  173. ? ?? ?? ?? ?? ? }
  174. ? ?? ?? ?? ?? ? return $arr;
  175. ? ?? ???}

Add good later we open the browser to access just the method
Like my http://localhost/cakephp/users/build_acl.
After running the program will automatically add all controller action to the ACOs table, you can now open the ACOs table should see a lot of records, such as


Next should be doing our most exciting thing, is to achieve the authority control, because we are ready to work in the early stage, and ultimately we are in order to achieve control permissions

1.??? ??? First introduce the following syntax, allowing access to $this->acl->allow ($aroAlias, $acoAlias);
Access denied to $this->acl->deny ($aroAlias, $acoAlias);

You open the Aros_acos table to see, so far the table should still not be recorded
We can write an initialization function
I also put this piece of code in the user controller

  1. function Initdb () {
  2. ? ? $group =& $this->user->group;
  3. ? ? Allow admins to everything
  4. ? ? $group->id = 1;? ???
  5. ? ? $this->acl->allow ($group, ' controllers ');

  6. ? ? Allow managers to posts and widgets
  7. ? ? $group->id = 2;
  8. ? ? $this->acl->deny ($group, ' controllers ');
  9. ? ? $this->acl->allow ($group, ' controllers/posts ');
  10. ? ? $this->acl->allow ($group, ' controllers/widgets ');

  11. ? ? Allow users to only add and edit on posts and widgets
  12. ? ? $group->id = 3;
  13. ? ? $this->acl->deny ($group, ' controllers ');? ?? ???
  14. ? ? $this->acl->allow ($group, ' controllers/posts/add ');
  15. ? ? $this->acl->allow ($group, ' controllers/posts/edit ');? ?? ???
  16. ? ? $this->acl->allow ($group, ' controllers/widgets/add ');
  17. ? ? $this->acl->allow ($group, ' controllers/widgets/edit ');
  18. }

Next we access the action in the browser

This is the time when you should find a lot more records in your Aros_acos table, haha, this is the secret of the ACL.

Next you can use different groups of user name login access, you can also modify the code inside the Initdb to test, as for the actual work of how to use, it depends on your own, and finally wish everyone a happy work! Finally I put the document here, interested friends can download the ACL in the detailed description of cakephp. RAR

Reprinted from: Batterylaptops

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.