|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Acl\Dbal; |
| 13 | + |
| 14 | +use Doctrine\DBAL\Schema\Schema as BaseSchema; |
| 15 | +use Doctrine\DBAL\Connection; |
| 16 | + |
| 17 | +/** |
| 18 | + * The schema used for the ACL system. |
| 19 | + * |
| 20 | + * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
| 21 | + */ |
| 22 | +final class Schema extends BaseSchema |
| 23 | +{ |
| 24 | + protected $options; |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructor |
| 28 | + * |
| 29 | + * @param array $options the names for tables |
| 30 | + * @param Connection $connection |
| 31 | + */ |
| 32 | + public function __construct(array $options, Connection $connection = null) |
| 33 | + { |
| 34 | + $schemaConfig = null === $connection ? null : $connection->getSchemaManager()->createSchemaConfig(); |
| 35 | + |
| 36 | + parent::__construct(array(), array(), $schemaConfig); |
| 37 | + |
| 38 | + $this->options = $options; |
| 39 | + |
| 40 | + $this->addClassTable(); |
| 41 | + $this->addSecurityIdentitiesTable(); |
| 42 | + $this->addObjectIdentitiesTable(); |
| 43 | + $this->addObjectIdentityAncestorsTable(); |
| 44 | + $this->addEntryTable(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Merges ACL schema with the given schema. |
| 49 | + * |
| 50 | + * @param BaseSchema $schema |
| 51 | + */ |
| 52 | + public function addToSchema(BaseSchema $schema) |
| 53 | + { |
| 54 | + foreach ($this->getTables() as $table) { |
| 55 | + $schema->_addTable($table); |
| 56 | + } |
| 57 | + |
| 58 | + foreach ($this->getSequences() as $sequence) { |
| 59 | + $schema->_addSequence($sequence); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds the class table to the schema |
| 65 | + */ |
| 66 | + protected function addClassTable() |
| 67 | + { |
| 68 | + $table = $this->createTable($this->options['class_table_name']); |
| 69 | + $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => 'auto')); |
| 70 | + $table->addColumn('class_type', 'string', array('length' => 200)); |
| 71 | + $table->setPrimaryKey(array('id')); |
| 72 | + $table->addUniqueIndex(array('class_type')); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Adds the entry table to the schema |
| 77 | + */ |
| 78 | + protected function addEntryTable() |
| 79 | + { |
| 80 | + $table = $this->createTable($this->options['entry_table_name']); |
| 81 | + |
| 82 | + $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => 'auto')); |
| 83 | + $table->addColumn('class_id', 'integer', array('unsigned' => true)); |
| 84 | + $table->addColumn('object_identity_id', 'integer', array('unsigned' => true, 'notnull' => false)); |
| 85 | + $table->addColumn('field_name', 'string', array('length' => 50, 'notnull' => false)); |
| 86 | + $table->addColumn('ace_order', 'smallint', array('unsigned' => true)); |
| 87 | + $table->addColumn('security_identity_id', 'integer', array('unsigned' => true)); |
| 88 | + $table->addColumn('mask', 'integer'); |
| 89 | + $table->addColumn('granting', 'boolean'); |
| 90 | + $table->addColumn('granting_strategy', 'string', array('length' => 30)); |
| 91 | + $table->addColumn('audit_success', 'boolean'); |
| 92 | + $table->addColumn('audit_failure', 'boolean'); |
| 93 | + |
| 94 | + $table->setPrimaryKey(array('id')); |
| 95 | + $table->addUniqueIndex(array('class_id', 'object_identity_id', 'field_name', 'ace_order')); |
| 96 | + $table->addIndex(array('class_id', 'object_identity_id', 'security_identity_id')); |
| 97 | + |
| 98 | + $table->addForeignKeyConstraint($this->getTable($this->options['class_table_name']), array('class_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); |
| 99 | + $table->addForeignKeyConstraint($this->getTable($this->options['oid_table_name']), array('object_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); |
| 100 | + $table->addForeignKeyConstraint($this->getTable($this->options['sid_table_name']), array('security_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Adds the object identity table to the schema |
| 105 | + */ |
| 106 | + protected function addObjectIdentitiesTable() |
| 107 | + { |
| 108 | + $table = $this->createTable($this->options['oid_table_name']); |
| 109 | + |
| 110 | + $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => 'auto')); |
| 111 | + $table->addColumn('class_id', 'integer', array('unsigned' => true)); |
| 112 | + $table->addColumn('object_identifier', 'string', array('length' => 100)); |
| 113 | + $table->addColumn('parent_object_identity_id', 'integer', array('unsigned' => true, 'notnull' => false)); |
| 114 | + $table->addColumn('entries_inheriting', 'boolean'); |
| 115 | + |
| 116 | + $table->setPrimaryKey(array('id')); |
| 117 | + $table->addUniqueIndex(array('object_identifier', 'class_id')); |
| 118 | + $table->addIndex(array('parent_object_identity_id')); |
| 119 | + |
| 120 | + $table->addForeignKeyConstraint($table, array('parent_object_identity_id'), array('id')); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Adds the object identity relation table to the schema |
| 125 | + */ |
| 126 | + protected function addObjectIdentityAncestorsTable() |
| 127 | + { |
| 128 | + $table = $this->createTable($this->options['oid_ancestors_table_name']); |
| 129 | + |
| 130 | + $table->addColumn('object_identity_id', 'integer', array('unsigned' => true)); |
| 131 | + $table->addColumn('ancestor_id', 'integer', array('unsigned' => true)); |
| 132 | + |
| 133 | + $table->setPrimaryKey(array('object_identity_id', 'ancestor_id')); |
| 134 | + |
| 135 | + $oidTable = $this->getTable($this->options['oid_table_name']); |
| 136 | + $table->addForeignKeyConstraint($oidTable, array('object_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); |
| 137 | + $table->addForeignKeyConstraint($oidTable, array('ancestor_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Adds the security identity table to the schema |
| 142 | + */ |
| 143 | + protected function addSecurityIdentitiesTable() |
| 144 | + { |
| 145 | + $table = $this->createTable($this->options['sid_table_name']); |
| 146 | + |
| 147 | + $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => 'auto')); |
| 148 | + $table->addColumn('identifier', 'string', array('length' => 200)); |
| 149 | + $table->addColumn('username', 'boolean'); |
| 150 | + |
| 151 | + $table->setPrimaryKey(array('id')); |
| 152 | + $table->addUniqueIndex(array('identifier', 'username')); |
| 153 | + } |
| 154 | +} |
0 commit comments