【问题标题】:cakephp 2.4.2 acl tutorial issuecakephp 2.4.2 acl 教程问题
【发布时间】:2013-11-22 19:36:01
【问题描述】:

我做了cakephp acl controlled application tutorial

我的问题出在这一步:

我们的控制器和模型现在已准备好添加一些初始数据,并且我们的 Group 和 User 模型绑定到 Acl 表。因此,通过浏览http://example.com/groups/addhttp://example.com/users/add 添加一些使用烘焙表单的组和用户。我做了以下几组:

当我尝试打开 */groups/add 或 */users/add 时,我收到错误消息“您无权访问该位置。”

我该如何解决这个问题?

这里是我的 GroupModel 和 UserModel。

组:

<?php
App::uses('AppModel', 'Model');
/**
 * Group Model
 *
 */
class Group extends AppModel {

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'name';

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        return null;
    }

    public function beforeFilter() {
        parent::beforeFilter();

        $this->Auth->allow();
    }

}

用户:

<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 */
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
    public $belongsTo = array('Group');
    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['group_id'])) {
            $groupId = $this->data['User']['group_id'];
        } else {
            $groupId = $this->field('group_id');
        }
        if (!$groupId) {
            return null;
        } else {
            return array('Group' => array('id' => $groupId));
        }
    }

    public function beforeSave($options = array()) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        return true;
    }

    public function beforeFilter() {
        parent::beforeFilter();

        $this->Auth->allow();
    }
}

【问题讨论】:

  • 将教程写到最后解释如何授予访问权限可能是个好主意...

标签: php cakephp cakephp-2.4


【解决方案1】:

您是否阅读了您链接的整章?大约半页你会发现这一行,我想这就是你所需要的

在我们设置 ACL 之前,我们需要添加一些用户和组。使用 AuthComponent 时,我们将无法访问我们的任何操作,因为我们没有登录。我们现在将添加一些例外,以便 AuthComponent 允许我们创建一些组和用户。在您的 GroupsController 和 UsersController 中添加以下内容:

public function beforeFilter() {
    parent::beforeFilter();

    // For CakePHP 2.0
    $this->Auth->allow('*');

    // For CakePHP 2.1 and up
    $this->Auth->allow();
}

【讨论】:

  • 是的,我阅读了整个页面,但是在我做“蛋糕烘烤模型”之后,模型不是像之前那样“蛋糕烘烤模型”我添加在模型上方的 smae,但问题仍然存在:(
  • 您是否将我上面发布的代码放入您的两个控制器中?
猜你喜欢
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2011-01-08
相关资源
最近更新 更多