Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

GestionePermessi::creare()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 13
nop 1
dl 0
loc 22
ccs 18
cts 18
cp 1
crap 6
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
6
7
/*
8
 * Se c'è l'accoppiata UTENTE + MODULO allora vale quel permesso
9
 * Se c'è l'accoppiata RUOLO + MODULO allora vale quel permesso
10
 * Altrimenti solo MODULO
11
 * Se non trovo informazioni di sorta, il modulo è chiuso
12
 */
13
14
class GestionePermessi
15
{
16
17
    protected $modulo;
18
    protected $crud;
19
    private $container;
20
21 14
    public function __construct(Container $container)
22
    {
23 14
        $this->container = $container;
24 14
    }
25
26 12
    private function presente($lettera)
27
    {
28 12
        if (stripos($this->crud, $lettera) !== false) {
29 11
            return true;
30
        } else {
31 2
            return false;
32
        }
33
    }
34
35 13
    public function leggere($parametri = array())
36
    {
37 13
        if (!$this->container->get('security.token_storage')->getToken()) {
38 1
            return null;
39
        }
40 12
        if (isset($parametri['modulo'])) {
41 12
            $this->modulo = $parametri['modulo'];
42 12
        }
43
44 12
        $this->setCrud();
45
46 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
47 12
        $q = $this->container->get('doctrine')
48 12
                ->getRepository('FiCoreBundle:Operatori')
49 12
                ->find($utente);
50
51 12
        $isSuperAdmin = false;
52 12
        if ($q) {
53 12
            if ($q->getRuoli()) {
54 12
                $isSuperAdmin = $q->getRuoli()->isSuperadmin();
55 12
            }
56 12
        }
57
58 12
        return $this->presente('R') || ($isSuperAdmin); //SuperAdmin
59
    }
60
61 13
    public function cancellare($parametri = array())
62
    {
63 13
        if (!$this->container->get('security.token_storage')->getToken()) {
64 1
            return null;
65
        }
66 12
        if (isset($parametri['modulo'])) {
67 12
            $this->modulo = $parametri['modulo'];
68 12
        }
69 12
        $this->setCrud();
70 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
71 12
        $q = $this->container->get('doctrine')
72 12
                ->getRepository('FiCoreBundle:Operatori')
73 12
                ->find($utente);
74
75 12
        $isSuperAdmin = false;
76 12
        if ($q) {
77 12
            if ($q->getRuoli()) {
78 12
                $isSuperAdmin = $q->getRuoli()->isSuperadmin();
79 12
            }
80 12
        }
81
82 12
        return $this->presente('D') || ($isSuperAdmin); //SuperAdmin
83
    }
84
85 13
    public function creare($parametri = array())
86
    {
87 13
        if (!$this->container->get('security.token_storage')->getToken()) {
88 1
            return null;
89
        }
90 12
        if (isset($parametri['modulo'])) {
91 12
            $this->modulo = $parametri['modulo'];
92 12
        }
93 12
        $this->setCrud();
94 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
95 12
        $q = $this->container->get('doctrine')
96 12
                ->getRepository('FiCoreBundle:Operatori')
97 12
                ->find($utente);
98
99 12
        $isSuperAdmin = false;
100 12
        if ($q) {
101 12
            if ($q->getRuoli()) {
102 12
                $isSuperAdmin = $q->getRuoli()->isSuperadmin();
103 12
            }
104 12
        }
105
106 12
        return $this->presente('C') || ($isSuperAdmin); //SuperAdmin
107
    }
108
109 13
    public function aggiornare($parametri = array())
110
    {
111 13
        if (!$this->container->get('security.token_storage')->getToken()) {
112 1
            return null;
113
        }
114
115 12
        if (isset($parametri['modulo'])) {
116 12
            $this->modulo = $parametri['modulo'];
117 12
        }
118 12
        $this->setCrud();
119 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
120 12
        $q = $this->container->get('doctrine')
121 12
                ->getRepository('FiCoreBundle:Operatori')
122 12
                ->find($utente);
123
124 12
        $isSuperAdmin = false;
125 12
        if ($q) {
126 12
            if ($q->getRuoli()) {
127 12
                $isSuperAdmin = $q->getRuoli()->isSuperadmin();
128 12
            }
129 12
        }
130
131 12
        return $this->presente('U') || ($isSuperAdmin); //SuperAdmin
132
    }
133
134
    public function sulmenu($parametri = array())
135
    {
136
        if (isset($parametri['modulo'])) {
137
            $this->modulo = $parametri['modulo'];
138
        }
139
        $permesso = $this->leggere($parametri) ||
140
                $this->cancellare($parametri) ||
141
                $this->creare($parametri) ||
142
                $this->aggiornare($parametri);
143
144
        if ($permesso) {
145
            return true;
146
        }
147
148
        return false;
149
    }
150
151 12
    public function setCrud($parametri = array())
152
    {
153 12
        if (isset($parametri['modulo'])) {
154
            $this->modulo = $parametri['modulo'];
155
        }
156
157 12
        $utentecorrente = $this->utentecorrente();
158
159 12
        $q = $this->container->get('doctrine')
160 12
                ->getRepository('FiCoreBundle:Permessi')
161 12
                ->findOneBy(array('operatori_id' => $utentecorrente['id'], 'modulo' => $this->modulo));
162
163 12
        if ($q) {
164
            $this->crud = $q->getCrud();
165
166
            return;
167
        }
168
169 12
        $q = $this->container->get('doctrine')
170 12
                ->getRepository('FiCoreBundle:Permessi')
171 12
                ->findOneBy(array('ruoli_id' => $utentecorrente['ruolo_id'], 'modulo' => $this->modulo, 'operatori_id' => null));
172
173 12
        if ($q) {
174 11
            $this->crud = $q->getCrud();
175
176 11
            return;
177
        }
178
179 1
        $q = $this->container->get('doctrine')
180 1
                ->getRepository('FiCoreBundle:Permessi')
181 1
                ->findOneBy(array('ruoli_id' => null, 'modulo' => $this->modulo, 'operatori_id' => null));
182
183 1
        if ($q) {
184
            $this->crud = $q->getCrud();
185
            return;
186
        }
187
188 1
        $this->crud = '';
189 1
    }
190
191 13
    public function utentecorrente()
192
    {
193 13
        $utentecorrente  = array();
194
        
195 13
        if (!$this->container->get('security.token_storage')->getToken()) {
196 1
            $utentecorrente['nome'] = 'Utente non registrato';
197 1
            $utentecorrente['id'] = 0;
198 1
            $utentecorrente['ruolo_id'] = 0;
199
200 1
            return $utentecorrente;
201
        }
202
203 12
        $utente = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
204 12
        $q = $this->container->get('doctrine')
205 12
                ->getRepository('FiCoreBundle:Operatori')
206 12
                ->find($utente);
207
208 12
        $utentecorrente['username'] = $utente;
209 12
        $utentecorrente['codice'] = $utente;
210
211 12
        if (!$q) {
212
            $utentecorrente['nome'] = 'Utente non registrato';
213
            $utentecorrente['id'] = 0;
214
            $utentecorrente['ruolo_id'] = 0;
215
216
            return $utentecorrente;
217
        }
218
219 12
        $utentecorrente['nome'] = $q->getOperatore();
220 12
        $utentecorrente['id'] = $q->getId();
221 12
        $utentecorrente['ruolo_id'] = ($q->getRuoli() ? $q->getRuoli()->getId() : 0);
222
223 12
        return $utentecorrente;
224
    }
225
226 13
    public function impostaPermessi($parametri = array())
227
    {
228 13
        $risposta = array();
229
230 13
        $risposta['permessiedit'] = $this->aggiornare($parametri);
231 13
        $risposta['permessidelete'] = $this->cancellare($parametri);
232 13
        $risposta['permessicreate'] = $this->creare($parametri);
233 13
        $risposta['permessiread'] = $this->leggere($parametri);
234
235 13
        return $risposta;
236
    }
237
}
238