2017-04-29 17:22:50 +00:00
|
|
|
<?php
|
|
|
|
|
2023-12-31 08:28:37 +00:00
|
|
|
namespace Wallabag\CoreBundle\Controller\Api;
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use Craue\ConfigBundle\Util\Config;
|
2022-12-19 09:37:22 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2017-04-29 17:22:50 +00:00
|
|
|
use FOS\UserBundle\Event\UserEvent;
|
|
|
|
use FOS\UserBundle\FOSUserEvents;
|
2022-08-28 00:01:46 +00:00
|
|
|
use FOS\UserBundle\Model\UserManagerInterface;
|
2017-04-29 17:22:50 +00:00
|
|
|
use JMS\Serializer\SerializationContext;
|
2023-07-02 06:27:07 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model;
|
2022-11-06 12:00:41 +00:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
2022-12-23 13:40:42 +00:00
|
|
|
use OpenApi\Annotations as OA;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-04-29 17:22:50 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-11-23 11:44:55 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2023-12-31 08:34:04 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\Api\Client;
|
2023-12-30 22:32:36 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\User;
|
2023-12-30 22:41:01 +00:00
|
|
|
use Wallabag\CoreBundle\Form\Type\NewUserType;
|
2017-04-29 17:22:50 +00:00
|
|
|
|
|
|
|
class UserRestController extends WallabagRestController
|
|
|
|
{
|
|
|
|
/**
|
2023-09-17 20:54:49 +00:00
|
|
|
* Retrieve current logged in user information.
|
2017-04-29 17:22:50 +00:00
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"User"},
|
2023-09-17 20:54:49 +00:00
|
|
|
* summary="Retrieve current logged in user information.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2022-11-06 12:00:41 +00:00
|
|
|
* response="200",
|
2023-07-02 06:27:07 +00:00
|
|
|
* description="Returned when successful",
|
|
|
|
* @Model(type=User::class, groups={"user_api"}))
|
2022-11-06 12:00:41 +00:00
|
|
|
* )
|
|
|
|
* )
|
2017-04-29 17:22:50 +00:00
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/user.{_format}", methods={"GET"}, name="api_get_user", defaults={"_format": "json"})
|
|
|
|
*
|
2017-04-29 17:22:50 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function getUserAction()
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
return $this->sendUser($this->getUser());
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-08 12:25:44 +00:00
|
|
|
* Register an user and create a client.
|
2017-04-29 17:22:50 +00:00
|
|
|
*
|
2022-11-06 12:00:41 +00:00
|
|
|
* @Operation(
|
|
|
|
* tags={"User"},
|
|
|
|
* summary="Register an user and create a client.",
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\RequestBody(
|
|
|
|
* @OA\JsonContent(
|
|
|
|
* type="object",
|
2023-07-02 06:27:07 +00:00
|
|
|
* required={"username", "password", "email"},
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Property(
|
|
|
|
* property="username",
|
|
|
|
* description="The user's username",
|
2023-07-02 06:27:07 +00:00
|
|
|
* type="string",
|
|
|
|
* example="wallabag",
|
2022-12-23 13:40:42 +00:00
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="password",
|
|
|
|
* description="The user's password",
|
2023-07-02 06:27:07 +00:00
|
|
|
* type="string",
|
|
|
|
* example="hidden_value",
|
2022-12-23 13:40:42 +00:00
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="email",
|
|
|
|
* description="The user's email",
|
2023-07-02 06:27:07 +00:00
|
|
|
* type="string",
|
|
|
|
* example="wallabag@wallabag.io",
|
2022-12-23 13:40:42 +00:00
|
|
|
* ),
|
|
|
|
* @OA\Property(
|
|
|
|
* property="client_name",
|
|
|
|
* description="The client name (to be used by your app)",
|
2023-07-02 06:27:07 +00:00
|
|
|
* type="string",
|
|
|
|
* example="Fancy App",
|
2022-12-23 13:40:42 +00:00
|
|
|
* ),
|
|
|
|
* )
|
2022-11-06 12:00:41 +00:00
|
|
|
* ),
|
2022-12-23 13:40:42 +00:00
|
|
|
* @OA\Response(
|
2023-07-02 06:27:07 +00:00
|
|
|
* response="201",
|
|
|
|
* description="Returned when successful",
|
|
|
|
* @Model(type=User::class, groups={"user_api_with_client"})),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="403",
|
|
|
|
* description="Server doesn't allow registrations"
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="400",
|
|
|
|
* description="Request is incorrectly formatted"
|
2022-11-06 12:00:41 +00:00
|
|
|
* )
|
2017-04-29 17:22:50 +00:00
|
|
|
* )
|
2017-05-30 05:56:01 +00:00
|
|
|
*
|
|
|
|
* @todo Make this method (or the whole API) accessible only through https
|
|
|
|
*
|
2022-11-23 11:44:55 +00:00
|
|
|
* @Route("/api/user.{_format}", methods={"PUT"}, name="api_put_user", defaults={"_format": "json"})
|
|
|
|
*
|
2017-04-29 17:22:50 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2022-12-19 09:37:22 +00:00
|
|
|
public function putUserAction(Request $request, Config $craueConfig, UserManagerInterface $userManager, EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher)
|
2017-04-29 17:22:50 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
if (!$this->getParameter('fosuser_registration') || !$craueConfig->get('api_user_registration')) {
|
|
|
|
$json = $this->serializer->serialize(['error' => "Server doesn't allow registrations"], 'json');
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-06-02 18:03:25 +00:00
|
|
|
return (new JsonResponse())
|
|
|
|
->setJson($json)
|
|
|
|
->setStatusCode(JsonResponse::HTTP_FORBIDDEN);
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$user = $userManager->createUser();
|
2022-11-23 14:51:33 +00:00
|
|
|
\assert($user instanceof User);
|
2017-06-02 08:27:15 +00:00
|
|
|
// user will be disabled BY DEFAULT to avoid spamming account to be enabled
|
2017-06-02 08:19:33 +00:00
|
|
|
$user->setEnabled(false);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$form = $this->createForm(NewUserType::class, $user, [
|
2017-05-30 05:56:01 +00:00
|
|
|
'csrf_protection' => false,
|
|
|
|
]);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
// simulate form submission
|
|
|
|
$form->submit([
|
|
|
|
'username' => $request->request->get('username'),
|
|
|
|
'plainPassword' => [
|
|
|
|
'first' => $request->request->get('password'),
|
|
|
|
'second' => $request->request->get('password'),
|
|
|
|
],
|
|
|
|
'email' => $request->request->get('email'),
|
|
|
|
]);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if ($form->isSubmitted() && false === $form->isValid()) {
|
|
|
|
$view = $this->view($form, 400);
|
|
|
|
$view->setFormat('json');
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
// handle errors in a more beautiful way than the default view
|
2017-12-08 11:10:26 +00:00
|
|
|
$data = json_decode($this->handleView($view)->getContent(), true)['errors']['children'];
|
2017-05-30 05:56:01 +00:00
|
|
|
$errors = [];
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['username']['errors'])) {
|
|
|
|
$errors['username'] = $this->translateErrors($data['username']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['email']['errors'])) {
|
|
|
|
$errors['email'] = $this->translateErrors($data['email']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['plainPassword']['children']['first']['errors'])) {
|
|
|
|
$errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize(['error' => $errors], 'json');
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-06-02 18:03:25 +00:00
|
|
|
return (new JsonResponse())
|
|
|
|
->setJson($json)
|
|
|
|
->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-06-07 21:23:28 +00:00
|
|
|
// create a default client
|
|
|
|
$client = new Client($user);
|
2017-06-08 12:25:44 +00:00
|
|
|
$client->setName($request->request->get('client_name', 'Default client'));
|
2017-06-07 21:23:28 +00:00
|
|
|
|
2022-12-19 09:37:22 +00:00
|
|
|
$entityManager->persist($client);
|
2017-06-07 21:23:28 +00:00
|
|
|
|
|
|
|
$user->addClient($client);
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$userManager->updateUser($user);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
|
|
|
// dispatch a created event so the associated config will be created
|
2022-12-19 09:37:22 +00:00
|
|
|
$eventDispatcher->dispatch(new UserEvent($user, $request), FOSUserEvents::USER_CREATED);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-06-07 21:23:28 +00:00
|
|
|
return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
/**
|
|
|
|
* Send user response.
|
|
|
|
*
|
2017-06-07 21:23:28 +00:00
|
|
|
* @param string $group Used to define with serialized group might be used
|
|
|
|
* @param int $status HTTP Status code to send
|
2017-05-30 05:56:01 +00:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2017-06-07 21:23:28 +00:00
|
|
|
private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK)
|
2017-05-30 05:56:01 +00:00
|
|
|
{
|
2022-12-19 09:37:22 +00:00
|
|
|
$json = $this->serializer->serialize(
|
2017-05-30 05:56:01 +00:00
|
|
|
$user,
|
|
|
|
'json',
|
2017-06-07 21:23:28 +00:00
|
|
|
SerializationContext::create()->setGroups([$group])
|
2017-05-30 05:56:01 +00:00
|
|
|
);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-06-02 18:03:25 +00:00
|
|
|
return (new JsonResponse())
|
|
|
|
->setJson($json)
|
|
|
|
->setStatusCode($status);
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
/**
|
|
|
|
* Translate errors message.
|
|
|
|
*
|
|
|
|
* @param array $errors
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function translateErrors($errors)
|
|
|
|
{
|
|
|
|
$translatedErrors = [];
|
|
|
|
foreach ($errors as $error) {
|
2022-12-19 09:37:22 +00:00
|
|
|
$translatedErrors[] = $this->translator->trans($error);
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $translatedErrors;
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|