DroitsController.php 4.31 KB
Newer Older
1
2
3
4
5
6
7
<?php

namespace UnicaenAuth\Controller;

use UnicaenAuth\Form\Droits\Traits\RoleFormAwareTrait;
use UnicaenAuth\Service\Traits\PrivilegeServiceAwareTrait;
use UnicaenAuth\Service\Traits\RoleServiceAwareTrait;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
use Laminas\Form\Form;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

/**
 * Description of DroitsController
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
class DroitsController extends AbstractActionController
{
    use RoleServiceAwareTrait;
    use RoleFormAwareTrait;
    use PrivilegeServiceAwareTrait;

    public function indexAction()
    {
        return [];
    }

    public function rolesAction()
    {
        $roles = $this->getServiceRole()->getList();

        return compact('roles');
    }

    public function roleEditionAction()
    {
        $roleId = $this->params()->fromRoute('role');
        $role   = $this->getServiceRole()->get($roleId);
        $errors = [];

        $form = $this->getFormDroitsRole();
        if (empty($role)) {
            $title = 'Création d\'un nouveau rôle';
            $role  = $this->getServiceRole()->newEntity();
            $form->setObject($role);
        } else {
            $title = 'Édition du rôle';
            $form->bind($role);
        }
        $form->setAttribute('action', $this->url()->fromRoute(null, [], [], true));

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                try {
                    $this->getServiceRole()->save($role);
                    $form->get('id')->setValue($role->getId()); // transmet le nouvel ID
                } catch (\Exception $e) {
                    $errors[] = $e->getMessage();
                }
            }
        }

        return compact('form', 'title', 'errors');
    }

    public function roleSuppressionAction()
    {
        $roleId = $this->params()->fromRoute('role');
        $role   = $this->getServiceRole()->get($roleId);

        $title  = "Suppression du rôle";
        $form   = $this->getFormSupprimer();
        $errors = [];

        if ($this->getRequest()->isPost()) {
            try {
                $this->getServiceRole()->delete($role);
            } catch (\Exception $e) {
                $errors[] = $e->getMessage();
            }
        }

        return compact('role', 'title', 'form', 'errors');
    }

    public function privilegesAction()
    {
        $ps         = $this->getServicePrivilege()->getList();
        $privileges = [];
        foreach ($ps as $privilege) {
            $categorie = $privilege->getCategorie();
            if (!isset($privileges[$categorie->getCode()])) {
                $privileges[$categorie->getCode()] = [
                    'categorie'  => $categorie,
                    'privileges' => [],
                ];
            }
            $privileges[$categorie->getCode()]['privileges'][] = $privilege;
        }

        $roles = $this->getServiceRole()->getList();

        return compact('privileges', 'roles');
    }

    public function privilegesModifierAction()
    {
        $roleId = $this->params()->fromPost('role');
        $role   = $this->getServiceRole()->get($roleId);

        $privilegeId = $this->params()->fromPost('privilege');
        $privilege   = $this->getServicePrivilege()->get($privilegeId);

        $action    = $this->params()->fromPost('action');

        switch ($action) {
            case 'accorder':
                $this->getServicePrivilege()->addRole($privilege,$role);
                break;
            case 'refuser':
                $this->getServicePrivilege()->removeRole($privilege,$role);
                break;
        }

        $viewModel = new ViewModel();
        $viewModel->setVariables(compact('role', 'privilege'))
            ->setTerminal(true);

        return $viewModel;
    }

    public function getFormSupprimer()
    {
        $form = new Form();
        $form->add([
            'name' => 'id',
            'type' => 'Hidden',
        ]);
        $form->add([
            'name' => 'submit',
            'type'  => 'Submit',
            'attributes' => [
                'value' => 'Je confirme la suppression',
            ],
        ]);
        $form->setAttribute('action', $this->url()->fromRoute(null, [], [], true));
        return $form;
    }
}