Introducing Auth & Acl control into CakePHP project

巴扎黑
Release: 2016-11-11 09:49:14
Original
1217 people have browsed it

Simply record the steps here for later 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'  
        );  
    }  
}
Copy after login

2. Generate acl table

Bash code

./Console/cake schema create DbAcl

Add groups and users

Set up 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']);  
    }  
 }
Copy after login

File/app/Model/Group.php

Php code

class Group extends AppModel {  
    public $actsAs = array('Acl' => array('type' => 'requester'));  
  
    public function parentNode() {  
        return null;  
    }  
}
Copy after login

Use bake to generate mvc files for Users and Groups, 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');  
  利用bash命令生成可用的acos数据
Bash代码  
./Console/cake AclExtras.AclExtras aco_sync
Copy after login

5. Supplement login and logout

Php code

<!-- login.ctp -->  
<h2>Login</h2>  
<?php  
echo $this->Form->create(&#39;User&#39;, array(  
    &#39;url&#39; => array(  
        &#39;controller&#39; => &#39;users&#39;,  
        &#39;action&#39; => &#39;login&#39;  
    )  
));  
echo $this->Form->input(&#39;User.username&#39;);  
echo $this->Form->input(&#39;User.password&#39;);  
echo $this->Form->end(&#39;Login&#39;);  
?>  
############分割线########  
// action  
public function login() {  
    if ($this->Session->read(&#39;Auth.User&#39;)) {  
        $this->Session->setFlash(&#39;You are logged in!&#39;);  
        return $this->redirect(&#39;/&#39;);  
    }  
}
Copy after login


Php code

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

6. ACO related
acos display using TreeBehavior

Php code

// /app/Model/Aco.php 文件  
public $actsAs = array(&#39;Tree&#39;);  
public $displayField = &#39;alias&#39;;  
  
// 输出  
$this->Aco->generateTreeList(null, null, null, &#39;   &#39;);
Copy after login

7. Permission allocation

Php code

public function initDB() {  
    $group = $this->User->Group;  
  
    // Allow admins to everything  
    $group->id = 1;  
    $this->Acl->allow($group, &#39;controllers&#39;);  
  
    // allow managers to posts and widgets  
    $group->id = 2;  
    $this->Acl->deny($group, &#39;controllers&#39;);  
    $this->Acl->allow($group, &#39;controllers/Posts&#39;);  
    $this->Acl->allow($group, &#39;controllers/Widgets&#39;);  
  
    // allow users to only add and edit on posts and widgets  
    $group->id = 3;  
    $this->Acl->deny($group, &#39;controllers&#39;);  
    $this->Acl->allow($group, &#39;controllers/Posts/add&#39;);  
    $this->Acl->allow($group, &#39;controllers/Posts/edit&#39;);  
    $this->Acl->allow($group, &#39;controllers/Widgets/add&#39;);  
    $this->Acl->allow($group, &#39;controllers/Widgets/edit&#39;);  
  
    // allow basic users to log out  
    $this->Acl->allow($group, &#39;controllers/users/logout&#39;);  
  
    // we add an exit to avoid an ugly "missing views" error message  
    echo "all done";  
    exit;  
}
Copy after login

8. Organizing

Php代码  
/** 
     * custom beforeFilter 
     */  
    public function beforeFilter() {  
        parent::beforeFilter();  
        $this->Auth->allow(&#39;XXX&#39;);  
        // $this->Auth->allow();  
    }
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!