Commit 4a2d712e authored by 王源's avatar 王源
Browse files

调整鉴权注解逻辑

parent fd888caa
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -52,7 +52,7 @@ class UserInfoController {}
function getUser {}
function getUser {}
```
```
参数: 
参数: 
> name, 前缀(字符串),默认为小写控制器名拼接方法名(user),如果填写指定的名称,会覆盖@AutoPerm的prefix和exclude
> name, 前缀(字符串),默认为蛇形方法名(user)
```
```
/**
/**
 * @Perm("get_user")
 * @Perm("get_user")
+17 −30
Original line number Original line Diff line number Diff line
@@ -67,36 +67,24 @@ class PermAnnotationAspect extends AbstractAspect
        /** @var AutoPerm $autoPerm */
        /** @var AutoPerm $autoPerm */
        /** @var Perm $perm */
        /** @var Perm $perm */
        [$autoPerm, $perm] = $this->getAnnotations($proceedingJoinPoint);
        [$autoPerm, $perm] = $this->getAnnotations($proceedingJoinPoint);
        if ($perm && $perm->name) {
            // 如果有指定权限名,直接返回
            return $perm->name;
        } else {
            $methodName = $proceedingJoinPoint->methodName;
        $className = $proceedingJoinPoint->className;
        $className = $proceedingJoinPoint->className;
            if ($autoPerm) {
        $methodName = $proceedingJoinPoint->methodName;
                if (in_array($methodName, $autoPerm->exclude)) {
        if ($autoPerm && in_array($methodName, $autoPerm->exclude)) {
                    // 排除不鉴权的方法
            return false; // 跳过不需要鉴权的方法
                    return false;
                }
                if ($autoPerm->prefix) {
                    // 如果有指定前缀,直接拼接返回
                    return $this->parseName($autoPerm->prefix, $methodName);
                }
            }
            return $this->parseName($this->genPrefix($className), $methodName);
        }
        }
        $prefix = $autoPerm && $autoPerm->prefix ? $autoPerm->prefix : $this->genPrefix($className);
        $name = $perm && $perm->name ? $perm->name : $this->genName($methodName);
        return $this->parsePermName($prefix, $name);
    }
    }


    // 拼接权限名
    // 拼接权限名
    protected function parseName($prefix, $methodName)
    protected function parsePermName($prefix, $name)
    {
    {
        // 注意每个应用的app_name的唯一性
        // 注意每个应用的app_name的唯一性
        $appName = $this->config->get('app_name');
        $appName = trim($this->config->get('app_name'), '_');
        if ($prefix[-1] !== '_') {
        $prefix = trim($prefix, '_');
            $prefix .= '_';
        $name = trim($name, '_');
        }
        return $appName . '_' . $prefix . '_' . $name;
        $methodName = $this->parseMethodName($methodName);
        return $appName . '_' . $prefix . $methodName;
    }
    }


    // 生成前缀
    // 生成前缀
@@ -109,13 +97,12 @@ class PermAnnotationAspect extends AbstractAspect
        return $prefix;
        return $prefix;
    }
    }


    // 处理方法名
    // 生成名称
    protected function parseMethodName(string $methodName): string
    protected function genName(string $methodName): string
    {
    {
        $name = Str::snake($methodName);
        $methodName = Str::snake($methodName);
        $name = str_replace('__', '_', $name);
        $methodName = str_replace('__', '_', $methodName);
        trim($name, '_');
        return $methodName;
        return $name;
    }
    }


    // 获取注解
    // 获取注解