CakePHP已授权,在传递参数时无法正常工作
如果记录id不属于用户,我将使用CakePHP已授权,在传递参数时无法正常工作,php,cakephp,authorization,cakephp-2.0,Php,Cakephp,Authorization,Cakephp 2.0,如果记录id不属于用户,我将使用isAuthorized拒绝对方法的访问。配置文件可以有多个文档,并且文档属于一个配置文件: Controller/DocumentsController.php public function add($id = null) { if ($this->request->is('post')) { $this->request->data['Document']['profile_id'] = $id;
isAuthorized
拒绝对方法的访问。配置文件可以有多个文档,并且文档属于一个配置文件:
Controller/DocumentsController.php
public function add($id = null) {
if ($this->request->is('post')) {
$this->request->data['Document']['profile_id'] = $id;
$this->request->data['Document']['user_id'] = $this->Auth->user('id');
$this->Document->create();
if ($this->Document->save($this->request->data)) {
$this->Session->setFlash(__('The document has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The document could not be saved. Please, try again.'));
}
}
}
public function isAuthorized($user) {
if ($this->action === 'index') {
return true;
}
if (in_array($this->action, array('view', 'add', 'edit', 'delete'))) {
$document_id = $this->request->params['pass'][0];
if ($this->Document->isOwnedBy($document_id, $user['id'])) {
return true;
}
}
return parent::isAuthorized($this->Auth->user());
}
Model/Document.php
public function isOwnedBy($document, $user) {
return $this->field('id', array('id' => $document, 'user_id' => $user)) === $document;
}
我正在通过Cake link helper将配置文件id
作为$id
传递到文档/add
从我的一个配置文件视图:
View/Profiles/View.ctp
echo $this->Html->link('New Document',
array('controller' => 'documents', 'action' => 'add',$profile['Profile']['id'])
);
当我从profiles/view
单击新文档时,会发生什么情况?它发送请求但不重定向,只是刷新页面,或者重定向回profiles/view
,不确定是哪个。我的第一个猜测是,由于我没有在DocumentsController
中的isAuthorized
回调中定义profile id
,isOwnedBy
返回false。关于如何在中获取配置文件id的任何建议都在文档控制器
中授权
提前谢谢 解决这个问题相对容易。当使用其他控制器的参数进行isAuthorized时,请确保参考正确的模型
if ($this->Document->Profile->isOwnedBy($document_id, $user['id'])) {
return true;
}
你能做更多的测试/调试来确定它是否进入了
isAuthorized
,如果是的话,它在哪里失败了…等等?是的,当我将add methdod添加到允许的操作列表中时,我可以添加文档没有问题。