3 namespace Doctrine\Tests\Common\Annotations\Fixtures;
5 use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Template;
6 use Doctrine\Tests\Common\Annotations\Fixtures\Annotation\Route;
9 * @Route("/someprefix")
10 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
15 * @Route("/", name="_demo")
18 public function indexAction()
24 * @Route("/hello/{name}", name="_demo_hello")
27 public function helloAction($name)
29 return array('name' => $name);
33 * @Route("/contact", name="_demo_contact")
36 public function contactAction()
38 $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'));
44 $this->get('session')->setFlash('notice', 'Message sent!');
46 return new RedirectResponse($this->generateUrl('_demo'));
49 return array('form' => $form);
53 * Creates the ACL for the passed object identity
55 * @param ObjectIdentityInterface $oid
58 private function createObjectIdentity(ObjectIdentityInterface $oid)
60 $classId = $this->createOrRetrieveClassId($oid->getType());
62 $this->connection->executeQuery($this->getInsertObjectIdentitySql($oid->getIdentifier(), $classId, true));
66 * Returns the primary key for the passed class type.
68 * If the type does not yet exist in the database, it will be created.
70 * @param string $classType
73 private function createOrRetrieveClassId($classType)
75 if (false !== $id = $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn()) {
79 $this->connection->executeQuery($this->getInsertClassSql($classType));
81 return $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn();
85 * Returns the primary key for the passed security identity.
87 * If the security identity does not yet exist in the database, it will be
90 * @param SecurityIdentityInterface $sid
93 private function createOrRetrieveSecurityIdentityId(SecurityIdentityInterface $sid)
95 if (false !== $id = $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn()) {
99 $this->connection->executeQuery($this->getInsertSecurityIdentitySql($sid));
101 return $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn();
105 * Deletes all ACEs for the given object identity primary key.
107 * @param integer $oidPK
110 private function deleteAccessControlEntries($oidPK)
112 $this->connection->executeQuery($this->getDeleteAccessControlEntriesSql($oidPK));
116 * Deletes the object identity from the database.
121 private function deleteObjectIdentity($pk)
123 $this->connection->executeQuery($this->getDeleteObjectIdentitySql($pk));
127 * Deletes all entries from the relations table from the database.
132 private function deleteObjectIdentityRelations($pk)
134 $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk));
138 * This regenerates the ancestor table which is used for fast read access.
140 * @param AclInterface $acl
143 private function regenerateAncestorRelations(AclInterface $acl)
146 $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk));
147 $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $pk));
149 $parentAcl = $acl->getParentAcl();
150 while (null !== $parentAcl) {
151 $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $parentAcl->getId()));
153 $parentAcl = $parentAcl->getParentAcl();
158 * This processes changes on an ACE related property (classFieldAces, or objectFieldAces).
160 * @param string $name
161 * @param array $changes
164 private function updateFieldAceProperty($name, array $changes)
166 $sids = new \SplObjectStorage();
167 $classIds = new \SplObjectStorage();
168 $currentIds = array();
169 foreach ($changes[1] as $field => $new) {
170 for ($i=0,$c=count($new); $i<$c; $i++) {
173 if (null === $ace->getId()) {
174 if ($sids->contains($ace->getSecurityIdentity())) {
175 $sid = $sids->offsetGet($ace->getSecurityIdentity());
177 $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity());
180 $oid = $ace->getAcl()->getObjectIdentity();
181 if ($classIds->contains($oid)) {
182 $classId = $classIds->offsetGet($oid);
184 $classId = $this->createOrRetrieveClassId($oid->getType());
187 $objectIdentityId = $name === 'classFieldAces' ? null : $ace->getAcl()->getId();
189 $this->connection->executeQuery($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, $field, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
190 $aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, $field, $i))->fetchColumn();
191 $this->loadedAces[$aceId] = $ace;
193 $aceIdProperty = new \ReflectionProperty('Symfony\Component\Security\Acl\Domain\Entry', 'id');
194 $aceIdProperty->setAccessible(true);
195 $aceIdProperty->setValue($ace, intval($aceId));
197 $currentIds[$ace->getId()] = true;
202 foreach ($changes[0] as $old) {
203 for ($i=0,$c=count($old); $i<$c; $i++) {
206 if (!isset($currentIds[$ace->getId()])) {
207 $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId()));
208 unset($this->loadedAces[$ace->getId()]);
215 * This processes changes on an ACE related property (classAces, or objectAces).
217 * @param string $name
218 * @param array $changes
221 private function updateAceProperty($name, array $changes)
223 list($old, $new) = $changes;
225 $sids = new \SplObjectStorage();
226 $classIds = new \SplObjectStorage();
227 $currentIds = array();
228 for ($i=0,$c=count($new); $i<$c; $i++) {
231 if (null === $ace->getId()) {
232 if ($sids->contains($ace->getSecurityIdentity())) {
233 $sid = $sids->offsetGet($ace->getSecurityIdentity());
235 $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity());
238 $oid = $ace->getAcl()->getObjectIdentity();
239 if ($classIds->contains($oid)) {
240 $classId = $classIds->offsetGet($oid);
242 $classId = $this->createOrRetrieveClassId($oid->getType());
245 $objectIdentityId = $name === 'classAces' ? null : $ace->getAcl()->getId();
247 $this->connection->executeQuery($this->getInsertAccessControlEntrySql($classId, $objectIdentityId, null, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure()));
248 $aceId = $this->connection->executeQuery($this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, null, $i))->fetchColumn();
249 $this->loadedAces[$aceId] = $ace;
251 $aceIdProperty = new \ReflectionProperty($ace, 'id');
252 $aceIdProperty->setAccessible(true);
253 $aceIdProperty->setValue($ace, intval($aceId));
255 $currentIds[$ace->getId()] = true;
259 for ($i=0,$c=count($old); $i<$c; $i++) {
262 if (!isset($currentIds[$ace->getId()])) {
263 $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId()));
264 unset($this->loadedAces[$ace->getId()]);
270 * Persists the changes which were made to ACEs to the database.
272 * @param \SplObjectStorage $aces
275 private function updateAces(\SplObjectStorage $aces)
277 foreach ($aces as $ace) {
278 $propertyChanges = $aces->offsetGet($ace);
281 if (isset($propertyChanges['mask'])) {
282 $sets[] = sprintf('mask = %d', $propertyChanges['mask'][1]);
284 if (isset($propertyChanges['strategy'])) {
285 $sets[] = sprintf('granting_strategy = %s', $this->connection->quote($propertyChanges['strategy']));
287 if (isset($propertyChanges['aceOrder'])) {
288 $sets[] = sprintf('ace_order = %d', $propertyChanges['aceOrder'][1]);
290 if (isset($propertyChanges['auditSuccess'])) {
291 $sets[] = sprintf('audit_success = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditSuccess'][1]));
293 if (isset($propertyChanges['auditFailure'])) {
294 $sets[] = sprintf('audit_failure = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditFailure'][1]));
297 $this->connection->executeQuery($this->getUpdateAccessControlEntrySql($ace->getId(), $sets));