Merge pull request #4165 from dokuwiki/bot/autofix
[dokuwiki.git] / lib / plugins / usermanager / cli.php
blob91cf0756152d86b4d5c94e43a45ec9443089b0cc
1 <?php
3 use dokuwiki\Extension\CLIPlugin;
4 use dokuwiki\Extension\AuthPlugin;
5 use splitbrain\phpcli\Options;
6 use splitbrain\phpcli\TableFormatter;
8 /**
9 * Class cli_plugin_usermanager
11 * Command Line component for the usermanager
13 * @license GPL2
14 * @author Karsten Kosmala <karsten.kosmala@gmail.com>
16 class cli_plugin_usermanager extends CLIPlugin
18 public function __construct()
20 parent::__construct();
21 auth_setup();
24 /** @inheritdoc */
25 protected function setup(Options $options)
27 // general setup
28 $options->setHelp(
29 "Manage users for this DokuWiki instance\n"
32 // list
33 $options->registerCommand('list', 'List users');
34 $options->registerOption('verbose', 'Show detailed user information', 'v', false, 'list');
36 // add
37 $options->registerCommand('add', 'Add an user to auth backend');
38 $options->registerArgument('login', 'Username', true, 'add');
39 $options->registerArgument('mail', 'Email address', true, 'add');
40 $options->registerArgument('name', 'Full name', false, 'add');
41 $options->registerArgument('groups', 'Groups to be added, comma-seperated', false, 'add');
42 $options->registerArgument('password', 'Password to set', false, 'add');
43 $options->registerOption('notify', 'Notify user', 'n', false, 'add');
45 // delete
46 $options->registerCommand('delete', 'Deletes user(s) from auth backend');
47 $options->registerArgument('name', 'Username(s), comma-seperated', true, 'delete');
49 // add to group
50 $options->registerCommand('addtogroup', 'Add user to group(s)');
51 $options->registerArgument('name', 'Username', true, 'addtogroup');
52 $options->registerArgument('group', 'Group(s), comma-seperated', true, 'addtogroup');
54 // remove from group
55 $options->registerCommand('removefromgroup', 'Remove user from group(s)');
56 $options->registerArgument('name', 'Username', true, 'removefromgroup');
57 $options->registerArgument('group', 'Group(s), comma-separated', true, 'removefromgroup');
60 /** @inheritdoc */
61 protected function main(Options $options)
63 /** @var AuthPlugin $auth */
64 global $auth;
66 if (!$auth instanceof AuthPlugin) {
67 $this->error($this->getLang('noauth'));
68 return 1;
71 switch ($options->getCmd()) {
72 case 'list':
73 $ret = $this->cmdList($options->getOpt('verbose'));
74 break;
75 case 'add':
76 $ret = $this->cmdAdd($options->getOpt('notify'), $options->getArgs());
77 break;
78 case 'delete':
79 $ret = $this->cmdDelete($options->getArgs());
80 break;
81 case 'addtogroup':
82 $ret = $this->cmdAddToGroup($options->getArgs());
83 break;
84 case 'removefromgroup':
85 $ret = $this->cmdRemoveFromGroup($options->getArgs());
86 break;
88 default:
89 echo $options->help();
90 $ret = 0;
93 exit($ret);
96 /**
97 * @param bool $showdetails
98 * @return int
100 protected function cmdList(bool $showdetails)
102 /** @var AuthPlugin $auth */
103 global $auth;
105 if (!$auth->canDo('getUsers')) {
106 $this->error($this->getLang('nosupport'));
107 return 1;
108 } else {
109 $this->listUsers($showdetails);
112 return 0;
116 * List the given users
118 * @param bool $details display details
120 protected function listUsers(bool $details = false)
122 /** @var AuthPlugin $auth */
123 global $auth;
124 $list = $auth->retrieveUsers();
126 $tr = new TableFormatter($this->colors);
128 foreach ($list as $username => $user) {
129 $content = [$username];
130 if ($details) {
131 $content[] = $user['name'];
132 $content[] = $user['mail'];
133 $content[] = implode(", ", $user['grps']);
135 echo $tr->format(
136 [15, 25, 25, 15],
137 $content
143 * Adds an user
145 * @param bool $notify display details
146 * @param array $args
147 * @return int
149 protected function cmdAdd(bool $notify, array $args)
151 /** @var AuthPlugin $auth */
152 global $auth;
154 if (!$auth->canDo('addUser')) {
155 $this->error($this->getLang('nosupport'));
156 return 1;
159 [$login, $mail, $name, $grps, $pass] = $args;
160 $grps = array_filter(array_map('trim', explode(',', $grps)));
162 if ($auth->canDo('modPass')) {
163 if (empty($pass)) {
164 if ($notify) {
165 $pass = auth_pwgen($login);
166 } else {
167 $this->error($this->getLang('add_fail'));
168 $this->error($this->getLang('addUser_error_missing_pass'));
169 return 1;
172 } elseif (!empty($pass)) {
173 $this->error($this->getLang('add_fail'));
174 $this->error($this->getLang('addUser_error_modPass_disabled'));
175 return 1;
178 if ($auth->triggerUserMod('create', [$login, $pass, $name, $mail, $grps])) {
179 $this->success($this->getLang('add_ok'));
180 } else {
181 $this->printErrorMessages();
182 $this->error($this->getLang('add_fail'));
183 $this->error($this->getLang('addUser_error_create_event_failed'));
184 return 1;
187 return 0;
191 * Deletes users
192 * @param array $args
193 * @return int
195 protected function cmdDelete(array $args)
197 /** @var AuthPlugin $auth */
198 global $auth;
200 if (!$auth->canDo('delUser')) {
201 $this->error($this->getLang('nosupport'));
202 return 1;
205 $users = explode(',', $args[0]);
206 $count = $auth->triggerUserMod('delete', [$users]);
208 if ($count != count($users)) {
209 $this->printErrorMessages();
210 $part1 = str_replace('%d', $count, $this->getLang('delete_ok'));
211 $part2 = str_replace('%d', (count($users) - $count), $this->getLang('delete_fail'));
212 $this->error("$part1, $part2");
213 return 1;
216 return 0;
220 * Adds an user to group(s)
222 * @param array $args
223 * @return int
225 protected function cmdAddToGroup(array $args)
227 /** @var AuthPlugin $auth */
228 global $auth;
230 [$name, $newgrps] = $args;
231 $newgrps = array_filter(array_map('trim', explode(',', $newgrps)));
232 $oldinfo = $auth->getUserData($name);
233 $changes = [];
235 if ($newgrps !== [] && $auth->canDo('modGroups')) {
236 $changes['grps'] = $oldinfo['grps'];
237 foreach ($newgrps as $group) {
238 if (!in_array($group, $oldinfo['grps'])) {
239 $changes['grps'][] = $group;
244 if (!empty(array_diff($changes['grps'], $oldinfo['grps']))) {
245 if ($auth->triggerUserMod('modify', [$name, $changes])) {
246 $this->success($this->getLang('update_ok'));
247 } else {
248 $this->printErrorMessages();
249 $this->error($this->getLang('update_fail'));
250 return 1;
254 return 0;
258 * Removes an user from group(s)
260 * @param array $args
261 * @return int
263 protected function cmdRemoveFromGroup(array $args)
265 /** @var AuthPlugin $auth */
266 global $auth;
268 [$name, $grps] = $args;
269 $grps = array_filter(array_map('trim', explode(',', $grps)));
270 $oldinfo = $auth->getUserData($name);
271 $changes = [];
273 if ($grps !== [] && $auth->canDo('modGroups')) {
274 $changes['grps'] = $oldinfo['grps'];
275 foreach ($grps as $group) {
276 if (($pos = array_search($group, $changes['grps'])) == !false) {
277 unset($changes['grps'][$pos]);
282 if (!empty(array_diff($oldinfo['grps'], $changes['grps']))) {
283 if ($auth->triggerUserMod('modify', [$name, $changes])) {
284 $this->success($this->getLang('update_ok'));
285 } else {
286 $this->printErrorMessages();
287 $this->error($this->getLang('update_fail'));
288 return 1;
292 return 0;
296 * Plugins triggered during user modification may cause failures and output messages via
297 * DokuWiki's msg() function
299 protected function printErrorMessages()
301 global $MSG;
302 if (isset($MSG)) {
303 foreach ($MSG as $msg) {
304 if ($msg['lvl'] === 'error') $this->error($msg['msg']);