Controller.php 11 KB

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