vendor/symfony/security-acl/Dbal/Schema.php line 34

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