Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未授权的重定向URL cakephp_Cakephp_Authentication - Fatal编程技术网

未授权的重定向URL cakephp

未授权的重定向URL cakephp,cakephp,authentication,Cakephp,Authentication,当isAuthorized=false将用户重定向到“/”时,是否有方法更改此设置。我想重定向到用户仪表板(/users/dashboard),并显示一条flash消息,上面写着“禁止访问”之类的内容 干杯 public function isAuthorized($user) { if (isset($user['role']) && $user['role'] === 'admin') { return true; //Admin can access

isAuthorized=false
将用户重定向到“/”时,是否有方法更改此设置。我想重定向到用户仪表板(/users/dashboard),并显示一条flash消息,上面写着“禁止访问”之类的内容

干杯

public function isAuthorized($user) {
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true; //Admin can access every action
    }
    return false; // The rest don't
}

如果他们正在注销,您可以通过以下方式将他们发送到您想要的地方:

$this->Auth->logoutRedirect
我个人会使用:

$this->Auth->authError = "You are not authorized to access.";

以将其重定向到root,并显示一条提示错误的闪存消息。

如果您的控制器正在评估您的ISAuthorized变量

您可以调用重定向函数

$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
如果您实际上在用户控制器allready中,只需调用

$this->redirect(array('action' => 'dashboard'));
如果没有,您在哪里检查IsAuthorized值

这不是一个理想的解决方案。然而,似乎无法使用当前内置的AuthComponent实现这一点

编辑:添加代码作为示例

public function isAuthorized($user) {
if (parent::isAuthorized($user)) {
    return true;
}
// Authorised actions
if (in_array($this->action, array('dashboard'))) {
    return true;
}
// Will break out on this call
$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
return false;
}

是AuthComponent的错误行为。

简言之:如果url被链接访问,框架能够重建路径,然后重定向到引用页面。否则(通过直接进入url栏)它将失败并重定向到主页。

“bug”已记录在案,并将在将来的版本中更正


请参阅:

我认为最好的方法是使用exception和扩展,如下所示:

AppController.php

 public function isAuthorized($user) {
    throw new ForbiddenException(__('You are not authorized to access.'));
 }
public function isAuthorized($user) {
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }
    return parent::isAuthorized($user);
}
AnotherController.php

 public function isAuthorized($user) {
    throw new ForbiddenException(__('You are not authorized to access.'));
 }
public function isAuthorized($user) {
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }
    return parent::isAuthorized($user);
}

使用此代码,您可以管理角色和错误。

我比@deep55做得好一点

isAuthorized()方法可以抛出异常没有问题,但我认为控制器的继承将允许我们使用第一个AppController.isAuthorized()而不是最后一个来改进授权算法

所以,这里是我的解决方案,假设我使用一个名为usilisateur的用户模型和一个名为role的角色模型

AppController:

/**
 * Parent method
 */
    public function isAuthorized($user){
        App::uses('Utilisateur','Model');
        $User = new Utilisateur();
        $isAdmin = $User->hasRole(10,$user['id']);
        if ($isAdmin) {
            return true;                
        }

    }
/**
 * Reject unauthorized actions
 */
    public function rejectRequest(){
        $errorMessage = __("Sorry, you can't do this.");
        if ($this->isRest()) {
            throw new ForbiddenException($errorMessage);
        } else {
            $this->Auth->authError = $errorMessage;
            $this->Auth->flash['params']['class'] = 'alert-danger';                
        }
        return false ;
    }
/**
 * Child method
 */
public function isAuthorized($user){
    if (parent::isAuthorized($user)) {
        return true;
    }

    if ( false ) {
        return true ;
    }
    if ( false ) {
        return true ;
    }

    return $this->rejectRequest() ;
}
利用我们的模型:

/**
 * hasRole method return true if the user belongs to the correct role group
 */
    public function hasRole($role_id, $user_id){
        if (!isset($user_id)) {
            if (!empty($this->id)) {
                $user_id = $this->id ;
            } else throw new Exception("Error, parameter $user_id is missing", 1);              
        }
        $user = $this->find('first',array(
            'conditions' => array('Utilisateur.id' => $user_id),
            'fields' => array('id'),
            'contain' => array('Role.id')
        ));
        $roles = $user['Role'];
        foreach ($roles as $r) {
            if ($role_id == $r['id']) {
                return true;
            }
        }
    }
最后,在特定控制器中:

/**
 * Parent method
 */
    public function isAuthorized($user){
        App::uses('Utilisateur','Model');
        $User = new Utilisateur();
        $isAdmin = $User->hasRole(10,$user['id']);
        if ($isAdmin) {
            return true;                
        }

    }
/**
 * Reject unauthorized actions
 */
    public function rejectRequest(){
        $errorMessage = __("Sorry, you can't do this.");
        if ($this->isRest()) {
            throw new ForbiddenException($errorMessage);
        } else {
            $this->Auth->authError = $errorMessage;
            $this->Auth->flash['params']['class'] = 'alert-danger';                
        }
        return false ;
    }
/**
 * Child method
 */
public function isAuthorized($user){
    if (parent::isAuthorized($user)) {
        return true;
    }

    if ( false ) {
        return true ;
    }
    if ( false ) {
        return true ;
    }

    return $this->rejectRequest() ;
}

对于Cake版本2,如中所述:

AuthComponent::$UnauthorizedDirect

控制未经授权访问的处理。默认情况下,未经授权的用户被重定向到引用者URL或AuthComponent:$LoginDirect或“/”。如果设置为false,则会引发一个禁止的异常,而不是重定向

您可以使用
unauthorizedDirect
属性将AuthComponent配置为在一个位置重定向到自定义页面。 只需将它设置在将Auth配置为组件的位置

'Auth' => array(

     ... other settings...,

    'unauthorizedRedirect' => '/users/dashboard'
)
重定向后,您可以打印由
authError
属性定义的错误消息

echo $this->Session->flash();
echo $this->Session->flash('auth');

但对于任何身份验证或授权错误,都将显示相同的消息。

我可以阻止他们注销吗?我只想限制访问权限(仅限于少数视图,因此ACL是多余的)。您限制它们的依据是什么?如果用户角色是admin,则他们可以访问导出视图。如果角色不是管理员,则当他们尝试访问导出视图时,应将其重定向到仪表板。我希望在isAuthorized中包含此内容,以便在需要时可以轻松添加新页面。为了查看仪表板,用户必须登录。如果答案为false,则调用上面列出的重定向。isAuthorized返回布尔值。但是,这将在cakecore中用于执行重定向。我只是简单地设置为真/假。我已经用代码更新了我的示例。您修改的功能是否在AppController中?文档说:如果所有检查都失败,用户将被重定向到他们来自的页面。但它似乎工作不正常。以上功能在appcontroller中。然后在我的UserController
公共函数isAuthorized($user){if(parent::isAuthorized($user)){return true;}