Commit 0962866d authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add filtering for Tokens index route also remove the token user as it is

now redundant
parent cfa4e1c0
......@@ -9,15 +9,15 @@ use Illuminate\Contracts\Auth\Guard;
use Source\Authorization\Authorizer;
use Source\Exceptions\AuthenticationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use Source\UseCases\Tokens\GetTokens\GetTokensUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase;
use Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Tokens\GetTokens\APIPresenter as GetTokensApiPresenter;
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
use Source\UseCases\DoorUser\UserDoorAccess\MePresenter as DoorUserMePresenter;
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
use Source\UseCases\TokenUser\GetUserTokens\APIPresenter as GetUserTokensAPIPresenter;
class MeController extends ApiController
{
......@@ -101,15 +101,14 @@ class MeController extends ApiController
}
/**
* @param \Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase $userTokens
* @param \Source\UseCases\Tokens\GetTokens\GetTokensUseCase $userTokens
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function tokens(GetUserTokensUseCase $userTokens): JsonResponse
public function tokens(GetTokensUseCase $userTokens): JsonResponse
{
$presenter = new GetUserTokensAPIPresenter();
$presenter = new GetTokensApiPresenter();
$userTokens->getTokensForUser($this->userId, $presenter);
$userTokens->filter($this->userId, null, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
......
......@@ -9,9 +9,9 @@ use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Overrides\ApiPresenter;
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase;
use Source\UseCases\Overrides\OverridesGet\OverridesGetUseCase;
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase;
use Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase;
use Source\UseCases\Overrides\OverridesGet\OverridesGetUseCase;
use Source\UseCases\Overrides\OverrideCreate\ApiPresenter as OverrideCreateApiPresenter;
class OverridesController extends ApiController
......
......@@ -9,7 +9,6 @@ use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCase;
use Source\UseCases\Schedules\APIPresenter as ScheduleApiPresenter;
use Source\UseCases\Schedules\ScheduleCreate\ScheduleCreateUseCase;
use Source\UseCases\Schedules\ScheduleUpdate\ScheduleUpdateUseCase;
use Source\UseCases\GroupSchedule\SchedulesForGroup\SchedulesForGroupUseCase;
use Source\UseCases\GroupSchedule\APIPresenter as SchedulesForGroupApiPresenter;
use Source\UseCases\Schedules\SchedulesGet\APIPresenter as SchedulesGetApiPresenter;
use Source\UseCases\GroupSchedule\ActiveSchedulesForGroup\ActiveSchedulesForGroupUseCase;
......@@ -70,7 +69,7 @@ class SchedulesController extends ApiController
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function between(SchedulesGetUseCase $schedulesBetween): JsonResponse
public function index(SchedulesGetUseCase $schedulesBetween): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
......
......@@ -5,12 +5,12 @@ namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Tokens\GetToken\GetTokenUseCase;
use Source\UseCases\Tokens\GetTokens\GetTokensUseCase;
use Source\UseCases\Tokens\CreateToken\CreateTokenUseCase;
use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCase;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCase;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCase;
use Source\UseCases\Tokens\GetToken\APIPresenter as GetTokenAPIPresenter;
use Source\UseCases\Tokens\GetAllTokens\APIPresenter as AllTokensAPIPresenter;
use Source\UseCases\Tokens\GetTokens\APIPresenter as AllTokensAPIPresenter;
use Source\UseCases\Tokens\CreateToken\APIPresenter as CreateTokenAPIPresenter;
use Source\UseCases\Tokens\ExpireToken\APIPresenter as ExpireTokenAPIPresenter;
use Source\UseCases\Tokens\UpdateToken\APIPresenter as UpdateTokenAPIPresenter;
......@@ -18,20 +18,32 @@ use Source\UseCases\Tokens\UpdateToken\APIPresenter as UpdateTokenAPIPresenter;
class TokensController extends ApiController
{
/**
* @param \Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCase $allTokens
* @param \Source\UseCases\Tokens\GetTokens\GetTokensUseCase $allTokens
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function index(GetAllTokensUseCase $allTokens): JsonResponse
public function index(GetTokensUseCase $allTokens): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_USERS);
$this->validate($this->request, [
'user_id' => 'integer',
'valid_at' => 'date',
]);
$presenter = new AllTokensAPIPresenter();
$allTokens->all($presenter);
$allTokens->filter(
$this->request->input('user_id'),
$this->request->input('valid_at'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
return $this->respondWithData($presenter->getViewModel([
'user_id' => $this->request->input('user_id'),
'valid_at' => $this->request->input('valid_at'),
]));
}
/**
......
......@@ -12,7 +12,6 @@ use Source\UseCases\Users\DeleteUser\DeleteUserUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCase;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase;
use Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase;
use Source\UseCases\DoorUser\UserDoorAccess\PrivilegedApiPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase;
......@@ -23,7 +22,6 @@ use Source\UseCases\Users\DeleteUser\APIPresenter as DeleteUserAPIPresenter;
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase;
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
use Source\UseCases\TokenUser\GetUserTokens\APIPresenter as GetUserTokensAPIPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as AddUserToGroupAPIPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\APIPresenter as RemoveUserFromGroupAPIPresenter;
......@@ -211,24 +209,6 @@ class UsersController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase $userTokens
* @param string $userId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function tokens(GetUserTokensUseCase $userTokens, string $userId): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new GetUserTokensAPIPresenter();
$userTokens->getTokensForUser($userId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase $userDoorAccessUseCase
* @param string $userId
......
......@@ -24,6 +24,7 @@ use Source\UseCases\Doors\GetDoors\GetDoorsUseCaseServiceProvider;
use Source\Gateways\Filesystem\FilesystemRepositoryServiceProvider;
use Source\UseCases\Groups\GetGroup\GetGroupUseCaseServiceProvider;
use Source\UseCases\Tokens\GetToken\GetTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\GetTokens\GetTokensUseCaseServiceProvider;
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCaseServiceProvider;
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCaseServiceProvider;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCaseServiceProvider;
......@@ -43,17 +44,15 @@ use Source\UseCases\Tokens\ExpireToken\ExpireTokenUseCaseServiceProvider;
use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCaseServiceProvider;
use Source\UseCases\Schedules\ScheduleGet\ScheduleGetUseCaseServiceProvider;
use Source\UseCases\Door\StatusResponse\StatusResponseUseCaseServiceProvider;
use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCaseServiceProvider;
use Source\UseCases\Overrides\OverridesGet\OverridesGetUseCaseServiceProvider;
use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCaseServiceProvider;
use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCaseServiceProvider;
use Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCaseServiceProvider;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
......@@ -146,12 +145,9 @@ class AppServiceProvider extends ServiceProvider
CreateTokenUseCaseServiceProvider::class,
ExpireTokenUseCaseServiceProvider::class,
UpdateTokenUseCaseServiceProvider::class,
GetAllTokensUseCaseServiceProvider::class,
GetTokensUseCaseServiceProvider::class,
TokenAuthenticateUseCaseServiceProvider::class,
// TokenUser
GetUserTokensUseCaseServiceProvider::class,
// Attempts
GetAttemptsUseCaseServiceProvider::class,
......
......@@ -39,7 +39,6 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{userId}/groups', [UsersController::class, 'getGroupsForUser']);
Route::post('{userId}/group/{groupId}', [UsersController::class, 'addUserToGroup']);
Route::delete('{userId}/group/{groupId}', [UsersController::class, 'removeUserFromGroup']);
Route::get('{userId}/tokens', [UsersController::class, 'tokens']);
Route::get('{userId}/doors', [UsersController::class, 'doors']);
});
......@@ -76,9 +75,9 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::group([
'prefix' => 'schedules',
], static function () {
Route::get('/', [SchedulesController::class, 'between']);
Route::get('{scheduleId}', [SchedulesController::class, 'get']);
Route::get('/', [SchedulesController::class, 'index']);
Route::post('/', [SchedulesController::class, 'create']);
Route::get('{scheduleId}', [SchedulesController::class, 'get']);
Route::patch('{scheduleId}', [SchedulesController::class, 'update']);
});
......@@ -86,8 +85,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
'prefix' => 'tokens',
], static function () {
Route::get('/', [TokensController::class, 'index']);
Route::get('{tokenId}', [TokensController::class, 'get']);
Route::post('/', [TokensController::class, 'store']);
Route::get('{tokenId}', [TokensController::class, 'get']);
Route::patch('{tokenId}', [TokensController::class, 'update']);
Route::post('{tokenId}/expire', [TokensController::class, 'expire']);
});
......@@ -107,13 +106,12 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::group([
'prefix' => 'overrides',
], static function () {
Route::post('/', [OverridesController::class, 'create']);
Route::get('/', [OverridesController::class, 'index']);
Route::patch('{overrideId}', [OverridesController::class, 'update']);
Route::post('/', [OverridesController::class, 'create']);
Route::get('{overrideId}', [OverridesController::class, 'get']);
Route::patch('{overrideId}', [OverridesController::class, 'update']);
});
Route::group([
'prefix' => 'me',
], static function () {
......
......@@ -159,24 +159,23 @@ class DatabaseTokensRepository implements TokensRepository
/**
* @inheritDoc
*/
public function all(): array
public function filter(?string $userId = null, ?Carbon $validAt = null): array
{
$tokens = \App\Token::query()->whereNotNull('name')->get()->values()->all();
$query = \App\Token::query()->whereNotNull('name');
return array_map(static function (\App\Token $token) {
return self::dbTokenToToken($token);
}, $tokens);
}
if ($userId) {
$query->where('user_id', $this->castToInt($userId));
}
/**
* @inheritDoc
*/
public function getTokensForUser(string $userId): array
{
$tokens = \App\Token::query()->whereNotNull('name')->where('user_id', '=', $userId)->get()->values()->all();
if ($validAt) {
$query->where(static function (Builder $builder) use ($validAt) {
$builder->where('expires_at', '>', $validAt)
->orWhereNull('expires_at');
});
}
return array_map(static function (\App\Token $token) {
return self::dbTokenToToken($token);
}, $tokens);
}, $query->get()->values()->all());
}
}
......@@ -45,21 +45,24 @@ class InMemoryTokensRepository implements TokensRepository
}
/**
* @param string|null $userId
* @param \Carbon\Carbon|null $validAt
* @return Token[]
*/
public function all(): array
public function filter(?string $userId = null, ?Carbon $validAt = null): array
{
return $this->tokens;
}
return array_values(array_filter($this->tokens, static function (Token $token) use ($userId, $validAt) {
$include = true;
if ($validAt) {
$include = $token->isValidAtTime($validAt);
}
/**
* @inheritDoc
*/
public function getTokensForUser(string $userId): array
{
return array_values(array_filter($this->tokens, static function (Token $token) use ($userId) {
return $token->hasName() && $token->hasUserIdOf($userId);
if ($userId !== null) {
$include = $include && $token->hasUserIdOf($userId);
}
return $include;
}));
}
......
......@@ -3,6 +3,7 @@
namespace Source\Gateways\Tokens;
use Carbon\Carbon;
use Source\Entities\Token;
use Source\Exceptions\EntityNotFoundException;
......@@ -30,15 +31,11 @@ interface TokensRepository
public function get(string $tokenId): ?Token;
/**
* @param string|null $userId
* @param \Carbon\Carbon|null $validAt
* @return \Source\Entities\Token[]
*/
public function all(): array;
/**
* @param string $userId
* @return \Source\Entities\Token[]
*/
public function getTokensForUser(string $userId): array;
public function filter(?string $userId = null, ?Carbon $validAt = null): array;
/**
* @param string $token
......
......@@ -28,7 +28,10 @@ class SchedulesGet implements SchedulesGetUseCase
public function filter(?string $groupId, ?int $type, ?string $begin, ?string $end, Presenter $presenter): void
{
$schedules = $this->schedules->filter(
$groupId, $type, $this->liberalCastToCarbon($begin), $this->liberalCastToCarbon($end)
$groupId,
$type,
$this->liberalCastToCarbon($begin),
$this->liberalCastToCarbon($end)
);
$response = new ResponseModel($schedules);
......
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Tokens\TokensRepository;
use Source\Exceptions\EntityNotFoundException;
class GetUserTokens implements GetUserTokensUseCase
{
/**
* @var \Source\Gateways\Tokens\TokensRepository
*/
protected TokensRepository $tokens;
/**
* @var \Source\Gateways\Users\UsersRepository
*/
protected UsersRepository $users;
public function __construct(TokensRepository $tokens, UsersRepository $users)
{
$this->users = $users;
$this->tokens = $tokens;
}
/**
* @inheritDoc
*/
public function getTokensForUser(string $userId, Presenter $presenter): void
{
if (!$this->users->get($userId)) {
throw new EntityNotFoundException();
}
$tokens = $this->tokens->getTokensForUser($userId);
$response = new ResponseModel($tokens);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
interface GetUserTokensUseCase
{
/**
* @param string $userId
* @param \Source\UseCases\TokenUser\GetUserTokens\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getTokensForUser(string $userId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Tokens\TokensRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetUserTokensUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetUserTokensUseCase::class, static function (Application $app) {
return new GetUserTokens($app->make(TokensRepository::class), $app->make(UsersRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetUserTokensUseCase::class];
}
}
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
class ResponseModel
{
/**
* @var \Source\Entities\Token[]
*/
protected array $tokens;
/**
* @param \Source\Entities\Token[] $tokens
*/
public function __construct(array $tokens)
{
$this->tokens = $tokens;
}
/**
* @return \Source\Entities\Token[]
*/
public function getTokens(): array
{
return $this->tokens;
}
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
use Source\Entities\Token;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['tokens'] = array_map(function (Token $token) {
return $this->formatToken($token);
}, $responseModel->getTokens());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
interface GetAllTokensUseCase
{
/**
* @param \Source\UseCases\Tokens\GetAllTokens\Presenter $presenter
*/
public function all(Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Tokens\GetAllTokens;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
namespace Source\UseCases\Tokens\GetTokens;
use Source\Entities\Token;
use Source\Sanitize\Paginates;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
use Paginates;
protected array $tokens = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['tokens'] = array_map(function (Token $token) {
$this->tokens = array_map(function (Token $token) {
return $this->formatToken($token);
}, $responseModel->getTokens());
}
/** @inheritDoc */
public function getViewModel(): array
public function getViewModel(array $appends = []): array
{
return $this->viewModel;
return $this->paginate($this->tokens, $appends);
}