Implemented account system
This commit is contained in:
parent
51c20b55a0
commit
ace0de4063
25 changed files with 1543 additions and 40 deletions
80
src/php/Routing/Api/DataTables/AccountsEndpoint.php
Normal file
80
src/php/Routing/Api/DataTables/AccountsEndpoint.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Routing\Api\DataTables;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\Common\Collections\Expr\Comparison;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use GamesShop\Entities\Account\User;
|
||||
use GamesShop\Login\LoginHandler;
|
||||
use GamesShop\Login\UserPermission;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use League\Route\Http\Exception\ForbiddenException;
|
||||
use League\Route\Http\Exception\UnauthorizedException;
|
||||
use League\Route\RouteGroup;
|
||||
use League\Route\Router;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
final class AccountsEndpoint
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LoginHandler $loginHandler,
|
||||
private readonly EntityManager $entityManager,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if (!$this->loginHandler->isLoggedIn()) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
$user = $this->loginHandler->getCurrentUser();
|
||||
if (!$user->getPermission()->hasLevel(UserPermission::ADMIN)) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
|
||||
$params = $request->getQueryParams();
|
||||
$draw = $params['draw'];
|
||||
$start = $params['start'];
|
||||
$length = $params['length'];
|
||||
|
||||
$searchValue = $params['search']['value'];
|
||||
|
||||
$repo = $this->entityManager->getRepository(User::class);
|
||||
$total = $repo->count();
|
||||
|
||||
$criteria = Criteria::create();
|
||||
$criteria->where(Criteria::expr()->contains('name', $searchValue));
|
||||
$criteria->setFirstResult((int)$start);
|
||||
$criteria->setMaxResults((int)$length);
|
||||
|
||||
$values = $repo->matching($criteria);
|
||||
$filteredCount = $values->count();
|
||||
|
||||
return new JsonResponse([
|
||||
'draw' => $draw,
|
||||
'recordsTotal' => $total,
|
||||
'recordsFiltered' => $filteredCount,
|
||||
'data' =>
|
||||
$values->map(function (User $user) {
|
||||
return [
|
||||
'userid' => $user->getId(),
|
||||
'name' => $user->getName(),
|
||||
'profilePictureUrl' => $user->getProfilePictureUrl(),
|
||||
'permission' => $user->getPermission()->getHumanReadableName(),
|
||||
'permissionIndex' => $user->getPermission()->value,
|
||||
'loginMethod' => $user->getLoginMethod()->getHumanReadableName(),
|
||||
];
|
||||
})->toArray()
|
||||
]);
|
||||
}
|
||||
|
||||
public static function applyRoutes(RouteGroup $router) {
|
||||
$router->get('/accounts', AccountsEndpoint::class);
|
||||
}
|
||||
}
|
14
src/php/Routing/Api/DataTables/DataTablesAPIRoutes.php
Normal file
14
src/php/Routing/Api/DataTables/DataTablesAPIRoutes.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GamesShop\Routing\Api\DataTables;
|
||||
|
||||
use League\Route\RouteGroup;
|
||||
use League\Route\Router;
|
||||
|
||||
final class DataTablesAPIRoutes
|
||||
{
|
||||
public static function setupRoutes(RouteGroup $group): void {
|
||||
AccountsEndpoint::applyRoutes($group);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue