feat: disable/block teamer users as admin

This commit is contained in:
Björn Fromme
2025-07-22 17:11:39 +02:00
parent 0fc7684fc7
commit ea574427fb
10 changed files with 300 additions and 6 deletions
+1
View File
@@ -21,6 +21,7 @@ security:
provider: bpn_user_provider
custom_authenticators:
- App\Security\BpnAuthenticator
user_checker: App\Security\UserChecker
switch_user:
role: CAN_IMPERSONATE
logout:
+31
View File
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250722142834 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user ADD disabled_at DATETIME DEFAULT NULL COMMENT \'(DC2Type:datetime_immutable)\', ADD disabled_reason LONGTEXT DEFAULT NULL, ADD disabled_reason_internal LONGTEXT DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE user DROP disabled_at, DROP disabled_reason, DROP disabled_reason_internal');
}
}
@@ -0,0 +1,81 @@
<?php
namespace App\Controller\Admin\Teamer;
use App\Entity\Teamer;
use App\Form\DisableUserType;
use App\Htmx\HxRedirectResponse;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
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\Attribute\IsGranted;
class DisableUserController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly LoggerInterface $logger,
) {
}
#[Route('/admin/teamer/disable-user/{uuid}', name: 'app_admin_teamer_disable_user')]
#[IsGranted('ROLE_ADMIN')]
public function index(Teamer $teamer, Request $request): Response
{
$user = $teamer->getUser();
$form = $this->createForm(DisableUserType::class, $user, ['hx_post' => $request->getUri()]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setDisabledAt(new \DateTimeImmutable());
$this->entityManager->flush();
$this->addFlash('success', 'Der Benutzeraccount wurde gesperrt');
$this->logger->info('Disable user', [
'teamer' => $teamer->getFullName(),
'user' => $user->getEmail(),
]);
return new HxRedirectResponse($this->generateUrl('app_administrative_teamer_index'));
}
return $this->render('admin/teamer/modal_disable_user.html.twig', [
'form' => $form->createView(),
'teamer' => $teamer,
]);
}
#[Route('/administrative/teamer/enable-user/{uuid}', name: 'app_admin_teamer_enable_user')]
#[IsGranted('ROLE_ADMIN')]
public function enable(Teamer $teamer, Request $request): Response
{
$user = $teamer->getUser();
if (true === $request->isMethod(Request::METHOD_POST)) {
$user
->setDisabledAt(null)
->setDisabledReason(null)
->setDisabledReasonInternal(null)
;
$this->entityManager->flush();
$this->addFlash('success', 'Der Benutzeraccount wurde reaktiviert');
$this->logger->info('Enable user', [
'teamer' => $teamer->getFullName(),
'user' => $user->getEmail(),
]);
return new HxRedirectResponse($this->generateUrl('app_administrative_teamer_index'));
}
return $this->render('admin/teamer/modal_enable_user.html.twig', [
'teamer' => $teamer,
]);
}
}
+52 -1
View File
@@ -4,6 +4,7 @@ namespace App\Entity;
use App\Entity\Traits\TimestampableEntity;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
@@ -57,6 +58,15 @@ class User implements UserInterface, TimestampableEntityInterface
#[ORM\Column]
private bool $muteNotifications = false;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $disabledAt = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $disabledReason = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $disabledReasonInternal = null;
public function __construct()
{
$this->uuid = Uuid::v4();
@@ -169,7 +179,7 @@ class User implements UserInterface, TimestampableEntityInterface
$labels = [];
foreach ($this->roles as $role) {
$labels[] = match($role) {
$labels[] = match ($role) {
'ROLE_ADMIN' => 'Admin',
'ROLE_MANAGER' => 'Reisemanager',
'ROLE_HOUSE_MANAGER' => 'Hausleitung',
@@ -300,4 +310,45 @@ class User implements UserInterface, TimestampableEntityInterface
return false;
}
public function getDisabledAt(): ?\DateTimeImmutable
{
return $this->disabledAt;
}
public function setDisabledAt(?\DateTimeImmutable $disabledAt): static
{
$this->disabledAt = $disabledAt;
return $this;
}
public function isDisabled(): bool
{
return null !== $this->disabledAt;
}
public function getDisabledReason(): ?string
{
return $this->disabledReason;
}
public function setDisabledReason(?string $disabledReason): static
{
$this->disabledReason = $disabledReason;
return $this;
}
public function getDisabledReasonInternal(): ?string
{
return $this->disabledReasonInternal;
}
public function setDisabledReasonInternal(?string $disabledReasonInternal): static
{
$this->disabledReasonInternal = $disabledReasonInternal;
return $this;
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
class DisableUserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('disabledReason', TextareaType::class, [
'label' => 'Begründung',
'attr' => [
'data-controller' => 'textarea-autosize',
'data-action' => 'textarea-autosize#resize',
],
'constraints' => [
new NotBlank([
'message' => 'Bitte gib die Begründung ein',
]),
],
])
->add('disabledReasonInternal', TextareaType::class, [
'label' => 'Begründung intern',
'required' => false,
'attr' => [
'data-controller' => 'textarea-autosize',
'data-action' => 'textarea-autosize#resize',
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Security;
use App\Entity\User;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class UserChecker implements UserCheckerInterface
{
public function checkPreAuth(UserInterface $user): void
{
if (!$user instanceof User) {
return;
}
if (true === $user->isDisabled()) {
throw new CustomUserMessageAccountStatusException('Dein Account wurde gesperrt: '.$user->getDisabledReason());
}
}
public function checkPostAuth(UserInterface $user): void
{
}
}
@@ -0,0 +1,19 @@
{% extends 'htmx_modal.html.twig' %}
{% block title %}Benutzeraccount sperren{% endblock %}
{% block content %}
{{ form_start(form) }}
<h2 class="font-bold text-lg pb-4">
Teamer:in {{ teamer }}
</h2>
<div class="flex flex-col space-y-4 pb-4">
{{ form_row(form.disabledReason) }}
{{ form_row(form.disabledReasonInternal) }}
</div>
<button type="submit" class="btn bg-red-500 text-white">
Sperren
</button>
{{ form_rest(form) }}
{{ form_end(form) }}
{% endblock %}
@@ -0,0 +1,10 @@
{% extends 'htmx_confirmation_modal.html.twig' %}
{% block content %}
<div class="pb-4">
Möchtest du den Benutzeraccount von <em>{{ teamer }}</em> wirklich reaktivieren?
</div>
<div>
Der Grund der Sperrung war: <em>{{ teamer.user.disabledReasonInternal ?? teamer.user.disabledReason }}</em>
</div>
{% endblock %}
@@ -5,7 +5,7 @@
{% block content %}
{{ form_start(form) }}
<h2 class="font-bold text-lg pb-4">
Teamer {{ disposition.teamer }}
Teamer:in {{ disposition.teamer }}
</h2>
<div class="flex flex-col space-y-4 pb-4">
{{ form_row(form.calledOffBy) }}
@@ -101,15 +101,44 @@
{{ icon('check') }}
</button>
{% endif %}
{% if is_granted('CAN_IMPERSONATE', teamer.user) %}
<a href="{{ path('app_teamer_index', { '_switch_user': teamer.user.email }) }}" title="Als Teamer:in maskieren">
{{ icon('mask') }}
</a>
{% if teamer.user.disabled %}
{% if is_granted('ROLE_ADMIN') %}
<button type="button"
role="menuitem"
tabindex="-1"
hx-get="{{ path('app_admin_teamer_enable_user', { 'uuid': teamer.uuid }) }}"
hx-target="body"
hx-swap="beforeend">
{{ icon('unlocked') }}
</button>
{% endif %}
{% else %}
{% if is_granted('ROLE_ADMIN') %}
<button type="button"
class="text-red-500"
role="menuitem"
tabindex="-1"
hx-get="{{ path('app_admin_teamer_disable_user', { 'uuid': teamer.uuid }) }}"
hx-target="body"
hx-swap="beforeend">
{{ icon('locked') }}
</button>
{% endif %}
{% if is_granted('CAN_IMPERSONATE', teamer.user) %}
<a href="{{ path('app_teamer_index', { '_switch_user': teamer.user.email }) }}" title="Als Teamer:in maskieren">
{{ icon('mask') }}
</a>
{% endif %}
{% endif %}
<a href="{{ path('app_administrative_teamer_profile', { 'uuid': teamer.uuid, 'r': return_url() }) }}" title="Teamer:innenprofil {{ teamer }}">
{{ icon('user') }}
</a>
</div>
{% if teamer.user.disabled %}
<div class="py-1 px-2 mt-2 text-xs bg-red-500 text-white">
gesp. am {{ teamer.user.disabledAt | date('d.m.Y') }}
</div>
{% endif %}
</td>
</tr>
{% else %}