Commit 3e113228 authored by Jacob Priddy's avatar Jacob Priddy 👌

fix major bug on entity deletion and redirect new users to doorcode

creation page
parent 55b6d3dc
Pipeline #16521 canceled with stages
......@@ -88,7 +88,7 @@ services:
networks:
- doorcode
dbadmin:
image: dpage/pgadmin4:4.29
image: dpage/pgadmin4:4.30
volumes:
- doorcode_pgadmin-data:/var/lib/pgadmin
environment:
......
......@@ -54,19 +54,19 @@ class Door extends Authenticatable
// Delete all entries
/** @var \App\Entry $entry */
foreach ($door->entries() as $entry) {
foreach ($door->entries()->get() as $entry) {
$entry->delete();
}
// Delete all attempts
/** @var \App\Attempt $attempt */
foreach ($door->attempts() as $attempt) {
foreach ($door->attempts()->get() as $attempt) {
$attempt->delete();
}
// Delete all overrides
/** @var \App\Override $override */
foreach ($door->overrides() as $override) {
foreach ($door->overrides()->get() as $override) {
$override->delete();
}
});
......
......@@ -134,7 +134,16 @@ class AuthController extends ApiController
);
}
return redirect()->intended(url(config('saml.home_page')))->withCookie(
$response = redirect();
if ($presenter->isNewUser()) {
$response = $response->route('web.doorcode')
->with('message', 'I see you are a new user! Please set your doorcode for door access.');
} else {
$response = $response->intended(url(config('saml.home_page')));
}
return $response->withCookie(
$this->cookieJar->make(
'api_token',
$presenter->getViewModel()['token']['value'],
......
......@@ -29,11 +29,7 @@ class LoginController extends Controller
try {
$authenticateUseCase->attempt($presenter, $this->request->all());
} catch (AuthenticationException $e) {
throw ValidationException::withMessages([
'email' => [trans('auth.failed')],
]);
} catch (EntityNotFoundException $e) {
} catch (AuthenticationException | EntityNotFoundException $e) {
throw ValidationException::withMessages([
'email' => [trans('auth.failed')],
]);
......
......@@ -64,22 +64,22 @@ class User extends Authenticatable
// Delete all tokens
/** @var \App\Token $token */
foreach ($user->tokens() as $token) {
foreach ($user->tokens()->get() as $token) {
$token->delete();
}
/** @var \App\Entry $entry */
foreach ($user->entries() as $entry) {
foreach ($user->entries()->get() as $entry) {
$entry->delete();
}
/** @var \App\Override $override */
foreach ($user->overrides() as $override) {
foreach ($user->overrides()->get() as $override) {
$override->delete();
}
/** @var \App\Request $request */
foreach ($user->requests() as $request) {
foreach ($user->requests()->get() as $request) {
$request->delete();
}
});
......
......@@ -23,6 +23,7 @@ class APIPresenter extends BasePresenter implements Presenter
}
$this->viewModel['user'] = self::formatFullUser($user);
$this->viewModel['is_new'] = $responseModel->isNewUser();
$this->viewModel['token'] = [
'value' => $responseModel->getRawToken(),
'expires_at' => self::formatDateTime($token->getExpiresAt()),
......@@ -35,4 +36,14 @@ class APIPresenter extends BasePresenter implements Presenter
{
return $this->viewModel;
}
/**
* Returns if the user was newly created
*
* @return bool
*/
public function isNewUser(): bool
{
return $this->viewModel['is_new'];
}
}
......@@ -117,6 +117,7 @@ class Authenticate implements AuthenticateUseCase
* - Emplid found, email not found
* Update found emplid user
*/
$newlyCreated = false;
if (!$user) {
if ($emailUser) {
$user = $this->users->update($emailUser->getId(), new User(
......@@ -133,6 +134,8 @@ class Authenticate implements AuthenticateUseCase
$emailUser->getUpdatedAt()
));
} else {
// We want to direct new users to the
$newlyCreated = true;
$user = $this->users->create(new User(
0,
$samlUser->getFirstName(),
......@@ -177,7 +180,7 @@ class Authenticate implements AuthenticateUseCase
$token = $this->tokens->createLoginToken($user->getId(), $this->salt);
$response = new ResponseModel($user, $token->getRaw(), $token->getToken());
$response = new ResponseModel($user, $token->getRaw(), $token->getToken(), $newlyCreated);
$presenter->present($response);
}
......
......@@ -22,16 +22,23 @@ class ResponseModel
*/
protected string $tokenString;
/**
* @var bool
*/
protected bool $newUser;
/**
* @param User $user
* @param string $tokenString
* @param Token $token
* @param bool $newUser
*/
public function __construct(User $user, string $tokenString, Token $token)
public function __construct(User $user, string $tokenString, Token $token, bool $newUser = false)
{
$this->user = $user;
$this->token = $token;
$this->tokenString = $tokenString;
$this->newUser = $newUser;
}
/**
......@@ -57,4 +64,12 @@ class ResponseModel
{
return $this->tokenString;
}
/**
* @return bool
*/
public function isNewUser(): bool
{
return $this->newUser;
}
}
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