CakePHP中的用户编辑策略

CakePHP中的用户编辑策略,cakephp,authorization,cakephp-4.x,Cakephp,Authorization,Cakephp 4.x,我已经为用户控制器烘焙了策略。我希望应用策略,以便用户只能编辑自己的配置文件。但我正在与之斗争。我不知道如何定义$resource 若我按烘焙的方式运行代码,则会得到“未定义NULL的策略”,所以看起来好像未定义$resource 如果您试图在控制器$resource=$user->id中设置,但我得到“整数策略尚未定义” 问题出在ROOT\vendor\cakephp\authorization\src\Policy\OrmResolver.php,我应该添加新的解析器吗 在UserPolic

我已经为用户控制器烘焙了策略。我希望应用策略,以便用户只能编辑自己的配置文件。但我正在与之斗争。我不知道如何定义
$resource

若我按烘焙的方式运行代码,则会得到“未定义NULL的策略”,所以看起来好像未定义
$resource

如果您试图在控制器
$resource=$user->id
中设置,但我得到“整数策略尚未定义”

问题出在
ROOT\vendor\cakephp\authorization\src\Policy\OrmResolver.php
,我应该添加新的解析器吗

UserPolicy.php
中:

public function canUpdate(IdentityInterface $user, User $resource)    
    {
        $resource = $user->id;
        return $resource === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        $this->Authorization->authorize($resource, 'update');
        ...
public function canUpdate(IdentityInterface $user, Lesson $lesson)
    {
        // logged in users can edit their own lessons.
        return $lesson->user_id === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        //my: apply policy from LessonPolicy.php
        $this->Authorization->authorize($lesson,'update');
        ...
UsersController.php
中:

public function canUpdate(IdentityInterface $user, User $resource)    
    {
        $resource = $user->id;
        return $resource === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        $this->Authorization->authorize($resource, 'update');
        ...
public function canUpdate(IdentityInterface $user, Lesson $lesson)
    {
        // logged in users can edit their own lessons.
        return $lesson->user_id === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        //my: apply policy from LessonPolicy.php
        $this->Authorization->authorize($lesson,'update');
        ...
我还尝试在
UserPolicy.php
中更改:
公共函数canUpdate(IdentityInterface$user,user$user)
,但随后出现致命错误

Lesson政治对我来说没有任何问题
LessonPolicy.php

public function canUpdate(IdentityInterface $user, User $resource)    
    {
        $resource = $user->id;
        return $resource === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        $this->Authorization->authorize($resource, 'update');
        ...
public function canUpdate(IdentityInterface $user, Lesson $lesson)
    {
        // logged in users can edit their own lessons.
        return $lesson->user_id === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        //my: apply policy from LessonPolicy.php
        $this->Authorization->authorize($lesson,'update');
        ...
lessonscoontroller.php

public function canUpdate(IdentityInterface $user, User $resource)    
    {
        $resource = $user->id;
        return $resource === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        $this->Authorization->authorize($resource, 'update');
        ...
public function canUpdate(IdentityInterface $user, Lesson $lesson)
    {
        // logged in users can edit their own lessons.
        return $lesson->user_id === $user->getIdentifier();
    }
public function edit($id = null)
    {
        ...
        //my: apply policy from LessonPolicy.php
        $this->Authorization->authorize($lesson,'update');
        ...

关于“解析
$resource
”的确切含义,以及所显示的代码的确切问题,您在这里必须更具体一些。@ndm谢谢,我做了一些更新,希望现在更清楚您是如何获得用户实体的(
$resource
)从
UsersController::edit()方法中的数据库?一个简单的
Table::get()
(这是bake应该生成的)永远不会给你
null
@ndm,我对此感到迷茫。我已尝试设置$resource(
$resource=$user->id
)的值。我以前从未在代码中看到过这样的变量,这是我第一次烘焙用户策略。在数据库中的Users表中,没有这样的值。所以我不知道,我应该从哪里得到它。你面前有一个功能性的例子。“课程编辑”函数读取课程实体并将其传递给“授权”函数。同样,用户编辑函数应该读取用户实体并将其传递给authorize函数。