AclProviderTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Acl\Dbal;
  3. use Symfony\Component\Security\Acl\Dbal\AclProvider;
  4. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  5. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  6. use Symfony\Component\Security\Acl\Dbal\Schema;
  7. use Doctrine\DBAL\DriverManager;
  8. class AclProviderTest extends \PHPUnit_Framework_TestCase
  9. {
  10. protected $con;
  11. protected $insertClassStmt;
  12. protected $insertEntryStmt;
  13. protected $insertOidStmt;
  14. protected $insertOidAncestorStmt;
  15. protected $insertSidStmt;
  16. /**
  17. * @expectedException Symfony\Component\Security\Acl\Exception\AclNotFoundException
  18. * @expectedMessage There is no ACL for the given object identity.
  19. */
  20. public function testFindAclThrowsExceptionWhenNoAclExists()
  21. {
  22. $this->getProvider()->findAcl(new ObjectIdentity('foo', 'foo'));
  23. }
  24. /**
  25. * @expectedException Symfony\Component\Security\Acl\Exception\AclNotFoundException
  26. */
  27. public function testFindAclsThrowsExceptionUnlessAnACLIsFoundForEveryOID()
  28. {
  29. $oids = array();
  30. $oids[] = new ObjectIdentity('1', 'foo');
  31. $oids[] = new ObjectIdentity('foo', 'foo');
  32. $this->getProvider()->findAcls($oids);
  33. }
  34. public function testFindAcls()
  35. {
  36. $oids = array();
  37. $oids[] = new ObjectIdentity('1', 'foo');
  38. $oids[] = new ObjectIdentity('2', 'foo');
  39. $provider = $this->getProvider();
  40. $acls = $provider->findAcls($oids);
  41. $this->assertInstanceOf('SplObjectStorage', $acls);
  42. $this->assertEquals(2, count($acls));
  43. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl0 = $acls->offsetGet($oids[0]));
  44. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl1 = $acls->offsetGet($oids[1]));
  45. $this->assertTrue($oids[0]->equals($acl0->getObjectIdentity()));
  46. $this->assertTrue($oids[1]->equals($acl1->getObjectIdentity()));
  47. }
  48. public function testFindAclCachesAclInMemory()
  49. {
  50. $oid = new ObjectIdentity('1', 'foo');
  51. $provider = $this->getProvider();
  52. $acl = $provider->findAcl($oid);
  53. $this->assertSame($acl, $cAcl = $provider->findAcl($oid));
  54. $cAces = $cAcl->getObjectAces();
  55. foreach ($acl->getObjectAces() as $index => $ace) {
  56. $this->assertSame($ace, $cAces[$index]);
  57. }
  58. }
  59. public function testFindAcl()
  60. {
  61. $oid = new ObjectIdentity('1', 'foo');
  62. $provider = $this->getProvider();
  63. $acl = $provider->findAcl($oid);
  64. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Acl', $acl);
  65. $this->assertTrue($oid->equals($acl->getObjectIdentity()));
  66. $this->assertEquals(4, $acl->getId());
  67. $this->assertEquals(0, count($acl->getClassAces()));
  68. $this->assertEquals(0, count($this->getField($acl, 'classFieldAces')));
  69. $this->assertEquals(3, count($acl->getObjectAces()));
  70. $this->assertEquals(0, count($this->getField($acl, 'objectFieldAces')));
  71. $aces = $acl->getObjectAces();
  72. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\Entry', $aces[0]);
  73. $this->assertTrue($aces[0]->isGranting());
  74. $this->assertTrue($aces[0]->isAuditSuccess());
  75. $this->assertTrue($aces[0]->isAuditFailure());
  76. $this->assertEquals('all', $aces[0]->getStrategy());
  77. $this->assertSame(2, $aces[0]->getMask());
  78. // check ACE are in correct order
  79. $i = 0;
  80. foreach ($aces as $index => $ace) {
  81. $this->assertEquals($i, $index);
  82. $i++;
  83. }
  84. $sid = $aces[0]->getSecurityIdentity();
  85. $this->assertInstanceOf('Symfony\Component\Security\Acl\Domain\UserSecurityIdentity', $sid);
  86. $this->assertEquals('john.doe', $sid->getUsername());
  87. $this->assertEquals('SomeClass', $sid->getClass());
  88. }
  89. protected function setUp()
  90. {
  91. $this->con = DriverManager::getConnection(array(
  92. 'driver' => 'pdo_sqlite',
  93. 'memory' => true,
  94. ));
  95. // import the schema
  96. $schema = new Schema($options = $this->getOptions());
  97. foreach ($schema->toSql($this->con->getDatabasePlatform()) as $sql) {
  98. $this->con->exec($sql);
  99. }
  100. // populate the schema with some test data
  101. $this->insertClassStmt = $this->con->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)');
  102. foreach ($this->getClassData() as $data) {
  103. $this->insertClassStmt->execute($data);
  104. }
  105. $this->insertSidStmt = $this->con->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)');
  106. foreach ($this->getSidData() as $data) {
  107. $this->insertSidStmt->execute($data);
  108. }
  109. $this->insertOidStmt = $this->con->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)');
  110. foreach ($this->getOidData() as $data) {
  111. $this->insertOidStmt->execute($data);
  112. }
  113. $this->insertEntryStmt = $this->con->prepare('INSERT INTO acl_entries (id, class_id, object_identity_id, field_name, ace_order, security_identity_id, mask, granting, granting_strategy, audit_success, audit_failure) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
  114. foreach ($this->getEntryData() as $data) {
  115. $this->insertEntryStmt->execute($data);
  116. }
  117. $this->insertOidAncestorStmt = $this->con->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)');
  118. foreach ($this->getOidAncestorData() as $data) {
  119. $this->insertOidAncestorStmt->execute($data);
  120. }
  121. }
  122. protected function tearDown()
  123. {
  124. $this->con = null;
  125. }
  126. protected function getField($object, $field)
  127. {
  128. $reflection = new \ReflectionProperty($object, $field);
  129. $reflection->setAccessible(true);
  130. return $reflection->getValue($object);
  131. }
  132. protected function getEntryData()
  133. {
  134. // id, cid, oid, field, order, sid, mask, granting, strategy, a success, a failure
  135. return array(
  136. array(1, 1, 1, null, 0, 1, 1, 1, 'all', 1, 1),
  137. array(2, 1, 1, null, 1, 2, 1 << 2 | 1 << 1, 0, 'any', 0, 0),
  138. array(3, 3, 4, null, 0, 1, 2, 1, 'all', 1, 1),
  139. array(4, 3, 4, null, 2, 2, 1, 1, 'all', 1, 1),
  140. array(5, 3, 4, null, 1, 3, 1, 1, 'all', 1, 1),
  141. );
  142. }
  143. protected function getOidData()
  144. {
  145. // id, cid, oid, parent_oid, entries_inheriting
  146. return array(
  147. array(1, 1, '123', null, 1),
  148. array(2, 2, '123', 1, 1),
  149. array(3, 2, 'i:3:123', 1, 1),
  150. array(4, 3, '1', 2, 1),
  151. array(5, 3, '2', 2, 1),
  152. );
  153. }
  154. protected function getOidAncestorData()
  155. {
  156. return array(
  157. array(1, 1),
  158. array(2, 1),
  159. array(2, 2),
  160. array(3, 1),
  161. array(3, 3),
  162. array(4, 2),
  163. array(4, 1),
  164. array(4, 4),
  165. array(5, 2),
  166. array(5, 1),
  167. array(5, 5),
  168. );
  169. }
  170. protected function getSidData()
  171. {
  172. return array(
  173. array(1, 'SomeClass-john.doe', 1),
  174. array(2, 'MyClass-john.doe@foo.com', 1),
  175. array(3, 'FooClass-123', 1),
  176. array(4, 'MooClass-ROLE_USER', 1),
  177. array(5, 'ROLE_USER', 0),
  178. array(6, 'IS_AUTHENTICATED_FULLY', 0),
  179. );
  180. }
  181. protected function getClassData()
  182. {
  183. return array(
  184. array(1, 'Bundle\SomeVendor\MyBundle\Entity\SomeEntity'),
  185. array(2, 'Bundle\MyBundle\Entity\AnotherEntity'),
  186. array(3, 'foo'),
  187. );
  188. }
  189. protected function getOptions()
  190. {
  191. return array(
  192. 'oid_table_name' => 'acl_object_identities',
  193. 'oid_ancestors_table_name' => 'acl_object_identity_ancestors',
  194. 'class_table_name' => 'acl_classes',
  195. 'sid_table_name' => 'acl_security_identities',
  196. 'entry_table_name' => 'acl_entries',
  197. );
  198. }
  199. protected function getStrategy()
  200. {
  201. return new PermissionGrantingStrategy();
  202. }
  203. protected function getProvider()
  204. {
  205. return new AclProvider($this->con, $this->getStrategy(), $this->getOptions());
  206. }
  207. }