xref: /dokuwiki/inc/Remote/Response/User.php (revision 932ec837f7b9c68b77384b74f0f7b456bc4fb192)
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 */
1958ae4747SAndreas Gohr    public bool $isadmin;
206cce3332SAndreas Gohr    /** @var bool Whether the user is a manager */
2158ae4747SAndreas Gohr    public bool $ismanager;
226cce3332SAndreas Gohr
2358ae4747SAndreas Gohr    /**
2458ae4747SAndreas Gohr     * @param string $login defaults to the current user
2558ae4747SAndreas Gohr     * @param string $name
2658ae4747SAndreas Gohr     * @param string $mail
2758ae4747SAndreas Gohr     * @param string $groups
2858ae4747SAndreas Gohr     */
2958ae4747SAndreas Gohr    public function __construct($login = '', $name = '', $mail = '', $groups = [])
306cce3332SAndreas Gohr    {
316cce3332SAndreas Gohr        global $INPUT;
3258ae4747SAndreas Gohr        global $USERINFO;
3358ae4747SAndreas Gohr        global $auth;
346cce3332SAndreas Gohr
3558ae4747SAndreas Gohr        $this->login = $login;
3658ae4747SAndreas Gohr        $this->name = $name;
3758ae4747SAndreas Gohr        $this->mail = $mail;
3858ae4747SAndreas Gohr        $this->groups = $groups;
3958ae4747SAndreas Gohr
4058ae4747SAndreas Gohr        if ($this->login === '') {
4158ae4747SAndreas Gohr            $this->login = $INPUT->server->str('REMOTE_USER');
4258ae4747SAndreas Gohr        }
4358ae4747SAndreas Gohr
4458ae4747SAndreas Gohr        if ($this->login === '') {
4558ae4747SAndreas Gohr            throw new \RuntimeException('No user available');
4658ae4747SAndreas Gohr        }
4758ae4747SAndreas Gohr
4858ae4747SAndreas Gohr        // for current user, use $USERINFO to fill up
4958ae4747SAndreas Gohr        if ($this->login === $INPUT->server->str('REMOTE_USER')) {
5058ae4747SAndreas Gohr            $this->name = $this->name ?: $USERINFO['name'];
5158ae4747SAndreas Gohr            $this->mail = $this->mail ?: $USERINFO['mail'];
52*932ec837SAndreas Gohr            $this->groups = $this->groups ?: $USERINFO['grps'];
5358ae4747SAndreas Gohr        } else {
5458ae4747SAndreas Gohr            // for other users, use auth_getUserData to fill up
5558ae4747SAndreas Gohr            $userData = $auth->getUserData($this->login);
5658ae4747SAndreas Gohr            $this->name = $this->name ?: $userData['name'];
5758ae4747SAndreas Gohr            $this->mail = $this->mail ?: $userData['mail'];
58*932ec837SAndreas Gohr            $this->groups = $this->groups ?: $userData['grps'];
5958ae4747SAndreas Gohr        }
6058ae4747SAndreas Gohr
6158ae4747SAndreas Gohr        // check for admin and manager
6258ae4747SAndreas Gohr        $this->isadmin = auth_isAdmin($this->login, $this->groups);
6358ae4747SAndreas Gohr        $this->ismanager = auth_isManager($this->login, $this->groups);
646cce3332SAndreas Gohr    }
658268b284SAndreas Gohr
668268b284SAndreas Gohr    /** @inheritdoc */
678268b284SAndreas Gohr    public function __toString()
688268b284SAndreas Gohr    {
698268b284SAndreas Gohr        return $this->login;
708268b284SAndreas Gohr    }
716cce3332SAndreas Gohr}
72