PermissionGrantingStrategyTest.php 7.3 KB

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