CakePHP 3-关联表的所有权授权
在中,用户有条件地被授权使用以下代码根据所有权进行编辑和删除等操作:CakePHP 3-关联表的所有权授权,php,mysql,cakephp,authorization,cakephp-3.0,Php,Mysql,Cakephp,Authorization,Cakephp 3.0,在中,用户有条件地被授权使用以下代码根据所有权进行编辑和删除等操作: public function isAuthorized($user) { // All registered users can add articles if ($this->request->getParam('action') === 'add') { return true; } // The owner of an article can edit an
public function isAuthorized($user)
{
// All registered users can add articles
if ($this->request->getParam('action') === 'add') {
return true;
}
// The owner of an article can edit and delete it
if (in_array($this->request->getParam('action'), ['edit', 'delete'])) {
$articleId = (int)$this->request->getParam('pass.0');
if ($this->Articles->isOwnedBy($articleId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
public function isOwnedBy($articleId, $userId)
{
return $this->exists(['id' => $articleId, 'user_id' => $userId]);
}
我一直在尝试为自己的表实现类似的功能。例如,我有一个付款表,它通过几个不同的表链接到用户,如下所示:
- 用户->客户->预订->付款
- 客户表中的
=user\u id
(用户只有一个客户)Users->id
- 预订表中的
=customer\u id
(客户有许多预订)Customers->id
- 付款表中的
=booking\u id
(预订有很多付款)Bookings->id
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth',[
'authorize' => 'Controller',
]);
$this->Auth->allow(['display']); //primarily for PagesController, all other actions across the various controllers deny access by default
}
在我的工资控制器中,我有以下内容
public function initialize()
{
parent::initialize();
}
public function isAuthorized($user)
{
if (in_array($this->request->action,['view', 'edit', 'index', 'add']
return (bool)($user['role_id'] === 1); //admin functions
}
if (in_array($this->request->action,['cart'])) {
return (bool)($user['role_id'] === 2) //customer function
}
if (in_array($this->request->action, ['cart'])) {
$bookingId = (int)$this->request->getParam('pass.0');
if ($this->Payments->isOwnedBy($bookingId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
public function isOwnedBy($bookingId, $userId)
{
return $this->exists(['id' => $bookingId, 'user_id' => $userId]);
}
我不确定如何通过链接不同的表来确定所有权
- 目前,如果预订付款的客户#123可以更改URL,以便他们为预订付款#111,前提是数据库中存在预订李>
- 此外,预订ID将传递给购物车功能(因为客户为特定预订付费)。例如:如果客户为预订付费#123,则URL=localhost/project/payments/cart/123。提交购物车后,将创建一个新的付款条目
在\Cake\Network\Request中找不到方法“getParam”
在App\Model\Table\PaymentsTable中找不到方法“isOwnedBy”
但是,我已经浏览了整个BlogTutorial,找不到在模型中使用或设置getParam或isOwnedBy的任何其他地方。在PaymentsController中的IsAuthorized函数中:
if (in_array($this->request->action, ['cart'])) {
$id = $this->request->getParam('pass'); //use $this->request->param('pass') for CakePHP 3.3.x and below.
$booking = $this->Payments->Bookings->get($id,[
'contain' => ['Artists']
]);
if ($booking->artist->user_id == $user['id']) {
return true;
}
}
在您的
PaymentsController::isAuthorized()
中,如果(在数组($this->request->action,['cart']){中,当第二个条件(相同)满足时,第三个条件如何满足条件是真的吗?啊,是的,我忘记了。我删除了它。我现在得到的方法getParam在尝试访问购物车页面时不存在
,无论所有权如何。已修复,将很快发布答案。