xref: /dokuwiki/inc/Remote/Response/User.php (revision 58ae4747de65e434e2a4f2d10822e363198c89b8)
16cce3332SAndreas Gohr<?php
26cce3332SAndreas Gohr
36cce3332SAndreas Gohrnamespace dokuwiki\Remote\Response;
46cce3332SAndreas Gohr
56cce3332SAndreas Gohr/**
66cce3332SAndreas Gohr * Represents a user
76cce3332SAndreas Gohr */
86cce3332SAndreas Gohrclass User extends ApiResponse
96cce3332SAndreas Gohr{
106cce3332SAndreas Gohr    /** @var string The login name of the user */
116cce3332SAndreas Gohr    public $login;
126cce3332SAndreas Gohr    /** @var string The full name of the user */
136cce3332SAndreas Gohr    public $name;
146cce3332SAndreas Gohr    /** @var string The email address of the user */
156cce3332SAndreas Gohr    public $mail;
166cce3332SAndreas Gohr    /** @var array The groups the user is in */
176cce3332SAndreas Gohr    public $groups;
186cce3332SAndreas Gohr    /** @var bool Whether the user is a super user */
19*58ae4747SAndreas Gohr    public bool $isadmin;
206cce3332SAndreas Gohr    /** @var bool Whether the user is a manager */
21*58ae4747SAndreas Gohr    public bool $ismanager;
226cce3332SAndreas Gohr
23*58ae4747SAndreas Gohr    /**
24*58ae4747SAndreas Gohr     * @param string $login defaults to the current user
25*58ae4747SAndreas Gohr     * @param string $name
26*58ae4747SAndreas Gohr     * @param string $mail
27*58ae4747SAndreas Gohr     * @param string $groups
28*58ae4747SAndreas Gohr     */
29*58ae4747SAndreas Gohr    public function __construct($login = '', $name = '', $mail = '', $groups = [])
306cce3332SAndreas Gohr    {
316cce3332SAndreas Gohr        global $INPUT;
32*58ae4747SAndreas Gohr        global $USERINFO;
33*58ae4747SAndreas Gohr        global $auth;
346cce3332SAndreas Gohr
35*58ae4747SAndreas Gohr        $this->login = $login;
36*58ae4747SAndreas Gohr        $this->name = $name;
37*58ae4747SAndreas Gohr        $this->mail = $mail;
38*58ae4747SAndreas Gohr        $this->groups = $groups;
39*58ae4747SAndreas Gohr
40*58ae4747SAndreas Gohr        if ($this->login === '') {
41*58ae4747SAndreas Gohr            $this->login = $INPUT->server->str('REMOTE_USER');
42*58ae4747SAndreas Gohr        }
43*58ae4747SAndreas Gohr
44*58ae4747SAndreas Gohr        if ($this->login === '') {
45*58ae4747SAndreas Gohr            throw new \RuntimeException('No user available');
46*58ae4747SAndreas Gohr        }
47*58ae4747SAndreas Gohr
48*58ae4747SAndreas Gohr        // for current user, use $USERINFO to fill up
49*58ae4747SAndreas Gohr        if ($this->login === $INPUT->server->str('REMOTE_USER')) {
50*58ae4747SAndreas Gohr            $this->name = $this->name ?: $USERINFO['name'];
51*58ae4747SAndreas Gohr            $this->mail = $this->mail ?: $USERINFO['mail'];
52*58ae4747SAndreas Gohr            $this->groups = $this->mail ?: $USERINFO['grps'];
53*58ae4747SAndreas Gohr        } else {
54*58ae4747SAndreas Gohr            // for other users, use auth_getUserData to fill up
55*58ae4747SAndreas Gohr            $userData = $auth->getUserData($this->login);
56*58ae4747SAndreas Gohr            $this->name = $this->name ?: $userData['name'];
57*58ae4747SAndreas Gohr            $this->mail = $this->mail ?: $userData['mail'];
58*58ae4747SAndreas Gohr            $this->groups = $this->mail ?: $userData['grps'];
59*58ae4747SAndreas Gohr        }
60*58ae4747SAndreas Gohr
61*58ae4747SAndreas Gohr        // check for admin and manager
62*58ae4747SAndreas Gohr        $this->isadmin = auth_isAdmin($this->login, $this->groups);
63*58ae4747SAndreas Gohr        $this->ismanager = auth_isManager($this->login, $this->groups);
646cce3332SAndreas Gohr    }
656cce3332SAndreas Gohr}
66