Ajout CRUD pour gerer utilisateurs, uniquement accessible pour un admin

This commit is contained in:
Aymeric SERRA 2023-02-09 15:30:23 +01:00
parent 2cba9fc890
commit 47c8cdcda4
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
15 changed files with 265 additions and 8 deletions

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

View File

@ -94,3 +94,5 @@ Pas de commandes.
```bash
symfony console doctrine:fixtures:load
```
### Question 15

View File

@ -10,6 +10,7 @@ security:
class: App\Entity\User
property: email
role_hierarchy:
ROLE_ADMINISTRATEUR: [ROLE_INSTRUCTOR, ROLE_APPRENTI, ROLE_USER]
ROLE_INSTRUCTOR: ROLE_USER
ROLE_APPRENTI: ROLE_USER
firewalls:

View File

@ -13,7 +13,7 @@ class SecurityController extends AbstractController
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->getUser()) {
return $this->redirectToRoute('app_atelier_index');
return $this->redirectToRoute('app_index');
}
// get the login error if there is one

View File

@ -0,0 +1,80 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use App\Repository\UserRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[IsGranted('ROLE_ADMINISTRATEUR')]
#[Route('/user')]
class UserController extends AbstractController
{
#[Route('/', name: 'app_user_index', methods: ['GET'])]
public function index(UserRepository $userRepository): Response
{
return $this->render('user/index.html.twig', [
'users' => $userRepository->findAll(),
]);
}
#[Route('/new', name: 'app_user_new', methods: ['GET', 'POST'])]
public function new(Request $request, UserRepository $userRepository): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userRepository->save($user, true);
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/new.html.twig', [
'user' => $user,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_user_show', methods: ['GET'])]
public function show(User $user): Response
{
return $this->render('user/show.html.twig', [
'user' => $user,
]);
}
#[Route('/{id}/edit', name: 'app_user_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, User $user, UserRepository $userRepository): Response
{
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userRepository->save($user, true);
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/edit.html.twig', [
'user' => $user,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_user_delete', methods: ['POST'])]
public function delete(Request $request, User $user, UserRepository $userRepository): Response
{
if ($this->isCsrfTokenValid('delete' . $user->getId(), $request->request->get('_token'))) {
$userRepository->remove($user, true);
}
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
}

View File

@ -83,9 +83,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
$roles[] = 'ROLE_INSTRUCTOR';
$roles[] = 'ROLE_APPRENTI';
if (sizeof($roles) == 0) {
$roles[] = "ROLE_APPRENTI";
}
return array_unique($roles);
}

52
src/Form/UserType.php Normal file
View File

@ -0,0 +1,52 @@
<?php
namespace App\Form;
use App\Entity\User;
use PhpParser\Node\Expr\Array_;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email')
->add('roles', ChoiceType::class, [
'choices' => [
'Apprenti' => 'ROLE_APPRENTI',
'Instructeur' => 'ROLE_INSTRUCTOR',
'Admin' => 'ROLE_ADMINISTRATEUR'
],
'required' => true,
'multiple' => false,
'expanded' => false,
])
->add('nom')
->add('prenom');
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function ($rolesArray) {
// transform the array to a string
return count($rolesArray)? $rolesArray[0]: null;
},
function ($rolesString) {
// transform the string back to an array
return [$rolesString];
}
));
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}

View File

@ -46,7 +46,7 @@ class AppAuthentificatorAuthenticator extends AbstractLoginFormAuthenticator
return new RedirectResponse($targetPath);
}
return new RedirectResponse($this->urlGenerator->generate('app_atelier_index'));
return new RedirectResponse($this->urlGenerator->generate('app_index'));
}
protected function getLoginUrl(Request $request): string

View File

@ -13,16 +13,22 @@
<li class="nav-item">
<a class="nav-link" href="{{ path('app_atelier_index') }}">Atelier</a>
</li>
{% if app.user %}
{% if app.user and is_granted('ROLE_INSTRUCTOR') %}
<li class="nav-item">
<a class="nav-link" href="{{ path('app_atelier_by_me') }}">Mes Ateliers</a>
</li>
{% endif %}
{% if app.user %}
{% if app.user and is_granted('ROLE_APPRENTI') %}
<li class="nav-item">
<a class="nav-link" href="{{ path('app_atelier_inscrit') }}">Mes Formations</a>
</li>
{% endif %}
{% if app.user and is_granted('ROLE_ADMINISTRATEUR') %}
<li class="nav-item">
<a class="nav-link" href="{{ path('app_user_index') }}">Liste des Utilisateurs</a>
</li>
{% endif %}
</ul>
</div>
{% if app.user %}

View File

@ -0,0 +1,4 @@
<form method="post" action="{{ path('app_user_delete', {'id': user.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ user.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit User{% endblock %}
{% block body %}
<h1>Edit User</h1>
{{ include('user/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_user_index') }}">back to list</a>
{{ include('user/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,41 @@
{% extends 'base.html.twig' %}
{% block title %}User index{% endblock %}
{% block body %}
<h1>User index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Email</th>
<th>Roles</th>
<th>Nom</th>
<th>Prenom</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.email }}</td>
<td>{{ user.roles ? user.roles[0] : '' }}</td>
<td>{{ user.nom }}</td>
<td>{{ user.prenom }}</td>
<td>
<a href="{{ path('app_user_show', {'id': user.id}) }}">show</a>
<a href="{{ path('app_user_edit', {'id': user.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="7">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_user_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New User{% endblock %}
{% block body %}
<h1>Create new User</h1>
{{ include('user/_form.html.twig') }}
<a href="{{ path('app_user_index') }}">back to list</a>
{% endblock %}

View File

@ -0,0 +1,38 @@
{% extends 'base.html.twig' %}
{% block title %}User{% endblock %}
{% block body %}
<h1>User</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ user.id }}</td>
</tr>
<tr>
<th>Email</th>
<td>{{ user.email }}</td>
</tr>
<tr>
<th>Roles</th>
<td>{{ user.roles ? user.roles[0] : '' }}</td>
</tr>
<tr>
<th>Nom</th>
<td>{{ user.nom }}</td>
</tr>
<tr>
<th>Prenom</th>
<td>{{ user.prenom }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_user_index') }}">back to list</a>
<a href="{{ path('app_user_edit', {'id': user.id}) }}">edit</a>
{{ include('user/_delete_form.html.twig') }}
{% endblock %}