Part-DB.Part-DB-server/src/Controller/UserController.php

240 lines
9.2 KiB
PHP
Raw Normal View History

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;
2020-04-04 15:45:14 +02:00
use App\DataTables\LogDataTable;
use App\Entity\Attachments\UserAttachment;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\UserSystem\User;
use App\Events\SecurityEvent;
use App\Events\SecurityEvents;
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;
use App\Services\UserSystem\PermissionPresetsHelper;
use App\Services\UserSystem\PermissionSchemaUpdater;
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;
use Symfony\Component\Form\FormInterface;
2020-08-21 22:43:37 +02:00
use Symfony\Component\HttpFoundation\RedirectResponse;
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;
2019-04-28 14:18:11 +02:00
/**
* @Route("/user")
* Class UserController
*/
class UserController extends AdminPages\BaseAdminController
2019-03-14 19:10:11 +01:00
{
protected string $entity_class = User::class;
protected string $twig_template = 'admin/user_admin.html.twig';
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
protected function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
{
//Check if we editing a user and if we need to change the password of it
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());
$entity->setPassword($password);
//By default the user must change the password afterwards
$entity->setNeedPwChange(true);
$event = new SecurityEvent($entity);
$this->eventDispatcher->dispatch($event, SecurityEvents::PASSWORD_CHANGED);
}
2020-08-21 21:36:22 +02:00
return true;
}
2019-04-28 14:18:11 +02:00
/**
* @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="user_edit")
2019-04-28 14:18:11 +02:00
* @Route("/{id}/", requirements={"id"="\d+"})
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
*/
public function edit(User $entity, Request $request, EntityManagerInterface $em, PermissionPresetsHelper $permissionPresetsHelper, PermissionSchemaUpdater $permissionSchemaUpdater, ?string $timestamp = null): Response
2019-04-28 14:18:11 +02: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);
//Handle 2FA disabling
2020-01-04 20:24:09 +01:00
if ($request->request->has('reset_2fa')) {
//Check if the admin has the needed permissions
$this->denyAccessUnlessGranted('set_password', $entity);
if ($this->isCsrfTokenValid('reset_2fa'.$entity->getId(), $request->request->get('_token'))) {
//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);
}
foreach ($entity->getWebAuthnKeys() as $key) {
$em->remove($key);
}
//Invalidate trusted devices
$entity->invalidateTrustedDeviceTokens();
$em->flush();
$event = new SecurityEvent($entity);
$this->eventDispatcher->dispatch($event, SecurityEvents::TFA_ADMIN_RESET);
$this->addFlash('success', 'user.edit.reset_success');
} else {
$this->addFlash('danger', 'csfr_invalid');
}
}
//Handle permissions presets
if ($request->request->has('permission_preset')) {
$this->denyAccessUnlessGranted('edit_permissions', $entity);
if ($this->isCsrfTokenValid('reset_2fa'.$entity->getId(), $request->request->get('_token'))) {
$preset = $request->request->get('permission_preset');
$permissionPresetsHelper->applyPreset($entity, $preset);
$em->flush();
$this->addFlash('success', 'user.edit.permission_success');
//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()]);
} else {
$this->addFlash('danger', 'csfr_invalid');
}
}
return $this->_edit($entity, $request, $em, $timestamp);
2019-04-28 14:18:11 +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());
$entity->setPassword($password);
//By default the user must change the password afterwards
$entity->setNeedPwChange(true);
}
return true;
}
2019-04-28 14:18:11 +02:00
/**
* @Route("/new", name="user_new")
* @Route("/{id}/clone", name="user_clone")
2019-04-28 14:18:11 +02:00
* @Route("/")
*/
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer, ?User $entity = null): Response
2019-04-28 14:18:11 +02:00
{
return $this->_new($request, $em, $importer, $entity);
2019-04-28 14:18:11 +02:00
}
/**
* @Route("/{id}", name="user_delete", methods={"DELETE"}, requirements={"id"="\d+"})
2019-04-28 14:18:11 +02:00
*/
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
{
2019-11-10 14:00:56 +01:00
if (User::ID_ANONYMOUS === $entity->getID()) {
2020-01-05 22:49:00 +01:00
throw new InvalidArgumentException('You can not delete the anonymous user! It is needed for permission checking without a logged in user');
2019-09-19 13:49:10 +02:00
}
2019-04-28 14:18:11 +02:00
return $this->_delete($request, $entity, $recursionHelper);
}
/**
* @Route("/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);
}
/**
* @Route("/{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);
}
2019-03-14 19:10:11 +01:00
/**
2019-04-28 14:18:11 +02:00
* @Route("/info", name="user_info_self")
* @Route("/{id}/info", name="user_info")
2019-03-14 19:10:11 +01:00
*/
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
if (null === $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;
} else {
//Else we must check, if the current user is allowed to access $user
$this->denyAccessUnlessGranted('info', $user);
2019-03-14 19:10:11 +01: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
}
//Show permissions to user
$builder = $this->createFormBuilder()->add('permissions', PermissionsType::class, [
'mapped' => false,
'disabled' => true,
'inherit' => true,
'data' => $user,
]);
return $this->renderForm('users/user_info.html.twig', [
2019-04-28 14:18:11 +02:00
'user' => $user,
'form' => $builder->getForm(),
'datatable' => $table ?? null,
2019-04-28 14:18:11 +02:00
]);
2019-03-14 19:10:11 +01:00
}
}