1<?php
2
3namespace dokuwiki\Remote\Response;
4
5/**
6 * Represents a user
7 */
8class User extends ApiResponse
9{
10    /** @var string The login name of the user */
11    public $login;
12    /** @var string The full name of the user */
13    public $name;
14    /** @var string The email address of the user */
15    public $mail;
16    /** @var string[] The groups the user is in */
17    public $groups;
18    /** @var bool Whether the user is a super user */
19    public bool $isadmin;
20    /** @var bool Whether the user is a manager */
21    public bool $ismanager;
22
23    /**
24     * @param string $login defaults to the current user
25     * @param string $name
26     * @param string $mail
27     * @param string[] $groups
28     */
29    public function __construct($login = '', $name = '', $mail = '', $groups = [])
30    {
31        global $INPUT;
32        global $USERINFO;
33        global $auth;
34
35        $this->login = $login;
36        $this->name = $name;
37        $this->mail = $mail;
38        $this->groups = $groups;
39
40        if ($this->login === '') {
41            $this->login = $INPUT->server->str('REMOTE_USER');
42        }
43
44        if ($this->login === '') {
45            throw new \RuntimeException('No user available');
46        }
47
48        // for current user, use $USERINFO to fill up
49        if ($this->login === $INPUT->server->str('REMOTE_USER')) {
50            $this->name = $this->name ?: $USERINFO['name'];
51            $this->mail = $this->mail ?: $USERINFO['mail'];
52            $this->groups = $this->groups ?: $USERINFO['grps'];
53        } else {
54            // for other users, use auth_getUserData to fill up
55            $userData = $auth->getUserData($this->login);
56            $this->name = $this->name ?: $userData['name'];
57            $this->mail = $this->mail ?: $userData['mail'];
58            $this->groups = $this->groups ?: $userData['grps'];
59        }
60
61        // check for admin and manager
62        $this->isadmin = auth_isAdmin($this->login, $this->groups);
63        $this->ismanager = auth_isManager($this->login, $this->groups);
64    }
65
66    /** @inheritdoc */
67    public function __toString()
68    {
69        return $this->login;
70    }
71}
72