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

Merge branch '105-direct-new-users-to-doorcode-creation-page' into 'master'

Resolve "Direct New Users to Doorcode Creation Page"

Closes #105

See merge request !98
parents 55b6d3dc 7cc87368
Pipeline #17603 passed with stages
in 2 minutes and 9 seconds
...@@ -88,7 +88,7 @@ services: ...@@ -88,7 +88,7 @@ services:
networks: networks:
- doorcode - doorcode
dbadmin: dbadmin:
image: dpage/pgadmin4:4.29 image: dpage/pgadmin4:4.30
volumes: volumes:
- doorcode_pgadmin-data:/var/lib/pgadmin - doorcode_pgadmin-data:/var/lib/pgadmin
environment: environment:
......
ARG APP_IMAGE ARG APP_IMAGE
FROM ${APP_IMAGE} as source FROM ${APP_IMAGE} as source
FROM nginx:1.19.5-alpine FROM nginx:1.19.6-alpine
WORKDIR /var/www WORKDIR /var/www
......
...@@ -54,19 +54,19 @@ class Door extends Authenticatable ...@@ -54,19 +54,19 @@ class Door extends Authenticatable
// Delete all entries // Delete all entries
/** @var \App\Entry $entry */ /** @var \App\Entry $entry */
foreach ($door->entries() as $entry) { foreach ($door->entries()->get() as $entry) {
$entry->delete(); $entry->delete();
} }
// Delete all attempts // Delete all attempts
/** @var \App\Attempt $attempt */ /** @var \App\Attempt $attempt */
foreach ($door->attempts() as $attempt) { foreach ($door->attempts()->get() as $attempt) {
$attempt->delete(); $attempt->delete();
} }
// Delete all overrides // Delete all overrides
/** @var \App\Override $override */ /** @var \App\Override $override */
foreach ($door->overrides() as $override) { foreach ($door->overrides()->get() as $override) {
$override->delete(); $override->delete();
} }
}); });
......
...@@ -134,7 +134,16 @@ class AuthController extends ApiController ...@@ -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( $this->cookieJar->make(
'api_token', 'api_token',
$presenter->getViewModel()['token']['value'], $presenter->getViewModel()['token']['value'],
......
...@@ -29,11 +29,7 @@ class LoginController extends Controller ...@@ -29,11 +29,7 @@ class LoginController extends Controller
try { try {
$authenticateUseCase->attempt($presenter, $this->request->all()); $authenticateUseCase->attempt($presenter, $this->request->all());
} catch (AuthenticationException $e) { } catch (AuthenticationException | EntityNotFoundException $e) {
throw ValidationException::withMessages([
'email' => [trans('auth.failed')],
]);
} catch (EntityNotFoundException $e) {
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'email' => [trans('auth.failed')], 'email' => [trans('auth.failed')],
]); ]);
......
...@@ -64,22 +64,22 @@ class User extends Authenticatable ...@@ -64,22 +64,22 @@ class User extends Authenticatable
// Delete all tokens // Delete all tokens
/** @var \App\Token $token */ /** @var \App\Token $token */
foreach ($user->tokens() as $token) { foreach ($user->tokens()->get() as $token) {
$token->delete(); $token->delete();
} }
/** @var \App\Entry $entry */ /** @var \App\Entry $entry */
foreach ($user->entries() as $entry) { foreach ($user->entries()->get() as $entry) {
$entry->delete(); $entry->delete();
} }
/** @var \App\Override $override */ /** @var \App\Override $override */
foreach ($user->overrides() as $override) { foreach ($user->overrides()->get() as $override) {
$override->delete(); $override->delete();
} }
/** @var \App\Request $request */ /** @var \App\Request $request */
foreach ($user->requests() as $request) { foreach ($user->requests()->get() as $request) {
$request->delete(); $request->delete();
} }
}); });
......
...@@ -23,6 +23,7 @@ class APIPresenter extends BasePresenter implements Presenter ...@@ -23,6 +23,7 @@ class APIPresenter extends BasePresenter implements Presenter
} }
$this->viewModel['user'] = self::formatFullUser($user); $this->viewModel['user'] = self::formatFullUser($user);
$this->viewModel['is_new'] = $responseModel->isNewUser();
$this->viewModel['token'] = [ $this->viewModel['token'] = [
'value' => $responseModel->getRawToken(), 'value' => $responseModel->getRawToken(),
'expires_at' => self::formatDateTime($token->getExpiresAt()), 'expires_at' => self::formatDateTime($token->getExpiresAt()),
...@@ -35,4 +36,14 @@ class APIPresenter extends BasePresenter implements Presenter ...@@ -35,4 +36,14 @@ class APIPresenter extends BasePresenter implements Presenter
{ {
return $this->viewModel; 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 ...@@ -117,6 +117,7 @@ class Authenticate implements AuthenticateUseCase
* - Emplid found, email not found * - Emplid found, email not found
* Update found emplid user * Update found emplid user
*/ */
$newlyCreated = false;
if (!$user) { if (!$user) {
if ($emailUser) { if ($emailUser) {
$user = $this->users->update($emailUser->getId(), new User( $user = $this->users->update($emailUser->getId(), new User(
...@@ -133,6 +134,8 @@ class Authenticate implements AuthenticateUseCase ...@@ -133,6 +134,8 @@ class Authenticate implements AuthenticateUseCase
$emailUser->getUpdatedAt() $emailUser->getUpdatedAt()
)); ));
} else { } else {
// We want to direct new users to the
$newlyCreated = true;
$user = $this->users->create(new User( $user = $this->users->create(new User(
0, 0,
$samlUser->getFirstName(), $samlUser->getFirstName(),
...@@ -177,7 +180,7 @@ class Authenticate implements AuthenticateUseCase ...@@ -177,7 +180,7 @@ class Authenticate implements AuthenticateUseCase
$token = $this->tokens->createLoginToken($user->getId(), $this->salt); $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); $presenter->present($response);
} }
......
...@@ -22,16 +22,23 @@ class ResponseModel ...@@ -22,16 +22,23 @@ class ResponseModel
*/ */
protected string $tokenString; protected string $tokenString;
/**
* @var bool
*/
protected bool $newUser;
/** /**
* @param User $user * @param User $user
* @param string $tokenString * @param string $tokenString
* @param Token $token * @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->user = $user;
$this->token = $token; $this->token = $token;
$this->tokenString = $tokenString; $this->tokenString = $tokenString;
$this->newUser = $newUser;
} }
/** /**
...@@ -57,4 +64,12 @@ class ResponseModel ...@@ -57,4 +64,12 @@ class ResponseModel
{ {
return $this->tokenString; 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