diff --git a/src/Controller/UserSettingsController.php b/src/Controller/UserSettingsController.php index bb03b71f..c0d9f66d 100644 --- a/src/Controller/UserSettingsController.php +++ b/src/Controller/UserSettingsController.php @@ -407,6 +407,8 @@ class UserSettingsController extends AbstractController public function addApiToken(Request $request, EntityManagerInterface $entityManager): Response { $this->denyAccessUnlessGranted('@api.manage_tokens'); + //When user change its settings, he should be logged in fully. + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $token = new ApiToken(); $token->setUser($this->getUser()); @@ -450,4 +452,43 @@ class UserSettingsController extends AbstractController 'secret' => $secret, ]); } + + #[Route(path: '/api_token/delete', name: 'user_api_tokens_delete', methods: ['DELETE'])] + public function apiTokenRemove(Request $request, EntityManagerInterface $entityManager): Response + { + $this->denyAccessUnlessGranted('@api.manage_tokens'); + //When user change its settings, he should be logged in fully. + $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); + + $user = $this->getUser(); + if (!$user instanceof User) { + throw new RuntimeException('This controller only works only for Part-DB User objects!'); + } + + if (!$this->isCsrfTokenValid('delete'.$user->getID(), $request->request->get('_token'))) { + $this->addFlash('error', 'csfr_invalid'); + return $this->redirectToRoute('user_settings'); + } + + //Extract the token id from the request + $token_id = $request->request->getInt('token_id'); + + $token = $entityManager->find(ApiToken::class, $token_id); + if ($token === null) { + $this->addFlash('error', 'tfa_u2f.u2f_delete.not_existing'); + return $this->redirectToRoute('user_settings'); + } + //User can only delete its own API tokens + if ($token->getUser() !== $user) { + $this->addFlash('error', 'tfa_u2f.u2f_delete.access_denied'); + return $this->redirectToRoute('user_settings'); + } + + //Do the actual deletion + $entityManager->remove($token); + $entityManager->flush(); + + $this->addFlash('success', 'api_tokens.deleted'); + return $this->redirectToRoute('user_settings'); + } } diff --git a/templates/users/_api_tokens.html.twig b/templates/users/_api_tokens.html.twig index fa199744..43df3205 100644 --- a/templates/users/_api_tokens.html.twig +++ b/templates/users/_api_tokens.html.twig @@ -21,38 +21,51 @@

{% trans %}user.settings.api_tokens.no_api_tokens_yet{% endtrans %} {% else %} - - - - - - - - - - - - - - {% for api_token in user.apiTokens %} - {# @var api_token \App\Entity\UserSystem\ApiToken #} + + + +
{% trans %}api_tokens.name{% endtrans %}{% trans %}api_tokens.access_level{% endtrans %}{% trans %}api_tokens.expiration_date{% endtrans %}{% trans %}tfa_u2f.keys.added_date{% endtrans %}{% trans %}api_tokens.last_time_used{% endtrans %}
+ - - - - - + + + + + + - {% endfor %} - -
{{ api_token.name }}{{ api_token.level.translationKey|trans }} - {{ _self.format_date(api_token.validUntil) }} - {% if api_token.valid %} - {% trans %}api_token.valid{% endtrans %} - {% else %} - {% trans %}api_token.expired{% endtrans %} - {% endif %} - {{ _self.format_date(api_token.addedDate) }}{{ _self.format_date(api_token.lastTimeUsed) }}{% trans %}api_tokens.name{% endtrans %}{% trans %}api_tokens.access_level{% endtrans %}{% trans %}api_tokens.expiration_date{% endtrans %}{% trans %}tfa_u2f.keys.added_date{% endtrans %}{% trans %}api_tokens.last_time_used{% endtrans %}
+ + + + {% for api_token in user.apiTokens %} + {# @var api_token \App\Entity\UserSystem\ApiToken #} + + {{ api_token.name }} + {{ api_token.level.translationKey|trans }} + + {{ _self.format_date(api_token.validUntil) }} + {% if api_token.valid %} + {% trans %}api_token.valid{% endtrans %} + {% else %} + {% trans %}api_token.expired{% endtrans %} + {% endif %} + + {{ _self.format_date(api_token.addedDate) }} + {{ _self.format_date(api_token.lastTimeUsed) }} + + + + + {% endfor %} + + + {% endif %} diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index ccf1336f..9ac5b7f5 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -11765,5 +11765,29 @@ Please note, that you can not impersonate a disabled user. If you try you will g Manage API tokens + + + user.settings.api_tokens.delete.title + Do you really want to delete this API token? + + + + + user.settings.api_tokens.delete + Delete + + + + + user.settings.api_tokens.delete.message + The application, which uses this API token, will no longer have access to Part-DB. This action can not be undone! + + + + + api_tokens.deleted + API token deleted successfully! + +