vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Command/SetAclCommand.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\SecurityBundle\Command;
  11. @trigger_error(sprintf('Class "%s" is deprecated since Symfony 3.4 and will be removed in 4.0. Use Symfony\Bundle\AclBundle\Command\SetAclCommand instead.'SetAclCommand::class), \E_USER_DEPRECATED);
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Exception\InvalidArgumentException;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  21. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  22. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  23. use Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException;
  24. use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
  25. use Symfony\Component\Security\Acl\Permission\MaskBuilder;
  26. /**
  27.  * Sets ACL for objects.
  28.  *
  29.  * @author Kévin Dunglas <kevin@les-tilleuls.coop>
  30.  *
  31.  * @deprecated since version 3.4, to be removed in 4.0. See Symfony\Bundle\AclBundle\Command\SetAclCommand instead.
  32.  */
  33. class SetAclCommand extends ContainerAwareCommand
  34. {
  35.     protected static $defaultName 'acl:set';
  36.     private $provider;
  37.     /**
  38.      * @param MutableAclProviderInterface $provider
  39.      */
  40.     public function __construct($provider null)
  41.     {
  42.         if (!$provider instanceof MutableAclProviderInterface) {
  43.             parent::__construct($provider);
  44.             return;
  45.         }
  46.         parent::__construct();
  47.         $this->provider $provider;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function isEnabled()
  53.     {
  54.         if (null !== $this->provider) {
  55.             return parent::isEnabled();
  56.         }
  57.         if (!$this->getContainer()->has('security.acl.provider')) {
  58.             return false;
  59.         }
  60.         $provider $this->getContainer()->get('security.acl.provider');
  61.         if (!$provider instanceof MutableAclProviderInterface) {
  62.             return false;
  63.         }
  64.         return parent::isEnabled();
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     protected function configure()
  70.     {
  71.         $this
  72.             ->setDescription('Sets ACL for objects')
  73.             ->setHelp(<<<EOF
  74. The <info>%command.name%</info> command sets ACL.
  75. The ACL system must have been initialized with the <info>init:acl</info> command.
  76. To set <comment>VIEW</comment> and <comment>EDIT</comment> permissions for the user <comment>kevin</comment> on the instance of
  77. <comment>Acme\MyClass</comment> having the identifier <comment>42</comment>:
  78.   <info>php %command.full_name% --user=Symfony/Component/Security/Core/User/User:kevin VIEW EDIT Acme/MyClass:42</info>
  79. Note that you can use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any
  80. problem.
  81. To set permissions for a role, use the <info>--role</info> option:
  82.   <info>php %command.full_name% --role=ROLE_USER VIEW Acme/MyClass:1936</info>
  83. To set permissions at the class scope, use the <info>--class-scope</info> option:
  84.   <info>php %command.full_name% --class-scope --user=Symfony/Component/Security/Core/User/User:anne OWNER Acme/MyClass:42</info>
  85.   
  86. EOF
  87.             )
  88.             ->addArgument('arguments'InputArgument::IS_ARRAY InputArgument::REQUIRED'A list of permissions and object identities (class name and ID separated by a column)')
  89.             ->addOption('user'nullInputOption::VALUE_REQUIRED InputOption::VALUE_IS_ARRAY'A list of security identities')
  90.             ->addOption('role'nullInputOption::VALUE_REQUIRED InputOption::VALUE_IS_ARRAY'A list of roles')
  91.             ->addOption('class-scope'nullInputOption::VALUE_NONE'Use class-scope entries')
  92.         ;
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     protected function execute(InputInterface $inputOutputInterface $output)
  98.     {
  99.         (new SymfonyStyle($input$output instanceof ConsoleOutputInterface $output->getErrorOutput() : $output))->warning('Command "acl:set" is deprecated since Symfony 3.4 and will be removed from SecurityBundle in 4.0. Install symfony/acl-bundle to use this command.');
  100.         if (null === $this->provider) {
  101.             $this->provider $this->getContainer()->get('security.acl.provider');
  102.         }
  103.         // Parse arguments
  104.         $objectIdentities = [];
  105.         $maskBuilder $this->getMaskBuilder();
  106.         foreach ($input->getArgument('arguments') as $argument) {
  107.             $data explode(':'$argument2);
  108.             if (\count($data) > 1) {
  109.                 $objectIdentities[] = new ObjectIdentity($data[1], strtr($data[0], '/''\\'));
  110.             } else {
  111.                 $maskBuilder->add($data[0]);
  112.             }
  113.         }
  114.         // Build permissions mask
  115.         $mask $maskBuilder->get();
  116.         $userOption $input->getOption('user');
  117.         $roleOption $input->getOption('role');
  118.         $classScopeOption $input->getOption('class-scope');
  119.         if (empty($userOption) && empty($roleOption)) {
  120.             throw new InvalidArgumentException('A Role or a User must be specified.');
  121.         }
  122.         // Create security identities
  123.         $securityIdentities = [];
  124.         if ($userOption) {
  125.             foreach ($userOption as $user) {
  126.                 $data explode(':'$user2);
  127.                 if (=== \count($data)) {
  128.                     throw new InvalidArgumentException('The user must follow the format "Acme/MyUser:username".');
  129.                 }
  130.                 $securityIdentities[] = new UserSecurityIdentity($data[1], strtr($data[0], '/''\\'));
  131.             }
  132.         }
  133.         if ($roleOption) {
  134.             foreach ($roleOption as $role) {
  135.                 $securityIdentities[] = new RoleSecurityIdentity($role);
  136.             }
  137.         }
  138.         // Sets ACL
  139.         foreach ($objectIdentities as $objectIdentity) {
  140.             // Creates a new ACL if it does not already exist
  141.             try {
  142.                 $this->provider->createAcl($objectIdentity);
  143.             } catch (AclAlreadyExistsException $e) {
  144.             }
  145.             $acl $this->provider->findAcl($objectIdentity$securityIdentities);
  146.             foreach ($securityIdentities as $securityIdentity) {
  147.                 if ($classScopeOption) {
  148.                     $acl->insertClassAce($securityIdentity$mask);
  149.                 } else {
  150.                     $acl->insertObjectAce($securityIdentity$mask);
  151.                 }
  152.             }
  153.             $this->provider->updateAcl($acl);
  154.         }
  155.     }
  156.     /**
  157.      * Gets the mask builder.
  158.      *
  159.      * @return MaskBuilder
  160.      */
  161.     protected function getMaskBuilder()
  162.     {
  163.         return new MaskBuilder();
  164.     }
  165. }