【问题标题】:Cakephp 2.1 rename ACL Table without ACL ModelCakephp 2.1 重命名没有 ACL 模型的 ACL 表
【发布时间】:2014-03-04 08:05:07
【问题描述】:

我在使用 ACL 时遇到问题。在我的 CoreUsersController 中,我进行了以下调用:

if(!$this->Acl->check('User::'.$user['User']['id'], 'CoreUser', 'read')) {
    die('PERM DENIED');
}

ACL 作为组件包含在我的 AppController 中。数据库表看起来像:

  • 表“acos”
  • 表“core_groups”(as aros)
  • 表“aros_acos”作为 core_groups 和 acos 之间的连接。

这是抛出的错误: 错误:模型 Aro 的表 aros 未在数据源默认值中找到。


还有我的代码:

1.用户模型:

    App::uses('AuthComponent', 'Controller/Component');

    class CoreUser extends AppModel {

            public $useDbConfig = 'default';
            public $useTable    = 'core_users';

            public $hasAndBelongsToMany = array(
                'MemberOf' => array(
                    'className' => 'CoreGroup',
                    'joinTable' => 'core_users_groups',
                    'foreignKey' => 'user_id',
                    'associationForeignKey' => 'group_id'
                )
            );

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

            public function parentNode() {
                // stuff in here should be correct
            }

}

2。组模型:

App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class CoreGroup extends AppModel {

        public $useDbConfig = 'default';
        public $useTable    = 'core_groups';

        public $hasAndBelongsToMany = array(
            'Member' => array(
                'className' => 'CoreUser',
                'joinTable' => 'core_users_groups',
                'foreignKey' => 'group_id',
                'associationForeignKey' => 'user_id'
            )
        );
        public $name = 'Group';
        public $actsAs = array('Acl' => array('type' => 'requester'));

        public function parentNode() {
            return null;
        }
}

我的问题:如何告诉 ACL 使用“core_groups”-Table 而不是“aros”-table?注意:我按照 Cake-Book 的建议为两个模型添加了“ActsAs”!

【问题讨论】:

  • 您是在 shell 中使用此命令:./Console/cake schema create DbAcl 来创建 acl 表还是您手动执行的?

标签: php cakephp acl


【解决方案1】:

如果您想继续使用 Cake 的 ACL,那么最好有三个单独的表:

1. aros
2. acos
3. aros_acos

这些可以使用 AclShell 生成:

./Console/cake schema create DbAcl

不建议将您的应用模型之一用作 ARO 或 ACO,因为 ARO 和 ACO 模型是作为树形结构实现的。这些应该与您的应用程序中“充当”ACO / ARO 的模型分开。

当您将$actsAs 属性中的Acl 行为附加到您的用户/组模型时,Acl 行为的回调就会启动。这些使用在您的用户/组模型中定义的parentNode() 方法来创建ARO reference 在您的用户/组模型中创建的行。这些引用分别存储在 ARO 表中,指向实际的用户和组。因此,ARO 表是必要的。

我建议您阅读本书中的the ACL tutorial,因为您的实现将非常相似。在使用用户和组之间的 HABTM 关系时要小心,因为这需要一些调整。

【讨论】:

  • 感谢您的帮助!我阅读了(许多)ACL 教程,好几遍,但对我来说很难理解,因为大多数教程都是理论性的,并且没有(完整的、深度嵌套的)示例应用程序..
  • 本书中的教程确实将用户 ARO 嵌套在组 ARO 中。您的情况的唯一区别是用户和组之间的关系。无论如何,由于我的回答中提到的原因,您应该使用单独的 aros 表。您可以参考core ARO model 并查看您的 CoreGroup 模型表将无法复制行为像树等的功能。尝试使用单独的表,如果您有任何其他问题/错误,请告诉我们。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 2013-01-11
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多