授权和重定向


Yii authorization and redirecting

我是Yii的新手,我需要修复一些东西:有一些带有一些动作的控制器,如果用户试图加载一些动作,我需要检查他是否被授权;如果是,那么做一些动作,如果不是-重定向到登录页面。我该怎么做呢?我希望我不需要通过Yii::user和重定向手动在每个动作检查?提前感谢。

使用动作过滤器,当您创建yii crud时,有基本的访问控制过滤器。这里有一些关于过滤器的文档:
http://www.yiiframework.com/doc/guide/1.1/en/basics.controller过滤器

访问控制的现成解决方案的主题在这里:http://www.yiiframework.com/doc/guide/1.1/pl/topics.auth#sec-3

总之,在控制器中创建方法:

    public function filters()
    {
        return array(
            'accessControl',
        );
    }

然后用method:

定义操作的访问规则
public function accessRules()
{
    return array(
        array('deny',
            'actions'=>array('create', 'edit'),
            'users'=>array('?'),
        ),
        array('allow',
            'actions'=>array('delete'),
            'roles'=>array('admin'),
        ),
        array('deny',
            'actions'=>array('delete'),
            'users'=>array('*'),
        ),
    );
}

编辑

如果用户没有经过身份验证,定义重定向的url,在user组件中定义:

'user' => [
                'class' => 'CWebUser', // or custom
                'allowAutoLogin' => true, // for autologin
                'loginUrl' => ['/ua/user/login'], // if not authenticated go here
     ]

我已经创建了AdminModule.php。在这里,我创建了一个函数来检查用户是否登录。还有一些不需要登录的公共页面。

public function beforeControllerAction($controller, $action)
{
    if(parent::beforeControllerAction($controller, $action))
    {   
        // this overwrites everything in the controller
        $controller->layout = 'admin';
        // this method is called before any module controller action is performed
        // you may place customized code here
        $route=$controller->id.'/'.$action->id;
        $publicPages=array(
            'user/login',
            'default/error',
        );
        if($this->password!==false && Yii::app()->user->isGuest && !in_array($route,$publicPages))
            Yii::app()->user->loginRequired();
        else
            return true;
    }
    else
        return false;
}

作为Mohit Bhansali示例的简化版本,我在我的AdminModule中实现了以下代码:

public function beforeControllerAction($controller, $action)
{
    if(parent::beforeControllerAction($controller, $action)) {
        if (Yii::app()->user->isGuest) {
            Yii::app()->user->loginRequired();
        } elseif (Yii::app()->user->id != 'myadminuser') {
            throw new CHttpException(403, 'You are not authorized to perform this action.');
        }
        return true;
    } else {
        return false;
    }
}

然后我还能够从每个管理模块控制器中删除filters()中返回的accessControl数组(但是必须保留空的filter()函数),并完全删除accessRules()。例如,下面是DefaultController:

class DefaultController extends Controller
{
    public function filters()
    {
    }
    public function actionIndex()
    {
        $this->render('index');
    }
}

还值得一提的是,建议使用RBAC不仅仅用于简单的访问控制,如果我们在这里使用RBAC,我们会调用Yii::app()->user->checkAccess(),而不是在AdminModule中检查Yii::app()->user->id。

就加上这个

if(Yii::app()->user->getId()===null) {
      // This is the case where user is not logged in so redirect
     $this->redirect(array('site/login'));
} else {
     // this is the case where user is logged in 
     // do your business 
}

有一个非常好的SO张贴已经存在,你可以参考你的情况

如何在Yii的索引页上认证用户

希望能有所帮助