forked from 1up-lab/OneupAclBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAclCommand.php
153 lines (131 loc) · 4.38 KB
/
CreateAclCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Oneup\AclBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
/**
* CreateAclCommand
*
* @uses ContainerAwareCommand
* @author Julien Deniau <julien.deniau@mapado.com>
*/
class CreateAclCommand extends ContainerAwareCommand
{
/**
* configure
*
* @access protected
* @return void
*/
protected function configure()
{
$this
->setName('oneup:acl:create')
->setDescription('Create an ACL for an Object')
->addArgument(
'objectClass',
InputArgument::REQUIRED,
'Wich object type ?'
)
->addArgument(
'objectId',
InputArgument::REQUIRED,
'Wich object id ?'
)
->addArgument(
'username',
InputArgument::REQUIRED,
'Wich user ?'
)
->addOption(
'doctrine',
'd',
InputOption::VALUE_REQUIRED,
'doctrine or doctrine_mongodb'
)
->addOption(
'entity-manager',
'm',
InputOption::VALUE_REQUIRED,
'Entity manager'
)
;
}
/**
* execute
*
* @param InputInterface $input
* @param OutputInterface $output
* @access protected
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// creating the ACL
$objectClass = $input->getArgument('objectClass');
$objectId = $input->getArgument('objectId');
$doctrine = $input->getOption('doctrine');
$entityManager = $input->getOption('entity-manager');
if (!$doctrine) {
$doctrine = 'doctrine';
}
if ($doctrine != 'doctrine' && $doctrine != 'doctrine_mongodb') {
$output->writeln('<error>You have to choose between "doctrine" and "doctrine_mongodb"</error>');
return 1;
}
$object = $this->get($doctrine)
->getManager($entityManager ?: null)
->getRepository($objectClass)
->find($objectId);
if (!$object) {
$output->writeln('<error>Unable to find the ' . $objectClass . ':' . $objectId . '</error>');
return 1;
}
$aclProvider = $this->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($object);
try {
$acl = $aclProvider->findAcl($objectIdentity);
} catch (\Symfony\Component\Security\Acl\Exception\AclNotFoundException $e) {
$acl = $aclProvider->createAcl($objectIdentity);
}
// retrieving the security identity of the currently logged-in user
$username = $input->getArgument('username');
$user = $this->get('fos_user.user_manager')
->findUserByUsernameOrEmail($username);
if (!$user) {
$output->writeln('<error>User ' . $username . ' not found.</error>');
return 1;
}
$securityIdentity = UserSecurityIdentity::fromAccount($user);
// grant owner access
$dialog = $this->getHelperSet()->get('dialog');
$maskList = array('VIEW', 'EDIT', 'CREATE', 'DELETE', 'UNDELETE', 'OPERATOR', 'MASTER', 'OWNER');
$maskInt = $dialog->select(
$output,
'Please select the ACL (default: VIEW)',
$maskList,
0
);
$mask = constant('Symfony\Component\Security\Acl\Permission\MaskBuilder::MASK_' . $maskList[$maskInt]);
$acl->insertObjectAce($securityIdentity, $mask);
$aclProvider->updateAcl($acl);
$output->writeln('<info>ACL successfully updated.</info>');
return 0;
}
/**
* get
*
* @param mixed $key
* @access protected
* @return mixed
*/
protected function get($key)
{
return $this->getContainer()->get($key);
}
}