cranetm / yii2-json-rpc-2.0

Other
26 stars 18 forks source link

no need for before action on an empty action since we work thru json-rpc #28

Open sagittaracc opened 3 years ago

sagittaracc commented 3 years ago

Now it works just like this

<?php

namespace app\controllers;

use \JsonRpc2\Controller;
use app\components\JwtAccessControl;

class ApiController extends Controller
{
    /**
    * {@inheritdoc}
    */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => JwtAccessControl::className(),
                'except' => ['login'],
            ],
        ];
    }

    public function actionLogin() {
        return 'login';
    }

    public function actionUpdate() {
        return 'update';
    }
}
<?php

namespace app\components;

use Yii;
use yii\base\ActionFilter;

class JwtAccessControl extends ActionFilter
{
  public function beforeAction($action)
  {
    if (true)
      throw new \JsonRpc2\extensions\AuthException('Missing auth', \JsonRpc2\extensions\AuthException::MISSING_AUTH);

    return parent::beforeAction($action);
  }

  public function afterAction($action, $result)
  {
    return parent::afterAction($action, $result);
  }
}