Commit 7fc7ac8a authored by Jacob Priddy's avatar Jacob Priddy 👌

flash users' doorcode to them when they update it

parent 9725d71b
Pipeline #13236 failed with stages
in 1 minute and 15 seconds
......@@ -13,6 +13,7 @@ use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
use Source\UseCases\Tokens\GetTokens\GetTokensUseCase;
use Source\UseCases\Users\UpdateUser\UpdateCurrentUser;
use Source\UseCases\Requests\GetRequest\GetRequestUseCase;
use Source\UseCases\Users\UpdateUser\DoorcodeWebPresenter;
use Source\UseCases\Requests\GetRequests\GetRequestsUseCase;
use Source\UseCases\Requests\CreateRequest\CreateRequestUseCase;
use Source\UseCases\Requests\DeleteRequest\DeleteRequestUseCase;
......@@ -21,6 +22,7 @@ use Source\UseCases\Requests\InteractWithRequest\InteractWithRequestUseCase;
use Source\UseCases\Users\UpdateUser\WebPresenter as UpdateUserWebPresenter;
use Source\UseCases\Requests\GetRequests\WebPresenter as RequestsWebPresenter;
use Source\UseCases\Requests\DeleteRequest\WebPresenter as DeleteRequestWebPresenter;
use Source\UseCases\Users\UpdateUser\DoorcodeWebPresenter as UpdateUserDoorcodeWebPresenter;
class MeController extends Controller
{
......@@ -38,7 +40,7 @@ class MeController extends Controller
'doorcode' => 'required|string|numeric|digits_between:4,248',
]);
$presenter = new UpdateUserWebPresenter();
$presenter = new DoorcodeWebPresenter();
$userUpdate->update(
$this->authorizer->getCurrentUserId(),
......
......@@ -4,6 +4,9 @@
@section('content')
<div>
@include('partials.form-errors')
@include('partials.message')
<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
......@@ -15,9 +18,6 @@
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">
......
<?php
namespace Source\UseCases\Users\UpdateUser;
use Source\UseCases\BasePresenter;
class DoorcodeWebPresenter 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 changed doorcode to ' . $responseModel->getDoorcode();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
......@@ -14,6 +14,11 @@ class ResponseModel
*/
protected ?User $user = null;
/**
* @var string|null Users new doorcode
*/
protected ?string $doorcode = null;
/**
* @return User
*/
......@@ -29,4 +34,20 @@ class ResponseModel
{
$this->user = $user;
}
/**
* @param string $doorcode
*/
public function setDoorcode(string $doorcode): void
{
$this->doorcode = $doorcode;
}
/**
* @return string|null
*/
public function getDoorcode(): ?string
{
return $this->doorcode;
}
}
......@@ -56,11 +56,11 @@ class UpdateCurrentUser implements UpdateUserUseCase
$doorcode = $attributes['doorcode'] ?? null;
if ($doorcode) {
if ($user->getEmplid()) {
$doorcode = $user->getEmplid() . $doorcode;
$doorcode = ltrim($user->getEmplid(), '0') . $doorcode;
} elseif (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');
' digits long including the length of your student ID (leading 0\'s are stripped from your ID).');
$presenter->present($response);
......@@ -86,7 +86,7 @@ class UpdateCurrentUser implements UpdateUserUseCase
try {
$returnedUser = $this->users->update($user->getId(), $newUser);
} catch (EntityExistsException $e) {
} catch (EntityExistsException) {
//
}
......@@ -94,6 +94,9 @@ class UpdateCurrentUser implements UpdateUserUseCase
$response->setError('Unable to update user.');
} else {
$response->setUser($returnedUser);
if ($doorcode) {
$response->setDoorcode($doorcode);
}
}
$presenter->present($response);
......
......@@ -174,7 +174,7 @@ class CurrentUserUpdateUseCaseTest extends TestCase
self::assertTrue($this->response->hasError());
self::assertEquals(
'Doorcode must be at least 11 digits long since you do not have an emplid',
'Doorcode must be at least 11 digits long including the length of your student ID (leading 0\'s are stripped from your ID).',
$this->response->getError()
);
}
......@@ -194,4 +194,20 @@ class CurrentUserUpdateUseCaseTest extends TestCase
self::assertFalse($this->response->hasError());
self::assertEquals(HashedSearchable::hash('', 'emplid1234'), $this->usersRepository->get('69')->getDoorcode());
}
/**
* @test
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityExistsException
*/
public function it_prepends_emplid_to_doorcode_without_leading_zeros(): void
{
$user = new User(69, '', '', '', '', '0000234');
$this->usersRepository->create($user);
$this->handleTest('69', ['doorcode' => '01234']);
self::assertFalse($this->response->hasError());
self::assertEquals(HashedSearchable::hash('', '23401234'), $this->usersRepository->get('69')->getDoorcode());
}
}
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