0

独自のデータとユーザー ID を保持するモデル「イベント」があります。イベントが属するユーザーにのみアクセスを許可するにはどうすればよいですか?

4

1 に答える 1

3

ビューメソッドの先頭にチェックを追加するだけで、AuthenticatedユーザーIDがイベントのユーザーIDと一致するかどうかを確認できます。そうでない場合は、「アクセスが拒否されました」というフラッシュメッセージでリダイレクトします。たとえば、EventsControllerに次を追加します。

public function view($event_id) {
    // Make sure the event exists at all
    if (!$this->Event->exists($event_id)) {
        throw new NotFoundException(__('No such event.'));
    }

    // Get the event data
    $event = $this->Event->findById($event_id);

    // Match the authenticated user with the user_id of the event.
    if ($this->Auth->User('id') != $event['Event']['user_id']) {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('This is not your event!'));
        $this->redirect(array('action' => 'index'));
    }

    /**
     * If this point is reached, the user is the owner of the event.
     * The rest of your logic goes below this point.
     */
}
于 2013-02-13T10:22:32.380 に答える