xref: /dokuwiki/lib/plugins/usermanager/remote.php (revision e4e3d43949dfb6b53840595de532df30a6fdba7b)
1<?php
2
3use dokuwiki\Extension\AuthPlugin;
4use dokuwiki\Extension\RemotePlugin;
5use dokuwiki\Remote\AccessDeniedException;
6use dokuwiki\Remote\RemoteException;
7
8/**
9 * DokuWiki Plugin usermanager (Action Component)
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author Chris Smith <chris@jalakai.co.uk>
13 */
14class remote_plugin_usermanager extends RemotePlugin
15{
16
17    /**
18     * Create a new user
19     *
20     * If no password is provided, a password is auto generated. If the user can't be created
21     * by the auth backend a return value of `false` is returned. You need to check this return
22     * value rather than relying on the error code only.
23     *
24     * Superuser permission are required to create users.
25     *
26     * @param string $user The user's login name
27     * @param string $name The user's full name
28     * @param string $mail The user's email address
29     * @param string[] $groups The groups the user should be in
30     * @param string $password The user's password, empty for autogeneration
31     * @param bool $notify Whether to send a notification email to the user
32     * @return bool Wether the user was successfully created
33     * @throws AccessDeniedException
34     * @throws RemoteException
35     * @todo handle error messages from auth backend
36     */
37    public function createUser($user, $name, $mail, $groups, $password = '', $notify = false)
38    {
39        if (!auth_isadmin()) {
40            throw new AccessDeniedException('Only admins are allowed to create users', 114);
41        }
42
43        /** @var AuthPlugin $auth */
44        global $auth;
45
46        if (!$auth->canDo('addUser')) {
47            throw new AccessDeniedException(
48                sprintf('Authentication backend %s can\'t do addUser', $auth->getPluginName()),
49                114
50            );
51        }
52
53        $user = trim($auth->cleanUser($user));
54        $name = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $name));
55        $mail = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $mail));
56
57        if ($user === '') throw new RemoteException('empty or invalid user', 401);
58        if ($name === '') throw new RemoteException('empty or invalid user name', 402);
59        if (!mail_isvalid($mail)) throw new RemoteException('empty or invalid mail address', 403);
60
61        if ((string)$password === '') {
62            try {
63                $password = auth_pwgen($user);
64            } catch (\Exception $e) {
65                throw new RemoteException('Could not generate password', 404); // FIXME adjust code
66            }
67        }
68
69        if (!is_array($groups) || $groups === []) {
70            $groups = null;
71        }
72
73        $ok = (bool)$auth->triggerUserMod('create', [$user, $password, $name, $mail, $groups]);
74
75        if ($ok && $notify) {
76            auth_sendPassword($user, $password);
77        }
78
79        return $ok;
80    }
81
82
83    /**
84     * Remove a user
85     *
86     * You need to be a superuser to delete users.
87     *
88     * @param string[] $user The login name of the user to delete
89     * @return bool wether the user was successfully deleted
90     * @throws AccessDeniedException
91     * @todo handle error messages from auth backend
92     */
93    public function deleteUser($user)
94    {
95        if (!auth_isadmin()) {
96            throw new AccessDeniedException('Only admins are allowed to delete users', 114);
97        }
98        /** @var AuthPlugin $auth */
99        global $auth;
100        return (bool)$auth->triggerUserMod('delete', [[$user]]);
101    }
102}
103