mdmsoft / yii2-admin

Auth manager for Yii2 (RBAC Manager)
GNU General Public License v3.0
1.16k stars 574 forks source link

AccessControl failed for yii\rest\Controller #312

Open desaikalpesh34 opened 7 years ago

desaikalpesh34 commented 7 years ago

Hey this module does not support for rest api controller

I have configured api like

<?php

$params = array_merge(
        // require(__DIR__ . '/../../common/config/params.php'),
        //require(__DIR__ . '/../../common/config/params-local.php'),
        require(__DIR__ . '/params.php')
        //require(__DIR__ . '/params-local.php')
);

return [
    'id'         => 'my-project-app',
    'basePath'   => dirname(__DIR__),
    'bootstrap'  => ['log'],
    'modules'    => [
        'v1' => [
            'basePath' => '@app/modules/v1',
            'class'    => 'api\modules\v1\Module'
        ]
    ],
    'components' => [
        'user'       => [
            'identityClass'   => 'api\modules\v1\models\ApiUserIdentity',
            'enableAutoLogin' => false,
            'loginUrl' =>null,
            'enableSession' => false,
        ],

        'request'    => [
            'enableCookieValidation' => false,
            'enableCsrfValidation'   => false,
            //'cookieValidationKey' => 'xxxxxxx',
            'parsers'                => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
        'response' => [
            'on beforeSend' => function ($event) 
            {
                 $response = $event->sender;
                 //return $response;
                 $data =$response->data;
                if ($response->data !== null && $response->statusCode < 400) {

                    $response->data = [
                            'success' => true,
                            'data' => $data,
                            'status' => $response->statusCode,
                        ];
                         // ...customize the response data further here...
                        //$response->statusCode = 200;            
                    } else
                    { 
                        // show error
                        $response->data = [
                            'success' => false,
                            'error' => $data,
                            'status' => $response->statusCode,
                        ];
                    }
            }
        ],

        'log'        => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets'    => [
                [
                    'class'  => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => '/v1/error/error',
        ],
        'urlManager' => [
            'enablePrettyUrl'     => true,
            'enableStrictParsing' => true,
            'showScriptName'      => false,
            'rules'               => require('url_rules.php'),
        ]
    ],
    'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
    ],
    'params'     => $params,
];

its break down my app. is there extra setting required??

mdmunir commented 7 years ago

Are you sure the error come from AccessControl?

desaikalpesh34 commented 7 years ago

Yes

mdmunir commented 7 years ago

what the error?

desaikalpesh34 commented 7 years ago

before adding behavior its work perfectly http://prntscr.com/dgc8di

when i am adding behavior then errors show like http://prntscr.com/dgc7xk

in frontend and backend work perfectly and as expected.

m i missing something or there is bug??

desaikalpesh34 commented 7 years ago

any thought??

desaikalpesh34 commented 7 years ago

I have solved this by configuring Controller behavior... like

<?php
namespace api\modules\v1\controllers;
use Yii;
use yii\rest\Controller;
class HelloController extends Controller
{
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                [
                    'class' => HttpBasicAuth::className(),
                    'auth' => function ($username, $password) {
                        $user = User::findByLogin($username);
                        return $user->validatePassword($password)
                            ? $user
                            : null;
                    }
                ],
                HttpBearerAuth::className(),
                QueryParamAuth::className()
            ],
        ];
        $behaviors['access'] = [
                'class' => 'mdm\admin\components\AccessControl',
        ];
        return $behaviors;
    }
    public function actionIndex()
    {
        return Yii::$app->id;
    }
}

and its working like charm. Thank you @mdmunir