授权不适用于 DELETE 操作

Authorization NOT applied to DELETE actions

当我删除 一个实体时,cakephp 警告该请求未应用授权检查。当我返回到之前的操作时,实体被删除。它以某种方式绕过 authorization 中间件。 PS 我正在使用作曲家的框架应用程序。我还没有对删除操作申请授权。我预计删除会失败,但它仍然存在。

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']);


}

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

授权不会自动发生,除非您明确 configure/setup 一些自动发出支票的东西,例如 request authorization middleware, or model and action based authorization. If you don't have something like that configured, you have to issue authorization checks manually 在需要的地方。

没有应用检查的消息主要是调试辅助,检查发生在中间件之后你的控制器代码已经运行(这真的不可能更早地检查它,因为您的代码几乎可以在代码中的任何位置自由应用授权检查。

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

$this->Authorization->skipAuthorization();

另见