0

我正在尝试根据用户的类型(即学生、管理员、员工)授权用户,这是我编写的代码AppController.php

public function isAuthorized($user = null) {
    // Any registered user can access public functions
    if (empty($this->request->params['admin']) && empty($this->request->params["staff"])) {
        return true;
    }
    if(isset($this->request->params["staff"])){ 
        return (bool)($user["type"]==="staff" || $user["type"]==="admin");
    }
    // Only admins can access admin functions
    if (isset($this->request->params['admin'])) {
        return (bool)($user['type'] === 'admin');
    }
    // Default deny
    return false;
}

以“学生”身份登录后,我在地址栏中键入localhost/kit-web/admin以确保“学生”没有访问管理页面的权限。但奇怪的是,网页被重定向到localhost/kit-web/kit-web. 这导致了missing Kit-webController.php错误。如果任何用户有权访问某个页面,那么一切正常。

我猜一个可能的原因是我使用composer安装了CakePHP,所以目录配置与解压缩的不同。但是,我已经按照 cakephp 教程 配置了ROOT、 、 APP_DIR 的位置。 有谁知道出了什么问题?CAKE_CORE_INCLUDE_PATH

4

1 回答 1

0

我已经自己解决了这个问题。阅读CakePHP的核心源文件,发现如果不指定“unauthorizedRedirect”字段,AuthComponent默认重定向到“/{app directory}”(即$this->redirect("/{app directory}")。 $this->redirect("/{app directory}") 不会将页面重定向到“/”,因为 {app directory} 被视为控制器名称。我不确定这是否是错误。但我希望这有帮助如果有人在同样的问题上苦苦挣扎。

于 2014-06-14T11:38:26.347 回答