GestionePermessi   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 84.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 36
eloc 91
c 1
b 0
f 0
dl 0
loc 175
ccs 84
cts 99
cp 0.8485
rs 9.52

11 Methods

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