0

これはコントローラーにあります。

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

私の見解の1つでは、次のようなものがあります。

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>

ID問題は、Firebug(または任意の開発者ツール)を使用して見つかったものを更新すると、action="/Grid/memberlists/delete/9"ほとんど何でも削除できることです!別のアカウントからでも。セキュリティコンポーネントをオンにしているのに。

これを行うための適切な方法は何でしょうか?account_id現在ログインしているユーザーのaccount_idと照合することを考えています。しかし、CakePHPにこの問題を修正するためのすぐに使えるものがあるかどうか、私はただ興味がありますか?

4

2 に答える 2

3

モデルにコールバックを追加beforeDeleteし、データベースにクエリを実行して、ユーザーがレコードの削除を許可されているか、または所有者であるかを確認できます。

于 2012-06-20T05:55:20.073 に答える
2

ユーザーに属さないものを削除するなど、ユーザーがさまざまなアクションを完了できないようにするには、認証コンポーネントを使用する必要があります。

Account モデルがユーザー データを格納していると仮定しています。クックブックのチュートリアルに従う必要がありますが、削除の許可がどのように拒否されるかを強調しました。

isAuthorized メソッドは次のようになります。

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

そして、これはモデルに入ります:

public function isOwnedBy($memberList, $account) {
    return $this->field('id', array('id' => $memberList, 'account_id' => $account)) === $post;
}
于 2012-06-20T06:35:10.360 に答える