AclProviderTest.php 8.7 KB

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