SetAclCommandTest.php 7.4 KB

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