2012-06-20 2 views
0

나는 이것을 내 컨트롤러에 가지고 있습니다.CakePHP : 안전하게 레코드를 삭제하는 올바른 방법

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

내 모델은 다음과 같습니다 (belongsTo를)

내보기 중 하나에서
<?php 

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

, 나는 이런 식으로 뭔가가 :

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

과 같은 HTML을 생성 :

<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> 

문제는 Firebug (또는 개발자 도구)를 사용하여 action="/Grid/memberlists/delete/9"에있는 ID을 업데이트하면 아무 것도 삭제할 수 있습니다! 다른 계정에서도. 보안 구성 요소가 켜져 있어도.

이 작업을 수행하는 올바른 방법은 무엇입니까? 현재 로그인 한 사용자의 account_id에 대해 account_id을 확인하려고합니다. CakePHP가이 문제를 해결할 수있는 무엇인가를 가지고 있다면 궁금합니다.

답변

3

beforeDelete 콜백을 모델에 추가하고 데이터베이스를 쿼리하여 사용자가 레코드를 삭제할 수 있는지 확인하고 해당 소유자인지 확인할 수 있습니다.

+0

내가 CakePHP의 새로운 오전이 대답은 구현하기가 매우 단순하게 발견했다. 또한 모델에서이 작업을 수행하는 데 많은 의미가있었습니다. – wenbert

2

사용자가 속한 것을 삭제하는 등 다른 작업을 실제로 수행하지 못하게하려면 Auth Component을 사용해야합니다.

계정 모델에 사용자 데이터가 저장되어 있다고 가정합니다. 요리 책의 지침서를 따라야 할 필요가 있지만 삭제에 대한 사용 권한 거부 방법을 강조했습니다.

public function isAuthorized($account) { 
    // The owner of a post can edit and delete it 
    if (in_array($this->action, array('edit', 'delete'))) { 
     $memberListId = $this->request->params['pass'][0]; 
     if ($this->MemberList->isOwnedBy($memberListId, $account['id'])) { 
      return true; 
     } 
    } 

    // Default deny 
    return false; 
} 

을 그리고이 모델에 갈 것 :

귀하의 isAuthorized 방법은 다음과 같이 보일 것이다

public function isOwnedBy($memberList, $account) { 
    return $this->field('id', array('id' => $memberList, 'account_id' => $account)) === $post; 
} 
관련 문제