>  기사  >  백엔드 개발  >  CakePHP 프로젝트에 인증 및 Acl 제어 기능 도입

CakePHP 프로젝트에 인증 및 Acl 제어 기능 도입

巴扎黑
巴扎黑원래의
2016-11-11 09:49:141207검색

나중에 참조할 수 있도록 여기에 단계를 기록해 두세요.

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

2. acl 테이블 생성

Bash 코드

./Console/cake 스키마 생성 DbAcl

3. 그룹 및 사용자 추가

모델 파일/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']);  
    }  
 }

File/app/Model/Group.php

Php 코드

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

baking를 사용하여 사용자 및 그룹에 대한 mvc 파일을 생성하고, 그룹 및 사용자를 추가합니다. aros 데이터를 생성합니다.

4. AclExtras를 사용하여 aco 테이블 데이터 생성
AclExtras를 다운로드하여 /app/Plugin/ 디렉터리에 설치합니다.

Php 코드

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

5. 로그인 및 로그아웃 추가

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;);  
    }  
}


Php 코드

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

6. ACO 관련
acos의 표시는 TreeBehavior를 사용합니다

Php 코드

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

7. 권한 할당

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;  
}

8. 정리

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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.