diff --git a/app/migrations/20230906.210154_0_add_active_at_column_to_users_table.php b/app/migrations/20230906.210154_0_add_active_at_column_to_users_table.php new file mode 100644 index 00000000..6655b1e4 --- /dev/null +++ b/app/migrations/20230906.210154_0_add_active_at_column_to_users_table.php @@ -0,0 +1,27 @@ +table('users') + ->addColumn('active_at', 'datetime', [ + 'nullable' => true, + 'default' => null, + ]) + ->update(); + } + + public function down(): void + { + $this->table('users') + ->dropColumn('active_at') + ->update(); + } +} diff --git a/app/src/Auth/AuthMiddleware.php b/app/src/Auth/AuthMiddleware.php index f4943cbb..9720fa5f 100644 --- a/app/src/Auth/AuthMiddleware.php +++ b/app/src/Auth/AuthMiddleware.php @@ -6,6 +6,7 @@ use App\Database\User; use App\Service\UserOptionsService; +use App\Service\UserService; use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -23,6 +24,7 @@ class AuthMiddleware implements MiddlewareInterface const USER_LOCALE = 'X-Internal-UserLocale'; public function __construct( + private readonly UserService $userService, private readonly UserOptionsService $userOptionsService, ) { } @@ -44,19 +46,28 @@ public function process(Request $request, RequestHandlerInterface $handler): Res return $this->unauthenticated(); } + $this->trackActiveAt($actor); + return $handler->handle( $request->withAddedHeader(self::HEADER_USER_ID, (string) $actor->id) ->withAttribute(self::USER_LOCALE, $this->userOptionsService->getLocale($actor)) ); } - /** - * @return \Psr\Http\Message\ResponseInterface - */ private function unauthenticated(): Response { return new JsonResponse([ 'message' => $this->say('error_authentication_required'), ], 401); } + + private function trackActiveAt(User $user): void + { + $user->activeAt = new \DateTimeImmutable(); + + try { + $this->userService->store($user); + } catch (\Throwable) { + } + } } diff --git a/app/src/Database/User.php b/app/src/Database/User.php index 0f307b3c..17bea735 100644 --- a/app/src/Database/User.php +++ b/app/src/Database/User.php @@ -58,6 +58,9 @@ class User implements PasswordContainerInterface #[ORM\Column(type: 'datetime', name: 'updated_at')] public \DateTimeImmutable $updatedAt; + #[ORM\Column(type: 'datetime', name: 'active_at', nullable: true, default: null)] + public ?\DateTimeImmutable $activeAt = null; + #[ORM\Relation\BelongsTo(target: Currency::class, innerKey: 'default_currency_code', cascade: true, load: 'eager')] private Currency $defaultCurrency;