auth
auth copied to clipboard
Auth注解不支持PHP8的注解形式
好像目前还不支持PHP8的注解形式
好像目前还不支持PHP8的注解形式
`<?php
declare (strict_types=1); /**
- This file is part of hyperf-ext/auth.
- @link https://github.com/hyperf-ext/auth
- @contact [email protected]
- @license https://github.com/hyperf-ext/auth/blob/master/LICENSE */ namespace HyperfExt\Auth\Aspect;
use Hyperf\Di\Annotation\Aspect; use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use HyperfExt\Auth\Annotations\Auth; use HyperfExt\Auth\Contracts\AuthenticatableInterface; use HyperfExt\Auth\Exceptions\AuthenticationException;
#[Aspect] class AuthAspect extends AbstractAspect {
public $annotations = [Auth::class];
#[Inject]
protected \\HyperfExt\Auth\Contracts\AuthManagerInterface $auth;
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$annotation = $proceedingJoinPoint->getAnnotationMetadata();
$authAnnotation = $annotation->class[Auth::class] ?? $annotation->method[Auth::class];
$guards = empty($authAnnotation->guards) ? [null] : $authAnnotation->guards;
$passable = $authAnnotation->passable;
foreach ($guards as $name) {
$guard = $this->auth->guard($name);
if (!$guard->user() instanceof AuthenticatableInterface and !$passable) {
throw new AuthenticationException('Unauthenticated.', $guards);
}
}
return $proceedingJoinPoint->process();
}
}`