簡體   English   中英

如何在CakePHP 2中只編輯自己的數據?

[英]How to edit only their own data in CakePHP 2?

如何讓注冊用戶只能編輯自己的數據,不能編輯其他人。 什么時候設置ACL(aro和aco)。 我的設置:

class 用戶擴展 AppModel {

public function bindNode($user) {

    return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);

}

class AppController 擴展 Controller {

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);

您應該將isAuthorized方法添加到您的 controller。在此方法中,您檢查用戶是否有權使用他們傳遞的參數嘗試執行的操作。 你可以使用這樣的代碼:

switch ($this->action) {
    case 'delete':
    case 'edit':
        // id of what they are trying to edit
        $this->Topic->id = $this->params['pass'][0];
        // id of the owner of what they are trying to edit
        $ownerId = $this->Topic->field('user_id');

        $userId = $this->Auth->user('id');
        if ($ownerId == $userId) {
            // allow users to edit or delete their own topics
            return TRUE;
        } else {
            // allow admin group to edit any topic
            return $this->Auth->user('group') == 'admin';
        }
}

如果您想使用 Cake 的 ACL 系統來檢查權限而不是像“用戶是管理員組的成員”這樣的硬編碼檢查,請參閱此處的教程: http://jonisalonen.com/2010/role-based-acl-in -cakephp/雖然它是為 Cake 1.3 編寫的,但我還沒有檢查是否存在重大差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM