Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Kunstmaan/AdminBundle/Command/UpdateAclCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Kunstmaan\AdminBundle\Service\AclManager;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ChoiceQuestion;
12
use Symfony\Component\Security\Acl\Domain\Acl;
13
use Symfony\Component\Security\Acl\Domain\Entry;
14
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
15
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
16
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
17
use Symfony\Component\Security\Acl\Permission\PermissionMapInterface;
18
19
/**
20
 * Permissions update of ACL entries for all nodes for given role.
21
 *
22
 * @final since 5.1
23
 * NEXT_MAJOR extend from `Command` and remove `$this->getContainer` usages
24
 */
25
class UpdateAclCommand extends ContainerAwareCommand
26
{
27
    /** @var AclManager */
28
    private $aclManager;
29
30
    /** @var PermissionMapInterface */
31
    private $permissionMap;
32
33
    /** @var EntityManagerInterface */
34
    private $em;
35
36
    /** @var  */
37
    private $roles;
38
39
    public function __construct(/*AclManager*/ $aclManager = null, EntityManagerInterface $em = null, PermissionMapInterface $permissionMap = null, array $roles = null)
40
    {
41
        parent::__construct();
42
43
        if (!$aclManager instanceof AclManager) {
44
            @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version symfony 3.4 and will be removed in symfony 4.0. If the command was registered by convention, make it a service instead. ', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
46
            $this->setName(null === $aclManager ? 'kuma:acl:update' : $aclManager);
47
48
            return;
49
        }
50
51
        $this->aclManager = $aclManager;
52
        $this->em = $em;
53
        $this->permissionMap = $permissionMap;
54
        $this->roles = $roles;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function configure()
61
    {
62
        parent::configure();
63
64
        $this->setName('kuma:acl:update')
65
            ->setDescription('Permissions update of ACL entries for all nodes for given role')
66
            ->setHelp("The <info>kuma:acl:update</info> will update ACL entries for the nodes of the current project" .
67
                "with given role and permissions");
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $helper = $this->getHelper('question');
76
        if (null === $this->aclManager) {
77
            $this->aclManager = $this->getContainer()->get('kunstmaan_admin.acl.manager');
78
        }
79
        if (null === $this->em ) {
80
            $this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
81
        }
82
        if (null === $this->permissionMap) {
83
            $this->permissionMap = $this->getContainer()->get('security.acl.permission.map');
84
        }
85
        if (null === $this->roles) {
86
            $this->roles = $this->getContainer()->getParameter('security.role_hierarchy.roles');
87
        }
88
89
        // Select Role
90
        $question = new ChoiceQuestion('Select role', array_keys($this->roles));
91
        $question->setErrorMessage('Role %s is invalid.');
92
        $role = $helper->ask($input, $output, $question);
93
94
        // Select Permission(s)
95
        $permissionMap = $this->permissionMap;
96
        $question = new ChoiceQuestion('Select permissions(s) (separate by ",")',
97
            $permissionMap->getPossiblePermissions());
98
        $question->setMultiselect(true);
99
        $mask = array_reduce($helper->ask($input, $output, $question), function ($a, $b) use ($permissionMap) {
100
            return $a | $permissionMap->getMasks($b, null)[0];
101
        }, 0);
102
103
        // Fetch all nodes & grant access
104
        $nodes = $this->em->getRepository('KunstmaanNodeBundle:Node')->findAll();
105
106
        $this->aclManager->updateNodesAclToRole($nodes, $role, $mask);
107
108
        $output->writeln(count($nodes) . ' nodes processed.');
109
    }
110
111
}
112