SecurityIdentityRetrievalStrategyTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\RoleSecurityIdentity;
  12. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  13. use Symfony\Component\Security\Acl\Domain\SecurityIdentityRetrievalStrategy;
  14. class SecurityIdentityRetrievalStrategyTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @dataProvider getSecurityIdentityRetrievalTests
  18. */
  19. public function testGetSecurityIdentities($user, array $roles, $authenticationStatus, array $sids)
  20. {
  21. $strategy = $this->getStrategy($roles, $authenticationStatus);
  22. if ('anonymous' === $authenticationStatus) {
  23. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken')
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. } else {
  27. $class = '';
  28. if (is_string($user)) {
  29. $class = 'MyCustomTokenImpl';
  30. }
  31. $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
  32. ->setMockClassName($class)
  33. ->getMock();
  34. }
  35. $token
  36. ->expects($this->once())
  37. ->method('getRoles')
  38. ->will($this->returnValue(array('foo')))
  39. ;
  40. if ('anonymous' === $authenticationStatus) {
  41. $token
  42. ->expects($this->never())
  43. ->method('getUser')
  44. ;
  45. } else {
  46. $token
  47. ->expects($this->once())
  48. ->method('getUser')
  49. ->will($this->returnValue($user))
  50. ;
  51. }
  52. $extractedSids = $strategy->getSecurityIdentities($token);
  53. foreach ($extractedSids as $index => $extractedSid) {
  54. if (!isset($sids[$index])) {
  55. $this->fail(sprintf('Expected SID at index %d, but there was none.', true));
  56. }
  57. if (false === $sids[$index]->equals($extractedSid)) {
  58. $this->fail(sprintf('Index: %d, expected SID "%s", but got "%s".', $index, $sids[$index], $extractedSid));
  59. }
  60. }
  61. }
  62. public function getSecurityIdentityRetrievalTests()
  63. {
  64. return array(
  65. array($this->getAccount('johannes', 'FooUser'), array('ROLE_USER', 'ROLE_SUPERADMIN'), 'fullFledged', array(
  66. new UserSecurityIdentity('johannes', 'FooUser'),
  67. new RoleSecurityIdentity('ROLE_USER'),
  68. new RoleSecurityIdentity('ROLE_SUPERADMIN'),
  69. new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
  70. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  71. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  72. )),
  73. array('johannes', array('ROLE_FOO'), 'fullFledged', array(
  74. new UserSecurityIdentity('johannes', 'MyCustomTokenImpl'),
  75. new RoleSecurityIdentity('ROLE_FOO'),
  76. new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
  77. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  78. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  79. )),
  80. array(new CustomUserImpl('johannes'), array('ROLE_FOO'), 'fullFledged', array(
  81. new UserSecurityIdentity('johannes', 'Symfony\Component\Security\Acl\Tests\Domain\CustomUserImpl'),
  82. new RoleSecurityIdentity('ROLE_FOO'),
  83. new RoleSecurityIdentity('IS_AUTHENTICATED_FULLY'),
  84. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  85. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  86. )),
  87. array($this->getAccount('foo', 'FooBarUser'), array('ROLE_FOO'), 'rememberMe', array(
  88. new UserSecurityIdentity('foo', 'FooBarUser'),
  89. new RoleSecurityIdentity('ROLE_FOO'),
  90. new RoleSecurityIdentity('IS_AUTHENTICATED_REMEMBERED'),
  91. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  92. )),
  93. array('guest', array('ROLE_FOO'), 'anonymous', array(
  94. new RoleSecurityIdentity('ROLE_FOO'),
  95. new RoleSecurityIdentity('IS_AUTHENTICATED_ANONYMOUSLY'),
  96. )),
  97. );
  98. }
  99. protected function getAccount($username, $class)
  100. {
  101. $account = $this->getMock('Symfony\Component\Security\Core\User\UserInterface', array(), array(), $class);
  102. $account
  103. ->expects($this->any())
  104. ->method('getUsername')
  105. ->will($this->returnValue($username))
  106. ;
  107. return $account;
  108. }
  109. protected function getStrategy(array $roles = array(), $authenticationStatus = 'fullFledged')
  110. {
  111. $roleHierarchy = $this->getMock('Symfony\Component\Security\Core\Role\RoleHierarchyInterface');
  112. $roleHierarchy
  113. ->expects($this->once())
  114. ->method('getReachableRoles')
  115. ->with($this->equalTo(array('foo')))
  116. ->will($this->returnValue($roles))
  117. ;
  118. $trustResolver = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface', array(), array('', ''));
  119. $trustResolver
  120. ->expects($this->at(0))
  121. ->method('isAnonymous')
  122. ->will($this->returnValue('anonymous' === $authenticationStatus))
  123. ;
  124. if ('fullFledged' === $authenticationStatus) {
  125. $trustResolver
  126. ->expects($this->once())
  127. ->method('isFullFledged')
  128. ->will($this->returnValue(true))
  129. ;
  130. $trustResolver
  131. ->expects($this->never())
  132. ->method('isRememberMe')
  133. ;
  134. } elseif ('rememberMe' === $authenticationStatus) {
  135. $trustResolver
  136. ->expects($this->once())
  137. ->method('isFullFledged')
  138. ->will($this->returnValue(false))
  139. ;
  140. $trustResolver
  141. ->expects($this->once())
  142. ->method('isRememberMe')
  143. ->will($this->returnValue(true))
  144. ;
  145. } else {
  146. $trustResolver
  147. ->expects($this->at(1))
  148. ->method('isAnonymous')
  149. ->will($this->returnValue(true))
  150. ;
  151. $trustResolver
  152. ->expects($this->once())
  153. ->method('isFullFledged')
  154. ->will($this->returnValue(false))
  155. ;
  156. $trustResolver
  157. ->expects($this->once())
  158. ->method('isRememberMe')
  159. ->will($this->returnValue(false))
  160. ;
  161. }
  162. return new SecurityIdentityRetrievalStrategy($roleHierarchy, $trustResolver);
  163. }
  164. }
  165. class CustomUserImpl
  166. {
  167. protected $name;
  168. public function __construct($name)
  169. {
  170. $this->name = $name;
  171. }
  172. public function __toString()
  173. {
  174. return $this->name;
  175. }
  176. }