feat: set preferred role on login via get parameter
This commit is contained in:
@@ -19,16 +19,16 @@ class UserDataHandler
|
||||
) {
|
||||
}
|
||||
|
||||
public function collectRoles(CrmAttributesResponse $crmAttributes): array
|
||||
public function collectRoles(CrmAttributesResponse $crmAttributes, ?string $preferredRole): array
|
||||
{
|
||||
// Collect user's roles from CRM attributes
|
||||
$roles = [];
|
||||
|
||||
if ($crmAttributes->isAdmin()) {
|
||||
if ($crmAttributes->isAdmin() && (null === $preferredRole || 'admin' === $preferredRole)) {
|
||||
$roles[] = 'ROLE_ADMIN';
|
||||
} elseif ($crmAttributes->isManager()) {
|
||||
} elseif ($crmAttributes->isManager() && (null === $preferredRole || 'manager' === $preferredRole)) {
|
||||
$roles[] = 'ROLE_MANAGER';
|
||||
} elseif ($crmAttributes->isHouseManager()) {
|
||||
} elseif ($crmAttributes->isHouseManager() && (null === $preferredRole || 'house_manager' === $preferredRole)) {
|
||||
$roles[] = 'ROLE_HOUSE_MANAGER';
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Controller\Security;
|
||||
|
||||
use App\Entity\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
@@ -11,7 +12,7 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
class LoginController extends AbstractController
|
||||
{
|
||||
#[Route('/login', name: 'app_security_login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils): Response
|
||||
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response
|
||||
{
|
||||
// redirect to default route in case of active session
|
||||
if (null !== $user = $this->getUser()) {
|
||||
@@ -25,9 +26,12 @@ class LoginController extends AbstractController
|
||||
// last username entered by the user
|
||||
$lastUsername = $authenticationUtils->getLastUsername();
|
||||
|
||||
$role = $request->query->get('role');
|
||||
|
||||
return $this->render('security/login.html.twig', [
|
||||
'last_username' => $lastUsername,
|
||||
'error' => $error,
|
||||
'role' => $role,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -176,7 +176,7 @@ class User implements UserInterface, TimestampableEntityInterface
|
||||
if ($this->hasRole('ROLE_ADMIN')) {
|
||||
return 'app_admin_index';
|
||||
} elseif ($this->hasRole('ROLE_MANAGER')) {
|
||||
return 'app_admin_index';
|
||||
return 'app_manager_index';
|
||||
} elseif ($this->hasRole('ROLE_HOUSE_MANAGER')) {
|
||||
return 'app_house_manager_index';
|
||||
} else {
|
||||
|
||||
@@ -63,8 +63,9 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
|
||||
return null;
|
||||
}
|
||||
|
||||
// Finale checks and local user loading/creation
|
||||
$user = $this->getOrCreateLocalUser($response, $email, $password);
|
||||
// Final checks and local user loading/creation
|
||||
$preferredRole = $request->request->get('_role');
|
||||
$user = $this->getOrCreateLocalUser($response, $email, $password, $preferredRole);
|
||||
|
||||
if (null === $user) {
|
||||
return null;
|
||||
@@ -98,8 +99,12 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
|
||||
return new RedirectResponse($url);
|
||||
}
|
||||
|
||||
private function getOrCreateLocalUser(ProfileResponse $profileResponse, string $email, string $password): ?User
|
||||
{
|
||||
private function getOrCreateLocalUser(
|
||||
ProfileResponse $profileResponse,
|
||||
string $email,
|
||||
string $password,
|
||||
?string $preferredRole
|
||||
): ?User {
|
||||
// Fetch CRM attributes, early return in case of an API error
|
||||
try {
|
||||
/** @var CrmAttributesResponse $crmAttributes */
|
||||
@@ -111,7 +116,7 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
|
||||
// Collect user's roles from CRM attributes
|
||||
$roles = $this
|
||||
->userDataHandler
|
||||
->collectRoles($crmAttributes)
|
||||
->collectRoles($crmAttributes, $preferredRole)
|
||||
;
|
||||
|
||||
// User is expected to have at least one role
|
||||
|
||||
@@ -32,6 +32,9 @@
|
||||
Login
|
||||
</button>
|
||||
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
|
||||
{% if role is not null %}
|
||||
<input type="hidden" name="_role" value="{{ role }}">
|
||||
{% endif %}
|
||||
</form>
|
||||
<div class="pt-4">
|
||||
<a href="{{ path('app_security_password_reset') }}" class="text-sm underline">
|
||||
|
||||
Reference in New Issue
Block a user