Commit d2edd3c2 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Fix tests for new door authorizer

parent 6abd38ce
......@@ -56,7 +56,7 @@ class DoorController extends ApiController
*/
public function access(string $doorcode, AccessUseCase $access): JsonResponse
{
$access->protectUserDoorAccess($this->doorGuard->id(), $doorcode);
$access->protectUserDoorAccessAtTime($this->doorGuard->id(), $doorcode, Carbon::now());
return $this->respondStatus();
}
......
......@@ -32,7 +32,6 @@ use Source\Gateways\DoorSchedule\DoorScheduleRepositoryServiceProvider;
use Source\UseCases\Doors\GetAllDoors\GetAllDoorsUseCaseServiceProvider;
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCaseServiceProvider;
use Source\Gateways\RecurrenceSet\RecurrenceSetRepositoryServiceProvider;
use Source\UseCases\DoorUserGroup\DoorUserGroupMapUseCaseServiceProvider;
use Source\UseCases\Groups\CreateGroup\CreateGroupUseCaseServiceProvider;
use Source\UseCases\Groups\DeleteGroup\DeleteGroupUseCaseServiceProvider;
use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCaseServiceProvider;
......@@ -171,9 +170,6 @@ class AppServiceProvider extends ServiceProvider
SchedulesForGroupUseCaseServiceProvider::class,
ActiveSchedulesForGroupUseCaseServiceProvider::class,
// DoorUser
DoorUserGroupMapUseCaseServiceProvider::class,
// Overrides
OverrideGetUseCaseServiceProvider::class,
OverrideCreateUseCaseServiceProvider::class,
......
......@@ -5,6 +5,11 @@ namespace Source\Authorization;
interface Authorizer
{
/**
* @param string|null $id
*/
public function setCurrentUserId(?string $id): void;
/**
* Returns list of permission from \Source\Authorization\Permissions that the user has
*
......
......@@ -126,7 +126,7 @@ class Access implements AccessUseCase
* These authorizers are injected in the service provider, and we will go until allowed or denied
*/
if (!$doorId) {
if ($doorId === null) {
throw new AuthenticationException();
}
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Access;
use Carbon\Carbon;
use Source\Entities\User;
......
......@@ -10,8 +10,8 @@ use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Source\UseCases\Door\Access\Authorizers\CommandAuthorizer;
use Source\UseCases\Door\Access\Authorizers\OverrideAuthorizer;
use Source\UseCases\Door\Access\Authorizers\OpenModeAuthorizer;
use Source\UseCases\Door\Access\Authorizers\OverrideAuthorizer;
use Source\UseCases\Door\Access\Authorizers\ScheduleAuthorizer;
use Source\UseCases\Door\Access\Authorizers\CommanderAuthorizer;
......
......@@ -3,11 +3,10 @@
namespace Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Authorization\Authorizer;
use Source\Authorization\Permissions;
use Source\Authorization\ApiAuthorizer;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Door\Access\AccessAuthorizer;
use Source\UseCases\Door\Commands\CommandHandler;
......@@ -15,16 +14,16 @@ use Source\UseCases\Door\Commands\CommandHandler;
class CommandAuthorizer implements AccessAuthorizer
{
/**
* @var \Source\Authorization\ApiAuthorizer
* @var \Source\Authorization\Authorizer
*/
protected ApiAuthorizer $authorizer;
protected Authorizer $authorizer;
/**
* @var \Source\UseCases\Door\Commands\CommandHandler
*/
protected CommandHandler $commandHandler;
public function __construct(ApiAuthorizer $authorizer, CommandHandler $commandHandler)
public function __construct(Authorizer $authorizer, CommandHandler $commandHandler)
{
$this->authorizer = $authorizer;
$this->commandHandler = $commandHandler;
......
......@@ -3,22 +3,21 @@
namespace Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Authorization\Authorizer;
use Source\Authorization\Permissions;
use Source\Authorization\ApiAuthorizer;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Door\Access\AccessAuthorizer;
class CommanderAuthorizer implements AccessAuthorizer
{
/**
* @var \Source\Authorization\ApiAuthorizer
* @var \Source\Authorization\Authorizer
*/
protected ApiAuthorizer $authorizer;
protected Authorizer $authorizer;
public function __construct(ApiAuthorizer $authorizer)
public function __construct(Authorizer $authorizer)
{
$this->authorizer = $authorizer;
}
......
......@@ -3,11 +3,10 @@
namespace Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Schedule;
use InvalidArgumentException;
use Source\Entities\Schedule;
use Source\UseCases\Door\Access\AccessAuthorizer;
use Source\Gateways\DoorSchedule\DoorScheduleRepository;
use Source\Gateways\RecurrenceSet\RecurrenceSetRepository;
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Override;
......
......@@ -3,15 +3,14 @@
namespace Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Schedule;
use InvalidArgumentException;
use Source\Entities\Schedule;
use Source\Gateways\DoorUser\DoorUserRepository;
use Source\UseCases\Door\Access\AccessAuthorizer;
use Source\Gateways\Schedules\SchedulesRepository;
use Source\Gateways\RecurrenceSet\RecurrenceSetRepository;
use Source\UseCases\DoorUserGroup\DoorUserGroupMapUseCase;
class ScheduleAuthorizer implements AccessAuthorizer
{
......@@ -26,18 +25,18 @@ class ScheduleAuthorizer implements AccessAuthorizer
protected RecurrenceSetRepository $recurrenceSet;
/**
* @var \Source\UseCases\DoorUserGroup\DoorUserGroupMapUseCase
* @var \Source\Gateways\DoorUser\DoorUserRepository
*/
protected DoorUserGroupMapUseCase $doorUserMapper;
protected DoorUserRepository $doorUsers;
public function __construct(
SchedulesRepository $schedules,
RecurrenceSetRepository $recurrenceSet,
DoorUserGroupMapUseCase $doorUserMapper
DoorUserRepository $doorUsers
) {
$this->schedules = $schedules;
$this->recurrenceSet = $recurrenceSet;
$this->doorUserMapper = $doorUserMapper;
$this->doorUsers = $doorUsers;
}
/**
......@@ -46,7 +45,7 @@ class ScheduleAuthorizer implements AccessAuthorizer
public function check(?User $user, Carbon $date, string $doorId, string $doorcode, ?string $commandString): int
{
if ($user) {
$groups = $this->doorUserMapper->getGroupsForDoorUserIntersection($doorId, $user->getId());
$groups = $this->doorUsers->getDoorUserGroupIntersection($doorId, $user->getId());
foreach ($groups as $group) {
$schedules = $this->schedules->getActiveForGroupAndDate(
$group->getId(),
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Commands;
use Source\Entities\User;
abstract class Command
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Commands;
use Source\Entities\User;
interface CommandHandler
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Commands;
use Carbon\Laravel\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Commands;
use Source\Entities\User;
class DoorCommandHandler implements CommandHandler
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Door\Commands;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Override;
......@@ -55,14 +54,14 @@ class OverrideCommand extends Command
if ($length > 0) {
return $this->overrides->addOverride(new Override(
0,
"Open mode override created at keypad by {$user->getDisplayName()}.",
$user->getId(),
$doorId,
Override::TYPE_OPEN,
Carbon::now(),
Carbon::now()->addRealMinutes($length)
)) !== null;
0,
"Open mode override created at keypad by {$user->getDisplayName()}.",
$user->getId(),
$doorId,
Override::TYPE_OPEN,
Carbon::now(),
Carbon::now()->addRealMinutes($length)
)) !== null;
}
return false;
......@@ -71,8 +70,8 @@ class OverrideCommand extends Command
if ($length > 0) {
return $this->overrides->addOverride(new Override(
0,
"Closed mode override created at keypad by {$user->getDisplayName()}.",
0,
"Closed mode override created at keypad by {$user->getDisplayName()}.",
$user->getId(),
$doorId,
Override::TYPE_LOCKED,
......
<?php
namespace Source\UseCases\DoorUserGroup;
use Source\Gateways\DoorUser\DoorUserRepository;
class DoorUserGroupMap implements DoorUserGroupMapUseCase
{
/**
* @var \Source\Gateways\DoorUser\DoorUserRepository
*/
protected DoorUserRepository $doorUsers;
public function __construct(DoorUserRepository $doorUsers)
{
$this->doorUsers = $doorUsers;
}
/**
* @inheritDoc
*/
public function getGroupsForDoorUserIntersection(string $doorId, string $userId): array
{
return $this->doorUsers->getDoorUserGroupIntersection($doorId, $userId);
}
}
<?php
namespace Source\UseCases\DoorUserGroup;
interface DoorUserGroupMapUseCase
{
/**
* Returns groups that a door and a user both belong to
*
* @param string $doorId
* @param string $userId
* @return \Source\Entities\Group[]
*/
public function getGroupsForDoorUserIntersection(string $doorId, string $userId): array;
}
<?php
namespace Source\UseCases\DoorUserGroup;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\DoorUser\DoorUserRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class DoorUserGroupMapUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(DoorUserGroupMapUseCase::class, static function (Application $app) {
return new DoorUserGroupMap($app->make(DoorUserRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [DoorUserGroupMapUseCase::class];
}
}
<?php
namespace Tests\Doubles\Access;
use Carbon\Carbon;
use Source\UseCases\Door\Access\Authorizers\DoorOpenModeCheck\DoorOpenModeCheck;
class DoorOpenModeCheckStub implements DoorOpenModeCheck
{
public bool $checkReturn = false;
/**
* @inheritDoc
*/
public function checkOpenMode(string $doorId, Carbon $date): bool
{
return $this->checkReturn;
}
}
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