Schema.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\Schema\Schema as BaseSchema;
  12. use Doctrine\DBAL\Connection;
  13. /**
  14. * The schema used for the ACL system.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. final class Schema extends BaseSchema
  19. {
  20. protected $options;
  21. /**
  22. * Constructor.
  23. *
  24. * @param array $options the names for tables
  25. * @param Connection $connection
  26. */
  27. public function __construct(array $options, Connection $connection = null)
  28. {
  29. $schemaConfig = null === $connection ? null : $connection->getSchemaManager()->createSchemaConfig();
  30. parent::__construct(array(), array(), $schemaConfig);
  31. $this->options = $options;
  32. $this->addClassTable();
  33. $this->addSecurityIdentitiesTable();
  34. $this->addObjectIdentitiesTable();
  35. $this->addObjectIdentityAncestorsTable();
  36. $this->addEntryTable();
  37. }
  38. /**
  39. * Merges ACL schema with the given schema.
  40. *
  41. * @param BaseSchema $schema
  42. */
  43. public function addToSchema(BaseSchema $schema)
  44. {
  45. foreach ($this->getTables() as $table) {
  46. $schema->_addTable($table);
  47. }
  48. foreach ($this->getSequences() as $sequence) {
  49. $schema->_addSequence($sequence);
  50. }
  51. }
  52. /**
  53. * Adds the class table to the schema.
  54. */
  55. protected function addClassTable()
  56. {
  57. $table = $this->createTable($this->options['class_table_name']);
  58. $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
  59. $table->addColumn('class_type', 'string', array('length' => 200));
  60. $table->setPrimaryKey(array('id'));
  61. $table->addUniqueIndex(array('class_type'));
  62. }
  63. /**
  64. * Adds the entry table to the schema.
  65. */
  66. protected function addEntryTable()
  67. {
  68. $table = $this->createTable($this->options['entry_table_name']);
  69. $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
  70. $table->addColumn('class_id', 'integer', array('unsigned' => true));
  71. $table->addColumn('object_identity_id', 'integer', array('unsigned' => true, 'notnull' => false));
  72. $table->addColumn('field_name', 'string', array('length' => 50, 'notnull' => false));
  73. $table->addColumn('ace_order', 'smallint', array('unsigned' => true));
  74. $table->addColumn('security_identity_id', 'integer', array('unsigned' => true));
  75. $table->addColumn('mask', 'integer');
  76. $table->addColumn('granting', 'boolean');
  77. $table->addColumn('granting_strategy', 'string', array('length' => 30));
  78. $table->addColumn('audit_success', 'boolean');
  79. $table->addColumn('audit_failure', 'boolean');
  80. $table->setPrimaryKey(array('id'));
  81. $table->addUniqueIndex(array('class_id', 'object_identity_id', 'field_name', 'ace_order'));
  82. $table->addIndex(array('class_id', 'object_identity_id', 'security_identity_id'));
  83. $table->addForeignKeyConstraint($this->getTable($this->options['class_table_name']), array('class_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'));
  84. $table->addForeignKeyConstraint($this->getTable($this->options['oid_table_name']), array('object_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'));
  85. $table->addForeignKeyConstraint($this->getTable($this->options['sid_table_name']), array('security_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'));
  86. }
  87. /**
  88. * Adds the object identity table to the schema.
  89. */
  90. protected function addObjectIdentitiesTable()
  91. {
  92. $table = $this->createTable($this->options['oid_table_name']);
  93. $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
  94. $table->addColumn('class_id', 'integer', array('unsigned' => true));
  95. $table->addColumn('object_identifier', 'string', array('length' => 100));
  96. $table->addColumn('parent_object_identity_id', 'integer', array('unsigned' => true, 'notnull' => false));
  97. $table->addColumn('entries_inheriting', 'boolean');
  98. $table->setPrimaryKey(array('id'));
  99. $table->addUniqueIndex(array('object_identifier', 'class_id'));
  100. $table->addIndex(array('parent_object_identity_id'));
  101. $table->addForeignKeyConstraint($table, array('parent_object_identity_id'), array('id'));
  102. }
  103. /**
  104. * Adds the object identity relation table to the schema.
  105. */
  106. protected function addObjectIdentityAncestorsTable()
  107. {
  108. $table = $this->createTable($this->options['oid_ancestors_table_name']);
  109. $table->addColumn('object_identity_id', 'integer', array('unsigned' => true));
  110. $table->addColumn('ancestor_id', 'integer', array('unsigned' => true));
  111. $table->setPrimaryKey(array('object_identity_id', 'ancestor_id'));
  112. $oidTable = $this->getTable($this->options['oid_table_name']);
  113. $table->addForeignKeyConstraint($oidTable, array('object_identity_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'));
  114. $table->addForeignKeyConstraint($oidTable, array('ancestor_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE'));
  115. }
  116. /**
  117. * Adds the security identity table to the schema.
  118. */
  119. protected function addSecurityIdentitiesTable()
  120. {
  121. $table = $this->createTable($this->options['sid_table_name']);
  122. $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
  123. $table->addColumn('identifier', 'string', array('length' => 200));
  124. $table->addColumn('username', 'boolean');
  125. $table->setPrimaryKey(array('id'));
  126. $table->addUniqueIndex(array('identifier', 'username'));
  127. }
  128. }