3 use dokuwiki\Extension\CLIPlugin
;
4 use dokuwiki\Extension\AuthPlugin
;
5 use splitbrain\phpcli\Options
;
6 use splitbrain\phpcli\TableFormatter
;
9 * Class cli_plugin_usermanager
11 * Command Line component for the usermanager
14 * @author Karsten Kosmala <karsten.kosmala@gmail.com>
16 class cli_plugin_usermanager
extends CLIPlugin
18 public function __construct()
20 parent
::__construct();
25 protected function setup(Options
$options)
29 "Manage users for this DokuWiki instance\n"
33 $options->registerCommand('list', 'List users');
34 $options->registerOption('verbose', 'Show detailed user information', 'v', false, 'list');
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');
46 $options->registerCommand('delete', 'Deletes user(s) from auth backend');
47 $options->registerArgument('name', 'Username(s), comma-seperated', true, 'delete');
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');
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');
61 protected function main(Options
$options)
63 /** @var AuthPlugin $auth */
66 if (!$auth instanceof AuthPlugin
) {
67 $this->error($this->getLang('noauth'));
71 switch ($options->getCmd()) {
73 $ret = $this->cmdList($options->getOpt('verbose'));
76 $ret = $this->cmdAdd($options->getOpt('notify'), $options->getArgs());
79 $ret = $this->cmdDelete($options->getArgs());
82 $ret = $this->cmdAddToGroup($options->getArgs());
84 case 'removefromgroup':
85 $ret = $this->cmdRemoveFromGroup($options->getArgs());
89 echo $options->help();
97 * @param bool $showdetails
100 protected function cmdList(bool $showdetails)
102 /** @var AuthPlugin $auth */
105 if (!$auth->canDo('getUsers')) {
106 $this->error($this->getLang('nosupport'));
109 $this->listUsers($showdetails);
116 * List the given users
118 * @param bool $details display details
120 protected function listUsers(bool $details = false)
122 /** @var AuthPlugin $auth */
124 $list = $auth->retrieveUsers();
126 $tr = new TableFormatter($this->colors
);
128 foreach ($list as $username => $user) {
129 $content = [$username];
131 $content[] = $user['name'];
132 $content[] = $user['mail'];
133 $content[] = implode(", ", $user['grps']);
145 * @param bool $notify display details
149 protected function cmdAdd(bool $notify, array $args)
151 /** @var AuthPlugin $auth */
154 if (!$auth->canDo('addUser')) {
155 $this->error($this->getLang('nosupport'));
159 [$login, $mail, $name, $grps, $pass] = $args;
160 $grps = array_filter(array_map('trim', explode(',', $grps)));
162 if ($auth->canDo('modPass')) {
165 $pass = auth_pwgen($login);
167 $this->error($this->getLang('add_fail'));
168 $this->error($this->getLang('addUser_error_missing_pass'));
172 } elseif (!empty($pass)) {
173 $this->error($this->getLang('add_fail'));
174 $this->error($this->getLang('addUser_error_modPass_disabled'));
178 if ($auth->triggerUserMod('create', [$login, $pass, $name, $mail, $grps])) {
179 $this->success($this->getLang('add_ok'));
181 $this->printErrorMessages();
182 $this->error($this->getLang('add_fail'));
183 $this->error($this->getLang('addUser_error_create_event_failed'));
195 protected function cmdDelete(array $args)
197 /** @var AuthPlugin $auth */
200 if (!$auth->canDo('delUser')) {
201 $this->error($this->getLang('nosupport'));
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");
220 * Adds an user to group(s)
225 protected function cmdAddToGroup(array $args)
227 /** @var AuthPlugin $auth */
230 [$name, $newgrps] = $args;
231 $newgrps = array_filter(array_map('trim', explode(',', $newgrps)));
232 $oldinfo = $auth->getUserData($name);
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'));
248 $this->printErrorMessages();
249 $this->error($this->getLang('update_fail'));
258 * Removes an user from group(s)
263 protected function cmdRemoveFromGroup(array $args)
265 /** @var AuthPlugin $auth */
268 [$name, $grps] = $args;
269 $grps = array_filter(array_map('trim', explode(',', $grps)));
270 $oldinfo = $auth->getUserData($name);
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'));
286 $this->printErrorMessages();
287 $this->error($this->getLang('update_fail'));
296 * Plugins triggered during user modification may cause failures and output messages via
297 * DokuWiki's msg() function
299 protected function printErrorMessages()
303 foreach ($MSG as $msg) {
304 if ($msg['lvl'] === 'error') $this->error($msg['msg']);