-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathRememberLocalGroupsForPotentialMigrations.php
More file actions
80 lines (70 loc) · 1.87 KB
/
RememberLocalGroupsForPotentialMigrations.php
File metadata and controls
80 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\User_SAML\Migration;
use OC\Group\Database;
use OCA\User_SAML\GroupManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use UnexpectedValueException;
use function json_encode;
class RememberLocalGroupsForPotentialMigrations implements IRepairStep {
public function __construct(
private IGroupManager $groupManager,
private IConfig $config,
) {
}
public function getName(): string {
return 'Remember local groups that might belong to SAML';
}
/**
* Run repair step.
* Must throw exception on error.
*
* @param IOutput $output
* @throws \Exception in case of failure
* @since 9.1.0
*/
public function run(IOutput $output) {
$candidateInfo = $this->config->getAppValue('user_saml', GroupManager::LOCAL_GROUPS_CHECK_FOR_MIGRATION, '');
if ($candidateInfo !== '') {
return;
}
try {
$backend = $this->findBackend();
$groupIds = $this->findGroupIds($backend);
} catch (UnexpectedValueException $e) {
return;
}
$this->config->setAppValue(
'user_saml',
GroupManager::LOCAL_GROUPS_CHECK_FOR_MIGRATION,
json_encode([
'dropAfter' => time() + 86400 * 60, // 60 days
'groups' => $groupIds
])
);
}
protected function findGroupIds(Database $backend): array {
$groupIds = $backend->getGroups();
$adminGroupIndex = array_search('admin', $groupIds, true);
if ($adminGroupIndex !== false) {
unset($groupIds[$adminGroupIndex]);
}
return $groupIds;
}
protected function findBackend(): Database {
$groupBackends = $this->groupManager->getBackends();
foreach ($groupBackends as $backend) {
if ($backend instanceof Database) {
return $backend;
}
}
throw new UnexpectedValueException();
}
}