mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 11:22:10 +08:00
fix : first finish
This commit is contained in:
@@ -11,8 +11,28 @@ declare(strict_types=1);
|
|||||||
namespace App\Common\Repository;
|
namespace App\Common\Repository;
|
||||||
|
|
||||||
use App\Model\AdminRole;
|
use App\Model\AdminRole;
|
||||||
|
use Hyperf\Collection\Arr;
|
||||||
|
use Hyperf\Database\Model\Builder;
|
||||||
|
|
||||||
final class AdminRoleRepository extends BaseRepository
|
final class AdminRoleRepository extends BaseRepository
|
||||||
{
|
{
|
||||||
public function __construct(protected readonly AdminRole $model) {}
|
public function __construct(protected readonly AdminRole $model) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Builder $query
|
||||||
|
* @param array $params
|
||||||
|
* @return Builder
|
||||||
|
*/
|
||||||
|
public function handleSearch(Builder $query, array $params): Builder
|
||||||
|
{
|
||||||
|
return $query->when(Arr::get($params, 'name'), static function (Builder $query, $name) {
|
||||||
|
$query->where('name', 'like', '%' . $name . '%');
|
||||||
|
})->when(Arr::get($params, 'code'), static function (Builder $query, $code) {
|
||||||
|
$query->whereIn('code', Arr::wrap($code));
|
||||||
|
})->when(Arr::has($params, 'status'), static function (Builder $query) use ($params) {
|
||||||
|
$query->where('status', $params['status']);
|
||||||
|
})->when(Arr::get($params, 'created_at'), static function (Builder $query, $createdAt) {
|
||||||
|
$query->whereBetween('created_at', $createdAt);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ use App\Common\Interface\JwtInterface;
|
|||||||
use App\Constants\ResultCode;
|
use App\Constants\ResultCode;
|
||||||
use App\Exception\ErrException;
|
use App\Exception\ErrException;
|
||||||
use App\Middleware\Token\AbstractTokenMiddleware;
|
use App\Middleware\Token\AbstractTokenMiddleware;
|
||||||
|
use Hyperf\Context\Context;
|
||||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
use Lcobucci\JWT\Token\RegisteredClaims;
|
||||||
use Lcobucci\JWT\UnencryptedToken;
|
use Lcobucci\JWT\UnencryptedToken;
|
||||||
use function Hyperf\Support\env;
|
use function Hyperf\Support\env;
|
||||||
@@ -29,4 +30,13 @@ final class AdminTokenMiddleware extends AbstractTokenMiddleware
|
|||||||
|
|
||||||
if ($audience !== env('APP_NAME') .'_admin') throw new ErrException('token错误',ResultCode::JWT_ERROR);
|
if ($audience !== env('APP_NAME') .'_admin') throw new ErrException('token错误',ResultCode::JWT_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UnencryptedToken $token
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setContext(UnencryptedToken $token): void
|
||||||
|
{
|
||||||
|
Context::set('current_admin_id',(int)$token->claims()?->get(RegisteredClaims::ID) ?? 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ class PermissionMiddleware implements MiddlewareInterface
|
|||||||
/**
|
/**
|
||||||
* @var Permission[] $permissions
|
* @var Permission[] $permissions
|
||||||
*/
|
*/
|
||||||
|
$permissions = [];
|
||||||
$classAnnotation && $permissions[] = $classAnnotation;
|
$classAnnotation && $permissions[] = $classAnnotation;
|
||||||
$methodPermission = Arr::get($annotations, Permission::class);
|
$methodPermission = Arr::get($annotations, Permission::class);
|
||||||
$methodPermission && $permissions[] = $methodPermission;
|
$methodPermission && $permissions[] = $methodPermission;
|
||||||
|
|||||||
@@ -68,4 +68,13 @@ class RefreshAdminTokenMiddleware extends AbstractTokenMiddleware
|
|||||||
|
|
||||||
if ($audience !== env('APP_NAME') .'_admin') throw new ErrException('token错误',ResultCode::JWT_ERROR);
|
if ($audience !== env('APP_NAME') .'_admin') throw new ErrException('token错误',ResultCode::JWT_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UnencryptedToken $token
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setContext(UnencryptedToken $token): void
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ abstract class AbstractTokenMiddleware
|
|||||||
$this->checkToken->checkJwt($token);
|
$this->checkToken->checkJwt($token);
|
||||||
$this->checkIssuer($token);
|
$this->checkIssuer($token);
|
||||||
|
|
||||||
|
$this->setContext($token);
|
||||||
|
|
||||||
return $handler->handle(
|
return $handler->handle(
|
||||||
value(
|
value(
|
||||||
static function (ServerRequestPlusInterface $request, UnencryptedToken $token) {
|
static function (ServerRequestPlusInterface $request, UnencryptedToken $token) {
|
||||||
@@ -54,6 +56,12 @@ abstract class AbstractTokenMiddleware
|
|||||||
|
|
||||||
abstract public function getJwt(): JwtInterface;
|
abstract public function getJwt(): JwtInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UnencryptedToken $token
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
abstract public function setContext(UnencryptedToken $token): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
* @return Token
|
* @return Token
|
||||||
|
|||||||
@@ -85,8 +85,8 @@ class AdminMenu extends Model
|
|||||||
return $this->belongsToMany(
|
return $this->belongsToMany(
|
||||||
AdminRole::class,
|
AdminRole::class,
|
||||||
'admin_role_belongs_menu',
|
'admin_role_belongs_menu',
|
||||||
'menu_id',
|
'admin_menu_id',
|
||||||
'role_id'
|
'admin_role_id'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,9 +65,9 @@ class AdminRole extends Model
|
|||||||
{
|
{
|
||||||
return $this->belongsToMany(
|
return $this->belongsToMany(
|
||||||
AdminMenu::class,
|
AdminMenu::class,
|
||||||
'role_belongs_menu',
|
'admin_role_belongs_menu',
|
||||||
'role_id',
|
'admin_role_id',
|
||||||
'menu_id'
|
'admin_menu_id'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,8 +79,8 @@ class AdminRole extends Model
|
|||||||
return $this->belongsToMany(
|
return $this->belongsToMany(
|
||||||
AdminUser::class,
|
AdminUser::class,
|
||||||
'admin_user_belongs_role',
|
'admin_user_belongs_role',
|
||||||
'role_id',
|
'admin_role_id',
|
||||||
'user_id'
|
'admin_user_id'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,13 +36,13 @@ class AdminRoleRequest extends FormRequest
|
|||||||
'sort' => 'required|integer',
|
'sort' => 'required|integer',
|
||||||
'remark' => 'nullable|string|max:255',
|
'remark' => 'nullable|string|max:255',
|
||||||
'permissions' => 'sometimes|array',
|
'permissions' => 'sometimes|array',
|
||||||
'permissions.*' => 'string|exists:menu,name',
|
'permissions.*' => 'string|exists:admin_menu,name',
|
||||||
];
|
];
|
||||||
if ($this->isCreate()) {
|
if ($this->isCreate()) {
|
||||||
$rules['code'][] = 'unique:role,code';
|
$rules['code'][] = 'unique:admin_role,code';
|
||||||
}
|
}
|
||||||
if ($this->isUpdate()) {
|
if ($this->isUpdate()) {
|
||||||
$rules['code'][] = 'unique:role,code,' . $this->route('id');
|
$rules['code'][] = 'unique:admin_role,code,' . $this->route('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class AdminUserRequest extends FormRequest
|
|||||||
'remark' => 'sometimes|string|max:255',
|
'remark' => 'sometimes|string|max:255',
|
||||||
'password' => 'sometimes|string|min:6|max:20',
|
'password' => 'sometimes|string|min:6|max:20',
|
||||||
'role_codes' => 'required|array',
|
'role_codes' => 'required|array',
|
||||||
'role_codes.*' => 'string|exists:role,code',
|
'role_codes.*' => 'string|exists:admin_role,code',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,9 +40,10 @@ class MenuService extends BaseAdminService
|
|||||||
*/
|
*/
|
||||||
public function create(): array
|
public function create(): array
|
||||||
{
|
{
|
||||||
$data = array($this->getRequestData(),[
|
$data = array_merge($this->getRequestData(),[
|
||||||
'created_by' => $this->adminId
|
'created_by' => $this->adminId
|
||||||
]);
|
]);
|
||||||
|
if (empty($data['parent_id'])) $data['parent_id'] = 0;
|
||||||
/**
|
/**
|
||||||
* @var AdminMenu $model
|
* @var AdminMenu $model
|
||||||
*/
|
*/
|
||||||
@@ -75,7 +76,7 @@ class MenuService extends BaseAdminService
|
|||||||
*/
|
*/
|
||||||
public function update(int $id): array
|
public function update(int $id): array
|
||||||
{
|
{
|
||||||
$data = array($this->getRequestData(),[
|
$data = array_merge($this->getRequestData(),[
|
||||||
'updated_by' => $this->adminId
|
'updated_by' => $this->adminId
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ class RoleService extends BaseAdminService
|
|||||||
public function getRole(int $id): array
|
public function getRole(int $id): array
|
||||||
{
|
{
|
||||||
return $this->adminReturn->success(
|
return $this->adminReturn->success(
|
||||||
|
'success',
|
||||||
$this->adminRoleRepository
|
$this->adminRoleRepository
|
||||||
->findById($id)
|
->findById($id)
|
||||||
->adminMenus()
|
->adminMenus()
|
||||||
|
|||||||
@@ -44,17 +44,11 @@ class UserService extends BaseAdminService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected AdminRoleRepository $adminRoleRepository;
|
protected AdminRoleRepository $adminRoleRepository;
|
||||||
|
|
||||||
#[Inject]
|
|
||||||
protected RedisCache $redisCache;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function handle(): array
|
public function handle(): array
|
||||||
{
|
{
|
||||||
$this->redisCache->with()->set('123',1);
|
|
||||||
$this->redisCache->with()->lua(RateLimit::class)->check('user:123', 10, 60);
|
|
||||||
|
|
||||||
return $this->adminReturn->success(
|
return $this->adminReturn->success(
|
||||||
'success',
|
'success',
|
||||||
Arr::only(
|
Arr::only(
|
||||||
@@ -188,7 +182,7 @@ class UserService extends BaseAdminService
|
|||||||
'code' => $this->request->input('role_codes')
|
'code' => $this->request->input('role_codes')
|
||||||
])->map(static function(AdminRole $adminRole) {
|
])->map(static function(AdminRole $adminRole) {
|
||||||
return $adminRole->id;
|
return $adminRole->id;
|
||||||
})->all()
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->adminReturn->success();
|
return $this->adminReturn->success();
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\Admin;
|
namespace App\Service\Admin;
|
||||||
|
|
||||||
|
use App\Exception\ErrException;
|
||||||
use App\Lib\Jwt\RequestScopedTokenTrait;
|
use App\Lib\Jwt\RequestScopedTokenTrait;
|
||||||
use App\Lib\Return\AdminReturn;
|
use App\Lib\Return\AdminReturn;
|
||||||
use Hyperf\Context\Context;
|
use Hyperf\Context\Context;
|
||||||
use Hyperf\Di\Annotation\Inject;
|
use Hyperf\Di\Annotation\Inject;
|
||||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||||
use Lcobucci\JWT\Token\RegisteredClaims;
|
|
||||||
|
|
||||||
abstract class BaseAdminService
|
abstract class BaseAdminService
|
||||||
{
|
{
|
||||||
@@ -36,20 +36,27 @@ abstract class BaseAdminService
|
|||||||
protected AdminReturn $adminReturn;
|
protected AdminReturn $adminReturn;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员 id
|
* @param string $name
|
||||||
* @var int
|
* @return \current_admin_id|mixed
|
||||||
*/
|
*/
|
||||||
protected int $adminId = 0;
|
public function __get(string $name)
|
||||||
|
|
||||||
/**
|
|
||||||
* 主构造函数
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
{
|
||||||
$this->adminId = (int) $this->getToken()?->claims()?->get(RegisteredClaims::ID) ?? 0;
|
if ($name === 'adminId') return Context::get('current_admin_id',0);
|
||||||
if ($this->adminId > 0) Context::set('current_admin_id', $this->adminId);
|
|
||||||
|
if (!property_exists($this, $name)) throw new ErrException('属性未定义');
|
||||||
|
|
||||||
|
return $this->$name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 主构造函数
|
||||||
|
// */
|
||||||
|
// public function __construct()
|
||||||
|
// {
|
||||||
|
// $this->adminId = Context::get('current_admin_id',0);
|
||||||
|
// var_dump('BaseAdminService获取到的'.$this->adminId);
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 主函数抽象类
|
* 主函数抽象类
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ class LoginService extends BaseAdminService
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($adminInfo->status == AdminUserStatusCode::DISABLE) throw new ErrException('用户已禁用');
|
if ($adminInfo->status == AdminUserStatusCode::DISABLE) throw new ErrException('用户已禁用');
|
||||||
|
|
||||||
$jwtHandle = $this->tokenService->setJwt('admin')->getJwt();
|
$jwtHandle = $this->tokenService->setJwt('admin')->getJwt();
|
||||||
|
|
||||||
return $this->adminReturn->success('success',[
|
return $this->adminReturn->success('success',[
|
||||||
|
|||||||
@@ -55,20 +55,4 @@ final class BaseTokenService implements CheckTokenInterface
|
|||||||
$this->jwt = $jwt;
|
$this->jwt = $jwt;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param UnencryptedToken $token
|
|
||||||
* @return \Closure
|
|
||||||
*/
|
|
||||||
public function refreshToken(UnencryptedToken $token): \Closure
|
|
||||||
{
|
|
||||||
return value(static function (JwtInterface $jwt) use ($token) {
|
|
||||||
$jwt->addBlackList($token);
|
|
||||||
return [
|
|
||||||
'access_token' => $jwt->builderAccessToken($token->claims()->get(RegisteredClaims::ID))->toString(),
|
|
||||||
'refresh_token' => $jwt->builderRefreshToken($token->claims()->get(RegisteredClaims::ID))->toString(),
|
|
||||||
'expire_at' => (int) $jwt->getConfig('ttl', 0),
|
|
||||||
];
|
|
||||||
}, $this->getJwt());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user