@@ -12,57 +12,57 @@ discard block |
||
12 | 12 | |
13 | 13 | class UserService |
14 | 14 | { |
15 | - public function __construct( |
|
15 | + public function __construct( |
|
16 | 16 | private EntityManagerInterface $manager, |
17 | 17 | private UserPasswordEncoderInterface $encoder, |
18 | 18 | private FileService $fileService |
19 | - ) {} |
|
20 | - public function changePassword(User $user, $password) |
|
21 | - { |
|
19 | + ) {} |
|
20 | + public function changePassword(User $user, $password) |
|
21 | + { |
|
22 | 22 | if (!$this->getPermission()) |
23 | - throw new Exception("You should not pass!!!", 301); |
|
23 | + throw new Exception("You should not pass!!!", 301); |
|
24 | 24 | |
25 | 25 | $user->setHash( |
26 | - $this->encoder->encodePassword($user, $password) |
|
26 | + $this->encoder->encodePassword($user, $password) |
|
27 | 27 | ); |
28 | 28 | |
29 | 29 | $this->manager->persist($user); |
30 | 30 | $this->manager->flush(); |
31 | 31 | return $user; |
32 | - } |
|
32 | + } |
|
33 | 33 | |
34 | - public function changeApiKey(User $user) |
|
35 | - { |
|
34 | + public function changeApiKey(User $user) |
|
35 | + { |
|
36 | 36 | if (!$this->getPermission()) |
37 | - throw new Exception("You should not pass!!!", 301); |
|
37 | + throw new Exception("You should not pass!!!", 301); |
|
38 | 38 | |
39 | 39 | $user->generateApiKey(); |
40 | 40 | |
41 | 41 | $this->manager->persist($user); |
42 | 42 | $this->manager->flush(); |
43 | 43 | return $user; |
44 | - } |
|
44 | + } |
|
45 | 45 | |
46 | 46 | |
47 | - public function discoveryUser($email, $password, $firstName, $lastName) |
|
48 | - { |
|
47 | + public function discoveryUser($email, $password, $firstName, $lastName) |
|
48 | + { |
|
49 | 49 | |
50 | 50 | $user = $this->manager->getRepository(User::class) |
51 | - ->findOneBy([ |
|
51 | + ->findOneBy([ |
|
52 | 52 | 'username' => $email, |
53 | - ]); |
|
53 | + ]); |
|
54 | 54 | |
55 | 55 | |
56 | 56 | $people = $this->discoveryPeople($email, $firstName, $lastName); |
57 | 57 | |
58 | 58 | if (!$user) |
59 | - $user = $this->createUser($people, $email, $password); |
|
59 | + $user = $this->createUser($people, $email, $password); |
|
60 | 60 | |
61 | 61 | return $user; |
62 | - } |
|
62 | + } |
|
63 | 63 | |
64 | - public function getUserSession($user) |
|
65 | - { |
|
64 | + public function getUserSession($user) |
|
65 | + { |
|
66 | 66 | |
67 | 67 | // get contact data from user |
68 | 68 | |
@@ -71,93 +71,93 @@ discard block |
||
71 | 71 | $number = ''; |
72 | 72 | |
73 | 73 | if ($user->getPeople()->getEmail()->count() > 0) |
74 | - $email = $user->getPeople()->getEmail()->first()->getEmail(); |
|
74 | + $email = $user->getPeople()->getEmail()->first()->getEmail(); |
|
75 | 75 | |
76 | 76 | if ($user->getPeople()->getPhone()->count() > 0) { |
77 | - $phone = $user->getPeople()->getPhone()->first(); |
|
78 | - $code = $phone->getDdd(); |
|
79 | - $number = $phone->getPhone(); |
|
77 | + $phone = $user->getPeople()->getPhone()->first(); |
|
78 | + $code = $phone->getDdd(); |
|
79 | + $number = $phone->getPhone(); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | return [ |
83 | - 'id' => $user->getPeople()->getId(), |
|
84 | - 'username' => $user->getUsername(), |
|
85 | - 'roles' => $user->getRoles(), |
|
86 | - 'api_key' => $user->getApiKey(), |
|
87 | - 'people' => $user->getPeople()->getId(), |
|
88 | - 'mycompany' => $this->getCompanyId($user), |
|
89 | - 'realname' => $this->getUserRealName($user->getPeople()), |
|
90 | - 'avatar' => $this->fileService->getFileUrl($user->getPeople()), |
|
91 | - 'email' => $email, |
|
92 | - 'phone' => sprintf('%s%s', $code, $number), |
|
93 | - 'active' => (int) $user->getPeople()->getEnabled(), |
|
83 | + 'id' => $user->getPeople()->getId(), |
|
84 | + 'username' => $user->getUsername(), |
|
85 | + 'roles' => $user->getRoles(), |
|
86 | + 'api_key' => $user->getApiKey(), |
|
87 | + 'people' => $user->getPeople()->getId(), |
|
88 | + 'mycompany' => $this->getCompanyId($user), |
|
89 | + 'realname' => $this->getUserRealName($user->getPeople()), |
|
90 | + 'avatar' => $this->fileService->getFileUrl($user->getPeople()), |
|
91 | + 'email' => $email, |
|
92 | + 'phone' => sprintf('%s%s', $code, $number), |
|
93 | + 'active' => (int) $user->getPeople()->getEnabled(), |
|
94 | 94 | ]; |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | - private function getUserRealName(People $people): string |
|
98 | - { |
|
97 | + private function getUserRealName(People $people): string |
|
98 | + { |
|
99 | 99 | $realName = 'John Doe'; |
100 | 100 | |
101 | 101 | if ($people->getPeopleType() == 'J') |
102 | - $realName = $people->getAlias(); |
|
102 | + $realName = $people->getAlias(); |
|
103 | 103 | |
104 | 104 | else { |
105 | - if ($people->getPeopleType() == 'F') { |
|
105 | + if ($people->getPeopleType() == 'F') { |
|
106 | 106 | $realName = $people->getName(); |
107 | 107 | $realName .= ' ' . $people->getAlias(); |
108 | 108 | $realName = trim($realName); |
109 | - } |
|
109 | + } |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | return $realName; |
113 | - } |
|
113 | + } |
|
114 | 114 | |
115 | - public function discoveryPeople($mail, $firstName = '', $lastName = '') |
|
116 | - { |
|
115 | + public function discoveryPeople($mail, $firstName = '', $lastName = '') |
|
116 | + { |
|
117 | 117 | $email = $this->manager->getRepository(Email::class) |
118 | - ->findOneBy([ |
|
118 | + ->findOneBy([ |
|
119 | 119 | 'email' => $mail, |
120 | - ]); |
|
120 | + ]); |
|
121 | 121 | if ($email) { |
122 | - $people = $email->getPeople(); |
|
122 | + $people = $email->getPeople(); |
|
123 | 123 | } else { |
124 | - $email = new Email(); |
|
125 | - $email->setEmail($mail); |
|
126 | - $this->manager->persist($email); |
|
124 | + $email = new Email(); |
|
125 | + $email->setEmail($mail); |
|
126 | + $this->manager->persist($email); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | if (!$people) { |
130 | 130 | |
131 | - $lang = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-BR']); |
|
132 | - $people = new People(); |
|
133 | - $people->setAlias($firstName); |
|
134 | - $people->setName($lastName); |
|
135 | - $people->setLanguage($lang); |
|
136 | - //$people->setBilling(0); |
|
137 | - //$people->setBillingDays('daily'); |
|
138 | - //$people->setPaymentTerm(1); |
|
139 | - //$people->setIcms(0); |
|
140 | - $email->setPeople($people); |
|
141 | - $this->manager->persist($email); |
|
131 | + $lang = $this->manager->getRepository(Language::class)->findOneBy(['language' => 'pt-BR']); |
|
132 | + $people = new People(); |
|
133 | + $people->setAlias($firstName); |
|
134 | + $people->setName($lastName); |
|
135 | + $people->setLanguage($lang); |
|
136 | + //$people->setBilling(0); |
|
137 | + //$people->setBillingDays('daily'); |
|
138 | + //$people->setPaymentTerm(1); |
|
139 | + //$people->setIcms(0); |
|
140 | + $email->setPeople($people); |
|
141 | + $this->manager->persist($email); |
|
142 | 142 | } |
143 | 143 | |
144 | 144 | $this->manager->persist($people); |
145 | 145 | $this->manager->flush(); |
146 | 146 | return $people; |
147 | - } |
|
147 | + } |
|
148 | 148 | |
149 | - public function createUser(People $people, $username, $password) |
|
150 | - { |
|
149 | + public function createUser(People $people, $username, $password) |
|
150 | + { |
|
151 | 151 | if (!$this->getPermission()) |
152 | - throw new Exception("You should not pass!!!", 301); |
|
152 | + throw new Exception("You should not pass!!!", 301); |
|
153 | 153 | |
154 | 154 | $user = $this->manager->getRepository(User::class) |
155 | - ->findOneBy([ |
|
155 | + ->findOneBy([ |
|
156 | 156 | 'username' => $username, |
157 | - ]); |
|
157 | + ]); |
|
158 | 158 | |
159 | 159 | if ($user) |
160 | - throw new Exception("User already exists", 301); |
|
160 | + throw new Exception("User already exists", 301); |
|
161 | 161 | |
162 | 162 | $user = new User(); |
163 | 163 | $user->setPeople($people); |
@@ -167,28 +167,28 @@ discard block |
||
167 | 167 | $this->manager->persist($user); |
168 | 168 | $this->manager->flush(); |
169 | 169 | return $user; |
170 | - } |
|
170 | + } |
|
171 | 171 | |
172 | 172 | |
173 | - public function getCompany(User $user) |
|
174 | - { |
|
173 | + public function getCompany(User $user) |
|
174 | + { |
|
175 | 175 | $peopleLink = $user->getPeople()->getLink()->first(); |
176 | 176 | |
177 | 177 | if ($peopleLink !== false && $peopleLink->getCompany() instanceof People) |
178 | - return $peopleLink->getCompany(); |
|
179 | - } |
|
178 | + return $peopleLink->getCompany(); |
|
179 | + } |
|
180 | 180 | |
181 | - public function getCompanyId(User $user) |
|
182 | - { |
|
181 | + public function getCompanyId(User $user) |
|
182 | + { |
|
183 | 183 | $company = $this->getCompany($user); |
184 | 184 | return $company ? $company->getId() : null; |
185 | - } |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * @todo arrumar |
|
189 | - */ |
|
190 | - private function getPermission() |
|
191 | - { |
|
187 | + /** |
|
188 | + * @todo arrumar |
|
189 | + */ |
|
190 | + private function getPermission() |
|
191 | + { |
|
192 | 192 | return true; |
193 | - } |
|
193 | + } |
|
194 | 194 | } |
@@ -19,8 +19,9 @@ discard block |
||
19 | 19 | ) {} |
20 | 20 | public function changePassword(User $user, $password) |
21 | 21 | { |
22 | - if (!$this->getPermission()) |
|
23 | - throw new Exception("You should not pass!!!", 301); |
|
22 | + if (!$this->getPermission()) { |
|
23 | + throw new Exception("You should not pass!!!", 301); |
|
24 | + } |
|
24 | 25 | |
25 | 26 | $user->setHash( |
26 | 27 | $this->encoder->encodePassword($user, $password) |
@@ -33,8 +34,9 @@ discard block |
||
33 | 34 | |
34 | 35 | public function changeApiKey(User $user) |
35 | 36 | { |
36 | - if (!$this->getPermission()) |
|
37 | - throw new Exception("You should not pass!!!", 301); |
|
37 | + if (!$this->getPermission()) { |
|
38 | + throw new Exception("You should not pass!!!", 301); |
|
39 | + } |
|
38 | 40 | |
39 | 41 | $user->generateApiKey(); |
40 | 42 | |
@@ -55,8 +57,9 @@ discard block |
||
55 | 57 | |
56 | 58 | $people = $this->discoveryPeople($email, $firstName, $lastName); |
57 | 59 | |
58 | - if (!$user) |
|
59 | - $user = $this->createUser($people, $email, $password); |
|
60 | + if (!$user) { |
|
61 | + $user = $this->createUser($people, $email, $password); |
|
62 | + } |
|
60 | 63 | |
61 | 64 | return $user; |
62 | 65 | } |
@@ -70,8 +73,9 @@ discard block |
||
70 | 73 | $code = ''; |
71 | 74 | $number = ''; |
72 | 75 | |
73 | - if ($user->getPeople()->getEmail()->count() > 0) |
|
74 | - $email = $user->getPeople()->getEmail()->first()->getEmail(); |
|
76 | + if ($user->getPeople()->getEmail()->count() > 0) { |
|
77 | + $email = $user->getPeople()->getEmail()->first()->getEmail(); |
|
78 | + } |
|
75 | 79 | |
76 | 80 | if ($user->getPeople()->getPhone()->count() > 0) { |
77 | 81 | $phone = $user->getPeople()->getPhone()->first(); |
@@ -98,10 +102,9 @@ discard block |
||
98 | 102 | { |
99 | 103 | $realName = 'John Doe'; |
100 | 104 | |
101 | - if ($people->getPeopleType() == 'J') |
|
102 | - $realName = $people->getAlias(); |
|
103 | - |
|
104 | - else { |
|
105 | + if ($people->getPeopleType() == 'J') { |
|
106 | + $realName = $people->getAlias(); |
|
107 | + } else { |
|
105 | 108 | if ($people->getPeopleType() == 'F') { |
106 | 109 | $realName = $people->getName(); |
107 | 110 | $realName .= ' ' . $people->getAlias(); |
@@ -148,16 +151,18 @@ discard block |
||
148 | 151 | |
149 | 152 | public function createUser(People $people, $username, $password) |
150 | 153 | { |
151 | - if (!$this->getPermission()) |
|
152 | - throw new Exception("You should not pass!!!", 301); |
|
154 | + if (!$this->getPermission()) { |
|
155 | + throw new Exception("You should not pass!!!", 301); |
|
156 | + } |
|
153 | 157 | |
154 | 158 | $user = $this->manager->getRepository(User::class) |
155 | 159 | ->findOneBy([ |
156 | 160 | 'username' => $username, |
157 | 161 | ]); |
158 | 162 | |
159 | - if ($user) |
|
160 | - throw new Exception("User already exists", 301); |
|
163 | + if ($user) { |
|
164 | + throw new Exception("User already exists", 301); |
|
165 | + } |
|
161 | 166 | |
162 | 167 | $user = new User(); |
163 | 168 | $user->setPeople($people); |
@@ -174,8 +179,9 @@ discard block |
||
174 | 179 | { |
175 | 180 | $peopleLink = $user->getPeople()->getLink()->first(); |
176 | 181 | |
177 | - if ($peopleLink !== false && $peopleLink->getCompany() instanceof People) |
|
178 | - return $peopleLink->getCompany(); |
|
182 | + if ($peopleLink !== false && $peopleLink->getCompany() instanceof People) { |
|
183 | + return $peopleLink->getCompany(); |
|
184 | + } |
|
179 | 185 | } |
180 | 186 | |
181 | 187 | public function getCompanyId(User $user) |