Completed
Push — 5.0 ( a48099...63af02 )
by
unknown
11:33
created

Controller/RolesController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\UserManagementBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Kunstmaan\AdminBundle\Controller\BaseSettingsController;
8
use Kunstmaan\AdminBundle\Entity\Role;
9
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
10
use Kunstmaan\AdminBundle\Form\RoleType;
11
use Kunstmaan\AdminListBundle\AdminList\AdminList;
12
use Kunstmaan\UserManagementBundle\AdminList\RoleAdminListConfigurator;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
20
/**
21
 * Settings controller handling everything related to creating, editing, deleting and listing roles in an admin list
22
 */
23 View Code Duplication
class RolesController extends BaseSettingsController
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    /**
26
     * List roles
27
     *
28
     * @Route   ("/", name="KunstmaanUserManagementBundle_settings_roles")
29
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
30
     *
31
     * @throws AccessDeniedException
32
     * @return array
33
     */
34
    public function listAction(Request $request)
35
    {
36
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
37
38
        $em        = $this->getDoctrine()->getManager();
39
        /* @var AdminList $adminlist */
40
        $adminlist = $this->get("kunstmaan_adminlist.factory")->createList(new RoleAdminListConfigurator($em));
41
        $adminlist->bindRequest($request);
42
43
        return array(
44
            'adminlist' => $adminlist,
45
        );
46
    }
47
48
    /**
49
     * Add a role
50
     *
51
     * @Route("/add", name="KunstmaanUserManagementBundle_settings_roles_add")
52
     * @Method({"GET", "POST"})
53
     * @Template()
54
     *
55
     * @throws AccessDeniedException
56
     * @return array|RedirectResponse
57
     */
58
    public function addAction(Request $request)
59
    {
60
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
61
62
        /* @var $em EntityManager */
63
        $em = $this->getDoctrine()->getManager();
64
        $role = new Role('');
65
        $form = $this->createForm(RoleType::class, $role);
66
67
        if ($request->isMethod('POST')) {
68
            $form->handleRequest($request);
69
            if ($form->isSubmitted() && $form->isValid()) {
70
                $em->persist($role);
71
                $em->flush();
72
73
                $this->addFlash(
74
                    FlashTypes::SUCCESS,
75
                    $this->get('translator')->trans('kuma_user.roles.add.flash.success.%role%', [
76
                        '%role%' => $role->getRole()
77
                    ])
78
                );
79
80
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
81
            }
82
        }
83
84
        return array(
85
            'form' => $form->createView(),
86
        );
87
    }
88
89
    /**
90
     * Edit a role
91
     *
92
     * @param int $id
93
     *
94
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_roles_edit")
95
     * @Method({"GET", "POST"})
96
     * @Template()
97
     *
98
     * @throws AccessDeniedException
99
     * @return array|RedirectResponse
100
     */
101
    public function editAction(Request $request, $id)
102
    {
103
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
104
105
        /* @var $em EntityManager */
106
        $em = $this->getDoctrine()->getManager();
107
        /* @var Role $role */
108
        $role = $em->getRepository('KunstmaanAdminBundle:Role')->find($id);
109
        $form = $this->createForm(RoleType::class, $role);
110
111
        if ($request->isMethod('POST')) {
112
            $form->handleRequest($request);
113
            if ($form->isSubmitted() && $form->isValid()) {
114
                $em->persist($role);
115
                $em->flush();
116
117
                $this->addFlash(
118
                    FlashTypes::SUCCESS,
119
                    $this->get('translator')->trans('kuma_user.roles.edit.flash.success.%role%', [
120
                        '%role%' => $role->getRole()
121
                    ])
122
                );
123
124
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
125
            }
126
        }
127
128
        return array(
129
            'form' => $form->createView(),
130
            'role' => $role
131
        );
132
    }
133
134
    /**
135
     * Delete a role
136
     *
137
     * @param int $id
138
     *
139
     * @Route ("/{id}/delete", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_roles_delete")
140
     * @Method({"GET", "POST"})
141
     *
142
     * @throws AccessDeniedException
143
     * @return RedirectResponse
144
     */
145
    public function deleteAction($id)
146
    {
147
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
148
149
        /* @var $em EntityManager */
150
        $em = $this->getDoctrine()->getManager();
151
        /* @var Role $role */
152
        $role = $em->getRepository('KunstmaanAdminBundle:Role')->find($id);
153
        if (!is_null($role)) {
154
            $em->remove($role);
155
            $em->flush();
156
157
            $this->addFlash(
158
                FlashTypes::SUCCESS,
159
                $this->get('translator')->trans('kuma_user.roles.delete.flash.success.%role%', [
160
                    '%role%' => $role->getRole()
161
                ])
162
            );
163
        }
164
165
        return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_roles'));
166
    }
167
168
}
169