|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Cdf\BiCoreBundle\Utils\Permessi; |
|
10
|
|
|
|
|
11
|
|
|
use Cdf\BiCoreBundle\Entity\Permessi; |
|
12
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
14
|
|
|
|
|
15
|
|
|
class PermessiUtils |
|
16
|
|
|
{ |
|
17
|
18 |
|
public function __construct(ObjectManager $em, TokenStorageInterface $user) |
|
18
|
|
|
{ |
|
19
|
18 |
|
$this->em = $em; |
|
|
|
|
|
|
20
|
18 |
|
$this->user = $user->getToken()->getUser(); |
|
|
|
|
|
|
21
|
18 |
|
} |
|
22
|
13 |
|
public function canRead($modulo) |
|
23
|
|
|
{ |
|
24
|
13 |
|
$permessi = $this->em->getRepository(Permessi::class)->findPermessoModuloOperatore($modulo, $this->user); |
|
25
|
13 |
|
$canread = false; |
|
|
|
|
|
|
26
|
13 |
|
if ($permessi) { |
|
27
|
4 |
|
if (stripos(strtoupper($permessi->getCrud()), 'R') !== false) { |
|
28
|
4 |
|
$canread = true; |
|
29
|
|
|
} |
|
30
|
|
|
} else { |
|
31
|
13 |
|
if ($this->user->isSuperadmin()) { |
|
32
|
11 |
|
$canread = true; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
13 |
|
return $canread; |
|
36
|
|
|
} |
|
37
|
12 |
|
public function canCreate($modulo) |
|
38
|
|
|
{ |
|
39
|
12 |
|
$permessi = $this->em->getRepository(Permessi::class)->findPermessoModuloOperatore($modulo, $this->user); |
|
|
|
|
|
|
40
|
12 |
|
$cancreate = false; |
|
41
|
12 |
|
if ($permessi) { |
|
42
|
4 |
|
if (stripos(strtoupper($permessi->getCrud()), 'C') !== false) { |
|
43
|
4 |
|
$cancreate = true; |
|
44
|
|
|
} |
|
45
|
|
|
} else { |
|
46
|
9 |
|
if ($this->user->isSuperadmin()) { |
|
47
|
8 |
|
$cancreate = true; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
12 |
|
return $cancreate; |
|
51
|
|
|
} |
|
52
|
15 |
|
public function canUpdate($modulo) |
|
53
|
|
|
{ |
|
54
|
15 |
|
$permessi = $this->em->getRepository(Permessi::class)->findPermessoModuloOperatore($modulo, $this->user); |
|
|
|
|
|
|
55
|
15 |
|
$canupdate = false; |
|
56
|
15 |
|
if ($permessi) { |
|
57
|
4 |
|
if (stripos(strtoupper($permessi->getCrud()), 'U') !== false) { |
|
58
|
4 |
|
$canupdate = true; |
|
59
|
|
|
} |
|
60
|
|
|
} else { |
|
61
|
12 |
|
if ($this->user->isSuperadmin()) { |
|
62
|
10 |
|
$canupdate = true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
15 |
|
return $canupdate; |
|
66
|
|
|
} |
|
67
|
12 |
|
public function canDelete($modulo) |
|
68
|
|
|
{ |
|
69
|
12 |
|
$permessi = $this->em->getRepository(Permessi::class)->findPermessoModuloOperatore($modulo, $this->user); |
|
|
|
|
|
|
70
|
12 |
|
$candelete = false; |
|
71
|
12 |
|
if ($permessi) { |
|
72
|
4 |
|
if (stripos(strtoupper($permessi->getCrud()), 'D') !== false) { |
|
73
|
4 |
|
$candelete = true; |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
9 |
|
if ($this->user->isSuperadmin()) { |
|
77
|
8 |
|
$candelete = true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
12 |
|
return $candelete; |
|
81
|
|
|
} |
|
82
|
11 |
|
public function toJson($modulo) |
|
83
|
|
|
{ |
|
84
|
|
|
return array( |
|
85
|
11 |
|
"read" => $this->canRead($modulo), |
|
|
|
|
|
|
86
|
11 |
|
"create" => $this->canCreate($modulo), |
|
|
|
|
|
|
87
|
11 |
|
"delete" => $this->canDelete($modulo), |
|
|
|
|
|
|
88
|
11 |
|
"update" => $this->canUpdate($modulo) |
|
|
|
|
|
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|