2011-05-26 24 views
0
訪問記錄

我只是想知道是否有這樣做的蛋糕,而不是寫代碼的邏輯我自己(雖然它不會是困難的,它可以快速/有效的方法只是有點不潔)。驗證,如果一個用戶應該能夠在CakePHP中

假設你有一個正常的users表,然後posts表。每個用戶都可以有很多職位和user_idposts鏈接到usersid

本教程顯示了編輯工作記錄這樣:

function edit($id = null) { 
    $this->Post->id = $id; 
    if (empty($this->data)) { 
     $this->data = $this->Post->read(); 
    } else { 
     if ($this->Post->save($this->data)) { 
      $this->Session->setFlash('Your post has been updated.'); 
      $this->redirect(array('action' => 'index')); 
     } 
    } 
} 

什麼是有效的檢查,最好的辦法,user.id = post.user_id沒有做的,如果檢查,然後重定向了。我實際上想要「綁定」/「限制」頁面,以便傳遞給edit()$id是他們在posts表中創建該書籍(或至少是當前「所有者」)的頁面。

更簡潔,我的問題是:什麼是的蛋糕版本:

$this->Post->read(); 
if ($this->Post->data['Post']['user_id'] != $my_current_user_id){ 
    // Redirect 
} 

(僞代碼,但我希望它給的想法)。我已經通過文件拖網,但我似乎無法看到這(對我來說,它只是「有意義」它的存在給圖書館的其餘如何真棒是他們爲你做什麼用最少的工作)。

回答

0

不是真的,沒有。您將始終必須顯式執行記錄級訪問檢查,因爲它們比Auth控制器方法權限更自由。即使使用非常自動化的ACL,您至少需要調用ACL訪問檢查。我通常這樣做:

public function edit($id) { 
    $post = $this->Post->find('first', array('conditions' => array('Post.user_id' => $this->Auth->user('id'), 'Post.id' => $id)); 
    if (!$post) { 
     $this->cakeError('error403'); 
    } 

    if ($this->data) { 
     ... 
    } else { 
     $this->data = $post; 
    } 
}