Настройте Сценарий PowerShell для Автоматического Выполнения

Мне удалось решить проблему, добавив пользовательский слушатель. Он ловит ModelManagerException при удалении ограниченного объекта. Это работает для всех зарегистрированных администраторов. Вот класс:

<?php

namespace App\EventListener;

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sonata\AdminBundle\Exception\ModelManagerException;

class ModelManagerExceptionResponseListener
{
    private $session;
    private $router;
    private $em;

    public function __construct(SessionInterface $session, UrlGeneratorInterface $router, EntityManagerInterface $em)
    {
        $this->session = $session;
        $this->router = $router;
        $this->em = $em;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // get the exception
        $exception =  $event->getException();
        // we proceed only if it is ModelManagerException
        if (!$exception instanceof ModelManagerException) {
            return;
        }

        // get the route and id
        // if it wasn't a delete route we don't want to proceed
        $request = $event->getRequest();
        $route = $request->get('_route');
        $id = $request->get('id');
        if (substr($route, -6) !== 'delete') {
            return;
        }
        $route = str_replace('delete', 'edit', $route);

        // get the message
        // we proceed only if it is the desired message
        $message = $exception->getMessage();
        $failure = 'Failed to delete object: ';
        if (strpos($message, $failure) < 0) {
            return;
        }

        // get the object that can't be deleted
        $entity = str_replace($failure, '', $message);
        $repository = $this->em->getRepository($entity);
        $object = $repository->findOneById($id);

        $this->session->getFlashBag()
            ->add(
                'sonata_flash_error',
                sprintf('The item "%s" can not be deleted because other items depend on it.', $object)
            )
        ;

        // redirect to the edit form of the object
        $url = $this->router->generate($route, ['id' => $id]);
        $response = new RedirectResponse($url);
        $event->setResponse($response);
    }
}

И мы регистрируем службу:

app.event_listener.pdoexception_listener:
    class: App\EventListener\ModelManagerExceptionResponseListener
    arguments:
        - '@session'
        - '@router'
        - '@doctrine.orm.entity_manager'
    tags:
        - { name: kernel.event_listener, event: kernel.exception }
    public: true # this maybe isn't needed

Вероятно, удаление любого объекта за пределами администратора не будет разрешено в моем конкретном случае. Поэтому это решение удовлетворяет требованиям. Я надеюсь, что этот пример может помочь другим. Вам придется адаптировать некоторые детали в соответствии с вашими потребностями.

16
задан Yaakov Ellis 27 August 2008 в 07:05
поделиться

2 ответа

Сохраните свой сценарий как .ps1 файл и запустите его с помощью powershell.exe, как это:

powershell.exe .\foo.ps1

Удостоверяются, что Вы указываете полный путь к сценарию и удостоверяетесь, что установили свой уровень политики выполнения, по крайней мере, на "RemoteSigned" так, чтобы могли быть запущены неподписанные локальные скрипты.

8
ответ дан 30 November 2019 в 21:03
поделиться

Источник для ответа Matt.

я могу заставить это работать путем двойного щелчка по файлу путем создания пакетного файла со следующим в нем:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
3
ответ дан 30 November 2019 в 21:03
поделиться
Другие вопросы по тегам:

Похожие вопросы: