douxuelv7755 2012-06-20 05:29
浏览 14
已采纳

CakePHP:安全删除记录的正确方法

I have this in my Controller.

public function delete($id) {
    if($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    $this->Memberlist->id = $id;
    if (!$this->Memberlist->exists()) {
        throw new NotFoundException(__('Invalid list.'));
    }
    if ($this->Memberlist->delete()) {
        $this->Session->setFlash(__('List deleted.'), 'success');
        return $this->redirect(array('action'=>'index'));
    }
    $this->Session->setFlash(__('List was not deleted.'), 'error');
    return $this->redirect(array('action'=>'index'));
}

My Model looks like this: (belongsTo)

<?php

class Memberlist extends AppModel {
    public $name = 'Memberlist';
    public $belongsTo = array(
            'Account' => array(
            'className' => 'Account',
            'foreignKey' => 'account_id'
        )
    );

In one of my views, I have something like this:

echo $this->Form->postLink('Delete', 
                    array('action' => 'delete', $list['Memberlist']['id']),
                    array('class'=>'btn-mini btn', 'confirm' => 'Are you sure?'));

Which creates a HTML like this:

<form id="post_4fe15efc0d284" method="post" style="display:none;" name="post_4fe15efc0d284" action="/Grid/memberlists/delete/9">
<input type="hidden" value="POST" name="_method">
<input id="Token1627936788" type="hidden" value="8756f7ad21f3ab93dd6fb9a4861e3aed4496f3f9" name="data[_Token][key]">
<div style="display:none;">
</form>
<a class="btn-mini btn" onclick="if (confirm('Are you sure?')) { document.post_4fe15efc0d284.submit(); } event.returnValue = false; return false;" href="#">Delete</a>

The problem is that when I update the ID found in action="/Grid/memberlists/delete/9" using Firebug (or any developer tool), I can pretty much delete anything! Even from a different account. Even though I have the Security Component turned on.

What would be the proper way to do this? I am thinking of checking the account_id against the account_id of the currently logged-in user. But I am just curious if CakePHP has something out-of-the-box that fixes this issue?

  • 写回答

2条回答 默认 最新

  • dongwen1909 2012-06-20 05:55
    关注

    You could add beforeDelete callback into your model, and query database and check if user is allowed to delete record and or is he owner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。