鉴于我是 CakePHP 的新手,我不禁注意到授权中的明显失败。同样,当我添加授权代码并以 user1 身份登录到文章/添加页面时,继续CMS 教程,一切都很好。然后我从下拉列表中选择用户(在运行cake bake all articles
命令并按照教程重新修改 ArticlesController.php 并且不修改 '.ctp 文件后),用户 ID = 2,创建文章并能够保存它同样,虽然我以 user1 的身份登录,用户 ID = 1!我也觉得很奇怪。然后我尝试编辑 user2 的文章,它正确地给出了“未经授权的访问错误”,但是当我尝试编辑 user1 自己的文章时,它给出了如下错误:
Notice (8): Trying to get property 'user_id' of non-object [APP/Controller\ArticlesController.php, line 154]
Warning (512): Unable to emit headers. Headers sent in file=D:\dev\cakePHP\projects\cake_cms\vendor\cakephp\cakephp\src\Error\Debugger.php line=856 [CORE\src\Http\ResponseEmitter.php, line 51]
Warning (2): Cannot modify header information - headers already sent by (output started at D:\dev\cakePHP\projects\cake_cms\vendor\cakephp\cakephp\src\Error\Debugger.php:856) [CORE\src\Http\ResponseEmitter.php, line 152]
Warning (2): Cannot modify header information - headers already sent by (output started at D:\dev\cakePHP\projects\cake_cms\vendor\cakephp\cakephp\src\Error\Debugger.php:856) [CORE\src\Http\ResponseEmitter.php, line 181]
Warning (2): Cannot modify header information - headers already sent by (output started at D:\dev\cakePHP\projects\cake_cms\vendor\cakephp\cakephp\src\Error\Debugger.php:856) [CORE\src\Http\ResponseEmitter.php, line 181]
Warning (2): Cannot modify header information - headers already sent by (output started at D:\dev\cakePHP\projects\cake_cms\vendor\cakephp\cakephp\src\Error\Debugger.php:856) [CORE\src\Http\ResponseEmitter.php, line 181]
错误信息(ArticlesController.php,第 154 行)对应的代码如下:
public function isAuthorized($user)
{
$action = $this->request->getParam('action');
// The add and tags actions are always allowed to logged in users.
if (in_array($action, ['add', 'tags'])) {
return true;
}
// All other actions require a slug.
$slug = $this->request->getParam('pass.0');
if (!$slug) {
return false;
}
// Check that the article belongs to the current user.
$article = $this->Articles->findBySlug($slug)->first();
<**line 154**> return $article->user_id === $user['id'];
}
这意味着无论哪个用户登录,编辑功能都不起作用。
So here is my question:
- 当一个用户(比如 user1)尝试使用除他自己以外的用户 ID 保存文章时,授权如何工作?
- 为什么授权码不允许user1编辑自己的文章?
在此先感谢,斯鲁普