From 148d85121a2d47bf99effd63674b2a3669a1f633 Mon Sep 17 00:00:00 2001 From: Craig Williams Date: Mon, 9 Oct 2023 13:28:27 +0100 Subject: [PATCH] Users that are already logged in (i.e. through a password reset) should be forwarded to the dashboard ONLY if they have permission, otherwise, send them back to the index page --- src/ServicesProvider/ServicesProvider.php | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ServicesProvider/ServicesProvider.php b/src/ServicesProvider/ServicesProvider.php index dbc5bf7..1fecdf6 100644 --- a/src/ServicesProvider/ServicesProvider.php +++ b/src/ServicesProvider/ServicesProvider.php @@ -47,6 +47,35 @@ class ServicesProvider return $classMapper; }); + /* + * Returns a callback that forwards to dashboard if user is already logged in. + * + * @return callable + */ + $container['redirect.onAlreadyLoggedIn'] = function ($c) { + /* + * This method is invoked when a user attempts to perform certain public actions when they are already logged in. + * + * @todo Forward to user's landing page or last visited page + * @param \Psr\Http\Message\ServerRequestInterface $request + * @param \Psr\Http\Message\ResponseInterface $response + * @param array $args + * @return \Psr\Http\Message\ResponseInterface + */ + return function (Request $request, Response $response, array $args) use ($c) { + /** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */ + $authorizer = $c->authorizer; + + $currentUser = $c->authenticator->user(); + + if ($authorizer->checkAccess($currentUser, 'uri_dashboard')) { + return $response->withHeader('UF-Redirect', $c->router->pathFor('dashboard')); + } else { + return $response->withHeader('UF-Redirect', $c->router->pathFor('index')); + } + }; + }; + /* * Returns a callback that handles setting the `UF-Redirect` header after a successful login. *