|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OCA\User_LDAP\Migration; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use OC\Hooks\PublicEmitter; |
| 9 | +use OCP\DB\Exception; |
| 10 | +use OCP\DB\ISchemaWrapper; |
| 11 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 12 | +use OCP\DB\Types; |
| 13 | +use OCP\IDBConnection; |
| 14 | +use OCP\IUserManager; |
| 15 | +use OCP\Migration\IOutput; |
| 16 | +use OCP\Migration\SimpleMigrationStep; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class Version1120Date20210917155206 extends SimpleMigrationStep { |
| 20 | + |
| 21 | + /** @var IDBConnection */ |
| 22 | + private $dbc; |
| 23 | + /** @var IUserManager */ |
| 24 | + private $userManager; |
| 25 | + /** @var LoggerInterface */ |
| 26 | + private $logger; |
| 27 | + |
| 28 | + public function __construct(IDBConnection $dbc, IUserManager $userManager, LoggerInterface $logger) { |
| 29 | + $this->dbc = $dbc; |
| 30 | + $this->userManager = $userManager; |
| 31 | + $this->logger = $logger; |
| 32 | + } |
| 33 | + |
| 34 | + public function getName() { |
| 35 | + return 'Adjust LDAP user and group id column lengths to match server lengths'; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param IOutput $output |
| 40 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 41 | + * @param array $options |
| 42 | + */ |
| 43 | + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 44 | + // ensure that there is no user or group id longer than 64char in LDAP table |
| 45 | + $this->handleIDs('ldap_group_mapping', false); |
| 46 | + $this->handleIDs('ldap_user_mapping', true); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param IOutput $output |
| 51 | + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 52 | + * @param array $options |
| 53 | + * @return null|ISchemaWrapper |
| 54 | + */ |
| 55 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 56 | + /** @var ISchemaWrapper $schema */ |
| 57 | + $schema = $schemaClosure(); |
| 58 | + |
| 59 | + $changeSchema = false; |
| 60 | + foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { |
| 61 | + $table = $schema->getTable($tableName); |
| 62 | + $column = $table->getColumn('owncloud_name'); |
| 63 | + if ($column->getLength() > 64) { |
| 64 | + $column->setLength(64); |
| 65 | + $changeSchema = true; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return $changeSchema ? $schema : null; |
| 70 | + } |
| 71 | + |
| 72 | + protected function handleIDs(string $table, bool $emitHooks) { |
| 73 | + $q = $this->getSelectQuery($table); |
| 74 | + $u = $this->getUpdateQuery($table); |
| 75 | + |
| 76 | + $r = $q->executeQuery(); |
| 77 | + while ($row = $r->fetch()) { |
| 78 | + $newId = hash('sha256', $row['owncloud_name'], false); |
| 79 | + if ($emitHooks) { |
| 80 | + $this->emitUnassign($row['owncloud_name'], true); |
| 81 | + } |
| 82 | + $u->setParameter('uuid', $row['directory_uuid']); |
| 83 | + $u->setParameter('newId', $newId); |
| 84 | + try { |
| 85 | + $u->executeStatement(); |
| 86 | + if ($emitHooks) { |
| 87 | + $this->emitUnassign($row['owncloud_name'], false); |
| 88 | + $this->emitAssign($newId); |
| 89 | + } |
| 90 | + } catch (Exception $e) { |
| 91 | + $this->logger->error('Failed to shorten owncloud_name "{oldId}" to "{newId}" (UUID: "{uuid}" of {table})', |
| 92 | + [ |
| 93 | + 'app' => 'user_ldap', |
| 94 | + 'oldId' => $row['owncloud_name'], |
| 95 | + 'newId' => $newId, |
| 96 | + 'uuid' => $row['directory_uuid'], |
| 97 | + 'table' => $table, |
| 98 | + 'exception' => $e, |
| 99 | + ] |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + $r->closeCursor(); |
| 104 | + } |
| 105 | + |
| 106 | + protected function getSelectQuery(string $table): IQueryBuilder { |
| 107 | + $q = $this->dbc->getQueryBuilder(); |
| 108 | + $q->select('owncloud_name', 'directory_uuid') |
| 109 | + ->from($table) |
| 110 | + ->where($q->expr()->like('owncloud_name', $q->createNamedParameter(str_repeat('_', 65) . '%'), Types::STRING)); |
| 111 | + return $q; |
| 112 | + } |
| 113 | + |
| 114 | + protected function getUpdateQuery(string $table): IQueryBuilder { |
| 115 | + $q = $this->dbc->getQueryBuilder(); |
| 116 | + $q->update($table) |
| 117 | + ->set('owncloud_name', $q->createParameter('newId')) |
| 118 | + ->where($q->expr()->eq('directory_uuid', $q->createParameter('uuid'))); |
| 119 | + return $q; |
| 120 | + } |
| 121 | + |
| 122 | + protected function emitUnassign(string $oldId, bool $pre): void { |
| 123 | + if ($this->userManager instanceof PublicEmitter) { |
| 124 | + $this->userManager->emit('\OC\User', $pre ? 'pre' : 'post' . 'UnassignedUserId', [$oldId]); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + protected function emitAssign(string $newId): void { |
| 129 | + if ($this->userManager instanceof PublicEmitter) { |
| 130 | + $this->userManager->emit('\OC\User', 'assignedUserId', [$newId]); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments