2012-02-29 49 views
1

我想保护应用程序中的某些方法,例如发布,取消发布和关闭。通过保护,我不是指用户授权,而是以某种方式调用方法的能力。使用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']))); 
      } 
     } 

这是一个很好的做法吗?

+0

我通过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

回答

6

(假设的CakePHP 2.0这里)

首先,而不是调用上的邮政裸片/获取检查。抛出一个异常,在蛋糕的例外是精彩(Exceptions):

if (!$this->request->is('get')) { 
    throw new MethodNotAllowedException(); 
} 

蛋糕还提供了一种方法来生成模板链接,删除(通过邮寄)。

<?php echo $this->Form->postLink('Delete', 
    array('action' => 'delete', $post['Post']['id']), 
    array('confirm' => 'Are you sure?')); 
?> 

编辑(链接):postLink

这是我在一个堆栈溢出第一个答案。希望它有帮助。

+0

+1。这是正确的方法。 – 2012-02-29 23:04:37

+0

@arcynum它对我有用.....谢谢........ – 2014-01-31 10:58:46

1

这听起来像你需要ACL或某种形式的权限,如果你只想让某些用户执行该功能。你不会使用像你这样的东西的原因是(if($post['Post']['user_id'] != $this->Auth->user('id'))),因为你最终会通过你的代码库在很多函数中复制那些代码。这是非常草率的。

但是,您只想确保提交是以特定方式提交的,那么抛出错误的方法就是要走的路。你应该能够保持提交相同的功能里面,像这样:

public function publish($id = null) { 
    if (!$id || !$this->request->is('get') || !$this->request->is('post') || !$this->request->is('put')) { 
     throw new MethodNotAllowedException(); 
    } 

    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']))); 
    } 

    if ($this->request->is('get')) { 
     // perform your get request here 
    } 

} 
0

用最少的代码 $这个 - >请求 - > allowMethod(“后”);