gpt4 book ai didi

php - cakePHP isAuthorized 不工作

转载 作者:可可西里 更新时间:2023-11-01 14:04:25 26 4
gpt4 key购买 nike

我有一个表,它将我的用户 (user_levels) 链接到用户表 (user_level_id)。第 5 级是管理员。

我想限制某些操作被查看并了解我可以使用 isAuthorized 来做到这一点。我照着书做了,我很确定我做对了,但它不起作用。它允许任何已登录的用户仍然可以访问任何操作,尽管我在 isAuthorized 中拒绝了它。

这是我的代码:

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

最佳答案

看起来您只是缺少配置,只是告诉 AuthComponent 使用 isAuthorized()

'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)
)
),
'authorize' => array('Controller'), // <- here
'redirect' => array("controller" => "users", "action" => "profile"),
'loginRedirect' => array("controller" => "users", "action" => "profile")
)

关于php - cakePHP isAuthorized 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10818792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com