Commit 96ad1bd2 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add routes to get details about given tokens for a user

- Added array_values to every array_filter to reindex arrays
parent 8e22ffcb
Pipeline #5595 passed with stages
in 3 minutes and 54 seconds
......@@ -11,9 +11,11 @@ use Source\Exceptions\AuthenticationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Users\UpdateUser\APIPresenter as UpdateUserAPIPresenter;
use Source\UseCases\GroupUser\GetUserGroups\APIPresenter as GetUserGroupsAPIPresenter;
use Source\UseCases\TokenUser\GetUserTokens\APIPresenter as GetUserTokensAPIPresenter;
class MeController extends ApiController
{
......@@ -98,4 +100,18 @@ class MeController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCase $userTokens
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function tokens(GetUserTokensUseCase $userTokens): JsonResponse
{
$presenter = new GetUserTokensAPIPresenter();
$userTokens->getTokensForUser($this->userId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -12,6 +12,7 @@ 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\GroupUser\AddUserToGroup\AddUserToGroupUseCase;
use Source\UseCases\Users\GetUser\APIPresenter as GetUserAPIPresenter;
use Source\UseCases\Users\GetAllUsers\APIPresenter as AllUsersAPIPresenter;
......@@ -20,6 +21,7 @@ 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;
......@@ -210,4 +212,22 @@ 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());
}
}
......@@ -38,6 +38,7 @@ 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\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
......@@ -113,6 +114,9 @@ class AppServiceProvider extends ServiceProvider
ExpireTokenUseCaseServiceProvider::class,
UpdateTokenUseCaseServiceProvider::class,
GetAllTokensUseCaseServiceProvider::class,
// TokenUser
GetUserTokensUseCaseServiceProvider::class,
];
/**
......
......@@ -35,6 +35,7 @@ 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::group([
......@@ -79,7 +80,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
'prefix' => 'me',
], static function () {
Route::get('/', [MeController::class, 'index']);
Route::get('/groups', [MeController::class, 'groups']);
Route::get('groups', [MeController::class, 'groups']);
Route::get('tokens', [MeController::class, 'tokens']);
Route::patch('/', [MeController::class, 'update']);
});
});
......@@ -192,4 +192,25 @@ class Token
{
return !$this->isValid();
}
/**
* @param string|null $id
* @return bool
*/
public function hasUserIdOf(?string $id): bool
{
if (!$id) {
return false;
}
return $this->userId === (int)$id;
}
/**
* @return bool
*/
public function hasName(): bool
{
return (bool)$this->name;
}
}
......@@ -101,12 +101,12 @@ class InMemoryDoorGroupRepository implements DoorGroupRepository
public function removeDoorFromGroup(string $doorId, string $groupId): void
{
if (isset($this->doorMap[$doorId])) {
$this->doorMap[$doorId] = array_filter(
$this->doorMap[$doorId] = array_values(array_filter(
$this->doorMap[$doorId],
static function (string $searchGroup) use ($groupId) {
return $searchGroup !== $groupId;
}
);
));
}
}
}
......@@ -99,9 +99,9 @@ class InMemoryDoorsRepository implements DoorsRepository
*/
public function delete(string $doorId): bool
{
$this->doors = array_filter($this->doors, static function (Door $door) use ($doorId) {
$this->doors = array_values(array_filter($this->doors, static function (Door $door) use ($doorId) {
return !$door->hasIdOf($doorId);
});
}));
return true;
}
......
......@@ -103,12 +103,12 @@ class InMemoryGroupUserRepository implements GroupUserRepository
public function removeUserFromGroup(string $userId, string $groupId): void
{
if (isset($this->groupMap[$userId])) {
$this->groupMap[$userId] = array_filter(
$this->groupMap[$userId] = array_values(array_filter(
$this->groupMap[$userId],
static function (string $searchGroup) use ($groupId) {
return $searchGroup !== $groupId;
}
);
));
}
}
}
......@@ -89,9 +89,9 @@ class InMemoryGroupsRepository implements GroupsRepository
*/
public function delete(string $groupId): bool
{
$this->groups = array_filter($this->groups, static function (Group $group) use ($groupId) {
$this->groups = array_values(array_filter($this->groups, static function (Group $group) use ($groupId) {
return !$group->hasIdOf($groupId);
});
}));
return true;
}
......
......@@ -160,4 +160,16 @@ class DatabaseTokensRepository implements TokensRepository
return self::dbTokenToToken($token);
}, $tokens);
}
/**
* @inheritDoc
*/
public function getTokensForUser(string $userId): array
{
$tokens = \App\Token::whereNotNull('name')->where('user_id', '=', $userId)->get();
return array_map(static function (\App\Token $token) {
return self::dbTokenToToken($token);
}, $tokens);
}
}
......@@ -9,7 +9,7 @@ use Source\Exceptions\EntityNotFoundException;
class InMemoryTokensRepository implements TokensRepository
{
protected static int $id = 1;
protected static int $idCounter = 100;
/**
* @var Token[]
......@@ -19,7 +19,10 @@ class InMemoryTokensRepository implements TokensRepository
/** @inheritDoc */
public function create(Token $token): Token
{
$token->setId(static::$id++);
if ($token->hasIdOf(0)) {
$token->setId(static::$idCounter++);
}
$this->tokens[] = $token;
return $token;
......@@ -50,6 +53,16 @@ class InMemoryTokensRepository implements TokensRepository
}
/**
* @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);
}));
}
/** @inheritDoc */
public function findValidToken(string $tokenToMatch): ?Token
{
......
......@@ -34,6 +34,12 @@ interface TokensRepository
*/
public function all(): array;
/**
* @param string $userId
* @return \Source\Entities\Token[]
*/
public function getTokensForUser(string $userId): array;
/**
* @param string $token
* @return Token|null
......
......@@ -88,9 +88,9 @@ class InMemoryUsersRepository implements UsersRepository
*/
public function delete(string $userId): bool
{
$this->users = array_filter($this->users, static function (User $user) use ($userId) {
$this->users = array_values(array_filter($this->users, static function (User $user) use ($userId) {
return !$user->hasIdOf($userId);
});
}));
return true;
}
......
<?php
namespace Source\UseCases\TokenUser\GetUserTokens;
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\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;
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;
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;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment