hyperf-view/builder/Aspect/AuthAspect.php

58 lines
1.5 KiB
PHP

<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare(strict_types=1);
namespace Builder\Aspect;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Di\Exception\Exception;
use Builder\Annotation\Auth;
use Builder\Exception\TokenException;
/**
* Class AuthAspect
* @package Builder\Aspect
*/
#[Aspect]
class AuthAspect extends AbstractAspect
{
public array $annotations = [
Auth::class
];
/**
* @param ProceedingJoinPoint $proceedingJoinPoint
* @return mixed
* @throws Exception
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
/** @var Auth $auth */
if (isset($proceedingJoinPoint->getAnnotationMetadata()->method[Auth::class])) {
$auth = $proceedingJoinPoint->getAnnotationMetadata()->method[Auth::class];
}
$scene = $auth->scene ?? 'default';
$loginUser = user($scene);
if (! $loginUser->check(null, $scene)) {
throw new TokenException(t('jwt.validate_fail'));
}
return $proceedingJoinPoint->process();
}
}