Completed
Pull Request — master (#3240)
by
unknown
03:43
created

cli_plugin_usermanager::cmdAdd()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 9
nop 2
dl 0
loc 36
rs 8.4106
c 0
b 0
f 0
1
<?php
2
3
use splitbrain\phpcli\TableFormatter;
4
5
/**
6
 * Class cli_plugin_usermanager
7
 *
8
 * Command Line component for the usermanager
9
 *
10
 * @license GPL2
11
 * @author Karsten Kosmala <[email protected]>
12
 */
13
class cli_plugin_usermanager extends DokuWiki_CLI_Plugin
14
{
15
    protected $auth = null;        // auth object
16
17
    public function __construct()
18
    {
19
        parent::__construct();
20
        global $auth;
21
22
        /** @var DokuWiki_Auth_Plugin $auth */
23
        auth_setup();
24
        $this->auth = $auth;
25
26
        $this->setupLocale();
27
    }
28
29
    /** @inheritdoc */
30
    protected function setup(\splitbrain\phpcli\Options $options)
31
    {
32
        // general setup
33
        $options->setHelp(
34
            "Manage users for this DokuWiki instance\n\n"
35
        );
36
37
        // list
38
        $options->registerCommand('list', 'List users');
39
        $options->registerOption('verbose', 'Show detailed user information', 'v', false, 'list');
40
41
        // add
42
        $options->registerCommand('add', 'Add an user to auth backend');
43
        $options->registerArgument('name', 'Username', true, 'add');
44
        $options->registerArgument('mail', 'mail address', true, 'add');
45
        $options->registerArgument('full_name', 'Full name', false, 'add');
46
        $options->registerArgument('groups', 'groups to be added', false, 'add');
47
        $options->registerArgument('password', 'password of user', false, 'add');
48
        $options->registerOption('notify', 'notify user', 'n', false, 'add');
49
50
        // delete
51
        $options->registerCommand('delete', 'Delete user from auth backend');
52
        $options->registerArgument('name', 'Username', true, 'delete');
53
    }
54
55
    /** @inheritdoc */
56
    protected function main(\splitbrain\phpcli\Options $options)
57
    {
58
        switch ($options->getCmd()) {
59
            case 'list':
60
                $ret = $this->cmdList($options->getOpt('verbose'));
61
                break;
62
            case 'add':
63
                $ret = $this->cmdAdd($options->getOpt('notify'), $options->getArgs());
64
                break;
65
            case 'delete':
66
                $ret = $this->cmdDelete($options->getArgs());
67
                break;
68
69
            default:
70
                echo $options->help();
71
                $ret = 0;
72
        }
73
74
        exit($ret);
75
    }
76
77
    /**
78
     * @param bool $showdetails
79
     * @return int
80
     * @throws \splitbrain\phpcli\Exception
81
     */
82
    protected function cmdList($showdetails)
83
    {
84
        if (!$this->auth->canDo('getUsers')) echo 'Authentication backend not available';
85
86
        $list = $this->getUsers();
87
        $this->listUsers($list, $showdetails);
88
89
        return 0;
90
    }
91
92
    /**
93
     * Get all users
94
     *
95
     * @return array
96
     */
97
    protected function getUsers()
98
    {
99
        return $this->auth->retrieveUsers();
100
    }
101
102
    /**
103
     * List the given users
104
     *
105
     * @param string[] list display details
106
     * @param bool $details display details
107
     * @throws \splitbrain\phpcli\Exception
108
     */
109
    protected function listUsers($list, bool $details = False)
110
    {
111
        $tr = new TableFormatter($this->colors);
112
113
        foreach ($list as $username => $user) {
114
            $content = [$username];
115
            if ($details) {
116
                array_push($content, $user['name']);
117
                array_push($content, $user['mail']);
118
                array_push($content, implode(", ", $user['grps']));
119
            }
120
            echo $tr->format(
121
                [15, 25, 25, 15],
122
                $content
123
            );
124
        }
125
    }
126
127
    protected function cmdAdd(bool $notify, array $args)
128
    {
129
        if (!$this->auth->canDo('addUser')) return false;
130
131
        $user = $args[0];
132
        $name = $args[1];
133
        $mail = $args[2];
134
        $grps = explode(',', $args[3]);
135
        $pass = $args[4];
136
137
        if ($this->auth->canDo('modPass')) {
138
            if (empty($pass)) {
139
                if ($notify) {
140
                    $pass = auth_pwgen($user);
141
                } else {
142
                    $this->error($this->lang['add_fail']);
143
                    $this->error($this->lang['addUser_error_missing_pass']);
144
                    return false;
145
                }
146
            }
147
        } else {
148
            if (!empty($pass)) {
149
                $this->error($this->lang['add_fail']);
150
                $this->error($this->lang['addUser_error_modPass_disabled']);
151
                return false;
152
            }
153
        }
154
155
        if (!$this->auth->triggerUserMod('create', array($user, $pass, $name, $mail, $grps))) {
156
            $this->error($this->lang['add_fail']);
157
            $this->error($this->lang['addUser_error_create_event_failed']);
158
            return false;
159
        }
160
161
        return 0;
162
    }
163
164
    protected function cmdDelete(array $args)
165
    {
166
        if (!$this->auth->canDo('delUser')) return false;
167
        $users = explode(',', $args[0]);
168
169
        $count = $this->auth->triggerUserMod('delete', array($users));
170
171
        if (!($count == count($users))) {
172
            $part1 = str_replace('%d', $count, $this->lang['delete_ok']);
173
            $part2 = str_replace('%d', (count($users)-$count), $this->lang['delete_fail']);
174
            $this->error("$part1, $part2");
175
        }
176
177
        return 0;
178
    }
179
}
180