PermissionGrantingStrategyTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Component\Security\Acl\Tests\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\PermissionGrantingStrategy;
  16. use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
  17. class PermissionGrantingStrategyTest extends \PHPUnit_Framework_TestCase
  18. {
  19. public function testIsGrantedObjectAcesHavePriority()
  20. {
  21. $strategy = new PermissionGrantingStrategy();
  22. $acl = $this->getAcl($strategy);
  23. $sid = new UserSecurityIdentity('johannes', 'Foo');
  24. $acl->insertClassAce($sid, 1);
  25. $acl->insertObjectAce($sid, 1, 0, false);
  26. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
  27. }
  28. public function testIsGrantedFallsBackToClassAcesIfNoApplicableObjectAceWasFound()
  29. {
  30. $strategy = new PermissionGrantingStrategy();
  31. $acl = $this->getAcl($strategy);
  32. $sid = new UserSecurityIdentity('johannes', 'Foo');
  33. $acl->insertClassAce($sid, 1);
  34. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  35. }
  36. public function testIsGrantedFavorsLocalAcesOverParentAclAces()
  37. {
  38. $strategy = new PermissionGrantingStrategy();
  39. $sid = new UserSecurityIdentity('johannes', 'Foo');
  40. $acl = $this->getAcl($strategy);
  41. $acl->insertClassAce($sid, 1);
  42. $parentAcl = $this->getAcl($strategy);
  43. $acl->setParentAcl($parentAcl);
  44. $parentAcl->insertClassAce($sid, 1, 0, false);
  45. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  46. }
  47. public function testIsGrantedFallsBackToParentAcesIfNoLocalAcesAreApplicable()
  48. {
  49. $strategy = new PermissionGrantingStrategy();
  50. $sid = new UserSecurityIdentity('johannes', 'Foo');
  51. $anotherSid = new UserSecurityIdentity('ROLE_USER', 'Foo');
  52. $acl = $this->getAcl($strategy);
  53. $acl->insertClassAce($anotherSid, 1, 0, false);
  54. $parentAcl = $this->getAcl($strategy);
  55. $acl->setParentAcl($parentAcl);
  56. $parentAcl->insertClassAce($sid, 1);
  57. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  58. }
  59. /**
  60. * @expectedException \Symfony\Component\Security\Acl\Exception\NoAceFoundException
  61. */
  62. public function testIsGrantedReturnsExceptionIfNoAceIsFound()
  63. {
  64. $strategy = new PermissionGrantingStrategy();
  65. $acl = $this->getAcl($strategy);
  66. $sid = new UserSecurityIdentity('johannes', 'Foo');
  67. $strategy->isGranted($acl, array(1), array($sid));
  68. }
  69. public function testIsGrantedFirstApplicableEntryMakesUltimateDecisionForPermissionIdentityCombination()
  70. {
  71. $strategy = new PermissionGrantingStrategy();
  72. $acl = $this->getAcl($strategy);
  73. $sid = new UserSecurityIdentity('johannes', 'Foo');
  74. $aSid = new RoleSecurityIdentity('ROLE_USER');
  75. $acl->insertClassAce($aSid, 1);
  76. $acl->insertClassAce($sid, 1, 1, false);
  77. $acl->insertClassAce($sid, 1, 2);
  78. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid, $aSid)));
  79. $acl->insertObjectAce($sid, 1, 0, false);
  80. $acl->insertObjectAce($aSid, 1, 1);
  81. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid, $aSid)));
  82. }
  83. public function testIsGrantedCallsAuditLoggerOnGrant()
  84. {
  85. $strategy = new PermissionGrantingStrategy();
  86. $acl = $this->getAcl($strategy);
  87. $sid = new UserSecurityIdentity('johannes', 'Foo');
  88. $logger = $this->getMock('Symfony\Component\Security\Acl\Model\AuditLoggerInterface');
  89. $logger
  90. ->expects($this->once())
  91. ->method('logIfNeeded')
  92. ;
  93. $strategy->setAuditLogger($logger);
  94. $acl->insertObjectAce($sid, 1);
  95. $acl->updateObjectAuditing(0, true, false);
  96. $this->assertTrue($strategy->isGranted($acl, array(1), array($sid)));
  97. }
  98. public function testIsGrantedCallsAuditLoggerOnDeny()
  99. {
  100. $strategy = new PermissionGrantingStrategy();
  101. $acl = $this->getAcl($strategy);
  102. $sid = new UserSecurityIdentity('johannes', 'Foo');
  103. $logger = $this->getMock('Symfony\Component\Security\Acl\Model\AuditLoggerInterface');
  104. $logger
  105. ->expects($this->once())
  106. ->method('logIfNeeded')
  107. ;
  108. $strategy->setAuditLogger($logger);
  109. $acl->insertObjectAce($sid, 1, 0, false);
  110. $acl->updateObjectAuditing(0, false, true);
  111. $this->assertFalse($strategy->isGranted($acl, array(1), array($sid)));
  112. }
  113. /**
  114. * @dataProvider getAllStrategyTests
  115. */
  116. public function testIsGrantedStrategies($maskStrategy, $aceMask, $requiredMask, $result)
  117. {
  118. $strategy = new PermissionGrantingStrategy();
  119. $acl = $this->getAcl($strategy);
  120. $sid = new UserSecurityIdentity('johannes', 'Foo');
  121. $acl->insertObjectAce($sid, $aceMask, 0, true, $maskStrategy);
  122. if (false === $result) {
  123. try {
  124. $strategy->isGranted($acl, array($requiredMask), array($sid));
  125. $this->fail('The ACE is not supposed to match.');
  126. } catch (NoAceFoundException $e) {
  127. }
  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. }