From 257917ea38836aa2cdd52f76bbcdae0da629e905 Mon Sep 17 00:00:00 2001
From: Marien Fressinaud
Date: Fri, 4 Aug 2023 17:04:04 +0200
Subject: [PATCH] Disallow the edition of profiles managed by LDAP
---
src/Controller/ProfileController.php | 24 ++-
templates/profile/edit.html.twig | 175 +++++++++++----------
tests/Controller/ProfileControllerTest.php | 29 ++++
translations/errors+intl-icu.en_GB.yaml | 1 +
translations/errors+intl-icu.fr_FR.yaml | 1 +
translations/messages+intl-icu.en_GB.yaml | 2 +
translations/messages+intl-icu.fr_FR.yaml | 2 +
7 files changed, 153 insertions(+), 81 deletions(-)
diff --git a/src/Controller/ProfileController.php b/src/Controller/ProfileController.php
index 64c7389b..0d40beb9 100644
--- a/src/Controller/ProfileController.php
+++ b/src/Controller/ProfileController.php
@@ -8,6 +8,7 @@
use App\Repository\UserRepository;
use App\Utils\ConstraintErrorsFormatter;
+use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
@@ -20,13 +21,16 @@
class ProfileController extends BaseController
{
#[Route('/profile', name: 'profile', methods: ['GET', 'HEAD'])]
- public function edit(): Response
- {
+ public function edit(
+ #[Autowire(env: 'bool:LDAP_ENABLED')]
+ string $ldapEnabled,
+ ): Response {
/** @var \App\Entity\User $user */
$user = $this->getUser();
return $this->render('profile/edit.html.twig', [
'name' => $user->getName(),
'email' => $user->getEmail(),
+ 'managedByLdap' => $ldapEnabled && $user->getAuthType() === 'ldap',
]);
}
@@ -38,6 +42,8 @@ public function update(
ValidatorInterface $validator,
RequestStack $requestStack,
TranslatorInterface $translator,
+ #[Autowire(env: 'bool:LDAP_ENABLED')]
+ string $ldapEnabled,
): Response {
/** @var \App\Entity\User $user */
$user = $this->getUser();
@@ -60,10 +66,22 @@ public function update(
/** @var string $csrfToken */
$csrfToken = $request->request->get('_csrf_token', '');
+ $managedByLdap = $ldapEnabled && $user->getAuthType() === 'ldap';
+
+ if ($managedByLdap) {
+ return $this->renderBadRequest('profile/edit.html.twig', [
+ 'name' => $name,
+ 'email' => $email,
+ 'managedByLdap' => $managedByLdap,
+ 'error' => $translator->trans('user.ldap.cannot_update_profile', [], 'errors'),
+ ]);
+ }
+
if (!$this->isCsrfTokenValid('update profile', $csrfToken)) {
return $this->renderBadRequest('profile/edit.html.twig', [
'name' => $name,
'email' => $email,
+ 'managedByLdap' => $managedByLdap,
'error' => $translator->trans('csrf.invalid', [], 'errors'),
]);
}
@@ -73,6 +91,7 @@ public function update(
return $this->renderBadRequest('profile/edit.html.twig', [
'name' => $name,
'email' => $email,
+ 'managedByLdap' => $managedByLdap,
'errors' => [
'password' => $translator->trans('user.password.dont_match', [], 'errors'),
],
@@ -92,6 +111,7 @@ public function update(
return $this->renderBadRequest('profile/edit.html.twig', [
'name' => $name,
'email' => $email,
+ 'managedByLdap' => $managedByLdap,
'errors' => ConstraintErrorsFormatter::format($errors),
]);
}
diff --git a/templates/profile/edit.html.twig b/templates/profile/edit.html.twig
index db82f16e..2dcf7f1a 100644
--- a/templates/profile/edit.html.twig
+++ b/templates/profile/edit.html.twig
@@ -29,12 +29,21 @@
{{ 'profile.note' | trans }}
+ {{ include('alerts/_alert.html.twig', {
+ type: 'info',
+ title: 'profile.ldap.information' | trans,
+ message: 'profile.ldap.managed' | trans,
+ }, with_context = false) }}
+
{% if errors.name is defined %}
@@ -55,6 +64,7 @@
aria-invalid="true"
aria-errormessage="name-error"
{% endif %}
+ {{ managedByLdap ? 'disabled' }}
/>
@@ -81,91 +91,98 @@
aria-invalid="true"
aria-errormessage="email-error"
{% endif %}
+ {{ managedByLdap ? 'disabled' }}
/>
-