mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 07:52:10 +08:00
feat : admin user finish
This commit is contained in:
39
app/Annotation/Permission.php
Normal file
39
app/Annotation/Permission.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AbstractAnnotation;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
||||
class Permission extends AbstractAnnotation
|
||||
{
|
||||
public const string OPERATION_AND = 'and';
|
||||
|
||||
public const string OPERATION_OR = 'or';
|
||||
|
||||
/**
|
||||
* @param array|string $code
|
||||
* @param string $operation
|
||||
*/
|
||||
public function __construct(
|
||||
protected array|string $code,
|
||||
protected string $operation = self::OPERATION_AND
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getCode(): array
|
||||
{
|
||||
return (array) $this->code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getOperation(): string
|
||||
{
|
||||
return $this->operation;
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,10 @@
|
||||
namespace App\Annotation;
|
||||
|
||||
use Attribute;
|
||||
use Hyperf\Di\Annotation\AbstractAnnotation;
|
||||
|
||||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)]
|
||||
class ResponseFormat
|
||||
class ResponseFormat extends AbstractAnnotation
|
||||
{
|
||||
/**
|
||||
* @param string $format
|
||||
|
||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Annotation\Permission;
|
||||
use App\Annotation\ResponseFormat;
|
||||
use App\Middleware\Token\AdminTokenMiddleware;
|
||||
use App\Service\Admin\AdminUser\UserService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
@@ -18,13 +20,19 @@ use Hyperf\Validation\Annotation\Scene;
|
||||
#[Middleware(AdminTokenMiddleware::class)]
|
||||
class AdminUserController
|
||||
{
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserService $service;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "getInfo", methods: "GET")]
|
||||
public function getInfo(): array
|
||||
{
|
||||
return (new UserService)->handle();
|
||||
return $this->service->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,6 +41,85 @@ class AdminUserController
|
||||
#[RequestMapping(path: "logout", methods: "POST")]
|
||||
public function logout(): array
|
||||
{
|
||||
return (new UserService)->logout();
|
||||
return $this->service->logout();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
#[Permission(code: 'permission:user:index')]
|
||||
public function pageList(): array
|
||||
{
|
||||
return $this->service->list();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "PUT")]
|
||||
#[Permission(code: 'permission:user:update')]
|
||||
public function updateInfo(): array
|
||||
{
|
||||
return $this->service->updateInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "password", methods: "PUT")]
|
||||
#[Permission(code: 'permission:user:password')]
|
||||
public function resetPassword(): array
|
||||
{
|
||||
return $this->service->resetPassword();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "POST")]
|
||||
#[Permission(code: 'permission:user:save')]
|
||||
public function createAdminUser(): array
|
||||
{
|
||||
return $this->service->createUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "", methods: "DELETE")]
|
||||
#[Permission(code: 'permission:user:delete')]
|
||||
public function deleteAdminUser(): array
|
||||
{
|
||||
return $this->service->deleteUser();
|
||||
}
|
||||
|
||||
#[RequestMapping(path: "{userId}", methods: "PUT")]
|
||||
#[Permission(code: 'permission:user:update')]
|
||||
public function saveInfo(int $userId): array
|
||||
{
|
||||
return $this->service->saveUser($userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "{userId}/roles", methods: "GET")]
|
||||
#[Permission(code: 'permission:user:getRole')]
|
||||
public function getAdminUserRole(int $userId): array
|
||||
{
|
||||
return $this->service->getUserRole($userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "{userId}/roles", methods: "PUT")]
|
||||
#[Permission(code: 'permission:user:setRole')]
|
||||
public function batchGrantRolesForAdminUser(int $userId): array
|
||||
{
|
||||
return $this->service->batchGrantRoleForUser($userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ErrException extends ServerException
|
||||
*/
|
||||
protected array $data = [];
|
||||
|
||||
public function __construct(string $message = "", int $code = 0, array $data = [], ?Throwable $previous = null)
|
||||
public function __construct(string $message = 'failed', int $code = 0, array $data = [], ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
$this->data = $data;
|
||||
|
||||
18
app/Repository/AdminRoleRepository.php
Normal file
18
app/Repository/AdminRoleRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Model\AdminRole;
|
||||
|
||||
final class AdminRoleRepository extends BaseRepository
|
||||
{
|
||||
public function __construct(protected readonly AdminRole $model) {}
|
||||
}
|
||||
@@ -11,6 +11,9 @@ declare(strict_types=1);
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Model\AdminUser;
|
||||
use Hyperf\Database\Concerns\BuildsQueries;
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\Database\Model\Model;
|
||||
|
||||
/**
|
||||
* Class AdminUserRepository
|
||||
@@ -31,13 +34,4 @@ final class AdminUserRepository extends BaseRepository
|
||||
->where('username', $username)
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $id
|
||||
* @return array|null
|
||||
*/
|
||||
public function findById(mixed $id): ?array
|
||||
{
|
||||
return $this->getQuery()->whereKey($id)->first()?->toArray() ?? null;
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,10 @@ namespace App\Service\Admin\AdminUser;
|
||||
|
||||
use App\Cache\Redis\Lua\RateLimit;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Exception\ErrException;
|
||||
use App\Lib\Jwt\RequestScopedTokenTrait;
|
||||
use App\Model\AdminRole;
|
||||
use App\Repository\AdminRoleRepository;
|
||||
use App\Repository\AdminUserRepository;
|
||||
use App\Service\Admin\BaseAdminService;
|
||||
use App\Service\BaseTokenService;
|
||||
@@ -31,9 +34,24 @@ class UserService extends BaseAdminService
|
||||
#[Inject]
|
||||
protected BaseTokenService $tokenService;
|
||||
|
||||
/**
|
||||
* @var AdminUserRepository
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminUserRepository $adminUserRepository;
|
||||
|
||||
/**
|
||||
* @var AdminRoleRepository
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminRoleRepository $adminRoleRepository;
|
||||
|
||||
#[Inject]
|
||||
protected RedisCache $redisCache;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$this->redisCache->with()->set('123',1);
|
||||
@@ -48,11 +66,6 @@ class UserService extends BaseAdminService
|
||||
);
|
||||
}
|
||||
|
||||
private function user()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -62,4 +75,127 @@ class UserService extends BaseAdminService
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
return $this->adminReturn->success('success',$this->adminUserRepository->page(
|
||||
$this->getRequestData(),
|
||||
$this->getCurrentPage(),
|
||||
$this->getPageSize()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function updateInfo(): array
|
||||
{
|
||||
$res = $this->adminUserRepository->updateById($this->adminId,Arr::except($this->getRequestData(),['password']));
|
||||
|
||||
if (!$res) throw new ErrException('修改失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function resetPassword(): array
|
||||
{
|
||||
$adminUserInfo = $this->adminUserRepository->findById($this->adminId);
|
||||
if (!$adminUserInfo) throw new ErrException('用户异常');
|
||||
|
||||
$adminUserInfo->resetPassword();
|
||||
if (!$adminUserInfo->save()) throw new ErrException('保存密码失败');
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function createUser(): array
|
||||
{
|
||||
if (! $this->adminUserRepository->create(array_merge(
|
||||
$this->getRequestData(),
|
||||
['created_by' => $this->adminId]
|
||||
))) throw new ErrException();
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function deleteUser(): array
|
||||
{
|
||||
if (! $this->adminUserRepository->deleteById($this->getRequestData())) throw new ErrException();
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function saveUser(int $userId): array
|
||||
{
|
||||
if (! $this->adminUserRepository->updateById(
|
||||
$userId,
|
||||
array_merge(
|
||||
$this->getRequestData(),
|
||||
['updated_by' => $this->adminId]
|
||||
)
|
||||
)) throw new ErrException();
|
||||
|
||||
return $this->adminReturn->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function getUserRole(int $userId): array
|
||||
{
|
||||
$userInfo = $this->adminUserRepository->findById($userId);
|
||||
|
||||
if (!$userInfo) throw new ErrException('获取用户信息失败');
|
||||
|
||||
return $this->adminReturn->success(
|
||||
'success',
|
||||
$userInfo->roles()->get()->map(
|
||||
static fn (AdminRole $adminRole) => $adminRole->only([
|
||||
'id','code','name'
|
||||
])
|
||||
)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
public function batchGrantRoleForUser(int $userId): array
|
||||
{
|
||||
$userInfo = $this->adminUserRepository->findById($userId);
|
||||
|
||||
if (!$userInfo) throw new ErrException('获取用户信息失败');
|
||||
|
||||
try {
|
||||
$userInfo->roles()->sync(
|
||||
$this->adminRoleRepository->list([
|
||||
'code' => $this->request->input('role_codes')
|
||||
])->map(static function(AdminRole $adminRole) {
|
||||
return $adminRole->id;
|
||||
})->all()
|
||||
);
|
||||
|
||||
return $this->adminReturn->success();
|
||||
} catch (\Throwable $e) {
|
||||
throw new ErrException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,4 +52,28 @@ abstract class BaseAdminService
|
||||
* 主函数抽象类
|
||||
*/
|
||||
abstract public function handle();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function getCurrentPage(): int
|
||||
{
|
||||
return (int) $this->request->input('page', 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function getPageSize(): int
|
||||
{
|
||||
return (int) $this->request->input('page_size', 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getRequestData(): array
|
||||
{
|
||||
return $this->request->all();
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ trait AdminUserTrait
|
||||
return json_decode($userInfo,true);
|
||||
}
|
||||
|
||||
$userInfo = $this->adminUserRepository->findById($adminId);
|
||||
$userInfo = $this->adminUserRepository->findById($adminId)?->toArray() ?? null;
|
||||
if (!$userInfo) return null;
|
||||
|
||||
Context::set($key, $userInfo);
|
||||
|
||||
@@ -16,6 +16,27 @@ Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{admin_token}}
|
||||
|
||||
|
||||
### 列表
|
||||
GET {{host}}/admin/adminUser/list
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{admin_token}}
|
||||
|
||||
|
||||
### 修改用户资料
|
||||
PUT {{host}}/admin/adminUser
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{admin_token}}
|
||||
|
||||
nickname=超超级管理员
|
||||
|
||||
|
||||
|
||||
### 获取角色
|
||||
GET {{host}}/admin/adminUser/1/roles
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{admin_token}}
|
||||
|
||||
|
||||
### 登录
|
||||
POST {{host}}/admin/login/refresh
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
Reference in New Issue
Block a user