AdminUserController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
dl 0
loc 69
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A adminOff() 0 10 1
A index() 0 7 1
A usersOnly() 0 7 1
A adminOn() 0 10 1
A adminOnly() 0 7 1
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
'ROLE_ADMIN' 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 ignore-type  annotation

65
        $users = $em->getRepository(User::class)->findByRole(/** @scrutinizer ignore-type */ 'ROLE_ADMIN');
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
Bug introduced by
'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 ignore-type  annotation

78
        $users = $em->getRepository(User::class)->findByRole(/** @scrutinizer ignore-type */ 'ROLE_USER');
Loading history...
79
80
        return $this->render('admin_user/index.html.twig', [
81
            'users' => $users,
82
        ]);
83
    }
84
}
85