@@ -119,6 +119,9 @@ |
||
119 | 119 | return 'multipart/form-data; boundary='.$this->boundary; |
120 | 120 | } |
121 | 121 | |
122 | + /** |
|
123 | + * @param string $json |
|
124 | + */ |
|
122 | 125 | public function post(string $path, $json, array $metadata = []): ResponseInterface |
123 | 126 | { |
124 | 127 | $multipartStream = new MultipartStream($this->generateData($json, $metadata), $this->boundary); |
@@ -144,7 +144,7 @@ |
||
144 | 144 | $default = $input->getOption('default'); |
145 | 145 | if (!$input->getArgument($name) && !$default) { |
146 | 146 | $question = new Question(sprintf('<question>Please enter %s:</question>', $name)); |
147 | - $question->setValidator(function ($argument) use ($name) { |
|
147 | + $question->setValidator(function($argument) use ($name) { |
|
148 | 148 | if (empty($argument) && $name !== $this->arguments[1]) { |
149 | 149 | throw new \RuntimeException(sprintf('The %s can not be empty', $name)); |
150 | 150 | } |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -180,88 +180,3 @@ |
||
180 | 180 | return new SingleResourceResponse([ |
181 | 181 | 'status' => 401, |
182 | 182 | 'message' => <<<'MESSAGE' |
183 | -Unauthorized (user not found in Superdesk). |
|
184 | -Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
|
185 | -MESSAGE, |
|
186 | - ], new ResponseContext(401)); |
|
187 | - } |
|
188 | - |
|
189 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
190 | - if (null === $publisherUser) { |
|
191 | - try { |
|
192 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
193 | - } catch (UsernameNotFoundException $e) { |
|
194 | - $publisherUser = null; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - if (null === $publisherUser) { |
|
199 | - /** @var UserInterface $publisherUser */ |
|
200 | - $publisherUser = $userManager->createUser(); |
|
201 | - $publisherUser->setUsername($superdeskUser['username']); |
|
202 | - $publisherUser->setEmail($superdeskUser['email']); |
|
203 | - $publisherUser->setRoles(['ROLE_INTERNAL_API']); |
|
204 | - $publisherUser->setFirstName(\array_key_exists('first_name', $superdeskUser) ? $superdeskUser['first_name'] : 'Anon.'); |
|
205 | - $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
|
206 | - $publisherUser->setPlainPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
|
207 | - $publisherUser->setEnabled(true); |
|
208 | - $userManager->updateUser($publisherUser); |
|
209 | - } |
|
210 | - |
|
211 | - if (null !== $publisherUser) { |
|
212 | - return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - return new SingleResourceResponse([ |
|
217 | - 'status' => 401, |
|
218 | - 'message' => 'Unauthorized', |
|
219 | - ], new ResponseContext(401)); |
|
220 | - } |
|
221 | - |
|
222 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface |
|
223 | - { |
|
224 | - /** @var ApiKeyInterface $apiKey */ |
|
225 | - $apiKey = $this->generateOrGetApiKey($user, $token); |
|
226 | - |
|
227 | - return new SingleResourceResponse([ |
|
228 | - 'token' => [ |
|
229 | - 'api_key' => $apiKey->getApiKey(), |
|
230 | - 'valid_to' => $apiKey->getValidTo(), |
|
231 | - ], |
|
232 | - 'user' => $user, |
|
233 | - ]); |
|
234 | - } |
|
235 | - |
|
236 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface |
|
237 | - { |
|
238 | - $apiKey = null; |
|
239 | - if (null !== $token) { |
|
240 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
241 | - } else { |
|
242 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
243 | - if (count($validKeys) > 0) { |
|
244 | - $apiKey = reset($validKeys); |
|
245 | - } |
|
246 | - } |
|
247 | - |
|
248 | - if (null === $apiKey) { |
|
249 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
250 | - |
|
251 | - try { |
|
252 | - $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
|
253 | - if (!$lock->acquire()) { |
|
254 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
255 | - } |
|
256 | - $this->apiKeyRepository->add($apiKey); |
|
257 | - $lock->release(); |
|
258 | - } catch (RuntimeException $e) { |
|
259 | - sleep(2); |
|
260 | - |
|
261 | - return $this->generateOrGetApiKey($user, $token); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - return $apiKey; |
|
266 | - } |
|
267 | -} |
@@ -86,7 +86,7 @@ |
||
86 | 86 | /** |
87 | 87 | * @param string $renderedTemplate |
88 | 88 | * @param array|string $fromEmail |
89 | - * @param array|string $toEmail |
|
89 | + * @param string $toEmail |
|
90 | 90 | */ |
91 | 91 | protected function sendEmailMessage($renderedTemplate, $fromEmail, $toEmail) |
92 | 92 | { |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | |
103 | 103 | if (!$input->getArgument('username')) { |
104 | 104 | $question = new Question('Please choose a username:'); |
105 | - $question->setValidator(function ($username) { |
|
105 | + $question->setValidator(function($username) { |
|
106 | 106 | if (empty($username)) { |
107 | 107 | throw new \Exception('Username can not be empty'); |
108 | 108 | } |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | |
115 | 115 | if (!$input->getArgument('email')) { |
116 | 116 | $question = new Question('Please choose an email:'); |
117 | - $question->setValidator(function ($email) { |
|
117 | + $question->setValidator(function($email) { |
|
118 | 118 | if (empty($email)) { |
119 | 119 | throw new \Exception('Email can not be empty'); |
120 | 120 | } |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | |
127 | 127 | if (!$input->getArgument('password')) { |
128 | 128 | $question = new Question('Please choose a password:'); |
129 | - $question->setValidator(function ($password) { |
|
129 | + $question->setValidator(function($password) { |
|
130 | 130 | if (empty($password)) { |
131 | 131 | throw new \Exception('Password can not be empty'); |
132 | 132 | } |