Adds share feature
This commit is contained in:
parent
287c1f67c5
commit
15a9dcf09b
14 changed files with 379 additions and 23 deletions
63
src/php/Routing/Api/DataTables/SharedUsersEndpoint.php
Normal file
63
src/php/Routing/Api/DataTables/SharedUsersEndpoint.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace GamesShop\Routing\Api\DataTables;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use GamesShop\Entities\Account\User;
|
||||
use GamesShop\Entities\GamesList;
|
||||
use GamesShop\Login\LoginHandler;
|
||||
use GamesShop\Login\UserPermission;
|
||||
use Laminas\Diactoros\Response\JsonResponse;
|
||||
use League\Route\Http\Exception\BadRequestException;
|
||||
use League\Route\Http\Exception\ForbiddenException;
|
||||
use League\Route\Http\Exception\UnauthorizedException;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
final class SharedUsersEndpoint
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LoginHandler $loginHandler,
|
||||
private readonly EntityManager $entityManager,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @throws UnauthorizedException
|
||||
* @throws ForbiddenException
|
||||
*/
|
||||
public function __invoke(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if (!$this->loginHandler->isLoggedIn()) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
$user = $this->loginHandler->getCurrentUser();
|
||||
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
$body = $request->getQueryParams();
|
||||
if (!array_key_exists('listid', $body)) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
|
||||
$claimer = $list->getClaimer();
|
||||
|
||||
return new JsonResponse(
|
||||
[ 'data' => $claimer
|
||||
->filter(fn ($claimerUser) => $claimerUser !== $user)
|
||||
->map(
|
||||
function (User $user) {
|
||||
|
||||
return [
|
||||
'id' => $user->getId(),
|
||||
'name' => $user->getName(),
|
||||
'icon' => $user->getProfilePictureUrl()
|
||||
];
|
||||
}
|
||||
)->toArray()
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue