Controller.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace Doctrine\Tests\Common\Annotations\Fixtures;
  3. use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Template;
  4. use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route;
  5. /**
  6. * @Route("/someprefix")
  7. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  8. */
  9. class Controller
  10. {
  11. /**
  12. * @Route("/", name="_demo")
  13. * @Template()
  14. */
  15. public function indexAction()
  16. {
  17. return array();
  18. }
  19. /**
  20. * @Route("/hello/{name}", name="_demo_hello")
  21. * @Template()
  22. */
  23. public function helloAction($name)
  24. {
  25. return array('name' => $name);
  26. }
  27. /**
  28. * @Route("/contact", name="_demo_contact")
  29. * @Template()
  30. */
  31. public function contactAction()
  32. {
  33. $form = ContactForm::create($this->get('form.context'), 'contact');
  34. $form->bind($this->container->get('request'), $form);
  35. if ($form->isValid()) {
  36. $form->send($this->get('mailer'));
  37. $this->get('session')->setFlash('notice', 'Message sent!');
  38. return new RedirectResponse($this->generateUrl('_demo'));
  39. }
  40. return array('form' => $form);
  41. }
  42. /**
  43. * Creates the ACL for the passed object identity
  44. *
  45. * @param ObjectIdentityInterface $oid
  46. * @return void
  47. */
  48. private function createObjectIdentity(ObjectIdentityInterface $oid)
  49. {
  50. $classId = $this->createOrRetrieveClassId($oid->getType());
  51. $this->connection->executeQuery($this->getInsertObjectIdentitySql($oid->getIdentifier(), $classId, true));
  52. }
  53. /**
  54. * Returns the primary key for the passed class type.
  55. *
  56. * If the type does not yet exist in the database, it will be created.
  57. *
  58. * @param string $classType
  59. * @return integer
  60. */
  61. private function createOrRetrieveClassId($classType)
  62. {
  63. if (false !== $id = $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn()) {
  64. return $id;
  65. }
  66. $this->connection->executeQuery($this->getInsertClassSql($classType));
  67. return $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn();
  68. }
  69. /**
  70. * Returns the primary key for the passed security identity.
  71. *
  72. * If the security identity does not yet exist in the database, it will be
  73. * created.
  74. *
  75. * @param SecurityIdentityInterface $sid
  76. * @return integer
  77. */
  78. private function createOrRetrieveSecurityIdentityId(SecurityIdentityInterface $sid)
  79. {
  80. if (false !== $id = $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn()) {
  81. return $id;
  82. }
  83. $this->connection->executeQuery($this->getInsertSecurityIdentitySql($sid));
  84. return $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn();
  85. }
  86. /**
  87. * Deletes all ACEs for the given object identity primary key.
  88. *
  89. * @param integer $oidPK
  90. * @return void
  91. */
  92. private function deleteAccessControlEntries($oidPK)
  93. {
  94. $this->connection->executeQuery($this->getDeleteAccessControlEntriesSql($oidPK));
  95. }
  96. /**
  97. * Deletes the object identity from the database.
  98. *
  99. * @param integer $pk
  100. * @return void
  101. */
  102. private function deleteObjectIdentity($pk)
  103. {
  104. $this->connection->executeQuery($this->getDeleteObjectIdentitySql($pk));
  105. }
  106. /**
  107. * Deletes all entries from the relations table from the database.
  108. *
  109. * @param integer $pk
  110. * @return void
  111. */
  112. private function deleteObjectIdentityRelations($pk)
  113. {
  114. $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk));
  115. }
  116. /**
  117. * This regenerates the ancestor table which is used for fast read access.
  118. *
  119. * @param AclInterface $acl
  120. * @return void
  121. */
  122. private function regenerateAncestorRelations(AclInterface $acl)
  123. {
  124. $pk = $acl->getId();
  125. $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk));
  126. $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $pk));
  127. $parentAcl = $acl->getParentAcl();
  128. while (null !== $parentAcl) {
  129. $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $parentAcl->getId()));
  130. $parentAcl = $parentAcl->getParentAcl();
  131. }
  132. }
  133. /**
  134. * This processes changes on an ACE related property (classFieldAces, or objectFieldAces).
  135. *
  136. * @param string $name
  137. * @param array $changes
  138. * @return void
  139. */
  140. private function updateFieldAceProperty($name, array $changes)
  141. {
  142. $sids = new \SplObjectStorage();
  143. $classIds = new \SplObjectStorage();
  144. $currentIds = array();
  145. foreach ($changes[1] as $field => $new) {
  146. for ($i=0,$c=count($new); $i<$c; $i++) {
  147. $ace = $new[$i];
  148. if (null === $ace->getId()) {
  149. if ($sids->contains($ace->getSecurityIdentity())) {
  150. $sid = $sids->offsetGet($ace->getSecurityIdentity());
  151. } else {
  152. $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity());
  153. }
  154. $oid = $ace->getAcl()->getObjectIdentity();
  155. if ($classIds->contains($oid)) {
  156. $classId = $classIds->offsetGet($oid);
  157. } else {
  158. $classId = $this->createOrRetrieveClassId($oid->getType());
  159. }
  160. $objectIdentityId = $name === 'classFieldAces' ? null : $ace->getAcl()->getId();
  161. $this->connection->executeQuery($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, $field, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
  162. $aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, $field, $i))->fetchColumn();
  163. $this->loadedAces[$aceId] = $ace;
  164. $aceIdProperty = new \ReflectionProperty('Symfony\Component\Security\Acl\Domain\Entry', 'id');
  165. $aceIdProperty->setAccessible(true);
  166. $aceIdProperty->setValue($ace, intval($aceId));
  167. } else {
  168. $currentIds[$ace->getId()] = true;
  169. }
  170. }
  171. }
  172. foreach ($changes[0] as $old) {
  173. for ($i=0,$c=count($old); $i<$c; $i++) {
  174. $ace = $old[$i];
  175. if (!isset($currentIds[$ace->getId()])) {
  176. $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId()));
  177. unset($this->loadedAces[$ace->getId()]);
  178. }
  179. }
  180. }
  181. }
  182. /**
  183. * This processes changes on an ACE related property (classAces, or objectAces).
  184. *
  185. * @param string $name
  186. * @param array $changes
  187. * @return void
  188. */
  189. private function updateAceProperty($name, array $changes)
  190. {
  191. list($old, $new) = $changes;
  192. $sids = new \SplObjectStorage();
  193. $classIds = new \SplObjectStorage();
  194. $currentIds = array();
  195. for ($i=0,$c=count($new); $i<$c; $i++) {
  196. $ace = $new[$i];
  197. if (null === $ace->getId()) {
  198. if ($sids->contains($ace->getSecurityIdentity())) {
  199. $sid = $sids->offsetGet($ace->getSecurityIdentity());
  200. } else {
  201. $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity());
  202. }
  203. $oid = $ace->getAcl()->getObjectIdentity();
  204. if ($classIds->contains($oid)) {
  205. $classId = $classIds->offsetGet($oid);
  206. } else {
  207. $classId = $this->createOrRetrieveClassId($oid->getType());
  208. }
  209. $objectIdentityId = $name === 'classAces' ? null : $ace->getAcl()->getId();
  210. $this->connection->executeQuery($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, null, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
  211. $aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, null, $i))->fetchColumn();
  212. $this->loadedAces[$aceId] = $ace;
  213. $aceIdProperty = new \ReflectionProperty($ace, 'id');
  214. $aceIdProperty->setAccessible(true);
  215. $aceIdProperty->setValue($ace, intval($aceId));
  216. } else {
  217. $currentIds[$ace->getId()] = true;
  218. }
  219. }
  220. for ($i=0,$c=count($old); $i<$c; $i++) {
  221. $ace = $old[$i];
  222. if (!isset($currentIds[$ace->getId()])) {
  223. $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId()));
  224. unset($this->loadedAces[$ace->getId()]);
  225. }
  226. }
  227. }
  228. /**
  229. * Persists the changes which were made to ACEs to the database.
  230. *
  231. * @param \SplObjectStorage $aces
  232. * @return void
  233. */
  234. private function updateAces(\SplObjectStorage $aces)
  235. {
  236. foreach ($aces as $ace) {
  237. $propertyChanges = $aces->offsetGet($ace);
  238. $sets = array();
  239. if (isset($propertyChanges['mask'])) {
  240. $sets[] = sprintf('mask = %d', $propertyChanges['mask'][1]);
  241. }
  242. if (isset($propertyChanges['strategy'])) {
  243. $sets[] = sprintf('granting_strategy = %s', $this->connection->quote($propertyChanges['strategy']));
  244. }
  245. if (isset($propertyChanges['aceOrder'])) {
  246. $sets[] = sprintf('ace_order = %d', $propertyChanges['aceOrder'][1]);
  247. }
  248. if (isset($propertyChanges['auditSuccess'])) {
  249. $sets[] = sprintf('audit_success = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditSuccess'][1]));
  250. }
  251. if (isset($propertyChanges['auditFailure'])) {
  252. $sets[] = sprintf('audit_failure = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditFailure'][1]));
  253. }
  254. $this->connection->executeQuery($this->getUpdateAccessControlEntrySql($ace->getId(), $sets));
  255. }
  256. }
  257. }