Commit 655527e6 authored by Jacob Priddy's avatar Jacob Priddy 👌

Create frontend for managing user-group relations

parent 3e70e3f9
......@@ -10,12 +10,20 @@ use Source\Exceptions\AuthorizationException;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
use Source\UseCases\Users\GetUsers\GetUsersUseCase;
use Source\UseCases\Groups\GetGroups\GetGroupsUseCase;
use Source\UseCases\Users\CreateUser\CreateUserUseCase;
use Source\UseCases\Users\DeleteUser\DeleteUserUseCase;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCase;
use Source\UseCases\GroupUser\RemoveUserFromGroup\APIPresenter as RemoveUserGroupPresenter;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase;
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as AddUserGroupPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase;
use Source\UseCases\GroupUser\GetUserGroups\WebPresenter as UserGroupsPresenter;
use Source\UseCases\Doors\GetDoors\MePresenter as DoorsPresenter;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase;
use Source\UseCases\Users\GetUser\WebPresenter as GetUserPresenter;
use Source\UseCases\Users\GetUsers\WebPresenter as GetUsersPresenter;
use Source\UseCases\Groups\GetGroups\WebPresenter as GroupsPresenter;
use Source\UseCases\Users\CreateUser\WebPresenter as CreateUserPresenter;
use Source\UseCases\Users\DeleteUser\APIPresenter as DeleteUserPresenter;
use Source\UseCases\Users\UpdateUser\WebPresenter as UpdateUserPresenter;
......@@ -160,4 +168,62 @@ class UsersController extends Controller
->with('doors', $presenter->getViewModel())
->with('userId', $userId);
}
/**
* @param string $userId
* @param \Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCase $userGroups
* @param \Source\UseCases\Groups\GetGroups\GetGroupsUseCase $groups
* @return \Illuminate\View\View
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function groups(string $userId, GetUserGroupsUseCase $userGroups, GetGroupsUseCase $groups): View
{
$groupsPresenter = new GroupsPresenter();
$groups->search(null, $groupsPresenter);
$presenter = new UserGroupsPresenter();
$userGroups->getGroupsForUser($userId, $presenter);
return view('admin.userGroups', array_merge(
$presenter->getViewModel(),
$groupsPresenter->all('allGroups'),
['userId' => $userId],
));
}
/**
* @param string $userId
* @param \Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCase $userGroup
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Validation\ValidationException
*/
public function addGroup(string $userId, AddUserToGroupUseCase $userGroup): RedirectResponse
{
$this->validate($this->request, [
'group_id' => 'required|string',
]);
$presenter = new AddUserGroupPresenter();
$userGroup->addUserToGroup([$userId], [$this->request->input('group_id')], $presenter);
return redirect(route('web.admin.users.groups', ['userId' => $userId]))->with($presenter->getViewModel());
}
/**
* @param string $userId
* @param string $groupId
* @param \Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCase $removeUser
* @return \Illuminate\Http\RedirectResponse
*/
public function removeGroup(string $userId, string $groupId, RemoveUserFromGroupUseCase $removeUser): RedirectResponse
{
$presenter = new RemoveUserGroupPresenter();
$removeUser->removeUserFromGroup([$userId], [$groupId], $presenter);
return redirect(route('web.admin.users.groups', ['userId' => $userId]))->with($presenter->getViewModel());
}
}
@extends('layouts.admin.tableData', [
'title' => 'Door Groups',
'route' => route('web.admin.doors.groups', ['doorId' => $doorId]),
'tableParams' => [
'headers' => $headers,
'paginatedData' => $groups,
'controls' => 'detachDoorGroup'
],
@extends('layouts.admin.groups', [
'resource' => 'door',
'control' => 'detachDoorGroup',
'resourceId' => $doorId,
])
@section('controls')
<button class="btn btn-primary float-right" data-toggle="modal" data-target="#attachGroup">
Add Group
</button>
<div class="modal fade" id="attachGroup" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add group to door.</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" action="{{ route('web.admin.doors.addGroup', ['doorId' => $doorId]) }}">
@csrf
<div class="modal-body">
<label for="groupSelect">
Select group
</label>
<select class="form-control" name="group_id" id="groupSelect" required>
@foreach($allGroups as $group)
<option value="{{ $group['id'] }}">
{{ $group['title'] }}
</option>
@endforeach
</select>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary" type="submit">
Add Group
</button>
</div>
</form>
</div>
</div>
</div>
@endsection
@extends('layouts.admin.groups', [
'resource' => 'user',
'control' => 'detachUserGroup',
'resourceId' => $userId,
])
@extends('layouts.admin.tableData', [
'title' => ucfirst($resource) . ' Groups',
'route' => route('web.admin.' . \Illuminate\Support\Str::plural($resource) . '.groups', [$resource . 'Id' => $resourceId]),
'tableParams' => [
'headers' => $headers,
'paginatedData' => $groups,
'controls' => $control,
],
])
@section('controls')
<button class="btn btn-primary float-right" data-toggle="modal" data-target="#attachGroup">
Add Group
</button>
<div class="modal fade" id="attachGroup" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add group to {{ $resource }}.</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="POST" action="{{ route('web.admin.' . \Illuminate\Support\Str::plural($resource) . '.groups', [$resource . 'Id' => $resourceId]) }}">
@csrf
<div class="modal-body">
<label for="groupSelect">
Select group
</label>
<select class="form-control" name="group_id" id="groupSelect" required>
@foreach($allGroups as $group)
<option value="{{ $group['id'] }}">
{{ $group['title'] }}
</option>
@endforeach
</select>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary" type="submit">
Add Group
</button>
</div>
</form>
</div>
</div>
</div>
@endsection
<form action="{{ route('web.admin.doors.removeGroup', ['doorId' => $doorId, 'groupId' => $id]) }}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit" data-toggle="tooltip" title="Detach door and group.">
<button class="btn btn-danger" type="submit" data-toggle="tooltip" title="Detach door from group.">
<i class="fas fa-trash"></i>
</button>
</form>
<form action="{{ route('web.admin.users.removeGroup', ['userId' => $userId, 'groupId' => $id]) }}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit" data-toggle="tooltip" title="Detach user from group.">
<i class="fas fa-trash"></i>
</button>
</form>
<a class="btn btn-secondary" href="{{ route('web.admin.entries.index', ['user_id' => $id]) }}" data-toggle="tooltip" title="View the user's door history">
<a class="btn btn-secondary" href="{{ route('web.admin.users.groups', ['userId' => $id]) }}" data-toggle="tooltip" title="Manage groups for the user.">
<i class="fas fa-object-group"></i>
</a>
<a class="btn btn-warning" href="{{ route('web.admin.entries.index', ['user_id' => $id]) }}" data-toggle="tooltip" title="View the user's door history">
<i class="fas fa-history"></i>
</a>
<a class="btn btn-dark" href="{{ route('web.admin.users.access', ['userId' => $id]) }}" data-toggle="tooltip" title="View the user's access schedule">
......
......@@ -88,6 +88,10 @@ Route::name('web.')->middleware(['auth:api'])->group(static function () {
Route::put('/{userId}', [UsersController::class, 'update'])->name('update');
Route::delete('/{userId}', [UsersController::class, 'destroy'])->name('destroy');
Route::get('/{userId}/access', [UsersController::class, 'access'])->name('access');
Route::get('/{userId}/groups', [UsersController::class, 'groups'])->name('groups');
Route::post('/{userId}/groups', [UsersController::class, 'addGroup'])->name('addGroup');
Route::delete('/{userId}/group/{groupId}', [UsersController::class, 'removeGroup'])->name('removeGroup');
});
Route::name('requests.')
......
<?php
namespace Source\UseCases\GroupUser\GetUserGroups;
use Source\Entities\Group;
use Source\Sanitize\Paginates;
use Source\UseCases\BasePresenter;
class WebPresenter extends BasePresenter implements Presenter
{
use Paginates;
protected array $groups = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->groups = array_map(static function (Group $group) {
return [
'id' => $group->getId(),
'title' => $group->getTitle(),
'description' => $group->getDescription(),
];
}, $responseModel->getGroups());
}
/** @inheritDoc */
public function getViewModel(): array
{
return [
'groups' => $this->webPaginate($this->groups),
'headers' => [
'ID' => 'id',
'Title' => 'title',
'Description' => 'description',
],
];
}
}
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