|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Bundle\SecurityBundle\Tests\Functional; |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Symfony package. |
| 7 | + * |
| 8 | + * (c) Fabien Potencier <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 14 | +use Symfony\Bundle\SecurityBundle\Command\InitAclCommand; |
| 15 | +use Symfony\Bundle\SecurityBundle\Command\SetAclCommand; |
| 16 | +use Symfony\Component\Console\Tester\CommandTester; |
| 17 | +use Symfony\Component\Security\Acl\Domain\ObjectIdentity; |
| 18 | +use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; |
| 19 | +use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; |
| 20 | +use Symfony\Component\Security\Acl\Exception\NoAceFoundException; |
| 21 | +use Symfony\Component\Security\Acl\Permission\BasicPermissionMap; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tests SetAclCommand |
| 25 | + * |
| 26 | + * @author Kévin Dunglas <[email protected]> |
| 27 | + */ |
| 28 | +class SetAclCommandTest extends WebTestCase |
| 29 | +{ |
| 30 | + const OBJECT_CLASS = 'Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\AclBundle\Entity\Car'; |
| 31 | + const SECURITY_CLASS = 'Symfony\Component\Security\Core\User\User'; |
| 32 | + |
| 33 | + public function testSetAclUser() |
| 34 | + { |
| 35 | + $objectId = 1; |
| 36 | + $securityUsername1 = 'kevin'; |
| 37 | + $securityUsername2 = 'anne'; |
| 38 | + $grantedPermission1 = 'VIEW'; |
| 39 | + $grantedPermission2 = 'EDIT'; |
| 40 | + |
| 41 | + $application = $this->getApplication(); |
| 42 | + $application->add(new SetAclCommand()); |
| 43 | + |
| 44 | + $setAclCommand = $application->find('acl:set'); |
| 45 | + $setAclCommandTester = new CommandTester($setAclCommand); |
| 46 | + $setAclCommandTester->execute(array( |
| 47 | + 'command' => 'acl:set', |
| 48 | + 'arguments' => array($grantedPermission1, $grantedPermission2, sprintf('%s:%s', self::OBJECT_CLASS, $objectId)), |
| 49 | + '--user' => array(sprintf('%s:%s', self::SECURITY_CLASS, $securityUsername1), sprintf('%s:%s', self::SECURITY_CLASS, $securityUsername2)) |
| 50 | + )); |
| 51 | + |
| 52 | + $objectIdentity = new ObjectIdentity($objectId, self::OBJECT_CLASS); |
| 53 | + $securityIdentity1 = new UserSecurityIdentity($securityUsername1, self::SECURITY_CLASS); |
| 54 | + $securityIdentity2 = new UserSecurityIdentity($securityUsername2, self::SECURITY_CLASS); |
| 55 | + $permissionMap = new BasicPermissionMap(); |
| 56 | + |
| 57 | + /** @var \Symfony\Component\Security\Acl\Model\AclProviderInterface $aclProvider */ |
| 58 | + $aclProvider = $application->getKernel()->getContainer()->get('security.acl.provider'); |
| 59 | + $acl = $aclProvider->findAcl($objectIdentity, array($securityIdentity1)); |
| 60 | + |
| 61 | + $this->assertTrue($acl->isGranted($permissionMap->getMasks($grantedPermission1, null), array($securityIdentity1))); |
| 62 | + $this->assertTrue($acl->isGranted($permissionMap->getMasks($grantedPermission1, null), array($securityIdentity2))); |
| 63 | + $this->assertTrue($acl->isGranted($permissionMap->getMasks($grantedPermission2, null), array($securityIdentity2))); |
| 64 | + |
| 65 | + try { |
| 66 | + $acl->isGranted($permissionMap->getMasks('OWNER', null), array($securityIdentity1)); |
| 67 | + $this->fail('NoAceFoundException not throwed'); |
| 68 | + } catch (NoAceFoundException $e) { |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + $acl->isGranted($permissionMap->getMasks('OPERATOR', null), array($securityIdentity2)); |
| 73 | + $this->fail('NoAceFoundException not throwed'); |
| 74 | + } catch (NoAceFoundException $e) { |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public function testSetAclRole() |
| 79 | + { |
| 80 | + $objectId = 1; |
| 81 | + $securityUsername = 'kevin'; |
| 82 | + $grantedPermission = 'VIEW'; |
| 83 | + $role = 'ROLE_ADMIN'; |
| 84 | + |
| 85 | + $application = $this->getApplication(); |
| 86 | + $application->add(new SetAclCommand()); |
| 87 | + |
| 88 | + $setAclCommand = $application->find('acl:set'); |
| 89 | + $setAclCommandTester = new CommandTester($setAclCommand); |
| 90 | + $setAclCommandTester->execute(array( |
| 91 | + 'command' => 'acl:set', |
| 92 | + 'arguments' => array($grantedPermission, sprintf('%s:%s', strtr(self::OBJECT_CLASS, '\\', '/'), $objectId)), |
| 93 | + '--role' => array($role) |
| 94 | + )); |
| 95 | + |
| 96 | + $objectIdentity = new ObjectIdentity($objectId, self::OBJECT_CLASS); |
| 97 | + $userSecurityIdentity = new UserSecurityIdentity($securityUsername, self::SECURITY_CLASS); |
| 98 | + $roleSecurityIdentity = new RoleSecurityIdentity($role); |
| 99 | + $permissionMap = new BasicPermissionMap(); |
| 100 | + |
| 101 | + /** @var \Symfony\Component\Security\Acl\Model\AclProviderInterface $aclProvider */ |
| 102 | + $aclProvider = $application->getKernel()->getContainer()->get('security.acl.provider'); |
| 103 | + $acl = $aclProvider->findAcl($objectIdentity, array($roleSecurityIdentity, $userSecurityIdentity)); |
| 104 | + |
| 105 | + $this->assertTrue($acl->isGranted($permissionMap->getMasks($grantedPermission, null), array($roleSecurityIdentity))); |
| 106 | + $this->assertTrue($acl->isGranted($permissionMap->getMasks($grantedPermission, null), array($roleSecurityIdentity))); |
| 107 | + |
| 108 | + try { |
| 109 | + $acl->isGranted($permissionMap->getMasks('VIEW', null), array($userSecurityIdentity)); |
| 110 | + $this->fail('NoAceFoundException not throwed'); |
| 111 | + } catch (NoAceFoundException $e) { |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + $acl->isGranted($permissionMap->getMasks('OPERATOR', null), array($userSecurityIdentity)); |
| 116 | + $this->fail('NoAceFoundException not throwed'); |
| 117 | + } catch (NoAceFoundException $e) { |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public function testSetAclClassScope() |
| 122 | + { |
| 123 | + $objectId = 1; |
| 124 | + $grantedPermission = 'VIEW'; |
| 125 | + $role = 'ROLE_USER'; |
| 126 | + |
| 127 | + $application = $this->getApplication(); |
| 128 | + $application->add(new SetAclCommand()); |
| 129 | + |
| 130 | + $setAclCommand = $application->find('acl:set'); |
| 131 | + $setAclCommandTester = new CommandTester($setAclCommand); |
| 132 | + $setAclCommandTester->execute(array( |
| 133 | + 'command' => 'acl:set', |
| 134 | + 'arguments' => array($grantedPermission, sprintf('%s:%s', self::OBJECT_CLASS, $objectId)), |
| 135 | + '--class-scope' => true, |
| 136 | + '--role' => array($role) |
| 137 | + )); |
| 138 | + |
| 139 | + $objectIdentity1 = new ObjectIdentity($objectId, self::OBJECT_CLASS); |
| 140 | + $objectIdentity2 = new ObjectIdentity(2, self::OBJECT_CLASS); |
| 141 | + $roleSecurityIdentity = new RoleSecurityIdentity($role); |
| 142 | + $permissionMap = new BasicPermissionMap(); |
| 143 | + |
| 144 | + /** @var \Symfony\Component\Security\Acl\Model\AclProviderInterface $aclProvider */ |
| 145 | + $aclProvider = $application->getKernel()->getContainer()->get('security.acl.provider'); |
| 146 | + |
| 147 | + $acl1 = $aclProvider->findAcl($objectIdentity1, array($roleSecurityIdentity)); |
| 148 | + $this->assertTrue($acl1->isGranted($permissionMap->getMasks($grantedPermission, null), array($roleSecurityIdentity))); |
| 149 | + |
| 150 | + $acl2 = $aclProvider->createAcl($objectIdentity2); |
| 151 | + $this->assertTrue($acl2->isGranted($permissionMap->getMasks($grantedPermission, null), array($roleSecurityIdentity))); |
| 152 | + } |
| 153 | + |
| 154 | + private function getApplication() |
| 155 | + { |
| 156 | + $kernel = $this->createKernel(array('test_case' => 'Acl')); |
| 157 | + $kernel->boot(); |
| 158 | + |
| 159 | + $application = new Application($kernel); |
| 160 | + $application->add(new InitAclCommand()); |
| 161 | + |
| 162 | + $initAclCommand = $application->find('init:acl'); |
| 163 | + $initAclCommandTester = new CommandTester($initAclCommand); |
| 164 | + $initAclCommandTester->execute(array('command' => 'init:acl')); |
| 165 | + |
| 166 | + return $application; |
| 167 | + } |
| 168 | +} |
0 commit comments