Auth & Acl Control introduced in CakePHP project

Source: Internet
Author: User
Tags acos
Simply record the operation steps here for later review.

First, the introduction of auth/app/controller/appcontroller.php

PHP code

Class AppController extends Controller {public      $components = Array ('          Acl ',          ' Auth ' = = Array (              ' Authorize ' = = Array (                  ' Actions ' = = Array (' Actionpath ' = ' controllers '))          ,          ' Session '      ) ;      Public $helpers = Array (' Html ', ' Form ', ' Session ');        Public Function Beforefilter () {          //configure authcomponent          $this->auth->loginaction = Array (            ' Controller ' = ' users ',            ' action ' = ' login '          ;          $this->auth->logoutredirect = Array (            ' controller ' = ' users ',            ' action ' = ' login '          );          $this->auth->loginredirect = Array (            ' controller ' = ' posts ',            ' action ' = ' add '          );      }  }

Second, generate ACL table

Bash code

./console/cake Schema Create Dbacl

Third, add groups and users

Set Model file/app/model/user.php

PHP code

Class User extends Appmodel {public      $belongsTo = Array (' Group ');      Public $actsAs = Array (' Acl ' = = Array (' type ' = ' = ' requester '));        Public Function ParentNode () {          if (! $this->id && emptyempty ($this->data)) {              return null;          }          if (isset ($this->data[' user '] [' group_id ')) {              $groupId = $this->data[' user '] [' group_id '];          } else {              $groupId = $this->field (' group_id ');          }          if (! $groupId) {              return null;          }          Return Array (' Group ' = = Array (' id ' = $groupId));      }      Public Function Bindnode ($user) {          return array (' model ' = ' Group ', ' foreign_key ' = ' = ' $user [' user '] [' group_id ' ]);      }   }

File/app/model/group.php

PHP code

Class Group extends Appmodel {public      $actsAs = Array (' Acl ' = = Array (' type ' = = ' requester '));        Public Function ParentNode () {          return null;      }  }

Use bake to generate the MVC files for users, groups, add groups and users, and generate AROS data.

Iv. using Aclextras to generate ACO table data
Download Aclextras install to the/app/plugin/directory

PHP code

app/config/boostrap.php  //...  Cakeplugin::load (' Aclextras ');    Use the Bash command to generate the available ACOs Data Bash code  ./console/cake Aclextras.aclextras Aco_sync

V. Add login and Logout

PHP code

 
    

Login

Form->create (' User ', Array (' url ' = = Array (' controller ' = ' users ', ' action ' = ' login ') ) )); echo $this->form->input (' user.username '); echo $this->form->input (' User.password '); echo $this->form->end (' Login '); > ########### #分割线 ######## //Action public function login () { if ($this->session->read (' Auth.user ')) { $this->session->setflash (' logged in! '); return $this->redirect ('/'); } }


PHP code

Public Function Logout () {      $this->redirect ($this->auth->logout ());  }

Vi. ACO-related
ACOs's display uses Treebehavior

PHP code

/app/model/aco.php file public  $actsAs = Array (' Tree ');  Public $displayField = ' Alias ';    Output  $this->aco->generatetreelist (null, NULL, NULL, '   );

Vii. Assignment of Rights

PHP code

Public Function Initdb () {$group = $this->user->group;      Allow admins to everything $group->id = 1;        $this->acl->allow ($group, ' controllers ');      Allow managers to posts and widgets $group->id = 2;      $this->acl->deny ($group, ' controllers ');      $this->acl->allow ($group, ' controllers/posts ');        $this->acl->allow ($group, ' controllers/widgets ');      Allow users to only add and edit on posts and widgets $group->id = 3;      $this->acl->deny ($group, ' controllers ');      $this->acl->allow ($group, ' controllers/posts/add ');      $this->acl->allow ($group, ' controllers/posts/edit ');      $this->acl->allow ($group, ' controllers/widgets/add ');        $this->acl->allow ($group, ' controllers/widgets/edit ');        Allow basic users to log out $this->acl->allow ($group, ' controllers/users/logout ');   We add an exit to avoid a ugly "missing views" error message   echo "All done";  Exit }

Eight, finishing

PHP code  /**      * Custom Beforefilter */public      function Beforefilter () {          parent::beforefilter ();          $this->auth->allow (' XXX ');          $this->auth->allow ();      }
  • Related Article

    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.