2012-08-29 73 views
0

在我的蛋糕PHP应用程序,我有一个编辑表单,其中“电子邮件”字段是只读的,这意味着用户无法更新它。 NOw如果我认为根据安全的观点,用户可以通过'萤火虫'或其他浏览器插件更新字段。停止字段更新cakephp

我正在使用$this->User->save($this->data)来保存更新的数据。通过这个功能,电子邮件也可以被更新。

我们有没有办法在蛋糕的PHP,以便我可以防止这个领域更新,就像通过在这里传递一个参数或类似的东西?

回答

1

你可以这样做:

$dontUpdateField = array('email'); 
$this->Model->save(
      $this->data, 
      true, 
      array_diff(array_keys($this->Model->schema()),$dontUpdateField) 
); 
+0

我得到这个错误..它不更新字段 – PHP

2

你可以简单地从$这个 - 删除电子邮件字段>数据:

unset($this->data['User']['email']); 
$this->User->save($this->data); 
0

您可以使用安全组件,使电子邮件隐藏。在使用这个组件时,隐藏的区域不会被改变,或者蛋糕会遮住窗体。

http://book.cakephp.org/1.3/en/view/1296/Security-Component

如果您的应用程序是公开的,强烈建议您使用安全,否则它是有点微不足道由形式提交额外的字段注入在你的模型数据,当你做$this->Model->save($this->data))额外的字段保存,除非你做了验证$ this-> data的每个字段的额外工作;

1

如果担心安全问题,只需拒绝任何具有意外值的数据即可。在蛋糕你可以做到这一点,但它可以适应任何框架/ cms

/** 
* Checks input array against array of expected values. 
* 
* Checks single dimension input array against array of expected values. 
* For best results put this is in app_controller. 
* 
* @param array $data - 1 dimensional array of values received from untrusted source 
* @param array $expected - list of expected fields 
* @return boolean - true if all fields are expected, false if any field is unexpected. 
*/ 
protected function _checkInput($data,$expected){ 
    foreach(array_keys($data) as $key){ 
    if (!in_array($key,$expected)){ 
    return; 
    } 
    } 
    return true; 
} 

/** 
* edit method. 
* 
* put this in <Model>_controller 
* @param string $id 
* @return void 
* @todo create errors controller to handle incorrect requests 
* @todo configure htaccess and Config/routes.php to redirect errors to errors controller 
* @todo setup log functionality to record hack attempts 
* @todo populate $expected with fields relevant to current model 
*/ 
function edit($id=null){ 
    $expected = ('expectedVal1', 'expectedVal2'); 
    $this->Model->id = $id; 
    if (!$this->Model->exists()) { 
    throw new NotFoundException(__('Invalid model')); 
    } 
    if ($this->request->is('post')) { 
    if (!$this->_checkData($this->request->data['Model'], $expected)) { 
     //log the ip address and time 
     //redirect to somewhere safe 
     $this->redirect(array('controller'=>'errors','action'=>'view', 405); 
    } 
    if ($this->Model->save($this->request->data)) { 
     //do post save routines 
     //redirect as necessary 
    } 
    else { 
     $this->Session->setFlash(__('The model could not be saved. Please, try again.')); 
    } 
    } 
    $this->set('model',$this->Model->read($expected,$id)); 
}