2012-08-29 108 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)); 
}