|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ControleOnline\Entity\People; |
|
6
|
|
|
use ControleOnline\Entity\PeopleDomain; |
|
7
|
|
|
use ControleOnline\Entity\PeopleLink; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
|
|
10
|
|
|
use App\Service\PeopleService; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class PeopleRoleService |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Entity Manager |
|
16
|
|
|
* |
|
17
|
|
|
* @var EntityManagerInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $manager = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Security |
|
23
|
|
|
* |
|
24
|
|
|
* @var Security |
|
25
|
|
|
*/ |
|
26
|
|
|
private $security = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* People Service |
|
30
|
|
|
* |
|
31
|
|
|
* @var PeopleService |
|
32
|
|
|
*/ |
|
33
|
|
|
private $people = null; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
EntityManagerInterface $entityManager, |
|
37
|
|
|
Security $security, |
|
38
|
|
|
PeopleService $peopleService |
|
39
|
|
|
) { |
|
40
|
|
|
$this->manager = $entityManager; |
|
41
|
|
|
$this->security = $security; |
|
42
|
|
|
$this->people = $peopleService; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isFranchisee(People $people): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return in_array('franchisee', $this->getAllRoles($people)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function isSuperAdmin(People $people): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return in_array('super', $this->getAllRoles($people)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function isSalesman(People $people): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return in_array('salesman', $this->getAllRoles($people)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getAllRoles(People $people = null): array |
|
61
|
|
|
{ |
|
62
|
|
|
|
|
63
|
|
|
$mainCompany = $this->getMainCompany(); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if ($people === null) { |
|
67
|
|
|
$people = $this->security->getUser()->getPeople(); |
|
68
|
|
|
if (!($people instanceof People)) { |
|
69
|
|
|
return ['guest']; |
|
70
|
|
|
} |
|
71
|
|
|
} else { |
|
72
|
|
|
$peopleCompany = $people->getLink()->first(); |
|
73
|
|
|
if ($peopleCompany === false) { |
|
74
|
|
|
return ['guest']; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$myCompany = $peopleCompany->getCompany(); |
|
|
|
|
|
|
79
|
|
|
return $this->getAllRolesByCompany($people, $myCompany); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getAllRolesByCompany(People $people, People $company): array |
|
83
|
|
|
{ |
|
84
|
|
|
$peopleRole = []; |
|
85
|
|
|
$mainCompany = $this->getMainCompany(); |
|
86
|
|
|
|
|
87
|
|
|
if ($company->getId() == $mainCompany->getId()) { |
|
88
|
|
|
$isSuper = $mainCompany->getCompany() |
|
|
|
|
|
|
89
|
|
|
->exists( |
|
90
|
|
|
function ($key, PeopleLink $peopleLink) use ($people) { |
|
91
|
|
|
return $peopleLink->getPeople()->getId() === $people->getId(); |
|
92
|
|
|
} |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
if ($isSuper) { |
|
96
|
|
|
$peopleRole[] = 'super'; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$peopleFranchisee = $this->manager->getRepository(People::class)->getPeopleLink($mainCompany, 'franchisee'); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$isFranchisee = $mainCompany->getCompany() |
|
103
|
|
|
->exists( |
|
104
|
|
|
function ($key, PeopleLink $peopleFranchisee) use ($people, $company) { |
|
105
|
|
|
foreach ($peopleFranchisee->getCompany() as $peopleLink) { |
|
106
|
|
|
if ($peopleLink->getCompany()->getId() == $company->getId() && $peopleLink->getPeople()->getId() === $people->getId()) { |
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
); |
|
112
|
|
|
$peopleRole[] = 'franchisee'; |
|
113
|
|
|
if ($isFranchisee) { |
|
114
|
|
|
$peopleRole[] = 'franchisee'; |
|
115
|
|
|
$peopleRole[] = 'admin'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$isClient = $company->getCompany() |
|
119
|
|
|
->exists( |
|
120
|
|
|
function ($key, PeopleLink $peopleLink) use ($people) { |
|
121
|
|
|
return $peopleLink->getPeople()->getId() === $people->getId(); |
|
122
|
|
|
} |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
if ($isClient) { |
|
126
|
|
|
$peopleRole[] = 'client'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$isClient = $people->getLink() |
|
131
|
|
|
->exists( |
|
132
|
|
|
function ($key, PeopleLink $peopleLink) use ($company) { |
|
133
|
|
|
return $this->manager->getRepository(PeopleClient::class)->findOneBy( |
|
|
|
|
|
|
134
|
|
|
[ |
|
135
|
|
|
'client' => $peopleLink->getCompany(), |
|
136
|
|
|
'company_id' => $company->getId() |
|
137
|
|
|
] |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
if ($isClient) { |
|
143
|
|
|
$peopleRole[] = 'client'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
$isSalesman = $company->getPeopleSalesman() |
|
|
|
|
|
|
148
|
|
|
->exists( |
|
149
|
|
|
function ($key, PeopleSalesman $peopleSalesman) use ($people) { |
|
|
|
|
|
|
150
|
|
|
return $peopleSalesman->getSalesman()->getId() === $people->getId(); |
|
151
|
|
|
} |
|
152
|
|
|
); |
|
153
|
|
|
if ($isSalesman) { |
|
154
|
|
|
$peopleRole[] = 'salesman'; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
return array_values(array_unique(empty($peopleRole) ? ['guest'] : $peopleRole)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Retorna a people da empresa principal segundo o dominio da api |
|
163
|
|
|
* |
|
164
|
|
|
* @return People |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getMainCompany(): People |
|
167
|
|
|
{ |
|
168
|
|
|
$domain = $_SERVER['HTTP_HOST']; |
|
169
|
|
|
$company = $this->manager->getRepository(PeopleDomain::class)->findOneBy(['domain' => $domain]); |
|
170
|
|
|
|
|
171
|
|
|
if ($company === null) |
|
172
|
|
|
throw new \Exception( |
|
173
|
|
|
sprintf('Main company "%s" not found', $domain) |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
return $company->getPeople(); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths