CakePHP專案中引入Auth & Acl 控制

巴扎黑
發布: 2016-11-11 09:49:14
原創
1218 人瀏覽過

 在此簡單記錄操作步驟,以便後製查閱。

     一、引入auth /app/Controller/AppController.php

Php程式碼  

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'  
        );  
    }  
}
登入後複製

     二、產生acl表

/cCate🠎 Ncobate 

  三、新增群組及使用者

設定Model檔案/app/Model/User.php

Php代碼  

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']);  
    }  
 }
登入後複製

   .php

Php代碼  

class Group extends AppModel {  
    public $actsAs = array('Acl' => array('type' => 'requester'));  
  
    public function parentNode() {  
        return null;  
    }  
}
登入後複製

    文件/app/Model/Group.php

Php  

//app/Config/boostrap.php  
// ...  
CakePlugin::load('AclExtras');  
  利用bash命令生成可用的acos数据
Bash代码  
./Console/cake AclExtras.AclExtras aco_sync
登入後複製

php


Php  

<!-- 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;);  
    }  
}
登入後複製

; aros數據。

四、利用AclExtras 產生aco表資料

下載AclExtras 安裝至/app/Plugin/ 目錄下

Php代碼  

public function logout() {  
    $this->redirect($this->Auth->logout());  
}
登入後複製
  五、補充login、Php代碼  

// /app/Model/Aco.php 文件  
public $actsAs = array(&#39;Tree&#39;);  
public $displayField = &#39;alias&#39;;  
  
// 输出  
$this->Aco->generateTreeList(null, null, null, &#39;   &#39;);
登入後複製

o🜠

Php代碼  

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;  
}
登入後複製

  六、ACO相關
   acos 的展示利用TreeBehavior

Php代碼  

Php代码  
/** 
     * custom beforeFilter 
     */  
    public function beforeFilter() {  
        parent::beforeFilter();  
        $this->Auth->allow(&#39;XXX&#39;);  
        // $this->Auth->allow();  
    }
登入後複製

  七、o

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!