sergisergio /
Ads
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Controller; |
||||
| 4 | |||||
| 5 | use App\Entity\User; |
||||
| 6 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
||||
| 7 | use Symfony\Component\Routing\Annotation\Route; |
||||
| 8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * @Security("has_role('ROLE_ADMIN')") |
||||
| 12 | */ |
||||
| 13 | class AdminUserController extends AbstractController |
||||
| 14 | { |
||||
| 15 | /** |
||||
| 16 | * @Route("/admin/users", name="admin_users") |
||||
| 17 | */ |
||||
| 18 | public function index() |
||||
| 19 | { |
||||
| 20 | $em = $this->getDoctrine()->getManager(); |
||||
| 21 | $listUsers = $em->getRepository(User::class)->findAll(); |
||||
| 22 | //dump($listUsers);die(); |
||||
| 23 | return $this->render('admin_user/index.html.twig', [ |
||||
| 24 | 'users' => $listUsers, |
||||
| 25 | ]); |
||||
| 26 | } |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * @Route("/admin/users/adminOn/{id}", name="admin_on") |
||||
| 30 | */ |
||||
| 31 | public function adminOn($id) |
||||
| 32 | { |
||||
| 33 | $em = $this->getDoctrine()->getManager(); |
||||
| 34 | $user = $em->getRepository(User::class)->find($id); |
||||
| 35 | |||||
| 36 | $user->setRoles(['ROLE_ADMIN']); |
||||
| 37 | $em->flush(); |
||||
| 38 | |||||
| 39 | $this->addFlash('success', 'Ce membre est désormais administrateur'); |
||||
| 40 | return $this->redirectToRoute('admin_users'); |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * @Route("/admin/users/adminOff/{id}", name="admin_off") |
||||
| 45 | */ |
||||
| 46 | public function adminOff($id) |
||||
| 47 | { |
||||
| 48 | $em = $this->getDoctrine()->getManager(); |
||||
| 49 | $user = $em->getRepository(User::class)->find($id); |
||||
| 50 | |||||
| 51 | $user->setRoles(['ROLE_USER']); |
||||
| 52 | $em->flush(); |
||||
| 53 | |||||
| 54 | $this->addFlash('success', 'Ce membre n\'est plus administrateur'); |
||||
| 55 | return $this->redirectToRoute('admin_users'); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * @Route("/admin/users/admin", name="admin_users_admin") |
||||
| 61 | */ |
||||
| 62 | public function adminOnly() |
||||
| 63 | { |
||||
| 64 | $em = $this->getDoctrine()->getManager(); |
||||
| 65 | $users = $em->getRepository(User::class)->findByRole('ROLE_ADMIN'); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 66 | |||||
| 67 | return $this->render('admin_user/index.html.twig', [ |
||||
| 68 | 'users' => $users, |
||||
| 69 | ]); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * @Route("/admin/users/users", name="admin_users_users") |
||||
| 74 | */ |
||||
| 75 | public function usersOnly() |
||||
| 76 | { |
||||
| 77 | $em = $this->getDoctrine()->getManager(); |
||||
| 78 | $users = $em->getRepository(User::class)->findByRole('ROLE_USER'); |
||||
|
0 ignored issues
–
show
'ROLE_USER' of type string is incompatible with the type array expected by parameter $roles of App\Repository\UserRepository::findByRole().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 79 | |||||
| 80 | return $this->render('admin_user/index.html.twig', [ |
||||
| 81 | 'users' => $users, |
||||
| 82 | ]); |
||||
| 83 | } |
||||
| 84 | } |
||||
| 85 |