PermissionGrantingStrategyTest.php 6.9 KB

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