Skip to content

Commit

Permalink
Refactor entity deletion handlers (#24989)
Browse files Browse the repository at this point in the history
- add "flush" parameter to delete handlers to be able to delete entities without flushing them to the database
- replace interface Oro\Bundle\SoapBundle\Handler\DeleteHandlerInterface with Oro\Bundle\EntityBundle\Handler\EntityDeleteHandlerInterface and Oro\Bundle\EntityBundle\Handler\EntityDeleteHandlerExtensionInterface
- remove the DIC tag "oro_activity.activity_entity_delete_handler"; the decoration of "oro_activity.activity_entity_delete_handler_extension" service should be used instead
- remove interface Oro\Bundle\ActivityBundle\Entity\Manager\ActivityEntityDeleteHandlerInterface; Oro\Bundle\ActivityBundle\Handler\ActivityEntityDeleteHandlerExtensionInterface should be used instead
- remove "delete_handler" configuration option from api.yml; Oro\Bundle\EntityBundle\Handler\EntityDeleteHandlerRegistry class is used to get the deletion handler instead
  • Loading branch information
vsoroka authored and yurio committed Aug 27, 2019
1 parent 8f45449 commit b312559
Show file tree
Hide file tree
Showing 63 changed files with 1,926 additions and 871 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ datagrids:
views_list: '@oro_account.accounts_view_list'

base-account-contacts-grid:
extended_entity_name: '%oro_contact.entity.class%'
extended_entity_name: Oro\Bundle\ContactBundle\Entity\Contact
acl_resource: oro_contact_view
source:
type: orm
Expand All @@ -255,7 +255,7 @@ datagrids:
- country.name as countryName
- address.postalCode as addressPostalCode
from:
- { table: '%oro_contact.entity.class%', alias: c }
- { table: Oro\Bundle\ContactBundle\Entity\Contact, alias: c }
join:
left:
- { join: c.addresses, alias: address, conditionType: WITH, condition: 'address.primary = true' }
Expand Down
4 changes: 1 addition & 3 deletions src/Oro/Bundle/AccountBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ parameters:
oro_account.account.manager.api.class: Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager
oro_account.type.account.api.class: Oro\Bundle\AccountBundle\Form\Type\AccountApiType

oro_account.activity_widget_provider.class: '%oro_activity.widget_provider.activities.menu.class%'

oro_account.provider.phone.account.class: Oro\Bundle\AccountBundle\Provider\AccountPhoneProvider

services:
Expand Down Expand Up @@ -87,7 +85,7 @@ services:
- { name: oro_form.autocomplete.search_handler, alias: accounts, acl_resource: oro_account_view }

oro_account.activity_widget_provider:
class: '%oro_account.activity_widget_provider.class%'
class: Oro\Bundle\UIBundle\Provider\TabMenuWidgetProvider
arguments:
- "@oro_entity.entity_identifier_accessor"
- "@oro_ui.twig.tab_extension"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ placeholders:
applicable: "@oro_ui.placeholder.filter->isInstanceOf($entity$, %oro_account.entity.account.class%)"
oro_contact_cases_grid:
template: OroCaseBundle:Case:contactCases.html.twig
applicable: "@oro_ui.placeholder.filter->isInstanceOf($entity$, %oro_contact.entity.class%)"
applicable: '@oro_ui.placeholder.filter->isInstanceOf($entity$, Oro\Bundle\ContactBundle\Entity\Contact)'
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
use Oro\Bundle\ChannelBundle\Event\ChannelDeleteEvent;
use Oro\Bundle\SecurityBundle\Annotation\Acl;
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
use Oro\Bundle\SecurityBundle\Exception\ForbiddenException;
use Oro\Bundle\SoapBundle\Controller\Api\Rest\RestController;
use Oro\Bundle\SoapBundle\Request\Parameters\Filter\BooleanParameterFilter;
use Oro\Bundle\SoapBundle\Request\Parameters\Filter\ChainParameterFilter;
use Oro\Bundle\SoapBundle\Request\Parameters\Filter\EntityClassParameterFilter;
use Oro\Bundle\SoapBundle\Request\Parameters\Filter\StringToArrayParameterFilter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
* API CRUD controller for Channel entity.
*
* @RouteResource("channel")
* @NamePrefix("oro_api_")
*/
class ChannelController extends RestController
{

/**
* Get channels.
*
Expand Down Expand Up @@ -122,12 +123,10 @@ public function deleteAction($id)

$this->getDeleteHandler()->handleDelete($id, $this->getManager());
$this->get('event_dispatcher')->dispatch(ChannelDeleteEvent::EVENT_NAME, new ChannelDeleteEvent($channel));
} catch (EntityNotFoundException $notFoundEx) {
} catch (EntityNotFoundException $e) {
return $this->handleView($this->view(null, Response::HTTP_NOT_FOUND));
} catch (ForbiddenException $forbiddenEx) {
return $this->handleView(
$this->view(['reason' => $forbiddenEx->getReason()], Response::HTTP_FORBIDDEN)
);
} catch (AccessDeniedException $e) {
return $this->handleView($this->view(['reason' => $e->getMessage()], Response::HTTP_FORBIDDEN));
}

return $this->handleView($this->view(null, Response::HTTP_NO_CONTENT));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Symfony\Component\HttpFoundation\Response;

/**
* REST API Contact Email Controller
* API CRUD controller for ContactEmail entity.
*
* @Rest\RouteResource("email")
* @Rest\NamePrefix("oro_api_")
*/
Expand Down Expand Up @@ -92,12 +93,4 @@ public function getForm()
{
return $this->get('oro_contact.form.type.contact_email.type');
}

/**
* {@inheritdoc}
*/
public function getDeleteHandler()
{
return $this->get('oro_contact.form.type.contact_email.handler');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\HttpFoundation\Response;

/**
* API CRUD controller for ContactPhone entity.
*
* @RouteResource("phone")
* @NamePrefix("oro_api_")
*/
Expand Down Expand Up @@ -170,12 +172,4 @@ public function getForm()
{
return $this->get('oro_contact.form.type.contact_phone.type');
}

/**
* {@inheritdoc}
*/
protected function getDeleteHandler()
{
return $this->get('oro_contact.form.type.contact_phone.handler');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* The controller for Contact entity.
*/
class ContactController extends Controller
{
/**
Expand Down Expand Up @@ -123,7 +126,7 @@ public function updateAction(Contact $entity)
public function indexAction()
{
return [
'entity_class' => $this->container->getParameter('oro_contact.entity.class')
'entity_class' => Contact::class
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Oro/Bundle/ContactBundle/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\HttpFoundation\Request;

/**
* The controller for Group entity.
* @Route("/group")
*/
class GroupController extends Controller
Expand Down Expand Up @@ -72,7 +73,7 @@ public function updateAction(Request $request, Group $entity)
public function indexAction()
{
return [
'entity_class' => $this->container->getParameter('oro_contact.group.entity.class')
'entity_class' => Group::class
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Oro\Bundle\ContactBundle\DependencyInjection\Compiler;

use Oro\Bundle\ContactBundle\Entity\Contact;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Adds Contact entity to the email provider service.
*/
class EmailHolderHelperConfigPass implements CompilerPassInterface
{
const SERVICE_KEY = 'oro_email.email_holder_helper';
private const SERVICE_KEY = 'oro_email.email_holder_helper';

/**
* {@inheritdoc}
Expand All @@ -18,10 +22,7 @@ public function process(ContainerBuilder $container)
return;
}

$serviceDef = $container->getDefinition(self::SERVICE_KEY);
$serviceDef->addMethodCall(
'addTargetEntity',
[$container->getParameter('oro_contact.entity.class')]
);
$container->getDefinition(self::SERVICE_KEY)
->addMethodCall('addTargetEntity', [Contact::class]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Oro\Bundle\ContactBundle\DependencyInjection\Compiler;

use Oro\Bundle\ContactBundle\Entity\Contact;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Adds Contact entity to the phone provider service.
*/
class PhoneProviderConfigPass implements CompilerPassInterface
{
const SERVICE_KEY = 'oro_address.provider.phone';
private const SERVICE_KEY = 'oro_address.provider.phone';

/**
* {@inheritdoc}
Expand All @@ -18,10 +22,7 @@ public function process(ContainerBuilder $container)
return;
}

$serviceDef = $container->getDefinition(self::SERVICE_KEY);
$serviceDef->addMethodCall(
'addTargetEntity',
[$container->getParameter('oro_contact.entity.class')]
);
$container->getDefinition(self::SERVICE_KEY)
->addMethodCall('addTargetEntity', [Contact::class]);
}
}
31 changes: 3 additions & 28 deletions src/Oro/Bundle/ContactBundle/Form/Handler/ContactEmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Doctrine\ORM\EntityManagerInterface;
use Oro\Bundle\ContactBundle\Entity\Contact;
use Oro\Bundle\ContactBundle\Entity\ContactEmail;
use Oro\Bundle\ContactBundle\Validator\ContactEmailDeleteValidator;
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
* The form handler for ContactEmail entity.
*/
class ContactEmailHandler
{
/** @var FormInterface */
Expand All @@ -23,30 +24,24 @@ class ContactEmailHandler
/** @var EntityManagerInterface */
protected $manager;

/** @var ContactEmailDeleteValidator */
protected $contactEmailDeleteValidator;

/** @var AuthorizationCheckerInterface */
protected $authorizationChecker;

/**
* @param FormInterface $form
* @param RequestStack $requestStack
* @param EntityManagerInterface $manager
* @param ContactEmailDeleteValidator $contactEmailDeleteValidator
* @param AuthorizationCheckerInterface $authorizationChecker
*/
public function __construct(
FormInterface $form,
RequestStack $requestStack,
EntityManagerInterface $manager,
ContactEmailDeleteValidator $contactEmailDeleteValidator,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->form = $form;
$this->requestStack = $requestStack;
$this->manager = $manager;
$this->contactEmailDeleteValidator = $contactEmailDeleteValidator;
$this->authorizationChecker = $authorizationChecker;
}

Expand Down Expand Up @@ -94,26 +89,6 @@ public function process(ContactEmail $entity)
return false;
}

/**
* @param $id
* @param ApiEntityManager $manager
* @throws \Exception
*/
public function handleDelete($id, ApiEntityManager $manager)
{
/** @var ContactEmail $contactEmail */
$contactEmail = $manager->find($id);
if (!$this->authorizationChecker->isGranted('EDIT', $contactEmail->getOwner())) {
throw new AccessDeniedException();
}

if ($this->contactEmailDeleteValidator->validate($contactEmail)) {
$em = $manager->getObjectManager();
$em->remove($contactEmail);
$em->flush();
}
}

/**
* @param ContactEmail $entity
* @param Contact $contact
Expand Down
32 changes: 3 additions & 29 deletions src/Oro/Bundle/ContactBundle/Form/Handler/ContactPhoneHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Doctrine\ORM\EntityManagerInterface;
use Oro\Bundle\ContactBundle\Entity\Contact;
use Oro\Bundle\ContactBundle\Entity\ContactPhone;
use Oro\Bundle\ContactBundle\Validator\ContactPhoneDeleteValidator;
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
* The form handler for ContactPhone entity.
*/
class ContactPhoneHandler
{
/** @var FormInterface */
Expand All @@ -23,30 +24,24 @@ class ContactPhoneHandler
/** @var EntityManagerInterface */
protected $manager;

/** @var ContactPhoneDeleteValidator */
protected $contactPhoneDeleteValidator;

/** @var AuthorizationCheckerInterface */
protected $authorizationChecker;

/**
* @param FormInterface $form
* @param RequestStack $requestStack
* @param EntityManagerInterface $manager
* @param ContactPhoneDeleteValidator $contactPhoneDeleteValidator
* @param AuthorizationCheckerInterface $authorizationChecker
*/
public function __construct(
FormInterface $form,
RequestStack $requestStack,
EntityManagerInterface $manager,
ContactPhoneDeleteValidator $contactPhoneDeleteValidator,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->form = $form;
$this->requestStack = $requestStack;
$this->manager = $manager;
$this->contactPhoneDeleteValidator = $contactPhoneDeleteValidator;
$this->authorizationChecker = $authorizationChecker;
}

Expand Down Expand Up @@ -94,27 +89,6 @@ public function process(ContactPhone $entity)
return false;
}

/**
* @param $id
* @param ApiEntityManager $manager
*
* @throws \Exception
*/
public function handleDelete($id, ApiEntityManager $manager)
{
/** @var ContactPhone $contactPhone */
$contactPhone = $manager->find($id);
if (!$this->authorizationChecker->isGranted('EDIT', $contactPhone->getOwner())) {
throw new AccessDeniedException();
}

if ($this->contactPhoneDeleteValidator->validate($contactPhone)) {
$em = $manager->getObjectManager();
$em->remove($contactPhone);
$em->flush();
}
}

/**
* @param ContactPhone $entity
* @param Contact $contact
Expand Down
Loading

0 comments on commit b312559

Please sign in to comment.