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