Tôi có một bảng chia tách người dùng của tôi (user_levels) được liên kết với bảng người dùng (user_level_id). Cấp 5 là quản trị viên.cakePHP isAuthorized không hoạt động
Tôi muốn giới hạn một số hành động nhất định không được xem và hiểu rằng tôi có thể thực hiện việc này với isAuthorized. Tôi đã đi theo cuốn sách và tôi khá chắc chắn rằng tôi có nó đúng, nhưng nó không hoạt động. Nó cho phép bất kỳ người dùng đăng nhập nào vẫn truy cập bất kỳ hành động nào mặc dù tôi từ chối nó trong isAuthorized.
Đây là mã của tôi:
App Controller:public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
),
'authError' => "Your username and password is incorrect, please try again.",
'authenticate' => array(
'Form' => array(
'scope' => array('User.user_status_id' => 1)
)
),
'redirect' => array("controller" => "users", "action" => "profile"),
'loginRedirect' => array("controller" => "users", "action" => "profile")
)
);
public function isAuthorized($user = null) {
if($this->Auth->user("user_level_id") == 5) {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow("display");
if($this->Auth->loggedIn() == true) {
$this->set("user_name",$this->Auth->user("first_name")." ".$this->Auth->user("last_name"));
$this->set("loggedIn",true);
if($this->Auth->user("user_type_id") == 5) {
$this->set("navigation","navigation_admin");
} else {
$this->set("navigation","navigation_loggedin");
}
} else {
$this->set("loggedIn",false);
$this->set("navigation","navigation_notloggedin");
}
}
}
// Users Controller:
public function beforeFilter() {
$this->Auth->allow("login");
parent::beforeFilter();
}
public function isAuthorized($user = null) {
if($this->Auth->user("user_level_id") == 5) {
return true;
}
// Default deny
return parent::isAuthorized($user);
}
bạn đang theo dõi sách nào? – Bahdeng