【问题标题】:Allowing admins and authors to edit data in CakePHP允许管理员和作者在 CakePHP 中编辑数据
【发布时间】:2014-03-21 21:32:57
【问题描述】:

我如何(安全地)只允许管理员和作者在 CakePHP 中编辑数据,而不参考硬编码的组 ID?

我在我的 CakePHP 2.4 应用程序中使用 Auth 和 ACL,所以通常我只会将编辑操作限制为管理员和版主,但我还需要允许作者编辑他们创建的数据。

我目前在我的 edit 方法中有这个,它可以工作,但使用硬编码值,这是不好的做法:我将 ACLS 设置为默认允许 edit,如果用户两者都不是,控制器将重定向作者也不是管理员。

有没有办法尊重 ACL 设置(从而避免硬编码的组 ID),同时为帖子作者打孔?

if ($this->Auth->user('id') != $this->Post->field('user_id')) {
    if ($this->Auth->user('group_id') > 2) {

    $this->Session->setFlash(__('You are not authorized to edit this post.'), 'flash/error');
    $this->redirect(array('action' => 'index'));
    }
}

【问题讨论】:

标签: php cakephp cakephp-2.0 cakephp-2.4


【解决方案1】:

您可以存储作者的“ID 或姓名”以启用它自己的版本。 您可以检查“管理员或作者或访客”行以检查是否编辑所有内容。

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html#authorization-who-s-allowed-to-access-what

这已经在文档中

public function isAuthorized($user) {
    // All registered users can add posts
    if ($this->action === 'add') {
        return true;
    }

    // The owner of a post can edit and delete it
    if (in_array($this->action, array('edit', 'delete'))) {
        $postId = $this->request->params['pass'][0];
        if ($this->Post->isOwnedBy($postId, $user['id'])) {
            return true;
        }
    }

    return parent::isAuthorized($user);
}

【讨论】:

    【解决方案2】:

    Follow CakePHP book.

    希望文章中的Controller::isAuthorized()Post::isOwnedBy()是你的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2015-01-01
      • 2011-06-20
      相关资源
      最近更新 更多