2019-03-14 19:10:11 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-14 19:10:11 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2023-06-11 14:55:06 +02:00
|
|
|
use App\Controller\AdminPages\BaseAdminController;
|
2020-04-04 15:45:14 +02:00
|
|
|
use App\DataTables\LogDataTable;
|
2019-09-24 18:28:35 +02:00
|
|
|
use App\Entity\Attachments\UserAttachment;
|
2020-06-07 21:11:09 +02:00
|
|
|
use App\Entity\Base\AbstractNamedDBElement;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2020-04-03 18:27:47 +02:00
|
|
|
use App\Events\SecurityEvent;
|
|
|
|
use App\Events\SecurityEvents;
|
2019-09-13 19:38:22 +02:00
|
|
|
use App\Form\Permissions\PermissionsType;
|
2019-04-28 14:18:11 +02:00
|
|
|
use App\Form\UserAdminForm;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\ImportExportSystem\EntityExporter;
|
|
|
|
use App\Services\ImportExportSystem\EntityImporter;
|
|
|
|
use App\Services\Trees\StructuralElementRecursionHelper;
|
2022-11-14 00:02:37 +01:00
|
|
|
use App\Services\UserSystem\PermissionPresetsHelper;
|
2023-01-08 00:35:43 +01:00
|
|
|
use App\Services\UserSystem\PermissionSchemaUpdater;
|
2019-03-15 18:04:15 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-08-14 19:32:53 +02:00
|
|
|
use Exception;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2020-04-04 15:45:14 +02:00
|
|
|
use Omines\DataTablesBundle\DataTableFactory;
|
2019-03-14 19:10:11 +01:00
|
|
|
use Symfony\Component\Asset\Packages;
|
2020-06-07 21:11:09 +02:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2020-08-21 22:43:37 +02:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2019-03-15 18:04:15 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2019-04-28 14:18:11 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2019-03-14 19:10:11 +01:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2023-08-20 12:42:56 +02:00
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
2019-03-14 19:10:11 +01:00
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/user')]
|
2023-06-11 14:55:06 +02:00
|
|
|
class UserController extends BaseAdminController
|
2019-03-14 19:10:11 +01:00
|
|
|
{
|
2023-02-03 23:43:44 +01:00
|
|
|
protected string $entity_class = User::class;
|
2023-02-04 23:34:39 +01:00
|
|
|
protected string $twig_template = 'admin/user_admin.html.twig';
|
2023-02-03 23:43:44 +01:00
|
|
|
protected string $form_class = UserAdminForm::class;
|
|
|
|
protected string $route_base = 'user';
|
|
|
|
protected string $attachment_class = UserAttachment::class;
|
|
|
|
protected ?string $parameter_class = null;
|
2019-04-28 14:18:11 +02:00
|
|
|
|
2020-06-07 21:11:09 +02:00
|
|
|
protected function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
|
|
|
|
{
|
2023-04-15 23:14:53 +02:00
|
|
|
//Check if we're editing a user and if we need to change the password of it
|
2020-06-07 21:11:09 +02:00
|
|
|
if ($entity instanceof User && !empty($form['new_password']->getData())) {
|
2021-10-02 20:41:14 +02:00
|
|
|
$password = $this->passwordEncoder->hashPassword($entity, $form['new_password']->getData());
|
2020-06-07 21:11:09 +02:00
|
|
|
$entity->setPassword($password);
|
2023-04-15 23:14:53 +02:00
|
|
|
//By default, the user must change the password afterward
|
2020-06-07 21:11:09 +02:00
|
|
|
$entity->setNeedPwChange(true);
|
|
|
|
|
|
|
|
$event = new SecurityEvent($entity);
|
|
|
|
$this->eventDispatcher->dispatch($event, SecurityEvents::PASSWORD_CHANGED);
|
|
|
|
}
|
2020-08-21 21:36:22 +02:00
|
|
|
|
2020-06-07 21:11:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
/**
|
2020-03-15 13:56:31 +01:00
|
|
|
*
|
2022-08-14 19:32:53 +02:00
|
|
|
* @throws Exception
|
2019-04-28 14:18:11 +02:00
|
|
|
*/
|
2024-03-03 19:57:31 +01:00
|
|
|
#[Route(path: '/{id}/edit/{timestamp}', name: 'user_edit', requirements: ['id' => '\d+'])]
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/', requirements: ['id' => '\d+'])]
|
2023-08-20 12:42:56 +02:00
|
|
|
public function edit(User $entity, Request $request, EntityManagerInterface $em, PermissionPresetsHelper $permissionPresetsHelper,
|
|
|
|
PermissionSchemaUpdater $permissionSchemaUpdater, ValidatorInterface $validator, ?string $timestamp = null): Response
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
2023-01-08 00:35:43 +01:00
|
|
|
//Do an upgrade of the permission schema if needed (so the user can see the permissions a user get on next request (even if it was not done yet)
|
|
|
|
$permissionSchemaUpdater->userUpgradeSchemaRecursively($entity);
|
2019-12-29 20:04:52 +01:00
|
|
|
|
2023-01-08 00:35:43 +01:00
|
|
|
//Handle 2FA disabling
|
2020-01-04 20:24:09 +01:00
|
|
|
if ($request->request->has('reset_2fa')) {
|
2019-12-29 20:04:52 +01:00
|
|
|
//Check if the admin has the needed permissions
|
|
|
|
$this->denyAccessUnlessGranted('set_password', $entity);
|
2023-06-18 00:00:58 +02:00
|
|
|
if ($this->isCsrfTokenValid('reset_2fa'.$entity->getID(), $request->request->get('_token'))) {
|
2019-12-29 20:04:52 +01:00
|
|
|
//Disable Google authenticator
|
|
|
|
$entity->setGoogleAuthenticatorSecret(null);
|
|
|
|
$entity->setBackupCodes([]);
|
|
|
|
//Remove all U2F keys
|
2022-10-05 21:59:42 +02:00
|
|
|
foreach ($entity->getLegacyU2FKeys() as $key) {
|
|
|
|
$em->remove($key);
|
|
|
|
}
|
2023-06-18 00:00:58 +02:00
|
|
|
foreach ($entity->getWebauthnKeys() as $key) {
|
2019-12-29 20:04:52 +01:00
|
|
|
$em->remove($key);
|
|
|
|
}
|
|
|
|
//Invalidate trusted devices
|
|
|
|
$entity->invalidateTrustedDeviceTokens();
|
|
|
|
$em->flush();
|
|
|
|
|
2020-04-03 18:27:47 +02:00
|
|
|
$event = new SecurityEvent($entity);
|
|
|
|
$this->eventDispatcher->dispatch($event, SecurityEvents::TFA_ADMIN_RESET);
|
|
|
|
|
2019-12-29 20:04:52 +01:00
|
|
|
$this->addFlash('success', 'user.edit.reset_success');
|
|
|
|
} else {
|
2023-08-20 12:42:56 +02:00
|
|
|
$this->addFlash('error', 'csfr_invalid');
|
2019-12-29 20:04:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:02:37 +01:00
|
|
|
//Handle permissions presets
|
|
|
|
if ($request->request->has('permission_preset')) {
|
|
|
|
$this->denyAccessUnlessGranted('edit_permissions', $entity);
|
2023-06-18 00:00:58 +02:00
|
|
|
if ($this->isCsrfTokenValid('reset_2fa'.$entity->getID(), $request->request->get('_token'))) {
|
2022-11-14 00:02:37 +01:00
|
|
|
$preset = $request->request->get('permission_preset');
|
|
|
|
|
|
|
|
$permissionPresetsHelper->applyPreset($entity, $preset);
|
|
|
|
|
2023-08-20 12:42:56 +02:00
|
|
|
//Ensure that the user is valid after applying the preset
|
|
|
|
$errors = $validator->validate($entity);
|
|
|
|
if (count($errors) > 0) {
|
|
|
|
$this->addFlash('error', 'validator.noLockout');
|
|
|
|
//Refresh the entity to remove the changes
|
|
|
|
$em->refresh($entity);
|
|
|
|
} else {
|
|
|
|
$em->flush();
|
2022-11-14 00:02:37 +01:00
|
|
|
|
2023-08-20 12:42:56 +02:00
|
|
|
$this->addFlash('success', 'user.edit.permission_success');
|
2022-11-14 00:02:37 +01:00
|
|
|
|
2023-08-20 12:42:56 +02:00
|
|
|
//We need to stop the execution here, or our permissions changes will be overwritten by the form values
|
|
|
|
return $this->redirectToRoute('user_edit', ['id' => $entity->getID()]);
|
|
|
|
}
|
2023-04-15 22:05:29 +02:00
|
|
|
|
2023-08-20 12:42:56 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->addFlash('error', 'csfr_invalid');
|
|
|
|
}
|
2022-11-14 00:02:37 +01:00
|
|
|
}
|
|
|
|
|
2020-03-04 21:54:03 +01:00
|
|
|
return $this->_edit($entity, $request, $em, $timestamp);
|
2019-04-28 14:18:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 21:11:09 +02:00
|
|
|
protected function additionalActionNew(FormInterface $form, AbstractNamedDBElement $entity): bool
|
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if ($entity instanceof User && !empty($form['new_password']->getData())) {
|
2021-10-02 20:41:14 +02:00
|
|
|
$password = $this->passwordEncoder->hashPassword($entity, $form['new_password']->getData());
|
2020-06-07 21:11:09 +02:00
|
|
|
$entity->setPassword($password);
|
2023-04-15 23:14:53 +02:00
|
|
|
//By default, the user must change the password afterward
|
2020-06-07 21:11:09 +02:00
|
|
|
$entity->setNeedPwChange(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/new', name: 'user_new')]
|
|
|
|
#[Route(path: '/{id}/clone', name: 'user_clone')]
|
|
|
|
#[Route(path: '/')]
|
2020-04-06 13:14:47 +02:00
|
|
|
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer, ?User $entity = null): Response
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
2020-04-06 13:14:47 +02:00
|
|
|
return $this->_new($request, $em, $importer, $entity);
|
2019-04-28 14:18:11 +02:00
|
|
|
}
|
|
|
|
|
2024-03-03 19:57:31 +01:00
|
|
|
#[Route(path: '/{id}', name: 'user_delete', requirements: ['id' => '\d+'], methods: ['DELETE'])]
|
2020-08-21 22:43:37 +02:00
|
|
|
public function delete(Request $request, User $entity, StructuralElementRecursionHelper $recursionHelper): RedirectResponse
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
2023-10-24 13:32:44 +02:00
|
|
|
//Disallow deleting the anonymous user
|
2019-11-10 14:00:56 +01:00
|
|
|
if (User::ID_ANONYMOUS === $entity->getID()) {
|
2023-10-24 13:32:44 +02:00
|
|
|
throw new \LogicException('You can not delete the anonymous user! It is needed for permission checking without a logged in user');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Disallow deleting the current logged-in user
|
|
|
|
if ($entity === $this->getUser()) {
|
|
|
|
throw new \LogicException('You can not delete your own user account!');
|
2019-09-19 13:49:10 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
return $this->_delete($request, $entity, $recursionHelper);
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/export', name: 'user_export_all')]
|
2020-02-02 14:05:36 +01:00
|
|
|
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
|
|
|
return $this->_exportAll($em, $exporter, $request);
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/{id}/export', name: 'user_export')]
|
2020-02-02 14:05:36 +01:00
|
|
|
public function exportEntity(User $entity, EntityExporter $exporter, Request $request): Response
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
|
|
|
return $this->_exportEntity($entity, $exporter, $request);
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/info', name: 'user_info_self')]
|
|
|
|
#[Route(path: '/{id}/info', name: 'user_info')]
|
2020-04-04 15:45:14 +02:00
|
|
|
public function userInfo(?User $user, Packages $packages, Request $request, DataTableFactory $dataTableFactory): Response
|
2019-03-14 19:10:11 +01:00
|
|
|
{
|
|
|
|
//If no user id was passed, then we show info about the current user
|
2023-06-11 14:55:06 +02:00
|
|
|
if (!$user instanceof User) {
|
2020-02-02 14:05:36 +01:00
|
|
|
$tmp = $this->getUser();
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!$tmp instanceof User) {
|
2020-02-02 14:05:36 +01:00
|
|
|
throw new InvalidArgumentException('Userinfo only works for database users!');
|
|
|
|
}
|
|
|
|
$user = $tmp;
|
2019-03-19 18:36:05 +01:00
|
|
|
} else {
|
|
|
|
//Else we must check, if the current user is allowed to access $user
|
2023-04-08 01:04:10 +02:00
|
|
|
$this->denyAccessUnlessGranted('info', $user);
|
2019-03-14 19:10:11 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 01:04:10 +02:00
|
|
|
//Only show the history table, if the user is the current user
|
|
|
|
if ($user === $this->getUser()) {
|
|
|
|
$table = $this->dataTableFactory->createFromType(
|
|
|
|
LogDataTable::class,
|
|
|
|
[
|
|
|
|
'filter_elements' => $user,
|
|
|
|
'mode' => 'element_history',
|
|
|
|
],
|
|
|
|
['pageLength' => 10]
|
|
|
|
)
|
|
|
|
->handleRequest($request);
|
|
|
|
|
|
|
|
if ($table->isCallback()) {
|
|
|
|
return $table->getResponse();
|
|
|
|
}
|
2020-04-04 15:45:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-13 19:38:22 +02:00
|
|
|
//Show permissions to user
|
2019-11-09 00:47:20 +01:00
|
|
|
$builder = $this->createFormBuilder()->add('permissions', PermissionsType::class, [
|
2019-09-13 19:38:22 +02:00
|
|
|
'mapped' => false,
|
|
|
|
'disabled' => true,
|
|
|
|
'inherit' => true,
|
2019-11-09 00:47:20 +01:00
|
|
|
'data' => $user,
|
2019-09-13 19:38:22 +02:00
|
|
|
]);
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
return $this->render('users/user_info.html.twig', [
|
2019-04-28 14:18:11 +02:00
|
|
|
'user' => $user,
|
2022-03-04 21:20:18 +01:00
|
|
|
'form' => $builder->getForm(),
|
2023-04-08 01:04:10 +02:00
|
|
|
'datatable' => $table ?? null,
|
2019-04-28 14:18:11 +02:00
|
|
|
]);
|
2019-03-14 19:10:11 +01:00
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|