Passed
Push — master ( 83de64...44197b )
by Anthony
02:43
created

AccessRightsController::deleteList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Controller;
4
5
use PiouPiou\RibsAdminBundle\Entity\AccessRight;
6
use PiouPiou\RibsAdminBundle\Service\Globals;
7
use PiouPiou\RibsAdminBundle\Service\ModuleService;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
class AccessRightsController extends AbstractController
15
{
16
    /**
17
     * @Route("/access-rights-management/", name="ribsadmin_access_rights")
18
     * @return Response
19
     */
20
    public function list(): Response
21
    {
22
        $em = $this->getDoctrine()->getManager();
23
        $acces_right = $em->getRepository("RibsAdminBundle:AccessRight")->findAll();
24
25
        return $this->render("@RibsAdmin/access-rights/list.html.twig", [
26
            "access_right" => $acces_right
27
        ]);
28
    }
29
30
    /**
31
     * @Route("/access-rights-management/create/", name="ribsadmin_access_rights_create")
32
     * @Route("/access-rights-management/edit/{guid}", name="ribsadmin_access_rights_edit")
33
     * @param Request $request
34
     * @param Globals $globals
35
     * @param ModuleService $module
36
     * @param string|null $guid
37
     * @return Response
38
     */
39
    public function edit(Request $request, Globals $globals, ModuleService $module, string $guid = null): Response
40
    {
41
        $em = $this->getDoctrine()->getManager();
42
        $list_rights_user = [];
43
44
        if ($guid === null) {
45
            $access_right = new AccessRight();
46
        } else {
47
            $access_right = $em->getRepository("RibsAdminBundle:AccessRight")->findOneBy(["guid" => $guid]);
48
            $list_rights_user = explode(",", $access_right->getAccessRights());
49
        }
50
51
        $admins = $em->getRepository("RibsAdminBundle:User")->findBy(["admin" => true, "archived" => false]);
52
53
        $form = $this->createForm("PiouPiou\RibsAdminBundle\Form\AccessRight", $access_right);
54
        $form->handleRequest($request);
55
56
        if ($form->isSubmitted() && $form->isValid()) {
57
            return $this->handleEditForm($request, $access_right);
58
        }
59
60
        return $this->render("@RibsAdmin/access-rights/edit.html.twig", [
61
            "access_right" => $access_right,
62
            "form" => $form->createView(),
63
            "form_errors" => $form->getErrors(),
64
            "list_rights_user" => $list_rights_user,
65
            "admins" => $admins,
66
            "ribs_admin_rights" => json_decode(file_get_contents($globals->getBaseBundlePath() . "/Resources/json/ribsadmin_rights.json")),
67
            "modules" => $module->getAllInfosModules()
68
        ]);
69
    }
70
71
    /**
72
     * @Route("/access-rights-management/delete/{guid}", name="ribsadmin_access_rights_delete")
73
     * @param string $guid
74
     * @return RedirectResponse function that delete an access right list
75
     */
76
    public function delete(string $guid): RedirectResponse
77
    {
78
        $em = $this->getDoctrine()->getManager();
79
        $list = $em->getRepository("RibsAdminBundle:AccessRight")->findOneBy(["guid" => $guid]);
80
81
        if ($list) {
82
            foreach ($list->getUsers() as $user) {
83
                $user->setAccessRightList(null);
84
            }
85
86
            $em->remove($list);
87
            $em->flush();
88
89
            $this->addFlash("success-flash", "The right list was deleted");
90
        } else {
91
            $this->addFlash("error-flash", "The right list wasn't found");
92
        }
93
94
        return $this->redirectToRoute("ribsadmin_access_rights");
95
    }
96
97
    /**
98
     * @param Request $request
99
     * @param AccessRight $access_right
100
     * @return RedirectResponse function that handle the form request
101
     */
102
    private function handleEditForm(Request $request, AccessRight $access_right): RedirectResponse
103
    {
104
        $em = $this->getDoctrine()->getManager();
105
106
        if ($request->get("right") === null) {
107
            $rights = "";
108
        } else {
109
            $rights = implode(",", $request->get("right"));
0 ignored issues
show
Bug introduced by
It seems like $request->get('right') can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

109
            $rights = implode(",", /** @scrutinizer ignore-type */ $request->get("right"));
Loading history...
110
        }
111
112
        $access_right->setAccessRights($rights);
113
        $em->persist($access_right);
114
        $em->flush();
115
116
        $em->getRepository("RibsAdminBundle:AccessRight")->deleteAllUsersList($access_right);
117
        $admins = $request->get("admins");
118
119
        if ($admins !== null) {
120
            foreach ($admins as $admin) {
121
                $em->getRepository("RibsAdminBundle:AccessRight")->setAccessRightListUser($access_right->getId(), $admin);
122
            }
123
        }
124
125
        $this->addFlash("success-flash", "The right list was correctly edited");
126
127
        return $this->redirectToRoute("ribsadmin_access_rights");
128
    }
129
}
130