Introduce Auth & Acl control in the CakePHP project

Source: Internet
Author: User
Tags acos
1. introduce authappControllerAppController. phpclassAppControllerextendsController {p... here, record the Operation steps for future reference.

1. introduce 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'          );      }  }

II. generate an acl table

Bash Code

./Console/cake schema create DbAcl

3. add groups and users

Set the 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 Users and Groups mvc files, add Groups and Users, and generate aros data.

4. use AclExtras to generate aco table data
Download AclExtras and install it in the/app/Plugin/directory.

Php code

// App/Config/boostrap. php //... cakePlugin: load ('aclextras'); use bash commands to generate available acos data Bash code. /Console/cake AclExtras. aclExtras aca_sync

5. supplement 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');?> ############ Split line ######## // action public function login () {if ($ this-> Session-> read ('auth. user ') {$ this-> Session-> setFlash (' You are logged in! '); Return $ this-> redirect ('/');}}


Php code

public function logout() {      $this->redirect($this->Auth->logout());  }

VI. ACO problems
Use TreeBehavior to display acos

Php code

/// App/Model/Aco. php file public $ actsAs = array ('tree'); public $ displayField = 'Alias'; // Output $ this-> Aco-> generateTreeList (null, '');

VII. Permission Assignment

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 an ugly "missing views" error message      echo "all done";      exit;  }

8. Organize

Php code/*** * m beforeFilter */public function beforeFilter () {parent: beforeFilter (); $ this-> Auth-> allow ('XXX '); // $ this-> Auth-> allow ();}

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.