【问题标题】:Authorization NOT applied to DELETE actions授权未应用于 DELETE 操作
【发布时间】:2020-04-13 23:14:02
【问题描述】:

当我删除一个实体时,cakephp 会警告该请求未应用授权检查。当我返回上一个操作时,实体被删除。不知何故,它是通过绕过 authorization 中间件来实现的。 附言 我正在使用作曲家的骨架应用程序。我没有对删除操作应用授权。我预计删除会失败,但它仍然存在。

CakePHP 4

控制器代码。

public function delete($id = null)
{
    $this->request->allowMethod(['post', 'delete']);
    $product = $this->Products->get($id);

    if ($this->Products->delete($product)) {
        $this->Flash->success(__('The product has been deleted.'));


    } else {
        $this->Flash->error(__('The product could not be deleted. Please, try again.'));
    }

    return $this->redirect(['action' => 'index']);


}

【问题讨论】:

  • 请分享您的控制器代码。
  • @AmanRawat 添加了控制器代码。

标签: cakephp authorization middleware


【解决方案1】:

您的代码没有绕过授权,只是没有应用任何授权检查。

授权不会自动发生,除非您明确配置/设置自动发出检查的内容,例如request authorization middlewaremodel and action based authorization。如果你没有配置类似的东西,你必须在需要的地方issue authorization checks manually

没有应用检查的消息主要是帮助调试,检查发生在中间件之后你的控制器代码已经运行(实际上不可能更早检查它,因为你的代码是几乎可以在代码中的任何位置免费应用授权检查)。

如果您明确希望某个操作需要授权,那么您可以通过调用其skipAuthorization() 方法来通知组件:

$this->Authorization->skipAuthorization();

另见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多