-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathWizardController.php
More file actions
162 lines (154 loc) · 5.12 KB
/
Copy pathWizardController.php
File metadata and controls
162 lines (154 loc) · 5.12 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\User_LDAP\Controller;
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ConnectionFactory;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\Settings\Admin;
use OCA\User_LDAP\WizardFactory;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\OCSController;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Server;
use OCP\User\Events\BeforeUserIdUnassignedEvent;
use OCP\User\Events\UserIdUnassignedEvent;
use Psr\Log\LoggerInterface;
class WizardController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private LoggerInterface $logger,
private ConnectionFactory $connectionFactory,
private IL10N $l,
private WizardFactory $wizardFactory,
private IEventDispatcher $eventDispatcher,
) {
parent::__construct($appName, $request);
}
/**
* Run a wizard action and returns the result
*
* @param string $configID ID of the LDAP configuration
* @param string $wizardAction Wizard action to run
* @param ?string $loginName Login name to test for testLoginName action
* @return DataResponse<Http::STATUS_OK, array{changes:array<string,int|string|list<int>|list<string>>,options?:array<string,list<string>>}, array{}>
* @throws OCSException
*
* 200: Wizard action result
*/
#[AuthorizedAdminSetting(settings: Admin::class)]
#[ApiRoute(verb: 'POST', url: '/api/v1/wizard/{configID}/{wizardAction}')]
public function action(
string $configID, string $wizardAction,
?string $loginName = null,
) {
try {
$wizard = $this->wizardFactory->get($configID);
switch ($wizardAction) {
case 'guessPortAndTLS':
case 'guessBaseDN':
case 'detectEmailAttribute':
case 'detectUserDisplayNameAttribute':
case 'determineGroupMemberAssoc':
case 'determineUserObjectClasses':
case 'determineGroupObjectClasses':
case 'determineGroupsForUsers':
case 'determineGroupsForGroups':
case 'determineAttributes':
case 'getUserListFilter':
case 'getUserLoginFilter':
case 'getGroupFilter':
case 'countUsers':
case 'countGroups':
case 'countInBaseDN':
try {
$result = $wizard->$wizardAction();
if ($result !== false) {
return new DataResponse($result->getResultArray());
}
} catch (\Exception $e) {
throw new OCSException($e->getMessage());
}
throw new OCSException();
case 'testLoginName':
try {
if ($loginName === null || $loginName === '') {
throw new OCSException('No login name passed');
}
$result = $wizard->$wizardAction($loginName);
if ($result !== false) {
return new DataResponse($result->getResultArray());
}
} catch (\Exception $e) {
throw new OCSException($e->getMessage());
}
throw new OCSException();
default:
throw new OCSException('Action ' . $wizardAction . 'does not exist');
break;
}
} catch (OCSException $e) {
throw $e;
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException('An issue occurred.');
}
}
/**
* Clear user or group mappings
*
* @param 'user'|'group' $subject Whether to clear group or user mappings
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
* @throws OCSException
*
* 200: Clearing was done successfuly
*/
#[AuthorizedAdminSetting(settings: Admin::class)]
#[ApiRoute(verb: 'POST', url: '/api/v1/wizard/clearMappings')]
public function clearMappings(
string $subject,
) {
$mapping = null;
try {
if ($subject === 'user') {
$mapping = Server::get(UserMapping::class);
$result = $mapping->clearCb(
function (string $uid): void {
$this->eventDispatcher->dispatchTyped(new BeforeUserIdUnassignedEvent($uid));
/** @psalm-suppress UndefinedInterfaceMethod For now we have to emit, will be removed when all hooks are removed */
Server::get(IUserManager::class)->emit('\OC\User', 'preUnassignedUserId', [$uid]);
},
function (string $uid): void {
$this->eventDispatcher->dispatchTyped(new UserIdUnassignedEvent($uid));
/** @psalm-suppress UndefinedInterfaceMethod For now we have to emit, will be removed when all hooks are removed */
Server::get(IUserManager::class)->emit('\OC\User', 'postUnassignedUserId', [$uid]);
}
);
} elseif ($subject === 'group') {
$mapping = Server::get(GroupMapping::class);
$result = $mapping->clear();
} else {
throw new OCSException('Unsupported subject ' . $subject);
}
if (!$result) {
throw new OCSException($this->l->t('Failed to clear the mappings.'));
}
return new DataResponse();
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
throw new OCSException('An issue occurred.');
}
}
}