| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace common\helpers;
- use common\enums\StatusEnum;
- use common\models\rbac\AuthItem;
- use Yii;
- /**
- * Class Auth
- * @package common\helpers
- * @author qimall
- */
- class Auth
- {
- protected static $auth = [];
- /**
- * 校验权限
- *
- * @param string $route
- * @param array $defaultAuth
- * @return bool
- * @throws \yii\web\UnauthorizedHttpException
- */
- public static function verify(string $route, $defaultAuth = [])
- {
- if (Yii::$app->services->auth->isSuperAdmin()) {
- return true;
- }
- // if (Yii::$app->services->rbacAuthRole->isMallAdmin() && strpos($route, 'admin/') === false) return true;
- if(empty($route)) return false;
- $route = trim($route);
- //没有写入权限列表的,不验证
- $res = AuthItem::findOne(['name'=>$route,'status'=>StatusEnum::ENABLED]);
- if(empty($res)) return true;
- $auth = !empty($defaultAuth) ? $defaultAuth : self::getAuth();
- if (
- in_array('/*', $auth) ||
- in_array('*', $auth) ||
- in_array($route, $auth) ||
- in_array(Url::to([$route]), $auth)
- ) {
- return true;
- }
- return self::multistageCheck($route, $auth);
- }
- /**
- * 过滤自己拥有的权限
- *
- * @param array $route
- * @return array
- * @throws \yii\web\UnauthorizedHttpException
- */
- public static function verifyBatch(array $route)
- {
- if (Yii::$app->services->auth->isSuperAdmin()) {
- return $route;
- }
- return array_intersect(self::getAuth(), $route);
- }
- /**
- * 支持通配符 *
- *
- * 例如:
- * /goods/*
- * /goods/index/*
- *
- * @param string $route 权限名称
- * @param array $auth 所有权限组
- * @param string $separator 分隔符
- * @return bool
- */
- public static function multistageCheck($route, array $auth, $separator = '/')
- {
- $key = $separator;
- $routeArr = explode($separator, $route);
- foreach ($routeArr as $value) {
- if (!empty($value)) {
- $key .= $value . $separator;
- if (in_array($key . '*', $auth)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 获取权限信息
- *
- * @return array
- * @throws \yii\web\UnauthorizedHttpException
- */
- public static function getAuth()
- {
- if (self::$auth) {
- return self::$auth;
- }
- $role = Yii::$app->services->rbacAuthRole->getRole();
- self::$auth = Yii::$app->services->rbacAuthItemChild->getAuthByRole($role, Yii::$app->id);
- return self::$auth;
- }
- }
|