-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stable22] ensure that user and group IDs in LDAP's tables are also max 64chars #28968
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
apps/user_ldap/lib/Migration/Version1120Date20210917155206.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\User_LDAP\Migration; | ||
|
||
use Closure; | ||
use OC\Hooks\PublicEmitter; | ||
use OCP\DB\Exception; | ||
use OCP\DB\ISchemaWrapper; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\DB\Types; | ||
use OCP\IDBConnection; | ||
use OCP\IUserManager; | ||
use OCP\Migration\IOutput; | ||
use OCP\Migration\SimpleMigrationStep; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Version1120Date20210917155206 extends SimpleMigrationStep { | ||
|
||
/** @var IDBConnection */ | ||
private $dbc; | ||
/** @var IUserManager */ | ||
private $userManager; | ||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
public function __construct(IDBConnection $dbc, IUserManager $userManager, LoggerInterface $logger) { | ||
$this->dbc = $dbc; | ||
$this->userManager = $userManager; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function getName() { | ||
return 'Adjust LDAP user and group id column lengths to match server lengths'; | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
* @param array $options | ||
*/ | ||
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
// ensure that there is no user or group id longer than 64char in LDAP table | ||
$this->handleIDs('ldap_group_mapping', false); | ||
$this->handleIDs('ldap_user_mapping', true); | ||
} | ||
|
||
/** | ||
* @param IOutput $output | ||
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
* @param array $options | ||
* @return null|ISchemaWrapper | ||
*/ | ||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
/** @var ISchemaWrapper $schema */ | ||
$schema = $schemaClosure(); | ||
|
||
$changeSchema = false; | ||
foreach (['ldap_user_mapping', 'ldap_group_mapping'] as $tableName) { | ||
$table = $schema->getTable($tableName); | ||
$column = $table->getColumn('owncloud_name'); | ||
if ($column->getLength() > 64) { | ||
$column->setLength(64); | ||
$changeSchema = true; | ||
} | ||
} | ||
|
||
return $changeSchema ? $schema : null; | ||
} | ||
|
||
protected function handleIDs(string $table, bool $emitHooks) { | ||
$q = $this->getSelectQuery($table); | ||
$u = $this->getUpdateQuery($table); | ||
|
||
$r = $q->executeQuery(); | ||
while ($row = $r->fetch()) { | ||
$newId = hash('sha256', $row['owncloud_name'], false); | ||
if ($emitHooks) { | ||
$this->emitUnassign($row['owncloud_name'], true); | ||
} | ||
$u->setParameter('uuid', $row['directory_uuid']); | ||
$u->setParameter('newId', $newId); | ||
try { | ||
$u->executeStatement(); | ||
if ($emitHooks) { | ||
$this->emitUnassign($row['owncloud_name'], false); | ||
$this->emitAssign($newId); | ||
} | ||
} catch (Exception $e) { | ||
$this->logger->error('Failed to shorten owncloud_name "{oldId}" to "{newId}" (UUID: "{uuid}" of {table})', | ||
[ | ||
'app' => 'user_ldap', | ||
'oldId' => $row['owncloud_name'], | ||
'newId' => $newId, | ||
'uuid' => $row['directory_uuid'], | ||
'table' => $table, | ||
'exception' => $e, | ||
] | ||
); | ||
} | ||
} | ||
$r->closeCursor(); | ||
} | ||
|
||
protected function getSelectQuery(string $table): IQueryBuilder { | ||
$q = $this->dbc->getQueryBuilder(); | ||
$q->select('owncloud_name', 'directory_uuid') | ||
->from($table) | ||
->where($q->expr()->like('owncloud_name', $q->createNamedParameter(str_repeat('_', 65) . '%'), Types::STRING)); | ||
return $q; | ||
} | ||
|
||
protected function getUpdateQuery(string $table): IQueryBuilder { | ||
$q = $this->dbc->getQueryBuilder(); | ||
$q->update($table) | ||
->set('owncloud_name', $q->createParameter('newId')) | ||
->where($q->expr()->eq('directory_uuid', $q->createParameter('uuid'))); | ||
return $q; | ||
} | ||
|
||
protected function emitUnassign(string $oldId, bool $pre): void { | ||
if ($this->userManager instanceof PublicEmitter) { | ||
$this->userManager->emit('\OC\User', $pre ? 'pre' : 'post' . 'UnassignedUserId', [$oldId]); | ||
} | ||
} | ||
|
||
protected function emitAssign(string $newId): void { | ||
if ($this->userManager instanceof PublicEmitter) { | ||
$this->userManager->emit('\OC\User', 'assignedUserId', [$newId]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume the cursor will operate on the old result set and won't lock the pending updates, hope this works with all DBMS ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why should it not? separate queries, separate cursors.