@@ -32,25 +32,25 @@ |
||
| 32 | 32 | use OCP\Migration\SimpleMigrationStep; |
| 33 | 33 | |
| 34 | 34 | class Version1130Date20220110154719 extends SimpleMigrationStep { |
| 35 | - public function getName() { |
|
| 36 | - return 'Drop ldap_group_mapping_backup'; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param IOutput $output |
|
| 41 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | - * @param array $options |
|
| 43 | - * @return null|ISchemaWrapper |
|
| 44 | - */ |
|
| 45 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 46 | - /** @var ISchemaWrapper $schema */ |
|
| 47 | - $schema = $schemaClosure(); |
|
| 48 | - |
|
| 49 | - if ($schema->hasTable('ldap_group_mapping_backup')) { |
|
| 50 | - $schema->dropTable('ldap_group_mapping_backup'); |
|
| 51 | - return $schema; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - return null; |
|
| 55 | - } |
|
| 35 | + public function getName() { |
|
| 36 | + return 'Drop ldap_group_mapping_backup'; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param IOutput $output |
|
| 41 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | + * @param array $options |
|
| 43 | + * @return null|ISchemaWrapper |
|
| 44 | + */ |
|
| 45 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 46 | + /** @var ISchemaWrapper $schema */ |
|
| 47 | + $schema = $schemaClosure(); |
|
| 48 | + |
|
| 49 | + if ($schema->hasTable('ldap_group_mapping_backup')) { |
|
| 50 | + $schema->dropTable('ldap_group_mapping_backup'); |
|
| 51 | + return $schema; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + return null; |
|
| 55 | + } |
|
| 56 | 56 | } |
@@ -31,47 +31,47 @@ |
||
| 31 | 31 | use OCP\Migration\IOutput; |
| 32 | 32 | |
| 33 | 33 | class Version1130Date20220110154717 extends GroupMappingMigration { |
| 34 | - public function getName() { |
|
| 35 | - return 'Copy ldap_group_mapping data to backup table if needed'; |
|
| 36 | - } |
|
| 34 | + public function getName() { |
|
| 35 | + return 'Copy ldap_group_mapping data to backup table if needed'; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param IOutput $output |
|
| 40 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 41 | - * @param array $options |
|
| 42 | - * @since 13.0.0 |
|
| 43 | - */ |
|
| 44 | - public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 45 | - /** @var ISchemaWrapper $schema */ |
|
| 46 | - $schema = $schemaClosure(); |
|
| 38 | + /** |
|
| 39 | + * @param IOutput $output |
|
| 40 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 41 | + * @param array $options |
|
| 42 | + * @since 13.0.0 |
|
| 43 | + */ |
|
| 44 | + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 45 | + /** @var ISchemaWrapper $schema */ |
|
| 46 | + $schema = $schemaClosure(); |
|
| 47 | 47 | |
| 48 | - if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 49 | - // Backup table does not exist |
|
| 50 | - return; |
|
| 51 | - } |
|
| 48 | + if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 49 | + // Backup table does not exist |
|
| 50 | + return; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $output->startProgress(); |
|
| 54 | - $this->copyGroupMappingData('ldap_group_mapping', 'ldap_group_mapping_backup'); |
|
| 55 | - $output->finishProgress(); |
|
| 56 | - } |
|
| 53 | + $output->startProgress(); |
|
| 54 | + $this->copyGroupMappingData('ldap_group_mapping', 'ldap_group_mapping_backup'); |
|
| 55 | + $output->finishProgress(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param IOutput $output |
|
| 60 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 61 | - * @param array $options |
|
| 62 | - * @return null|ISchemaWrapper |
|
| 63 | - */ |
|
| 64 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 65 | - /** @var ISchemaWrapper $schema */ |
|
| 66 | - $schema = $schemaClosure(); |
|
| 58 | + /** |
|
| 59 | + * @param IOutput $output |
|
| 60 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 61 | + * @param array $options |
|
| 62 | + * @return null|ISchemaWrapper |
|
| 63 | + */ |
|
| 64 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 65 | + /** @var ISchemaWrapper $schema */ |
|
| 66 | + $schema = $schemaClosure(); |
|
| 67 | 67 | |
| 68 | - if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 69 | - // Backup table does not exist |
|
| 70 | - return null; |
|
| 71 | - } |
|
| 68 | + if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 69 | + // Backup table does not exist |
|
| 70 | + return null; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $schema->dropTable('ldap_group_mapping'); |
|
| 73 | + $schema->dropTable('ldap_group_mapping'); |
|
| 74 | 74 | |
| 75 | - return $schema; |
|
| 76 | - } |
|
| 75 | + return $schema; |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -31,39 +31,39 @@ |
||
| 31 | 31 | |
| 32 | 32 | abstract class GroupMappingMigration extends SimpleMigrationStep { |
| 33 | 33 | |
| 34 | - /** @var IDBConnection */ |
|
| 35 | - private $dbc; |
|
| 34 | + /** @var IDBConnection */ |
|
| 35 | + private $dbc; |
|
| 36 | 36 | |
| 37 | - public function __construct(IDBConnection $dbc) { |
|
| 38 | - $this->dbc = $dbc; |
|
| 39 | - } |
|
| 37 | + public function __construct(IDBConnection $dbc) { |
|
| 38 | + $this->dbc = $dbc; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { |
|
| 42 | - $insert = $this->dbc->getQueryBuilder(); |
|
| 43 | - $insert->insert($destinationTable) |
|
| 44 | - ->values([ |
|
| 45 | - 'ldap_dn' => $insert->createParameter('ldap_dn'), |
|
| 46 | - 'owncloud_name' => $insert->createParameter('owncloud_name'), |
|
| 47 | - 'directory_uuid' => $insert->createParameter('directory_uuid'), |
|
| 48 | - 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), |
|
| 49 | - ]); |
|
| 41 | + protected function copyGroupMappingData(string $sourceTable, string $destinationTable): void { |
|
| 42 | + $insert = $this->dbc->getQueryBuilder(); |
|
| 43 | + $insert->insert($destinationTable) |
|
| 44 | + ->values([ |
|
| 45 | + 'ldap_dn' => $insert->createParameter('ldap_dn'), |
|
| 46 | + 'owncloud_name' => $insert->createParameter('owncloud_name'), |
|
| 47 | + 'directory_uuid' => $insert->createParameter('directory_uuid'), |
|
| 48 | + 'ldap_dn_hash' => $insert->createParameter('ldap_dn_hash'), |
|
| 49 | + ]); |
|
| 50 | 50 | |
| 51 | - $query = $this->dbc->getQueryBuilder(); |
|
| 52 | - $query->select('*') |
|
| 53 | - ->from($sourceTable); |
|
| 51 | + $query = $this->dbc->getQueryBuilder(); |
|
| 52 | + $query->select('*') |
|
| 53 | + ->from($sourceTable); |
|
| 54 | 54 | |
| 55 | 55 | |
| 56 | - $result = $query->executeQuery(); |
|
| 57 | - while ($row = $result->fetch()) { |
|
| 58 | - $insert |
|
| 59 | - ->setParameter('ldap_dn', $row['ldap_dn']) |
|
| 60 | - ->setParameter('owncloud_name', $row['owncloud_name']) |
|
| 61 | - ->setParameter('directory_uuid', $row['directory_uuid']) |
|
| 62 | - ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) |
|
| 63 | - ; |
|
| 56 | + $result = $query->executeQuery(); |
|
| 57 | + while ($row = $result->fetch()) { |
|
| 58 | + $insert |
|
| 59 | + ->setParameter('ldap_dn', $row['ldap_dn']) |
|
| 60 | + ->setParameter('owncloud_name', $row['owncloud_name']) |
|
| 61 | + ->setParameter('directory_uuid', $row['directory_uuid']) |
|
| 62 | + ->setParameter('ldap_dn_hash', $row['ldap_dn_hash']) |
|
| 63 | + ; |
|
| 64 | 64 | |
| 65 | - $insert->executeStatement(); |
|
| 66 | - } |
|
| 67 | - $result->closeCursor(); |
|
| 68 | - } |
|
| 65 | + $insert->executeStatement(); |
|
| 66 | + } |
|
| 67 | + $result->closeCursor(); |
|
| 68 | + } |
|
| 69 | 69 | } |
@@ -32,68 +32,68 @@ |
||
| 32 | 32 | use OCP\Migration\IOutput; |
| 33 | 33 | |
| 34 | 34 | class Version1130Date20220110154718 extends GroupMappingMigration { |
| 35 | - public function getName() { |
|
| 36 | - return 'Copy ldap_group_mapping data from backup table and if needed'; |
|
| 37 | - } |
|
| 35 | + public function getName() { |
|
| 36 | + return 'Copy ldap_group_mapping data from backup table and if needed'; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param IOutput $output |
|
| 41 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | - * @param array $options |
|
| 43 | - * @return null|ISchemaWrapper |
|
| 44 | - */ |
|
| 45 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 46 | - /** @var ISchemaWrapper $schema */ |
|
| 47 | - $schema = $schemaClosure(); |
|
| 39 | + /** |
|
| 40 | + * @param IOutput $output |
|
| 41 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 42 | + * @param array $options |
|
| 43 | + * @return null|ISchemaWrapper |
|
| 44 | + */ |
|
| 45 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 46 | + /** @var ISchemaWrapper $schema */ |
|
| 47 | + $schema = $schemaClosure(); |
|
| 48 | 48 | |
| 49 | - if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 50 | - // Backup table does not exist |
|
| 51 | - return null; |
|
| 52 | - } |
|
| 49 | + if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 50 | + // Backup table does not exist |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - $table = $schema->createTable('ldap_group_mapping'); |
|
| 55 | - $table->addColumn('ldap_dn', Types::STRING, [ |
|
| 56 | - 'notnull' => true, |
|
| 57 | - 'length' => 4096, |
|
| 58 | - 'default' => '', |
|
| 59 | - ]); |
|
| 60 | - $table->addColumn('owncloud_name', Types::STRING, [ |
|
| 61 | - 'notnull' => true, |
|
| 62 | - 'length' => 64, |
|
| 63 | - 'default' => '', |
|
| 64 | - ]); |
|
| 65 | - $table->addColumn('directory_uuid', Types::STRING, [ |
|
| 66 | - 'notnull' => true, |
|
| 67 | - 'length' => 255, |
|
| 68 | - 'default' => '', |
|
| 69 | - ]); |
|
| 70 | - $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 71 | - 'notnull' => false, |
|
| 72 | - 'length' => 64, |
|
| 73 | - ]); |
|
| 74 | - $table->setPrimaryKey(['owncloud_name']); |
|
| 75 | - $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); |
|
| 76 | - $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); |
|
| 54 | + $table = $schema->createTable('ldap_group_mapping'); |
|
| 55 | + $table->addColumn('ldap_dn', Types::STRING, [ |
|
| 56 | + 'notnull' => true, |
|
| 57 | + 'length' => 4096, |
|
| 58 | + 'default' => '', |
|
| 59 | + ]); |
|
| 60 | + $table->addColumn('owncloud_name', Types::STRING, [ |
|
| 61 | + 'notnull' => true, |
|
| 62 | + 'length' => 64, |
|
| 63 | + 'default' => '', |
|
| 64 | + ]); |
|
| 65 | + $table->addColumn('directory_uuid', Types::STRING, [ |
|
| 66 | + 'notnull' => true, |
|
| 67 | + 'length' => 255, |
|
| 68 | + 'default' => '', |
|
| 69 | + ]); |
|
| 70 | + $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 71 | + 'notnull' => false, |
|
| 72 | + 'length' => 64, |
|
| 73 | + ]); |
|
| 74 | + $table->setPrimaryKey(['owncloud_name']); |
|
| 75 | + $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_group_dn_hashes'); |
|
| 76 | + $table->addUniqueIndex(['directory_uuid'], 'ldap_group_directory_uuid'); |
|
| 77 | 77 | |
| 78 | - return $schema; |
|
| 79 | - } |
|
| 78 | + return $schema; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @param IOutput $output |
|
| 83 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 84 | - * @param array $options |
|
| 85 | - */ |
|
| 86 | - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
| 87 | - /** @var ISchemaWrapper $schema */ |
|
| 88 | - $schema = $schemaClosure(); |
|
| 81 | + /** |
|
| 82 | + * @param IOutput $output |
|
| 83 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 84 | + * @param array $options |
|
| 85 | + */ |
|
| 86 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
| 87 | + /** @var ISchemaWrapper $schema */ |
|
| 88 | + $schema = $schemaClosure(); |
|
| 89 | 89 | |
| 90 | - if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 91 | - // Backup table does not exist |
|
| 92 | - return; |
|
| 93 | - } |
|
| 90 | + if (!$schema->hasTable('ldap_group_mapping_backup')) { |
|
| 91 | + // Backup table does not exist |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - $output->startProgress(); |
|
| 96 | - $this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping'); |
|
| 97 | - $output->finishProgress(); |
|
| 98 | - } |
|
| 95 | + $output->startProgress(); |
|
| 96 | + $this->copyGroupMappingData('ldap_group_mapping_backup', 'ldap_group_mapping'); |
|
| 97 | + $output->finishProgress(); |
|
| 98 | + } |
|
| 99 | 99 | } |
@@ -38,138 +38,138 @@ |
||
| 38 | 38 | |
| 39 | 39 | class Version1130Date20211102154716 extends SimpleMigrationStep { |
| 40 | 40 | |
| 41 | - /** @var IDBConnection */ |
|
| 42 | - private $dbc; |
|
| 43 | - /** @var LoggerInterface */ |
|
| 44 | - private $logger; |
|
| 45 | - |
|
| 46 | - public function __construct(IDBConnection $dbc, LoggerInterface $logger) { |
|
| 47 | - $this->dbc = $dbc; |
|
| 48 | - $this->logger = $logger; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function getName() { |
|
| 52 | - return 'Adjust LDAP user and group ldap_dn column lengths and add ldap_dn_hash columns'; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param IOutput $output |
|
| 57 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 58 | - * @param array $options |
|
| 59 | - * @return null|ISchemaWrapper |
|
| 60 | - */ |
|
| 61 | - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 62 | - /** @var ISchemaWrapper $schema */ |
|
| 63 | - $schema = $schemaClosure(); |
|
| 64 | - |
|
| 65 | - $changeSchema = false; |
|
| 66 | - foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
| 67 | - $table = $schema->getTable($tableName); |
|
| 68 | - if (!$table->hasColumn('ldap_dn_hash')) { |
|
| 69 | - $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 70 | - 'notnull' => false, |
|
| 71 | - 'length' => 64, |
|
| 72 | - ]); |
|
| 73 | - $changeSchema = true; |
|
| 74 | - } |
|
| 75 | - $column = $table->getColumn('ldap_dn'); |
|
| 76 | - if ($tableName === 'ldap_user_mapping') { |
|
| 77 | - if ($column->getLength() < 4096) { |
|
| 78 | - $column->setLength(4096); |
|
| 79 | - $changeSchema = true; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if ($table->hasIndex('ldap_dn_users')) { |
|
| 83 | - $table->dropIndex('ldap_dn_users'); |
|
| 84 | - $changeSchema = true; |
|
| 85 | - } |
|
| 86 | - if (!$table->hasIndex('ldap_user_dn_hashes')) { |
|
| 87 | - $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_user_dn_hashes'); |
|
| 88 | - $changeSchema = true; |
|
| 89 | - } |
|
| 90 | - if (!$table->hasIndex('ldap_user_directory_uuid')) { |
|
| 91 | - $table->addUniqueIndex(['directory_uuid'], 'ldap_user_directory_uuid'); |
|
| 92 | - $changeSchema = true; |
|
| 93 | - } |
|
| 94 | - } else { |
|
| 95 | - // We need to copy the table twice to be able to change primary key, prepare the backup table |
|
| 96 | - $table2 = $schema->createTable('ldap_group_mapping_backup'); |
|
| 97 | - $table2->addColumn('ldap_dn', Types::STRING, [ |
|
| 98 | - 'notnull' => true, |
|
| 99 | - 'length' => 4096, |
|
| 100 | - 'default' => '', |
|
| 101 | - ]); |
|
| 102 | - $table2->addColumn('owncloud_name', Types::STRING, [ |
|
| 103 | - 'notnull' => true, |
|
| 104 | - 'length' => 64, |
|
| 105 | - 'default' => '', |
|
| 106 | - ]); |
|
| 107 | - $table2->addColumn('directory_uuid', Types::STRING, [ |
|
| 108 | - 'notnull' => true, |
|
| 109 | - 'length' => 255, |
|
| 110 | - 'default' => '', |
|
| 111 | - ]); |
|
| 112 | - $table2->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 113 | - 'notnull' => false, |
|
| 114 | - 'length' => 64, |
|
| 115 | - ]); |
|
| 116 | - $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); |
|
| 117 | - $changeSchema = true; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return $changeSchema ? $schema : null; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @param IOutput $output |
|
| 126 | - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 127 | - * @param array $options |
|
| 128 | - */ |
|
| 129 | - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
| 130 | - $this->handleDNHashes('ldap_group_mapping'); |
|
| 131 | - $this->handleDNHashes('ldap_user_mapping'); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - protected function handleDNHashes(string $table): void { |
|
| 135 | - $select = $this->getSelectQuery($table); |
|
| 136 | - $update = $this->getUpdateQuery($table); |
|
| 137 | - |
|
| 138 | - $result = $select->executeQuery(); |
|
| 139 | - while ($row = $result->fetch()) { |
|
| 140 | - $dnHash = hash('sha256', $row['ldap_dn'], false); |
|
| 141 | - $update->setParameter('name', $row['owncloud_name']); |
|
| 142 | - $update->setParameter('dn_hash', $dnHash); |
|
| 143 | - try { |
|
| 144 | - $update->executeStatement(); |
|
| 145 | - } catch (Exception $e) { |
|
| 146 | - $this->logger->error('Failed to add hash "{dnHash}" ("{name}" of {table})', |
|
| 147 | - [ |
|
| 148 | - 'app' => 'user_ldap', |
|
| 149 | - 'name' => $row['owncloud_name'], |
|
| 150 | - 'dnHash' => $dnHash, |
|
| 151 | - 'table' => $table, |
|
| 152 | - 'exception' => $e, |
|
| 153 | - ] |
|
| 154 | - ); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - $result->closeCursor(); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - protected function getSelectQuery(string $table): IQueryBuilder { |
|
| 161 | - $qb = $this->dbc->getQueryBuilder(); |
|
| 162 | - $qb->select('owncloud_name', 'ldap_dn', 'ldap_dn_hash') |
|
| 163 | - ->from($table) |
|
| 164 | - ->where($qb->expr()->isNull('ldap_dn_hash')); |
|
| 165 | - return $qb; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - protected function getUpdateQuery(string $table): IQueryBuilder { |
|
| 169 | - $qb = $this->dbc->getQueryBuilder(); |
|
| 170 | - $qb->update($table) |
|
| 171 | - ->set('ldap_dn_hash', $qb->createParameter('dn_hash')) |
|
| 172 | - ->where($qb->expr()->eq('owncloud_name', $qb->createParameter('name'))); |
|
| 173 | - return $qb; |
|
| 174 | - } |
|
| 41 | + /** @var IDBConnection */ |
|
| 42 | + private $dbc; |
|
| 43 | + /** @var LoggerInterface */ |
|
| 44 | + private $logger; |
|
| 45 | + |
|
| 46 | + public function __construct(IDBConnection $dbc, LoggerInterface $logger) { |
|
| 47 | + $this->dbc = $dbc; |
|
| 48 | + $this->logger = $logger; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function getName() { |
|
| 52 | + return 'Adjust LDAP user and group ldap_dn column lengths and add ldap_dn_hash columns'; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param IOutput $output |
|
| 57 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 58 | + * @param array $options |
|
| 59 | + * @return null|ISchemaWrapper |
|
| 60 | + */ |
|
| 61 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
|
| 62 | + /** @var ISchemaWrapper $schema */ |
|
| 63 | + $schema = $schemaClosure(); |
|
| 64 | + |
|
| 65 | + $changeSchema = false; |
|
| 66 | + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
|
| 67 | + $table = $schema->getTable($tableName); |
|
| 68 | + if (!$table->hasColumn('ldap_dn_hash')) { |
|
| 69 | + $table->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 70 | + 'notnull' => false, |
|
| 71 | + 'length' => 64, |
|
| 72 | + ]); |
|
| 73 | + $changeSchema = true; |
|
| 74 | + } |
|
| 75 | + $column = $table->getColumn('ldap_dn'); |
|
| 76 | + if ($tableName === 'ldap_user_mapping') { |
|
| 77 | + if ($column->getLength() < 4096) { |
|
| 78 | + $column->setLength(4096); |
|
| 79 | + $changeSchema = true; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if ($table->hasIndex('ldap_dn_users')) { |
|
| 83 | + $table->dropIndex('ldap_dn_users'); |
|
| 84 | + $changeSchema = true; |
|
| 85 | + } |
|
| 86 | + if (!$table->hasIndex('ldap_user_dn_hashes')) { |
|
| 87 | + $table->addUniqueIndex(['ldap_dn_hash'], 'ldap_user_dn_hashes'); |
|
| 88 | + $changeSchema = true; |
|
| 89 | + } |
|
| 90 | + if (!$table->hasIndex('ldap_user_directory_uuid')) { |
|
| 91 | + $table->addUniqueIndex(['directory_uuid'], 'ldap_user_directory_uuid'); |
|
| 92 | + $changeSchema = true; |
|
| 93 | + } |
|
| 94 | + } else { |
|
| 95 | + // We need to copy the table twice to be able to change primary key, prepare the backup table |
|
| 96 | + $table2 = $schema->createTable('ldap_group_mapping_backup'); |
|
| 97 | + $table2->addColumn('ldap_dn', Types::STRING, [ |
|
| 98 | + 'notnull' => true, |
|
| 99 | + 'length' => 4096, |
|
| 100 | + 'default' => '', |
|
| 101 | + ]); |
|
| 102 | + $table2->addColumn('owncloud_name', Types::STRING, [ |
|
| 103 | + 'notnull' => true, |
|
| 104 | + 'length' => 64, |
|
| 105 | + 'default' => '', |
|
| 106 | + ]); |
|
| 107 | + $table2->addColumn('directory_uuid', Types::STRING, [ |
|
| 108 | + 'notnull' => true, |
|
| 109 | + 'length' => 255, |
|
| 110 | + 'default' => '', |
|
| 111 | + ]); |
|
| 112 | + $table2->addColumn('ldap_dn_hash', Types::STRING, [ |
|
| 113 | + 'notnull' => false, |
|
| 114 | + 'length' => 64, |
|
| 115 | + ]); |
|
| 116 | + $table2->setPrimaryKey(['owncloud_name'], 'lgm_backup_primary'); |
|
| 117 | + $changeSchema = true; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return $changeSchema ? $schema : null; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param IOutput $output |
|
| 126 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 127 | + * @param array $options |
|
| 128 | + */ |
|
| 129 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
|
| 130 | + $this->handleDNHashes('ldap_group_mapping'); |
|
| 131 | + $this->handleDNHashes('ldap_user_mapping'); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + protected function handleDNHashes(string $table): void { |
|
| 135 | + $select = $this->getSelectQuery($table); |
|
| 136 | + $update = $this->getUpdateQuery($table); |
|
| 137 | + |
|
| 138 | + $result = $select->executeQuery(); |
|
| 139 | + while ($row = $result->fetch()) { |
|
| 140 | + $dnHash = hash('sha256', $row['ldap_dn'], false); |
|
| 141 | + $update->setParameter('name', $row['owncloud_name']); |
|
| 142 | + $update->setParameter('dn_hash', $dnHash); |
|
| 143 | + try { |
|
| 144 | + $update->executeStatement(); |
|
| 145 | + } catch (Exception $e) { |
|
| 146 | + $this->logger->error('Failed to add hash "{dnHash}" ("{name}" of {table})', |
|
| 147 | + [ |
|
| 148 | + 'app' => 'user_ldap', |
|
| 149 | + 'name' => $row['owncloud_name'], |
|
| 150 | + 'dnHash' => $dnHash, |
|
| 151 | + 'table' => $table, |
|
| 152 | + 'exception' => $e, |
|
| 153 | + ] |
|
| 154 | + ); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + $result->closeCursor(); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + protected function getSelectQuery(string $table): IQueryBuilder { |
|
| 161 | + $qb = $this->dbc->getQueryBuilder(); |
|
| 162 | + $qb->select('owncloud_name', 'ldap_dn', 'ldap_dn_hash') |
|
| 163 | + ->from($table) |
|
| 164 | + ->where($qb->expr()->isNull('ldap_dn_hash')); |
|
| 165 | + return $qb; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + protected function getUpdateQuery(string $table): IQueryBuilder { |
|
| 169 | + $qb = $this->dbc->getQueryBuilder(); |
|
| 170 | + $qb->update($table) |
|
| 171 | + ->set('ldap_dn_hash', $qb->createParameter('dn_hash')) |
|
| 172 | + ->where($qb->expr()->eq('owncloud_name', $qb->createParameter('name'))); |
|
| 173 | + return $qb; |
|
| 174 | + } |
|
| 175 | 175 | } |