請耐心等待,因爲我是一個編程/ cakephp noob,但我不知道如何限制用戶訪問其他用戶數據。當用戶登錄時,他們會得到他們列表的儀表盤(來自多個模型......餐廳,酒店,高爾夫球場,每個列表的圖像,位置信息,優惠券等等)。已登錄用戶可以訪問其他用戶的數據
根據模型/控制器的不同,我可以在瀏覽器欄/餐廳/編輯/ 1中設置用戶'3'(有酒店列表)類型,並編輯用戶'17'的餐廳信息ID = '1'。更糟糕的是,他們甚至可以訪問/ users/dashboard/17。如何限制用戶只能訪問他們自己的數據?我希望在AppController中可以使用某種'beforeAllow()'部分,它可以事先檢查用戶ID,如果他們試圖對其他用戶的數據執行CRUD操作,則會將它們踢回他們的儀表板。我仍然需要知道正確的代碼來限制用戶的訪問權限,對嗎?即使我使用ACL(我知道我應該但是在這個階段的學習過程中坦率地說我的頭腦太過分了),我仍然必須知道正確的代碼來限制用戶的訪問權限嗎?
下面是我的AppController:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'view'),
'logoutRedirect' => array('controller' => 'docs', 'action' => 'index'),
'authError' => 'Sorry, you are not authorized to view this page.'
)
);
function beforeFilter() {
$this->Auth->userModel = 'User';
$this->Auth->allow('join_now','debug','index', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us', 'view', 'results');
}
}
,這裏是一個樣本編輯功能(從我UnitsController編輯功能):
function edit($id) {
$this->set('title', 'Edit your property');
$this->Unit->id = $id;
if (empty($this->request->data)) {
$this->request->data = $this->Unit->read();
} else {
if ($this->Unit->saveAll($this->request->data)) {
$this->Session->setFlash('Your property has been updated.', 'success');
}
}
}
我會說,我的數據庫表中的每一個都有一個user_id字段,因此登錄用戶可以與每個模型的user_id匹配。
我認爲this SO question是我正在尋找的,但他們最終得到了一個切線,並從未回答用戶提出的原始問題。
我知道我會得到幾個「不,認真使用ACL組件」的答案,嘿嘿。 (插入另一個'蛋糕手冊是可怕的'評論)老實說,我可以與它,我只是不知道如何基於登錄的用戶ID扼殺行動,你上面的代碼幾乎是我一直在尋找。謝謝! – huzzah 2012-03-28 15:41:36