Bläddra i källkod

generate-object-acl command: get manipulator from manager type of the admin + updated options

Roel Sint 13 år sedan
förälder
incheckning
a5370771bb
3 ändrade filer med 61 tillägg och 30 borttagningar
  1. 53 20
      Command/GenerateObjectAclCommand.php
  2. 7 3
      Command/SetupAclCommand.php
  3. 1 7
      Util/AdminAclManipulator.php

+ 53 - 20
Command/GenerateObjectAclCommand.php

@@ -20,13 +20,24 @@ use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Output\Output;
 
 use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
 
 class GenerateObjectAclCommand extends ContainerAwareCommand
 {
+    /**
+     * @var string
+     */
+    protected $userEntityClass = '';
+
     public function configure()
     {
-        $this->setName('sonata:admin:generate-object-acl');
-        $this->setDescription('Install ACL for the objects of the Admin Classes');
+        $this
+            ->setName('sonata:admin:generate-object-acl')
+            ->setDescription('Install ACL for the objects of the Admin Classes.')
+            ->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.')
+            ->addOption('user_entity', null, InputOption::VALUE_OPTIONAL, 'Shortcut notation like <comment>AcmeDemoBundle:User</comment>. If not set, it will be asked the first time an object owner is set.')
+            ->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and if and what object owner to set.')
+        ;
     }
 
     public function execute(InputInterface $input, OutputInterface $output)
@@ -38,12 +49,19 @@ class GenerateObjectAclCommand extends ContainerAwareCommand
                 '',
                 'This command helps you generate ACL entities for the objects handled by the AdminBundle.',
                 '',
-                'Foreach Admin, you will be asked to generate the object ACL entities',
+                'If the step option is used, you will be asked for each Admin to generate the object ACL entities.',
                 'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
                 ''
         ));
 
-        $userEntityClass = '';
+        if ($input->getOption('user_entity')) {
+            try {
+                $this->getUserEntityClass($input, $output);
+            } catch (\Exception $e) {
+                $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
+                return;
+            }
+        }
 
         foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
 
@@ -55,34 +73,49 @@ class GenerateObjectAclCommand extends ContainerAwareCommand
                 continue;
             }
 
-            if (!$dialog->askConfirmation($output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), false)) {
+            if ($input->getOption('step') && !$dialog->askConfirmation($output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), false)) {
                 continue;
             }
 
             $securityIdentity = null;
-            if ($dialog->askConfirmation($output,"<question>Set an object owner?</question>\n", false)) {
+            if ($input->getOption('step') && $dialog->askConfirmation($output,"<question>Set an object owner?</question>\n", false)) {
                 $username = $dialog->askAndValidate($output, 'Please enter the username: ', 'Sonata\AdminBundle\Command\Validators::validateUsername');
-                if ($userEntityClass === '') {
-                    list($userBundle, $userEntity) = $dialog->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
-
-                    // Entity exists?
-                    try {
-                        $userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
-                    } catch (\Exception $e) {
-                        $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
-                        continue;
-                    }
-                }
-                $securityIdentity = new UserSecurityIdentity($username, $userEntityClass);
+
+                $securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output));
+            }
+            if (!$input->getOption('step') && $input->getOption('object_owner')) {
+                $securityIdentity = new UserSecurityIdentity($this->getOption('object_owner'), $this->getUserEntityClass($input, $output));
             }
 
-            $manipulatorId = 'sonata.admin.manipulator.acl.object.orm';
+            $manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType());
             if (!$this->getContainer()->has($manipulatorId)) {
                 $output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>');
                 continue;
             }
+            $manipulator = $this->getContainer()->get($manipulatorId);
+            if (!$manipulator instanceof ObjectAclManipulatorInterface) {
+                $output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', get_class($manipulator)));
+                continue;
+            }
+
+            $manipulator->batchConfigureAcls($output, $admin, $securityIdentity);
+        }
+    }
 
-            $this->getContainer()->get($manipulatorId)->batchConfigureAcls($output, $admin, $securityIdentity);
+    protected function getUserEntityClass(InputInterface $input, OutputInterface $output)
+    {
+        if ($this->userEntityClass === '') {
+            if ($input->getOption('user_entity')) {
+               list($userBundle, $userEntity) = Sonata\AdminBundle\Command\Validators::validateEntityName($this->getOption('user_entity'));
+               $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
+            } else {
+                list($userBundle, $userEntity) = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
+
+                // Entity exists?
+               $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
+            }
         }
+
+        return $this->userEntityClass;
     }
 }

+ 7 - 3
Command/SetupAclCommand.php

@@ -11,8 +11,6 @@
 
 namespace Sonata\AdminBundle\Command;
 
-
-
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputOption;
@@ -21,6 +19,7 @@ use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Output\Output;
 use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
 
+use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
 
 class SetupAclCommand extends ContainerAwareCommand
@@ -45,7 +44,12 @@ class SetupAclCommand extends ContainerAwareCommand
                 continue;
             }
 
-            $this->getContainer()->get('sonata.admin.manipulator.acl.admin')->configureAcls($output, $admin);
+            $manipulator = $this->getContainer()->get('sonata.admin.manipulator.acl.admin');
+            if (!$manipulator instanceof AdminAclManipulatorInterface) {
+                $output->writeln(sprintf('The interface "AdminAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', get_class($manipulator)));
+                continue;
+            }
+            $manipulator->configureAcls($output, $admin);
         }
     }
 }

+ 1 - 7
Util/AdminAclManipulator.php

@@ -31,13 +31,7 @@ class AdminAclManipulator implements AdminAclManipulatorInterface
     }
 
     /**
-     * Configure the object ACL for the passed object identities
-     *
-     * @param AdminInterface $admin
-     * @param array $oids an array of ObjectIdentityInterface implementations
-     * @param UserSecurityIdentity $securityIdentity
-     * @throws \Exception
-     * @return array [countAdded, countUpdated]
+     * {@inheritDoc}
      */
     public function configureAcls(OutputInterface $output, AdminInterface $admin)
     {