1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
|
|
|
8
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
|
|
|
10
|
|
|
|
11
|
|
|
use ControleOnline\Entity\People; |
|
|
|
|
12
|
|
|
use ControleOnline\Entity\User; |
13
|
|
|
use ControleOnline\Entity\Person; |
|
|
|
|
14
|
|
|
|
15
|
|
|
class CreateUserAction |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Entity Manager |
19
|
|
|
* |
20
|
|
|
* @var EntityManagerInterface |
21
|
|
|
*/ |
22
|
|
|
private $manager = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Request |
26
|
|
|
* |
27
|
|
|
* @var Request |
28
|
|
|
*/ |
29
|
|
|
private $request = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Security |
33
|
|
|
* |
34
|
|
|
* @var Security |
35
|
|
|
*/ |
36
|
|
|
private $security = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Current user |
40
|
|
|
* |
41
|
|
|
* @var \ControleOnline\Entity\User |
42
|
|
|
*/ |
43
|
|
|
private $currentUser = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Password encoder |
47
|
|
|
* |
48
|
|
|
* @var UserPasswordEncoderInterface |
49
|
|
|
*/ |
50
|
|
|
private $encoder = null; |
51
|
|
|
|
52
|
|
|
public function __construct(EntityManagerInterface $manager, Security $security, UserPasswordEncoderInterface $passwordEncoder) |
53
|
|
|
{ |
54
|
|
|
$this->manager = $manager; |
55
|
|
|
$this->security = $security; |
56
|
|
|
$this->currentUser = $security->getUser(); |
57
|
|
|
$this->encoder = $passwordEncoder; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __invoke(Person $data, Request $request): JsonResponse |
61
|
|
|
{ |
62
|
|
|
$this->request = $request; |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$payload = json_decode($this->request->getContent(), true); |
66
|
|
|
$result = $this->addUser($data, $payload); |
67
|
|
|
|
68
|
|
|
return new JsonResponse([ |
69
|
|
|
'response' => [ |
70
|
|
|
'data' => $result, |
71
|
|
|
'count' => 1, |
72
|
|
|
'error' => '', |
73
|
|
|
'success' => true, |
74
|
|
|
], |
75
|
|
|
], 200); |
76
|
|
|
|
77
|
|
|
} catch (\Exception $e) { |
78
|
|
|
|
79
|
|
|
return new JsonResponse([ |
80
|
|
|
'response' => [ |
81
|
|
|
'data' => [], |
82
|
|
|
'count' => 0, |
83
|
|
|
'error' => $e->getMessage(), |
84
|
|
|
'success' => false, |
85
|
|
|
], |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function addUser(Person $person, array $payload): ?array |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
$this->manager->getConnection()->beginTransaction(); |
95
|
|
|
|
96
|
|
|
if (!isset($payload['username']) || empty($payload['username'])) { |
97
|
|
|
throw new \InvalidArgumentException('Username param is not valid'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!isset($payload['password']) || empty($payload['password'])) { |
101
|
|
|
throw new \InvalidArgumentException('Password param is not valid'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$company = $this->manager->getRepository(People::class)->find($person->getId()); |
105
|
|
|
$user = $this->manager->getRepository(User::class)->findOneBy(['username' => $payload['username']]); |
106
|
|
|
if ($user instanceof User) { |
107
|
|
|
throw new \InvalidArgumentException('O username já esta em uso'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$user = new User(); |
111
|
|
|
$user->setUsername($payload['username']); |
112
|
|
|
$user->setHash ($this->encoder->encodePassword($user, $payload['password'])); |
113
|
|
|
$user->setPeople ($company); |
114
|
|
|
|
115
|
|
|
$this->manager->persist($user); |
116
|
|
|
|
117
|
|
|
$this->manager->flush(); |
118
|
|
|
$this->manager->getConnection()->commit(); |
119
|
|
|
|
120
|
|
|
return [ |
121
|
|
|
'id' => $user->getId() |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
if ($this->manager->getConnection()->isTransactionActive()) |
126
|
|
|
$this->manager->getConnection()->rollBack(); |
127
|
|
|
|
128
|
|
|
throw new \InvalidArgumentException($e->getMessage()); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function deleteUser(Person $person, array $payload): bool |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
try { |
135
|
|
|
$this->manager->getConnection()->beginTransaction(); |
136
|
|
|
|
137
|
|
|
if (!isset($payload['id'])) { |
138
|
|
|
throw new \InvalidArgumentException('Document id is not defined'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$company = $this->manager->getRepository(People::class)->find($person->getId()); |
142
|
|
|
$users = $this->manager->getRepository(User::class)->findBy(['people' => $company]); |
143
|
|
|
if (count($users) == 1) { |
144
|
|
|
throw new \InvalidArgumentException('Deve existir pelo menos um usuário'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$user = $this->manager->getRepository(User::class)->findOneBy(['id' => $payload['id'], 'people' => $company]); |
148
|
|
|
if (!$user instanceof User) { |
149
|
|
|
throw new \InvalidArgumentException('Person user was not found'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->manager->remove($user); |
153
|
|
|
|
154
|
|
|
$this->manager->flush(); |
155
|
|
|
$this->manager->getConnection()->commit(); |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
|
159
|
|
|
} catch (\Exception $e) { |
160
|
|
|
if ($this->manager->getConnection()->isTransactionActive()) |
161
|
|
|
$this->manager->getConnection()->rollBack(); |
162
|
|
|
|
163
|
|
|
throw new \InvalidArgumentException($e->getMessage()); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function getUsers(Person $person, ?array $payload = null): array |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$members = []; |
170
|
|
|
$company = $this->manager->getRepository(People::class )->find($person->getId()); |
171
|
|
|
$users = $this->manager->getRepository(User::class)->findBy(['people' => $company]); |
172
|
|
|
|
173
|
|
|
foreach ($users as $user) { |
174
|
|
|
$members[] = [ |
175
|
|
|
'id' => $user->getId(), |
176
|
|
|
'username' => $user->getUsername(), |
177
|
|
|
'apiKey' => $user->getApiKey(), |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return [ |
182
|
|
|
'members' => $members, |
183
|
|
|
'total' => count($members), |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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