【问题标题】:CakePHP 2.x ACL - Control at owner levelCakePHP 2.x ACL - 所有者级别的控制
【发布时间】:2015-10-15 06:45:20
【问题描述】:

我可以使用ACL 控制我的应用程序,一切都完美无缺,应用程序使用ACLAuth 运行顺畅。

现在的问题是:

我有两张桌子,usersposts。没有RBAC(基于角色的访问控制)。 我正在为每个用户设置denyallow,如下所示。

//allow User1 to do everything
$user->id=1;
$this->ACL->allow($user,'controllers');

//allow User2 to add, edit and view the posts 
$user->id=2;
$this->Acl->deny($user, 'controllers');
$this->Acl->allow($user, 'controllers/Posts');

但我遇到了一个问题:

user2 正在访问editposts user1

示例:

User1 创建了一个post1

现在User2 登录现在他可以编辑User1 的帖子(即post1- /localhost/myApp/posts/edit/1

问题:这个问题如何设置ACL权限,帖子的所有者只能编辑帖子,其他人不能。

我可以在控制器级别实现这一点,只需检查

if($_SESSION['Auth']['User']['id'] == $Post['Post']['user_id']){
    // you're the owner, so u can edit
}else{
    //u cant edit, this is not ur post
}

但我需要ACL 才能在这里工作,可以吗?请帮忙

谢谢

【问题讨论】:

  • 如果它是一个 javascript 问题,我会立即得到结果,但我运气不好,这个 cakephp。非常糟糕,:(
  • 不确定它是否有效,您是否尝试过类似以下的想法:$this->Acl->allow($user, 'controllers/Posts'/edit/1) 等等?
  • @arilia ,感谢您的评论我试过了,不工作你能建议任何其他解决方案吗:)
  • 我从未如此深入地使用 ACL,但我认为您必须首先将您的帖子设置为 acos public $actsAs = array('Acl' => array('type' => 'controlled')); 并为每个帖子创建一个节点,就像为每个用户创建一个节点一样
  • 阅读文档似乎可以为每个帖子创建一个 ACO 并为其设置用户级别的权限。但是您仍然需要使用$this->Acl->check(...) 来检查权限。 ACL 授权处理程序只是在操作级别检查权限。见manual

标签: php cakephp acl


【解决方案1】:

我会这样做

首先告诉 Cake Post 模型是 ACO

 // Post.php model file
 $actsAs = array('Acl' => array('type' => 'controlled'));

这样每次你创建一个新的蛋糕后都会自动在acos表中创建一个项目。

注意:您必须手动为之前创建的帖子创建节点,这样:

// for every Post in your posts table

$this->Acl->Aco->create(array('alias' => 'Post', 'id' => 123));
$this->Acl->Aco->save();

那么你必须在你的 Post Model 文件中定义一个parentNode() 函数

// Post.php model file
public function parentNode() {
    return null;
}

现在 ACL 身份验证处理程序仅在操作级别检查表单权限。换句话说,它只是检查您是否被允许访问该操作。然后它要求isAuthorized() 函数在控制器级别进行其他检查。

所以首先你必须为每个节点设置权限

$this->Acl->allow($user, 'controllers/Posts/edit/123')

然后在你的控制器中你必须这样做

 // PostsController.php 
 public function isAuthorized($user = null) {

    if ($this->request->action === 'edit') {
        $user = // retrieve the user array. i.e. from Session
        $post_id = $this->request->$this->request->pass[0];
        $post = array('alias' => 'Post', 'id' => $post_id );
        return this->Acl->check($user, $post);
    }
    return parent::isAuthorized($user);
}

你也可以实现 parentNode() 函数来返回 Post 的所有者而不是 null

// Post.php model file

// just an hint, the actual code should be 
// a bit more complex
public function parentNode() {
    $user_id = $this->field('user_id');
    return array('User' => array('id' => $user_id));
}

这种方式不必为每个帖子设置权限,因为 cake 会检查用户是否有权访问帖子的父节点(谁也是用户)。所以你只需要为每个用户设置权限

$this->Acl->allow($user, $user);

如果您遵循此方法,请记住也将用户设置为 ACO

// User.php Model file

$actsAs = array('Acl' => array('type' => 'both'));

我没有测试上面的代码,所以我猜也有很多错别字和错误。如果我有时间,我会在接下来的几天里做一些测试并改进我的答案

【讨论】:

  • 谢谢,我会立即尝试,看起来不错的解决方案:)
猜你喜欢
  • 1970-01-01
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多