我想保护应用程序中的某些方法,例如发布,取消发布和关闭。通过保护,我不是指用户授权,而是以某种方式调用方法的能力。使用POST而不是GET来保护CakePHP中的方法
我想过使用POST而不是GET来强制调用方法,这样一个人不能只将URL粘贴到地址中(即使我只能检查这个人的用户ID)。然而,这意味着每个方法调用都以不同的形式包装每个按钮。
或者,我可以使用GUID来允许GET方法,但要确保它只允许正确的人员执行该功能。
对此有何想法?
到目前为止,我曾尝试:
function publish($id = null)
{
$post = $this->Post->find('first',array('conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));
if ($this->request->is('post') || $this->request->is('put'))
{
$this->Post->id = $post['Post']['id'];
$this->Post->saveField('status', 1);
$this->Session->setFlash('Your post has been published!');
$this->redirect(array('controller'=>'posts','action'=>'view','id'=>Tiny::toTiny($post['Post']['id'])));
}
else
{
die('GET METHOD NOT ALLOWED');
}
}
但如上面所述,这意味着此方法需要在包含操作调用此方法的形式链接/按钮。如果我有几种方法,我需要几种形式...
干杯我曾经想过被允许GET方法,然后检查与登录用户ID进行比较后的用户ID
的一种方式像这样:
if ($this->request->is('get'))
{
if($post['Post']['user_id'] != $this->Auth->user('id'))
{
$this->Session->setFlash('You don\'t have permission to edit that post!');
$this->redirect(array('controller' => 'posts', 'action' => 'index'));
}
else
{
$this->Post->id = $post['Post']['id'];
$this->Post->saveField('status', 1);
$this->Session->setFlash('Your post has been published!');
$this->redirect(array('controller'=>'posts','action'=>'view','id'=>Tiny::toTiny($post['Post']['id'])));
}
}
这是一个很好的做法吗?
我通过StackOverflow学习了很多东西,所以如果它帮助我的话,我会问很多问题。人们在网上社区担任不同的角色。我在大学撰写了关于用户心理学的论文。你应该阅读它的一些:) http://www.google.co.uk/search?aq = 0&oq = social + roles + in&sourceid = chrome&ie = UTF-8&q = social + roles + in + online + community不是每个人都以相同的方式互动,每个用户都扮演不同的角色! – Cameron 2012-02-29 21:47:11