AccessControl.php 3.09 KB
<?php

namespace soa\components\filters;

use Yii;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use common\lib\security\AuthCode;

/**
 * 访问校验
 * @author wuxiao
 * @date 2016-8-19
 */
class AccessControl extends \yii\filters\AccessControl
{
    
    /**
     * @var array a list of access rule objects or configuration arrays for creating the rule objects.
     * If a rule is specified via a configuration array, it will be merged with [[ruleConfig]] first
     * before it is used for creating the rule object.
     * @see ruleConfig
     */
    public $rules = [
        [
            'allow' => false,
        ],
    ];
    /**
     * @var array list of action IDs that this filter should apply to. If this property is not set,
     * @see except
     */
    public $only;
    /**
     * @var array list of action IDs that this filter should not apply to.
     * @see only
     */
    public $except = [];
    
    public function init()
    {
        if (isset(Yii::$app->params['verifySign']) && !Yii::$app->params['verifySign'])
        { 
            //不检验client_secret参数
            $this->rules = ArrayHelper::merge([[
                'allow' => true,
            ],], $this->rules);
        }else{
            //通过client_secret参数做请求数据校验
            $params = $this->requests();
            $clientSecret = @$params['client_secret'];
            unset($params['client_secret'], $params['project'], $params['version'], $params['class_name'], $params['method_name']);
            $params['private_key'] = @AuthCode::$privateKey[strtolower($params['client_type'])];
            $_params = AuthCode::packageSort($params);
            $_makeKey = AuthCode::makeSign($_params);
            $verifySign = AuthCode::verifySign($_makeKey, $clientSecret);
            if ($verifySign == true) {
                $this->rules = ArrayHelper::merge([[
                    'allow' => true,
                ],], $this->rules);
            }
        }
        
        $this->denyCallback = function ($rule, $action) {
            //throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            echo json_encode([
                'code'      => 500,
                'message'   => '数据验证错误',
                'data'      => [],
                'timestamp' => time(),
                'md5'       => md5(json_encode([]))
            ]);
            Yii::$app->end();
        };
        parent::init();
    }
    
    public function beforeAction($action)
    {
        return parent::beforeAction($action);
    }
    
    /**
     * 请求参数
     * @return array|mixed
     */
    protected function requests($name = null, $defaultValue = null)
    {
        $params = array_merge(Yii::$app->request->get(), Yii::$app->request->post());

        if ($name === null) {
            return isset($params['parameters']) ? json_decode($params['parameters'], true) : $params;
        } else {
            return isset($params[$name]) ? $params[$name] : $defaultValue;
        }
    }
}