Commit f543ac4c authored by Jacob Priddy's avatar Jacob Priddy 👌

Finish endpoint for getting doors for a group

 - fix saml bug where it returns emplids not 7 characters
 - Fix door group relationship being named in wrong order
parent c75dd862
......@@ -96,9 +96,15 @@ $config = [
'preston.carman:I am young and hip' => [
'first_name' => ['Preston'],
'last_name' => ['Carman'],
'emplid' => ['69420'],
'emplid' => ['6942000'],
'email' => ['Preston.Carman@wallawalla.edu']
],
'admin:admin' => [
'first_name' => ['Sheev'],
'last_name' => ['Palpatine'],
'emplid' => ['order66'],
'email' => ['sithL0rd@senate.com']
]
],
......
......@@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Door extends Authenticatable
{
protected $fillable = ['*'];
protected $fillable = ['id', 'name', 'location', 'created_at', 'updated_at'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
......
......@@ -26,7 +26,7 @@ class DoorsController extends ApiController
*/
public function index(GetAllDoorsUseCase $getDoors): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new GetAllDoorsAPIPresenter();
......@@ -44,7 +44,7 @@ class DoorsController extends ApiController
*/
public function get(GetDoorUseCase $getDoor, string $doorId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new GetDoorAPIPresenter();
......@@ -63,7 +63,7 @@ class DoorsController extends ApiController
*/
public function store(CreateDoorUseCase $createDoor): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->validate($this->request, [
'location' => 'required|string|max:255',
......@@ -89,7 +89,7 @@ class DoorsController extends ApiController
*/
public function update(UpdateDoorUseCase $updateDoor, string $doorId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->validate($this->request, [
'location' => 'string|max:255',
......@@ -113,7 +113,7 @@ class DoorsController extends ApiController
*/
public function regenerateToken(GenerateDoorTokenUseCase $tokenGenerator, string $doorId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new CreateDoorAPIPresenter();
......@@ -131,7 +131,7 @@ class DoorsController extends ApiController
*/
public function delete(DeleteDoorUseCase $doorDelyeeter, string $doorId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_DOORS]);
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new DeleteDoorAPIPresenter();
......
......@@ -6,11 +6,13 @@ use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\Exceptions\DeleteFailedException;
use Source\UseCases\Groups\GetGroup\GetGroupUseCase;
use Source\UseCases\DoorGroup\GetGroupDoors\APIPresenter as GetGroupDoorsAPIPresenter;
use Source\UseCases\Groups\CreateGroup\CreateGroupUseCase;
use Source\UseCases\Groups\DeleteGroup\DeleteGroupUseCase;
use Source\UseCases\Groups\UpdateGroup\UpdateGroupUseCase;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCase;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCase;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCase;
use Source\UseCases\Groups\GetGroup\APIPresenter as GetGroupAPIPresenter;
use Source\UseCases\Groups\GetAllGroups\APIPresenter as AllGroupsAPIPresenter;
use Source\UseCases\Groups\CreateGroup\APIPresenter as CreateGroupAPIPresenter;
......@@ -30,7 +32,7 @@ class GroupsController extends ApiController
*/
public function store(CreateGroupUseCase $useCase): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_GROUPS]);
$this->authorizer->protect(Permissions::MANAGE_GROUPS);
$this->validate($this->request, [
'title' => 'required|string|max:255',
......@@ -53,7 +55,7 @@ class GroupsController extends ApiController
*/
public function delete(DeleteGroupUseCase $useCase, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_GROUPS]);
$this->authorizer->protect(Permissions::MANAGE_GROUPS);
$presenter = new DeleteGroupAPIPresenter();
......@@ -111,7 +113,7 @@ class GroupsController extends ApiController
*/
public function update(UpdateGroupUseCase $useCase, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_GROUPS]);
$this->authorizer->protect(Permissions::MANAGE_GROUPS);
$this->validate($this->request, [
'title' => 'required|string|max:255',
......@@ -146,4 +148,22 @@ class GroupsController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCase $groupDoors
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getDoorsForGroup(GetGroupDoorsUseCase $groupDoors, string $groupId): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new GetGroupDoorsAPIPresenter();
$groupDoors->getDoorsForGroup($groupId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -33,7 +33,7 @@ class UsersController extends ApiController
*/
public function index(GetAllUsersUseCase $getAllUsers): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new AllUsersAPIPresenter();
......@@ -51,7 +51,7 @@ class UsersController extends ApiController
*/
public function get(GetUserUseCase $getUser, string $userId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new GetUserAPIPresenter();
......@@ -70,7 +70,7 @@ class UsersController extends ApiController
*/
public function store(CreateUserUseCase $createUser): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$this->validate($this->request, [
'first_name' => 'required|string|max:255',
......@@ -104,7 +104,7 @@ class UsersController extends ApiController
*/
public function update(UpdateUserUseCase $updateUser, string $userId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$this->validate($this->request, [
'first_name' => 'string|max:255',
......@@ -141,7 +141,7 @@ class UsersController extends ApiController
*/
public function delete(DeleteUserUseCase $deleteUser, string $userId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new DeleteUserAPIPresenter();
......@@ -165,7 +165,7 @@ class UsersController extends ApiController
*/
public function addUserToGroup(AddUserToGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new AddUserToGroupAPIPresenter();
......@@ -184,7 +184,7 @@ class UsersController extends ApiController
*/
public function removeUserFromGroup(RemoveUserFromGroupUseCase $useCase, string $userId, string $groupId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new RemoveUserFromGroupAPIPresenter();
......@@ -202,7 +202,7 @@ class UsersController extends ApiController
*/
public function getGroupsForUser(GetUserGroupsUseCase $useCase, string $userId): JsonResponse
{
$this->authorizer->protectAll([Permissions::MANAGE_USERS]);
$this->authorizer->protect(Permissions::MANAGE_USERS);
$presenter = new GetUserGroupsAPIPresenter();
......
......@@ -29,6 +29,7 @@ use Source\UseCases\Token\Authenticate\AuthenticateUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
......@@ -75,6 +76,9 @@ class AppServiceProvider extends ServiceProvider
AddUserToGroupUseCaseServiceProvider::class,
RemoveUserFromGroupUseCaseServiceProvider::class,
// DoorGroup
GetGroupDoorsUseCaseServiceProvider::class,
// Doors
GetDoorUseCaseServiceProvider::class,
CreateDoorUseCaseServiceProvider::class,
......
......@@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGroupDoorTable extends Migration
class CreateDoorGroupTable extends Migration
{
/**
* Run the migrations.
......@@ -14,13 +14,12 @@ class CreateGroupDoorTable extends Migration
public function up()
{
// This table maps the permissions a specific group has to a specific door
Schema::create('group_door', function (Blueprint $table) {
Schema::create('door_group', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('door_id');
$table->foreign('door_id')->references('id')->on('doors');
$table->unsignedBigInteger('group_id');
$table->foreign('group_id')->references('id')->on('groups');
$table->boolean('open_mode_allowed')->default(false);
$table->timestamps();
});
}
......@@ -32,6 +31,6 @@ class CreateGroupDoorTable extends Migration
*/
public function down()
{
Schema::dropIfExists('group_door');
Schema::dropIfExists('door_group');
}
}
......@@ -32,7 +32,6 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::delete('{userId}', [UsersController::class, 'delete']);
Route::get('{userId}/groups', [UsersController::class, 'getGroupsForUser']);
Route::post('{userId}/group/{groupId}', [UsersController::class, 'addUserToGroup']);
Route::delete('{userId}/group/{groupId}', [UsersController::class, 'removeUserFromGroup']);
});
......@@ -47,6 +46,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::delete('{groupId}', [GroupsController::class, 'delete']);
Route::get('{groupId}/users', [GroupsController::class, 'getUsersForGroup']);
Route::get('{groupId}/doors', [GroupsController::class, 'getDoorsForGroup']);
});
Route::group([
......@@ -58,6 +58,10 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::put('{doorId}', [DoorsController::class, 'update']);
Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
Route::delete('{doorId}', [DoorsController::class, 'delete']);
// Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor']);
// Route::post('{doorId}/group/{groupId}', [DoorsController::class, 'addDoorToGroup']);
// Route::delete('{doorId}/group/{groupId}', [DoorsController::class, 'removeDoorFromGroup']);
});
Route::group([
......
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
use Source\Entities\Door;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['doors'] = array_map(function (Door $door) {
return $this->formatDoor($door);
}, $responseModel->getDoors());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
use Source\Gateways\DoorGroup\DoorGroupRepository;
class GetGroupDoors implements GetGroupDoorsUseCase
{
/**
* @var \Source\Gateways\DoorGroup\DoorGroupRepository
*/
protected DoorGroupRepository $repository;
public function __construct(DoorGroupRepository $repository)
{
$this->repository = $repository;
}
/**
* @inheritDoc
*/
public function getDoorsForGroup(string $groupId, Presenter $presenter): void
{
$doors = $this->repository->getDoorsForGroup($groupId);
$response = new ResponseModel($doors);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
interface GetGroupDoorsUseCase
{
/**
* @param string $groupId
* @param \Source\UseCases\DoorGroup\GetGroupDoors\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getDoorsForGroup(string $groupId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\DoorGroup\DoorGroupRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetGroupDoorsUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetGroupDoorsUseCase::class, static function (Application $app) {
return new GetGroupDoors($app->make(DoorGroupRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetGroupDoorsUseCase::class];
}
}
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\DoorGroup\GetGroupDoors;
class ResponseModel
{
/**
* @var \Source\Entities\Door[]
*/
protected array $doors;
public function __construct(array $doors)
{
$this->doors = $doors;
}
/**
* @return \Source\Entities\Door[]
*/
public function getDoors(): array
{
return $this->doors;
}
}
......@@ -50,7 +50,7 @@ class AuthorizerTest extends TestCase
$this->users = new InMemoryUsersRepository();
$this->groups = new InMemoryGroupsRepository();
$this->groupUser = new InMemoryGroupUserRepository($this->users, $this->groups);
$this->authorizer = new ApiAuthorizer($this->guard, $this->groupUser);
$this->authorizer = new ApiAuthorizer($this->guard, $this->users, $this->groups, $this->groupUser);
}
/**
......
Markdown is supported
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