2020-05-23 19:06:46 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Parts;
|
|
|
|
|
2020-05-24 18:26:10 +02:00
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
2020-05-23 19:06:46 +02:00
|
|
|
use App\Entity\Parts\Part;
|
2020-05-24 21:26:51 +02:00
|
|
|
use App\Repository\DBElementRepository;
|
2020-10-02 12:42:15 +02:00
|
|
|
use App\Repository\PartRepository;
|
2020-05-23 19:06:46 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-08-14 19:32:53 +02:00
|
|
|
use InvalidArgumentException;
|
2020-05-23 19:06:46 +02:00
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
|
|
|
use Symfony\Component\Security\Core\Security;
|
|
|
|
|
|
|
|
final class PartsTableActionHandler
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
private Security $security;
|
2020-05-23 19:06:46 +02:00
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $entityManager, Security $security)
|
|
|
|
{
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->security = $security;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 21:36:22 +02:00
|
|
|
* Converts the given array to an array of Parts.
|
|
|
|
*
|
|
|
|
* @param string $ids a comma separated list of Part IDs
|
|
|
|
*
|
2020-05-23 19:06:46 +02:00
|
|
|
* @return Part[]
|
|
|
|
*/
|
|
|
|
public function idStringToArray(string $ids): array
|
|
|
|
{
|
|
|
|
$id_array = explode(',', $ids);
|
|
|
|
|
2020-10-02 12:42:15 +02:00
|
|
|
/** @var PartRepository $repo */
|
2020-05-23 19:06:46 +02:00
|
|
|
$repo = $this->entityManager->getRepository(Part::class);
|
2020-08-21 21:36:22 +02:00
|
|
|
|
2020-05-23 19:06:46 +02:00
|
|
|
return $repo->getElementsFromIDArray($id_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-21 21:36:22 +02:00
|
|
|
* @param Part[] $selected_parts
|
2020-05-23 19:06:46 +02:00
|
|
|
*/
|
|
|
|
public function handleAction(string $action, array $selected_parts, ?int $target_id): void
|
|
|
|
{
|
|
|
|
//Iterate over the parts and apply the action to it:
|
|
|
|
foreach ($selected_parts as $part) {
|
|
|
|
if (!$part instanceof Part) {
|
2022-08-14 19:32:53 +02:00
|
|
|
throw new InvalidArgumentException('$selected_parts must be an array of Part elements!');
|
2020-05-23 19:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//We modify parts, so you have to have the permission to modify it
|
|
|
|
$this->denyAccessUnlessGranted('edit', $part);
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'favorite':
|
|
|
|
$part->setFavorite(true);
|
|
|
|
break;
|
|
|
|
case 'unfavorite':
|
|
|
|
$part->setFavorite(false);
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
$this->denyAccessUnlessGranted('delete', $part);
|
|
|
|
$this->entityManager->remove($part);
|
|
|
|
break;
|
2020-05-24 18:26:10 +02:00
|
|
|
case 'change_category':
|
|
|
|
$this->denyAccessUnlessGranted('category.edit', $part);
|
|
|
|
$part->setCategory($this->entityManager->find(Category::class, $target_id));
|
|
|
|
break;
|
|
|
|
case 'change_footprint':
|
|
|
|
$this->denyAccessUnlessGranted('footprint.edit', $part);
|
2020-08-21 21:36:22 +02:00
|
|
|
$part->setFootprint(null === $target_id ? null : $this->entityManager->find(Footprint::class, $target_id));
|
2020-05-24 18:26:10 +02:00
|
|
|
break;
|
|
|
|
case 'change_manufacturer':
|
|
|
|
$this->denyAccessUnlessGranted('manufacturer.edit', $part);
|
2020-08-21 21:36:22 +02:00
|
|
|
$part->setManufacturer(null === $target_id ? null : $this->entityManager->find(Manufacturer::class, $target_id));
|
2020-05-24 18:26:10 +02:00
|
|
|
break;
|
|
|
|
case 'change_unit':
|
|
|
|
$this->denyAccessUnlessGranted('unit.edit', $part);
|
2020-08-21 21:36:22 +02:00
|
|
|
$part->setPartUnit(null === $target_id ? null : $this->entityManager->find(MeasurementUnit::class, $target_id));
|
2020-05-24 18:26:10 +02:00
|
|
|
break;
|
2020-05-23 19:06:46 +02:00
|
|
|
|
|
|
|
default:
|
2022-08-14 19:32:53 +02:00
|
|
|
throw new InvalidArgumentException('The given action is unknown! ('.$action.')');
|
2020-05-23 19:06:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception unless the attributes are granted against the current authentication token and optionally
|
|
|
|
* supplied subject.
|
|
|
|
*
|
|
|
|
* @throws AccessDeniedException
|
|
|
|
*/
|
|
|
|
private function denyAccessUnlessGranted($attributes, $subject = null, string $message = 'Access Denied.'): void
|
|
|
|
{
|
|
|
|
if (!$this->security->isGranted($attributes, $subject)) {
|
|
|
|
$exception = new AccessDeniedException($message);
|
|
|
|
$exception->setAttributes($attributes);
|
|
|
|
$exception->setSubject($subject);
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 21:36:22 +02:00
|
|
|
}
|