【问题标题】:How to edit only their own data in CakePHP 2?如何在 CakePHP 2 中只编辑自己的数据?
【发布时间】:2012-04-10 06:34:12
【问题描述】:

如何使注册用户只能编辑自己的数据而不能编辑其他人。设置 ACL 时(aro 和 aco)。 我的设置:

类用户扩展 AppModel {

public function bindNode($user) {

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

}

类 AppController 扩展控制器 {

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

【问题讨论】:

  • 如何定义“自己的数据”?但一般情况下:check 在允许编辑的操作中。

标签: php cakephp


【解决方案1】:

您应该将isAuthorized 方法添加到您的控制器。在此方法中,您检查用户是否有权使用他们传递的参数执行他们尝试执行的操作。你可以使用这样的代码:

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 编写的,但我没有检查是否有重大差异。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多