Cakephp 3 - CRUD 插件 - 使用 auth 组件的 id
Cakephp 3 - CRUD plugin - Use id from auth component
目前,我正在使用 Cakephp 3 的 CRUD v4 插件。对于我的用户控制器中的编辑功能,重要的是只有用户自己可以更改他或她的凭据。我想通过从身份验证组件插入用户 ID 来实现这一点。以下控制器方法:
public function edit($id = null){
$this->Crud->on('beforeSave', function(\Cake\Event\Event $event) {
$event->subject()->entity->id = $this->Auth->user('id');
});
return $this->Crud->execute();
}
如何确保不需要通过 url 提供 ID?标准实现要求 url 像这样给出:http://domain.com/api/users/edit/1.json through PUT request. What I want to do is that a user can just fill in http://domain.com/api/users/edit.json 并发送一个 JSON 正文。
我已经尝试了几种方法:
- $id = null 当给定参数时,如上例所示。如果不在 url 中提供任何 id,这将引发 404 错误,这是由 FindMethodTrait.php
中的 _notFound 方法引起的
- 使用 beforeFind 而不是 beforeSave。这也不起作用,因为这不是编辑功能的合适方法。
- 只提供一个数据库中不存在的随机 ID。这将通过 404 错误。我认为这是出现问题的最重要标志(结合第 1 点)。因为我试图覆盖这个值,所以 CRUD 插件不允许我以完全忽略我的插入值的方式执行此操作(覆盖 $event->subject()->entity->id)。
- 尝试通过http://domain.com/api/users.json使用PUT访问方法。这将尝试将操作路由到索引方法。
- 只需检查几项:我的 AppController 中使用了 controllerTrait,并且未禁用 crud 编辑功能。
有谁知道我在这里做错了什么?这是一个错误吗?
我个人会使用Auth组件中的controller authorize来防止任何人更新别人的信息。这样你就不必更改 crud 代码。像这样的……
将此行添加到 Auth 组件(可能在您的 AppController 中)的配置中:
'authorize' => ['Controller']
然后,在应用程序控制器中创建一个名为 isAuthorized 的函数:
public function isAuthorized($user) {
return true;
}
然后,在您的 UsersController 中,您可以覆盖 isAuthorized 函数:
public function isAuthorized($user) {
// The owner of an article can edit and delete it
if (in_array($this->request->action, ['edit'])) {
$userId = (int)$this->request->params['pass'][0];
if ($user['id'] !== $userId) {
return false;
}
}
return parent::isAuthorized($user);
}
目前,我正在使用 Cakephp 3 的 CRUD v4 插件。对于我的用户控制器中的编辑功能,重要的是只有用户自己可以更改他或她的凭据。我想通过从身份验证组件插入用户 ID 来实现这一点。以下控制器方法:
public function edit($id = null){
$this->Crud->on('beforeSave', function(\Cake\Event\Event $event) {
$event->subject()->entity->id = $this->Auth->user('id');
});
return $this->Crud->execute();
}
如何确保不需要通过 url 提供 ID?标准实现要求 url 像这样给出:http://domain.com/api/users/edit/1.json through PUT request. What I want to do is that a user can just fill in http://domain.com/api/users/edit.json 并发送一个 JSON 正文。
我已经尝试了几种方法:
- $id = null 当给定参数时,如上例所示。如果不在 url 中提供任何 id,这将引发 404 错误,这是由 FindMethodTrait.php 中的 _notFound 方法引起的
- 使用 beforeFind 而不是 beforeSave。这也不起作用,因为这不是编辑功能的合适方法。
- 只提供一个数据库中不存在的随机 ID。这将通过 404 错误。我认为这是出现问题的最重要标志(结合第 1 点)。因为我试图覆盖这个值,所以 CRUD 插件不允许我以完全忽略我的插入值的方式执行此操作(覆盖 $event->subject()->entity->id)。
- 尝试通过http://domain.com/api/users.json使用PUT访问方法。这将尝试将操作路由到索引方法。
- 只需检查几项:我的 AppController 中使用了 controllerTrait,并且未禁用 crud 编辑功能。
有谁知道我在这里做错了什么?这是一个错误吗?
我个人会使用Auth组件中的controller authorize来防止任何人更新别人的信息。这样你就不必更改 crud 代码。像这样的……
将此行添加到 Auth 组件(可能在您的 AppController 中)的配置中:
'authorize' => ['Controller']
然后,在应用程序控制器中创建一个名为 isAuthorized 的函数:
public function isAuthorized($user) {
return true;
}
然后,在您的 UsersController 中,您可以覆盖 isAuthorized 函数:
public function isAuthorized($user) {
// The owner of an article can edit and delete it
if (in_array($this->request->action, ['edit'])) {
$userId = (int)$this->request->params['pass'][0];
if ($user['id'] !== $userId) {
return false;
}
}
return parent::isAuthorized($user);
}