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

Add doorcode changing for users

parent b4ba56a3
......@@ -4,33 +4,69 @@ namespace App\Http\Controllers\Web;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use Source\UseCases\Users\GetUser\GetUserUseCase;
use Source\UseCases\Tokens\GetTokens\WebPresenter as UserTokensWebPresenter;
use Source\UseCases\Tokens\GetTokens\WebPresenter as GetTokensWebPresenter;
use Source\UseCases\Tokens\GetTokens\GetTokensUseCase;
use Source\UseCases\Users\GetUser\WebPresenter as GetUserWebPresenter;
use Source\UseCases\Users\UpdateUser\UpdateCurrentUser;
use Source\UseCases\DoorUser\UserDoorAccess\UserDoorAccessUseCase;
use Source\UseCases\Users\UpdateUser\WebPresenter as UpdateUserWebPresenter;
class MeController extends Controller
{
/**
* @param \Source\UseCases\Users\GetUser\GetUserUseCase $getUser
* @param \Source\UseCases\Tokens\GetTokens\GetTokensUseCase $userTokens
* @return \Illuminate\View\View
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function index(
GetUserUseCase $getUser,
GetTokensUseCase $userTokens
): View {
$userPresenter = new GetUserWebPresenter();
$tokensPresenter = new UserTokensWebPresenter();
public function index(): View
{
return view('home');
}
/**
* @return \Illuminate\View\View
*/
public function doorcode(): View
{
return view('doorcode');
}
$userId = $this->authorizer->getCurrentUserId();
/**
* @param \Source\UseCases\Users\UpdateUser\UpdateCurrentUser $userUpdate
* @return \Illuminate\View\View
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityExistsException
*/
public function doorcodeUpdate(UpdateCurrentUser $userUpdate): View
{
$this->validate($this->request, [
'doorcode' => 'required|string|numeric|digits_between:4,248',
]);
$getUser->get($userId, $userPresenter);
$presenter = new UpdateUserWebPresenter();
$userTokens->filter($userId, null, $tokensPresenter);
$userUpdate->update(
$this->authorizer->getCurrentUserId(),
['doorcode' => $this->request->input('doorcode')],
$presenter
);
return view('doorcode', $presenter->getViewModel());
}
public function access(UserDoorAccessUseCase $userDoorAccessUseCase): View
{
}
/**
* @param \Source\UseCases\Tokens\GetTokens\GetTokensUseCase $userTokens
* @return \Illuminate\View\View
* @throws \Exception
*/
public function tokens(GetTokensUseCase $userTokens): View
{
$presenter = new GetTokensWebPresenter();
$userTokens->filter($this->authorizer->getCurrentUserId(), null, $presenter);
return view('home', $userPresenter->getViewModel());
return view('tokens', $presenter->getViewModel());
}
}
@extends('layouts.base')
@section('title', 'Change Doorcode')
@section('content')
<div>
<p>
Your student ID will automatically be prepended to the
code you set on this page. For example, if your student ID is <kbd>311546</kbd>, and you
type in <kbd>7653</kbd>, the final doorcode you need to type into the keypads
would be <kbd>3115467653#</kbd>.
</p>
<small>
If you do not have a student ID, the doorcode entered here is your full doorcode.
</small>
@include('partials.form-errors')
@include('partials.message')
<form action="{{ route('web.doorcode.change') }}" method="POST">
@csrf
<div class="form-group">
<label for="doorcode">
Enter your new doorcode here.
</label>
<input type="text" class="form-control" name="doorcode" id="doorcode" aria-describedby="doorcodeHelp" placeholder="Enter doorcode">
<small id="doorcodeHelp" class="form-text text-muted">Must be at least 4 digits long and a number.</small>
</div>
<button type="submit" class="btn btn-primary">Set Doorcode</button>
</form>
</div>
@endsection
......@@ -3,5 +3,10 @@
@section('title', 'Home')
@section('content')
@include('partials.user')
<div class="list-group">
<a class="list-group-item list-group-item-action" href="{{ route('web.doorcode') }}">Change Doorcode</a>
<a class="list-group-item list-group-item-action" href="{{ route('web.access') }}">View Door Access</a>
<a class="list-group-item list-group-item-action" href="{{ route('web.tokens') }}">View API Tokens</a>
<a class="list-group-item list-group-item-action" href="{{ route('saml.logout') }}">Logout</a>
</div>
@endsection
......@@ -28,13 +28,14 @@
<a class="nav-link" href="/api/docs/">Api Docs</a>
</li>
</ul>
<div class="dropdown">
<div class="dropdown mr-5">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Hello, {{ user()->getFirstName() }}
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="{{ route('web.index') }}">View Access</a>
<a class="dropdown-item" href="{{ route('web.doorcode') }}">Change Doorcode</a>
<a class="dropdown-item" href="{{ route('web.access') }}">View Access</a>
<a class="dropdown-item" href="{{ route('web.tokens') }}">View API Tokens</a>
<a class="dropdown-item" href="{{ route('saml.logout') }}">Logout</a>
</div>
</div>
......@@ -42,7 +43,12 @@
</nav>
<main role="main" class="container">
@yield('content')
<div>
<div class="page-header">
<h1> @yield('title') </h1>
</div>
@yield('content')
</div>
</main>
<script src="{{ asset('js/app.js') }}"></script>
......
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if(isset($error) && !$error && $message)
<div class="alert alert-success"> {{ $message }} </div>
@elseif(isset($error) && $error)
<div class="alert alert-danger"> {{ $message }} </div>
@endif
......@@ -16,3 +16,6 @@ use App\Http\Controllers\Web\MeController;
Route::get('/', [MeController::class, 'index'])->name('web.index');
Route::get('/door-code', [MeController::class, 'doorcode'])->name('web.doorcode');
Route::post('/door-code', [MeController::class, 'doorcodeUpdate'])->name('web.doorcode.change');
Route::get('/access', [MeController::class, 'access'])->name('web.access');
Route::get('/tokens', [MeController::class, 'tokens'])->name('web.tokens');
......@@ -8,12 +8,9 @@ class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
protected bool $error = false;
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->error = $responseModel->hasError();
if ($responseModel->hasError()) {
$this->viewModel['message'] = $responseModel->getError();
} else {
......@@ -21,14 +18,6 @@ class APIPresenter extends BasePresenter implements Presenter
}
}
/**
* @return bool
*/
public function hasError(): bool
{
return $this->error;
}
/** @inheritDoc */
public function getViewModel(): array
{
......
......@@ -56,7 +56,7 @@ class UpdateCurrentUser implements UpdateUserUseCase
if ($doorcode) {
if ($user->getEmplid()) {
$doorcode = $user->getEmplid() . $doorcode;
} elseif (strlen($doorcode) < self::MIN_USER_SET_DOORCODE_LEN) {
} else if (strlen($doorcode) < self::MIN_USER_SET_DOORCODE_LEN) {
$response->setError('Doorcode must be at least ' .
self::MIN_USER_SET_DOORCODE_LEN .
' digits long since you do not have an emplid');
......
<?php
namespace Source\UseCases\Users\UpdateUser;
use Source\UseCases\BasePresenter;
class WebPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['error'] = $responseModel->hasError();
$this->viewModel['message'] = $responseModel->getError() ?: 'Successfully updated doorcode.';
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
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