aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Remote/Response/User.php
blob: b45d91ce4102c939ab812f9c3c305df63f66aa5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace dokuwiki\Remote\Response;

/**
 * Represents a user
 */
class User extends ApiResponse
{
    /** @var string The login name of the user */
    public $login;
    /** @var string The full name of the user */
    public $name;
    /** @var string The email address of the user */
    public $mail;
    /** @var string[] The groups the user is in */
    public $groups;
    /** @var bool Whether the user is a super user */
    public bool $isadmin;
    /** @var bool Whether the user is a manager */
    public bool $ismanager;

    /**
     * @param string $login defaults to the current user
     * @param string $name
     * @param string $mail
     * @param string[] $groups
     */
    public function __construct($login = '', $name = '', $mail = '', $groups = [])
    {
        global $INPUT;
        global $USERINFO;
        global $auth;

        $this->login = $login;
        $this->name = $name;
        $this->mail = $mail;
        $this->groups = $groups;

        if ($this->login === '') {
            $this->login = $INPUT->server->str('REMOTE_USER');
        }

        if ($this->login === '') {
            throw new \RuntimeException('No user available');
        }

        // for current user, use $USERINFO to fill up
        if ($this->login === $INPUT->server->str('REMOTE_USER')) {
            $this->name = $this->name ?: $USERINFO['name'];
            $this->mail = $this->mail ?: $USERINFO['mail'];
            $this->groups = $this->groups ?: $USERINFO['grps'];
        } else {
            // for other users, use auth_getUserData to fill up
            $userData = $auth->getUserData($this->login);
            $this->name = $this->name ?: $userData['name'];
            $this->mail = $this->mail ?: $userData['mail'];
            $this->groups = $this->groups ?: $userData['grps'];
        }

        // check for admin and manager
        $this->isadmin = auth_isAdmin($this->login, $this->groups);
        $this->ismanager = auth_isManager($this->login, $this->groups);
    }

    /** @inheritdoc */
    public function __toString()
    {
        return $this->login;
    }
}