amnah / yii2-user

Yii2 user authentication module
MIT License
253 stars 104 forks source link

ForbiddenHttpException() give error in backend(advanced template) #148

Closed Abylaikhan closed 8 years ago

Abylaikhan commented 8 years ago

I add to backend controller method init()

    public function init()
    {
        $user_id = Yii::$app->getUser()->id;
        if($user_id){
            $user = \amnah\yii2\user\models\User::findOne($user_id);
            if ($user->can("admin")) {

            }else{
                throw new ForbiddenHttpException();
            }
        }else{
            \Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->loginUrl);
        }
        parent::init();
    }

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                        'roles' => ['?'], // " ? " for guest user
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }`

but it gave error like this: An Error occurred while handling another error: exception 'yii\web\ForbiddenHttpException' in C:\OpenServer\domains\yii2adv\backend\controllers\SiteController.php:28

How to solve it?

amnah commented 8 years ago

What's on line controllers\SiteController.php:28?

Have you tried searching for that error? Take a look at this: http://stackoverflow.com/questions/32737621/yii2-error-occurs-while-throwing-notfoundexception

Abylaikhan commented 8 years ago

On 28 line this code: throw new ForbiddenHttpException(); http://stackoverflow.com/questions/32737621/yii2-error-occurs-while-throwing-notfoundexception i can not solve problem( i have this error: `An Error occurred while handling another error: exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in C:\OpenServer\domains\yii2adv\backend\controllers\SiteController.php:28 Stack trace:

0 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Object.php(107): backend\controllers\SiteController->init()

1 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Controller.php(84): yii\base\Object->__construct(Array)

2 [internal function]: yii\base\Controller->__construct('site', Object(yii\web\Application), Array)

3 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\di\Container.php(368): ReflectionClass->newInstanceArgs(Array)

4 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\di\Container.php(153): yii\di\Container->build('backend\control...', Array, Array)

5 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\BaseYii.php(340): yii\di\Container->get('backend\control...', Array)

6 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(567): yii\BaseYii::createObject('backend\control...', Array)

7 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(520): yii\base\Module->createControllerByID('site')

8 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(448): yii\base\Module->createController('site/error')

9 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\web\ErrorHandler.php(93): yii\base\Module->runAction('site/error')

10 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\ErrorHandler.php(109): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))

11 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))

12 {main}

Previous exception: exception 'yii\web\NotFoundHttpException' with message 'The requested page does not exist.' in C:\OpenServer\domains\yii2adv\backend\controllers\SiteController.php:28 Stack trace:

0 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Object.php(107): backend\controllers\SiteController->init()

1 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Controller.php(84): yii\base\Object->__construct(Array)

2 [internal function]: yii\base\Controller->__construct('site', Object(yii\web\Application), Array)

3 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\di\Container.php(368): ReflectionClass->newInstanceArgs(Array)

4 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\di\Container.php(153): yii\di\Container->build('backend\control...', Array, Array)

5 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\BaseYii.php(340): yii\di\Container->get('backend\control...', Array)

6 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(567): yii\BaseYii::createObject('backend\control...', Array)

7 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(520): yii\base\Module->createControllerByID('site')

8 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Module.php(448): yii\base\Module->createController('')

9 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('', Array)

10 C:\OpenServer\domains\yii2adv\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))

11 C:\OpenServer\domains\yii2adv\backend\web\index.php(18): yii\base\Application->run()

12 {main}`

amnah commented 8 years ago

Looks like a routing/access-control issue. What url are you going to?

Abylaikhan commented 8 years ago

this url: http://yii2adv/backend/site/index when i logged in like user i have an error above.

amnah commented 8 years ago

I'm not quite sure what the issue is, but I think it's a conflict between your custom init() check and the access rules in behaviors()

Simplest solution would be to take out the access rules in behaviors(). There's no need for them if you always check for admin user anyway

adyoi commented 6 years ago

You must add 'error' on actions

'rules' => [
               [
                  'actions' => ['error', ...