PermissionGrantingStrategyTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Tests\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  12. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  13. use Symfony\Component\Security\Acl\Domain\Acl;
  14. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  15. use Symfony\Component\Security\Acl\Domain\Entry;
  16. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  17. use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
  18. class PermissionGrantingStrategyTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function testIsGrantedObjectAcesHavePriority()
  21. {
  22. $strategy = new PermissionGrantingStrategy();
  23. $acl = $this->getAcl($strategy);
  24. $sid = new UserSecurityIdentity('johannes', 'Foo');
  25. $acl->insertClassAce($sid, 1);
  26. $acl->insertObjectAce($sid, 1, 0, false);
  27. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
  28. }
  29. public function testIsGrantedFallsbackToClassAcesIfNoApplicableObjectAceWasFound()
  30. {
  31. $strategy = new PermissionGrantingStrategy();
  32. $acl = $this->getAcl($strategy);
  33. $sid = new UserSecurityIdentity('johannes', 'Foo');
  34. $acl->insertClassAce($sid, 1);
  35. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  36. }
  37. public function testIsGrantedFavorsLocalAcesOverParentAclAces()
  38. {
  39. $strategy = new PermissionGrantingStrategy();
  40. $sid = new UserSecurityIdentity('johannes', 'Foo');
  41. $acl = $this->getAcl($strategy);
  42. $acl->insertClassAce($sid, 1);
  43. $parentAcl = $this->getAcl($strategy);
  44. $acl->setParentAcl($parentAcl);
  45. $parentAcl->insertClassAce($sid, 1, 0, false);
  46. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  47. }
  48. public function testIsGrantedFallsBackToParentAcesIfNoLocalAcesAreApplicable()
  49. {
  50. $strategy = new PermissionGrantingStrategy();
  51. $sid = new UserSecurityIdentity('johannes', 'Foo');
  52. $anotherSid = new UserSecurityIdentity('ROLE_USER', 'Foo');
  53. $acl = $this->getAcl($strategy);
  54. $acl->insertClassAce($anotherSid, 1, 0, false);
  55. $parentAcl = $this->getAcl($strategy);
  56. $acl->setParentAcl($parentAcl);
  57. $parentAcl->insertClassAce($sid, 1);
  58. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  59. }
  60. /**
  61. * @expectedException Symfony\Component\Security\Acl\Exception\NoAceFoundException
  62. */
  63. public function testIsGrantedReturnsExceptionIfNoAceIsFound()
  64. {
  65. $strategy = new PermissionGrantingStrategy();
  66. $acl = $this->getAcl($strategy);
  67. $sid = new UserSecurityIdentity('johannes', 'Foo');
  68. $strategy->isGranted($acl, array(1), array($sid));
  69. }
  70. public function testIsGrantedFirstApplicableEntryMakesUltimateDecisionForPermissionIdentityCombination()
  71. {
  72. $strategy = new PermissionGrantingStrategy();
  73. $acl = $this->getAcl($strategy);
  74. $sid = new UserSecurityIdentity('johannes', 'Foo');
  75. $aSid = new RoleSecurityIdentity('ROLE_USER');
  76. $acl->insertClassAce($aSid, 1);
  77. $acl->insertClassAce($sid, 1, 1, false);
  78. $acl->insertClassAce($sid, 1, 2);
  79. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid, $aSid)));
  80. $acl->insertObjectAce($sid, 1, 0, false);
  81. $acl->insertObjectAce($aSid, 1, 1);
  82. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid, $aSid)));
  83. }
  84. public function testIsGrantedCallsAuditLoggerOnGrant()
  85. {
  86. $strategy = new PermissionGrantingStrategy();
  87. $acl = $this->getAcl($strategy);
  88. $sid = new UserSecurityIdentity('johannes', 'Foo');
  89. $logger = $this->getMock('Symfony\Component\Security\Acl\Model\AuditLoggerInterface');
  90. $logger
  91. ->expects($this->once())
  92. ->method('logIfNeeded')
  93. ;
  94. $strategy->setAuditLogger($logger);
  95. $acl->insertObjectAce($sid, 1);
  96. $acl->updateObjectAuditing(0, true, false);
  97. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  98. }
  99. public function testIsGrantedCallsAuditLoggerOnDeny()
  100. {
  101. $strategy = new PermissionGrantingStrategy();
  102. $acl = $this->getAcl($strategy);
  103. $sid = new UserSecurityIdentity('johannes', 'Foo');
  104. $logger = $this->getMock('Symfony\Component\Security\Acl\Model\AuditLoggerInterface');
  105. $logger
  106. ->expects($this->once())
  107. ->method('logIfNeeded')
  108. ;
  109. $strategy->setAuditLogger($logger);
  110. $acl->insertObjectAce($sid, 1, 0, false);
  111. $acl->updateObjectAuditing(0, false, true);
  112. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
  113. }
  114. /**
  115. * @dataProvider getAllStrategyTests
  116. */
  117. public function testIsGrantedStrategies($maskStrategy, $aceMask, $requiredMask, $result)
  118. {
  119. $strategy = new PermissionGrantingStrategy();
  120. $acl = $this->getAcl($strategy);
  121. $sid = new UserSecurityIdentity('johannes', 'Foo');
  122. $acl->insertObjectAce($sid, $aceMask, 0, true, $maskStrategy);
  123. if (false === $result) {
  124. try {
  125. $strategy->isGranted($acl, array($requiredMask), array($sid));
  126. $this->fail('The ACE is not supposed to match.');
  127. } catch (NoAceFoundException $noAce) { }
  128. } else {
  129. $this->assertTrue($strategy->isGranted($acl, array($requiredMask), array($sid)));
  130. }
  131. }
  132. public function getAllStrategyTests()
  133. {
  134. return array(
  135. array('all', 1 << 0 | 1 << 1, 1 << 0, true),
  136. array('all', 1 << 0 | 1 << 1, 1 << 2, false),
  137. array('all', 1 << 0 | 1 << 10, 1 << 0 | 1 << 10, true),
  138. array('all', 1 << 0 | 1 << 1, 1 << 0 | 1 << 1 || 1 << 2, false),
  139. array('any', 1 << 0 | 1 << 1, 1 << 0, true),
  140. array('any', 1 << 0 | 1 << 1, 1 << 0 | 1 << 2, true),
  141. array('any', 1 << 0 | 1 << 1, 1 << 2, false),
  142. array('equal', 1 << 0 | 1 << 1, 1 << 0, false),
  143. array('equal', 1 << 0 | 1 << 1, 1 << 1, false),
  144. array('equal', 1 << 0 | 1 << 1, 1 << 0 | 1 << 1, true),
  145. );
  146. }
  147. protected function getAcl($strategy)
  148. {
  149. static $id = 1;
  150. return new Acl($id++, new ObjectIdentity(1, 'Foo'), $strategy, array(), true);
  151. }
  152. public function setUp()
  153. {
  154. if (!class_exists('Doctrine\DBAL\DriverManager')) {
  155. $this->markTestSkipped('The Doctrine2 DBAL is required for this test');
  156. }
  157. }
  158. }