【问题标题】:cakePHP isAuthorized not workingcakePHP isAuthorized 不工作
【发布时间】:2012-05-30 14:53:50
【问题描述】:

我有一个表,它可以将我的用户 (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);
}

【问题讨论】:

  • 你在关注哪本书?

标签: php cakephp authentication controller


【解决方案1】:

看起来您只是缺少配置来告诉 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")
    )

【讨论】:

  • 嘿,杰里米,谢谢你的回复,只有 1 个问题,我还是不太明白我是如何阻止标准用户执行特定操作的。例如,我的用户控制器有许多操作,标准用户可以访问配置文件和注册,但我希望阻止添加、编辑和删除等操作。如何在 isAuthorized() 中执行此操作?我知道我不能使用允许和拒绝,因为这仅适用于未登录的用户。
  • 通常你会使用 ACL 来做类似的事情。但是,您仍然可以在每个控制器中使用自定义 isAuthorized() 来执行此操作,并为每个用户组存储一组允许的操作,然后进行检查。无论哪种方式,这足以成为一个新讨论的主题。
  • @jeremyharris 一个题外话;为什么存在“重定向”和“登录重定向”选项并指出相同的 URL?
  • @ozanmuyes 老实说,我认为我只是从 OP 复制/粘贴并修复了不起作用的部分。我没有检查一切。 redirect 似乎不需要,甚至不需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
相关资源
最近更新 更多