@@ -37,76 +37,76 @@ |
||
37 | 37 | use Symfony\Component\Console\Question\Question; |
38 | 38 | |
39 | 39 | class ResetUser extends Command { |
40 | - /** @var DeletedUsersIndex */ |
|
41 | - protected $dui; |
|
42 | - /** @var IUserManager */ |
|
43 | - private $userManager; |
|
44 | - /** @var UserPluginManager */ |
|
45 | - private $pluginManager; |
|
40 | + /** @var DeletedUsersIndex */ |
|
41 | + protected $dui; |
|
42 | + /** @var IUserManager */ |
|
43 | + private $userManager; |
|
44 | + /** @var UserPluginManager */ |
|
45 | + private $pluginManager; |
|
46 | 46 | |
47 | - public function __construct( |
|
48 | - DeletedUsersIndex $dui, |
|
49 | - IUserManager $userManager, |
|
50 | - UserPluginManager $pluginManager |
|
51 | - ) { |
|
52 | - $this->dui = $dui; |
|
53 | - $this->userManager = $userManager; |
|
54 | - $this->pluginManager = $pluginManager; |
|
55 | - parent::__construct(); |
|
56 | - } |
|
47 | + public function __construct( |
|
48 | + DeletedUsersIndex $dui, |
|
49 | + IUserManager $userManager, |
|
50 | + UserPluginManager $pluginManager |
|
51 | + ) { |
|
52 | + $this->dui = $dui; |
|
53 | + $this->userManager = $userManager; |
|
54 | + $this->pluginManager = $pluginManager; |
|
55 | + parent::__construct(); |
|
56 | + } |
|
57 | 57 | |
58 | - protected function configure() { |
|
59 | - $this |
|
60 | - ->setName('ldap:reset-user') |
|
61 | - ->setDescription('deletes an LDAP user independent of the user state') |
|
62 | - ->addArgument( |
|
63 | - 'uid', |
|
64 | - InputArgument::REQUIRED, |
|
65 | - 'the user id as used in Nextcloud' |
|
66 | - ) |
|
67 | - ->addOption( |
|
68 | - 'yes', |
|
69 | - 'y', |
|
70 | - InputOption::VALUE_NONE, |
|
71 | - 'do not ask for confirmation' |
|
72 | - ); |
|
73 | - } |
|
58 | + protected function configure() { |
|
59 | + $this |
|
60 | + ->setName('ldap:reset-user') |
|
61 | + ->setDescription('deletes an LDAP user independent of the user state') |
|
62 | + ->addArgument( |
|
63 | + 'uid', |
|
64 | + InputArgument::REQUIRED, |
|
65 | + 'the user id as used in Nextcloud' |
|
66 | + ) |
|
67 | + ->addOption( |
|
68 | + 'yes', |
|
69 | + 'y', |
|
70 | + InputOption::VALUE_NONE, |
|
71 | + 'do not ask for confirmation' |
|
72 | + ); |
|
73 | + } |
|
74 | 74 | |
75 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
76 | - try { |
|
77 | - $uid = $input->getArgument('uid'); |
|
78 | - $user = $this->userManager->get($uid); |
|
79 | - if (!$user instanceof IUser) { |
|
80 | - throw new \Exception('User not found'); |
|
81 | - } |
|
82 | - $backend = $user->getBackend(); |
|
83 | - if (!$backend instanceof User_Proxy) { |
|
84 | - throw new \Exception('The given user is not a recognized LDAP user.'); |
|
85 | - } |
|
86 | - if ($input->getOption('yes') === false) { |
|
87 | - /** @var QuestionHelper $helper */ |
|
88 | - $helper = $this->getHelper('question'); |
|
89 | - $q = new Question('Delete all local data of this user (y|N)? '); |
|
90 | - $input->setOption('yes', $helper->ask($input, $output, $q) === 'y'); |
|
91 | - } |
|
92 | - if ($input->getOption('yes') !== true) { |
|
93 | - throw new \Exception('Reset cancelled by operator'); |
|
94 | - } |
|
75 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
76 | + try { |
|
77 | + $uid = $input->getArgument('uid'); |
|
78 | + $user = $this->userManager->get($uid); |
|
79 | + if (!$user instanceof IUser) { |
|
80 | + throw new \Exception('User not found'); |
|
81 | + } |
|
82 | + $backend = $user->getBackend(); |
|
83 | + if (!$backend instanceof User_Proxy) { |
|
84 | + throw new \Exception('The given user is not a recognized LDAP user.'); |
|
85 | + } |
|
86 | + if ($input->getOption('yes') === false) { |
|
87 | + /** @var QuestionHelper $helper */ |
|
88 | + $helper = $this->getHelper('question'); |
|
89 | + $q = new Question('Delete all local data of this user (y|N)? '); |
|
90 | + $input->setOption('yes', $helper->ask($input, $output, $q) === 'y'); |
|
91 | + } |
|
92 | + if ($input->getOption('yes') !== true) { |
|
93 | + throw new \Exception('Reset cancelled by operator'); |
|
94 | + } |
|
95 | 95 | |
96 | - $this->dui->markUser($uid); |
|
97 | - $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true); |
|
98 | - if ($user->delete()) { |
|
99 | - $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
|
100 | - return 0; |
|
101 | - } |
|
102 | - } catch (\Throwable $e) { |
|
103 | - if (isset($pluginManagerSuppressed)) { |
|
104 | - $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
|
105 | - } |
|
106 | - $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
107 | - return 1; |
|
108 | - } |
|
109 | - $output->writeln('<error>Error while resetting user</error>'); |
|
110 | - return 2; |
|
111 | - } |
|
96 | + $this->dui->markUser($uid); |
|
97 | + $pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true); |
|
98 | + if ($user->delete()) { |
|
99 | + $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
|
100 | + return 0; |
|
101 | + } |
|
102 | + } catch (\Throwable $e) { |
|
103 | + if (isset($pluginManagerSuppressed)) { |
|
104 | + $this->pluginManager->setSuppressDeletion($pluginManagerSuppressed); |
|
105 | + } |
|
106 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
107 | + return 1; |
|
108 | + } |
|
109 | + $output->writeln('<error>Error while resetting user</error>'); |
|
110 | + return 2; |
|
111 | + } |
|
112 | 112 | } |
@@ -28,197 +28,197 @@ |
||
28 | 28 | use OC\User\Backend; |
29 | 29 | |
30 | 30 | class UserPluginManager { |
31 | - private $respondToActions = 0; |
|
32 | - |
|
33 | - private $which = [ |
|
34 | - Backend::CREATE_USER => null, |
|
35 | - Backend::SET_PASSWORD => null, |
|
36 | - Backend::GET_HOME => null, |
|
37 | - Backend::GET_DISPLAYNAME => null, |
|
38 | - Backend::SET_DISPLAYNAME => null, |
|
39 | - Backend::PROVIDE_AVATAR => null, |
|
40 | - Backend::COUNT_USERS => null, |
|
41 | - 'deleteUser' => null |
|
42 | - ]; |
|
43 | - |
|
44 | - /** @var bool */ |
|
45 | - private $suppressDeletion = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * @return int All implemented actions, except for 'deleteUser' |
|
49 | - */ |
|
50 | - public function getImplementedActions() { |
|
51 | - return $this->respondToActions; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Registers a user plugin that may implement some actions, overriding User_LDAP's user actions. |
|
56 | - * |
|
57 | - * @param ILDAPUserPlugin $plugin |
|
58 | - */ |
|
59 | - public function register(ILDAPUserPlugin $plugin) { |
|
60 | - $respondToActions = $plugin->respondToActions(); |
|
61 | - $this->respondToActions |= $respondToActions; |
|
62 | - |
|
63 | - foreach ($this->which as $action => $v) { |
|
64 | - if (is_int($action) && (bool)($respondToActions & $action)) { |
|
65 | - $this->which[$action] = $plugin; |
|
66 | - \OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']); |
|
67 | - } |
|
68 | - } |
|
69 | - if (method_exists($plugin,'deleteUser')) { |
|
70 | - $this->which['deleteUser'] = $plugin; |
|
71 | - \OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Signal if there is a registered plugin that implements some given actions |
|
77 | - * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function implementsActions($actions) { |
|
81 | - return ($actions & $this->respondToActions) == $actions; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Create a new user in LDAP Backend |
|
86 | - * |
|
87 | - * @param string $username The username of the user to create |
|
88 | - * @param string $password The password of the new user |
|
89 | - * @return string | false The user DN if user creation was successful. |
|
90 | - * @throws \Exception |
|
91 | - */ |
|
92 | - public function createUser($username, $password) { |
|
93 | - $plugin = $this->which[Backend::CREATE_USER]; |
|
94 | - |
|
95 | - if ($plugin) { |
|
96 | - return $plugin->createUser($username,$password); |
|
97 | - } |
|
98 | - throw new \Exception('No plugin implements createUser in this LDAP Backend.'); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Change the password of a user* |
|
103 | - * @param string $uid The username |
|
104 | - * @param string $password The new password |
|
105 | - * @return bool |
|
106 | - * @throws \Exception |
|
107 | - */ |
|
108 | - public function setPassword($uid, $password) { |
|
109 | - $plugin = $this->which[Backend::SET_PASSWORD]; |
|
110 | - |
|
111 | - if ($plugin) { |
|
112 | - return $plugin->setPassword($uid,$password); |
|
113 | - } |
|
114 | - throw new \Exception('No plugin implements setPassword in this LDAP Backend.'); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
119 | - * @param string $uid the Nextcloud user name |
|
120 | - * @return boolean either the user can or cannot |
|
121 | - * @throws \Exception |
|
122 | - */ |
|
123 | - public function canChangeAvatar($uid) { |
|
124 | - $plugin = $this->which[Backend::PROVIDE_AVATAR]; |
|
125 | - |
|
126 | - if ($plugin) { |
|
127 | - return $plugin->canChangeAvatar($uid); |
|
128 | - } |
|
129 | - throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.'); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Get the user's home directory |
|
134 | - * @param string $uid the username |
|
135 | - * @return boolean |
|
136 | - * @throws \Exception |
|
137 | - */ |
|
138 | - public function getHome($uid) { |
|
139 | - $plugin = $this->which[Backend::GET_HOME]; |
|
140 | - |
|
141 | - if ($plugin) { |
|
142 | - return $plugin->getHome($uid); |
|
143 | - } |
|
144 | - throw new \Exception('No plugin implements getHome in this LDAP Backend.'); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Get display name of the user |
|
149 | - * @param string $uid user ID of the user |
|
150 | - * @return string display name |
|
151 | - * @throws \Exception |
|
152 | - */ |
|
153 | - public function getDisplayName($uid) { |
|
154 | - $plugin = $this->which[Backend::GET_DISPLAYNAME]; |
|
155 | - |
|
156 | - if ($plugin) { |
|
157 | - return $plugin->getDisplayName($uid); |
|
158 | - } |
|
159 | - throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.'); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Set display name of the user |
|
164 | - * @param string $uid user ID of the user |
|
165 | - * @param string $displayName new user's display name |
|
166 | - * @return string display name |
|
167 | - * @throws \Exception |
|
168 | - */ |
|
169 | - public function setDisplayName($uid, $displayName) { |
|
170 | - $plugin = $this->which[Backend::SET_DISPLAYNAME]; |
|
171 | - |
|
172 | - if ($plugin) { |
|
173 | - return $plugin->setDisplayName($uid, $displayName); |
|
174 | - } |
|
175 | - throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.'); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Count the number of users |
|
180 | - * @return int|bool |
|
181 | - * @throws \Exception |
|
182 | - */ |
|
183 | - public function countUsers() { |
|
184 | - $plugin = $this->which[Backend::COUNT_USERS]; |
|
185 | - |
|
186 | - if ($plugin) { |
|
187 | - return $plugin->countUsers(); |
|
188 | - } |
|
189 | - throw new \Exception('No plugin implements countUsers in this LDAP Backend.'); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function canDeleteUser() { |
|
196 | - return !$this->suppressDeletion && $this->which['deleteUser'] !== null; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param $uid |
|
201 | - * @return bool |
|
202 | - * @throws \Exception |
|
203 | - */ |
|
204 | - public function deleteUser($uid) { |
|
205 | - $plugin = $this->which['deleteUser']; |
|
206 | - if ($plugin) { |
|
207 | - if ($this->suppressDeletion) { |
|
208 | - return false; |
|
209 | - } |
|
210 | - return $plugin->deleteUser($uid); |
|
211 | - } |
|
212 | - throw new \Exception('No plugin implements deleteUser in this LDAP Backend.'); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @param bool $value |
|
217 | - * @return bool – the value before the change |
|
218 | - */ |
|
219 | - public function setSuppressDeletion(bool $value): bool { |
|
220 | - $old = $this->suppressDeletion; |
|
221 | - $this->suppressDeletion = $value; |
|
222 | - return $old; |
|
223 | - } |
|
31 | + private $respondToActions = 0; |
|
32 | + |
|
33 | + private $which = [ |
|
34 | + Backend::CREATE_USER => null, |
|
35 | + Backend::SET_PASSWORD => null, |
|
36 | + Backend::GET_HOME => null, |
|
37 | + Backend::GET_DISPLAYNAME => null, |
|
38 | + Backend::SET_DISPLAYNAME => null, |
|
39 | + Backend::PROVIDE_AVATAR => null, |
|
40 | + Backend::COUNT_USERS => null, |
|
41 | + 'deleteUser' => null |
|
42 | + ]; |
|
43 | + |
|
44 | + /** @var bool */ |
|
45 | + private $suppressDeletion = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * @return int All implemented actions, except for 'deleteUser' |
|
49 | + */ |
|
50 | + public function getImplementedActions() { |
|
51 | + return $this->respondToActions; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Registers a user plugin that may implement some actions, overriding User_LDAP's user actions. |
|
56 | + * |
|
57 | + * @param ILDAPUserPlugin $plugin |
|
58 | + */ |
|
59 | + public function register(ILDAPUserPlugin $plugin) { |
|
60 | + $respondToActions = $plugin->respondToActions(); |
|
61 | + $this->respondToActions |= $respondToActions; |
|
62 | + |
|
63 | + foreach ($this->which as $action => $v) { |
|
64 | + if (is_int($action) && (bool)($respondToActions & $action)) { |
|
65 | + $this->which[$action] = $plugin; |
|
66 | + \OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']); |
|
67 | + } |
|
68 | + } |
|
69 | + if (method_exists($plugin,'deleteUser')) { |
|
70 | + $this->which['deleteUser'] = $plugin; |
|
71 | + \OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Signal if there is a registered plugin that implements some given actions |
|
77 | + * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function implementsActions($actions) { |
|
81 | + return ($actions & $this->respondToActions) == $actions; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Create a new user in LDAP Backend |
|
86 | + * |
|
87 | + * @param string $username The username of the user to create |
|
88 | + * @param string $password The password of the new user |
|
89 | + * @return string | false The user DN if user creation was successful. |
|
90 | + * @throws \Exception |
|
91 | + */ |
|
92 | + public function createUser($username, $password) { |
|
93 | + $plugin = $this->which[Backend::CREATE_USER]; |
|
94 | + |
|
95 | + if ($plugin) { |
|
96 | + return $plugin->createUser($username,$password); |
|
97 | + } |
|
98 | + throw new \Exception('No plugin implements createUser in this LDAP Backend.'); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Change the password of a user* |
|
103 | + * @param string $uid The username |
|
104 | + * @param string $password The new password |
|
105 | + * @return bool |
|
106 | + * @throws \Exception |
|
107 | + */ |
|
108 | + public function setPassword($uid, $password) { |
|
109 | + $plugin = $this->which[Backend::SET_PASSWORD]; |
|
110 | + |
|
111 | + if ($plugin) { |
|
112 | + return $plugin->setPassword($uid,$password); |
|
113 | + } |
|
114 | + throw new \Exception('No plugin implements setPassword in this LDAP Backend.'); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
119 | + * @param string $uid the Nextcloud user name |
|
120 | + * @return boolean either the user can or cannot |
|
121 | + * @throws \Exception |
|
122 | + */ |
|
123 | + public function canChangeAvatar($uid) { |
|
124 | + $plugin = $this->which[Backend::PROVIDE_AVATAR]; |
|
125 | + |
|
126 | + if ($plugin) { |
|
127 | + return $plugin->canChangeAvatar($uid); |
|
128 | + } |
|
129 | + throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.'); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Get the user's home directory |
|
134 | + * @param string $uid the username |
|
135 | + * @return boolean |
|
136 | + * @throws \Exception |
|
137 | + */ |
|
138 | + public function getHome($uid) { |
|
139 | + $plugin = $this->which[Backend::GET_HOME]; |
|
140 | + |
|
141 | + if ($plugin) { |
|
142 | + return $plugin->getHome($uid); |
|
143 | + } |
|
144 | + throw new \Exception('No plugin implements getHome in this LDAP Backend.'); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Get display name of the user |
|
149 | + * @param string $uid user ID of the user |
|
150 | + * @return string display name |
|
151 | + * @throws \Exception |
|
152 | + */ |
|
153 | + public function getDisplayName($uid) { |
|
154 | + $plugin = $this->which[Backend::GET_DISPLAYNAME]; |
|
155 | + |
|
156 | + if ($plugin) { |
|
157 | + return $plugin->getDisplayName($uid); |
|
158 | + } |
|
159 | + throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.'); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Set display name of the user |
|
164 | + * @param string $uid user ID of the user |
|
165 | + * @param string $displayName new user's display name |
|
166 | + * @return string display name |
|
167 | + * @throws \Exception |
|
168 | + */ |
|
169 | + public function setDisplayName($uid, $displayName) { |
|
170 | + $plugin = $this->which[Backend::SET_DISPLAYNAME]; |
|
171 | + |
|
172 | + if ($plugin) { |
|
173 | + return $plugin->setDisplayName($uid, $displayName); |
|
174 | + } |
|
175 | + throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.'); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Count the number of users |
|
180 | + * @return int|bool |
|
181 | + * @throws \Exception |
|
182 | + */ |
|
183 | + public function countUsers() { |
|
184 | + $plugin = $this->which[Backend::COUNT_USERS]; |
|
185 | + |
|
186 | + if ($plugin) { |
|
187 | + return $plugin->countUsers(); |
|
188 | + } |
|
189 | + throw new \Exception('No plugin implements countUsers in this LDAP Backend.'); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function canDeleteUser() { |
|
196 | + return !$this->suppressDeletion && $this->which['deleteUser'] !== null; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param $uid |
|
201 | + * @return bool |
|
202 | + * @throws \Exception |
|
203 | + */ |
|
204 | + public function deleteUser($uid) { |
|
205 | + $plugin = $this->which['deleteUser']; |
|
206 | + if ($plugin) { |
|
207 | + if ($this->suppressDeletion) { |
|
208 | + return false; |
|
209 | + } |
|
210 | + return $plugin->deleteUser($uid); |
|
211 | + } |
|
212 | + throw new \Exception('No plugin implements deleteUser in this LDAP Backend.'); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @param bool $value |
|
217 | + * @return bool – the value before the change |
|
218 | + */ |
|
219 | + public function setSuppressDeletion(bool $value): bool { |
|
220 | + $old = $this->suppressDeletion; |
|
221 | + $this->suppressDeletion = $value; |
|
222 | + return $old; |
|
223 | + } |
|
224 | 224 | } |