cakephp ACL:列出用户可以访问的所有文档

发布于 2024-12-24 19:46:50 字数 695 浏览 1 评论 0原文

我现在使用 cakephp 一段时间了,刚刚开始使用 ACL。我已经启动并运行了,除了一件事。如何找到当前用户可用的所有文档?

我在 Aros 表中设置了几个组(超级用户、管理员和普通用户)。我有几个文档,超级用户和管理员都应该可以访问,但普通用户只能访问特定的文档。我想到的最接近的事情是:

$this->data=$this->Document->find('all',array('fields'=>array('Document.id','Document.filename','Document.title')));
foreach($this->data as $i=>$document){
    if($this->Acl->check(array('model'=>'User','foreign_key'=>$this->Session->read('User.id')),array('model'=>'Document','foreign_key'=>$document['Document']['id']))!=1){
        unset($this->data[$i]);
    }
}

上述“解决方案”的问题是它首先查询所有文档(在不久的将来将变成数千个),然后通过删除所有无法访问的文档将其减少到可能的几个文档$this->data 数组中的文档...

I am using cakephp for some time now and just started with ACL. I've got it up and running, except one thing. How can I find all documents available to the current user?

I have several groups (super users, admins and general users) set up in the Aros table. I have several documents which should be all accessible to the super users and admins, but only specific ones to the general users. The closest thing I came up with is:

$this->data=$this->Document->find('all',array('fields'=>array('Document.id','Document.filename','Document.title')));
foreach($this->data as $i=>$document){
    if($this->Acl->check(array('model'=>'User','foreign_key'=>$this->Session->read('User.id')),array('model'=>'Document','foreign_key'=>$document['Document']['id']))!=1){
        unset($this->data[$i]);
    }
}

The problem with the above "solution" is that it first queries all documents (which will become several thousands in the near future) and then brings it down to potentially a couple of documents by deleting all inaccessible documents from the $this->data array...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-12-31 19:46:50

您可以在用户模型上创建一个 findAccessibleDocuments 方法来直接查询数据库。您必须弄清楚 ACL 表的结构才能提出查询。

class User extends AppModel {
    ...
    public function findAccessibleDocuments($userId = null){
        if (!$userId) $userId = $this->getID;
        return $this->query(/* select acos inner joining aros_acos and aros in which $userId is in. Optionally, you can inner join with Document to get more than their ids */);
    }

抱歉,我无法给您完整的答案,但我现在很着急,您可以继续努力,我稍后会再回复,看看进展如何,也许会对您有更多帮助。

You could create a findAccessibleDocuments method on the User Model that queries the database directly. You'll have to figure out the structure of the ACL tables to come up with the query.

class User extends AppModel {
    ...
    public function findAccessibleDocuments($userId = null){
        if (!$userId) $userId = $this->getID;
        return $this->query(/* select acos inner joining aros_acos and aros in which $userId is in. Optionally, you can inner join with Document to get more than their ids */);
    }

Sorry I can't give you the complete answer, but I'm in a rush now, you can work on it and I'll get back to this later to see how it went and maybe help you more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文