xref: /plugin/pureldap/classes/Client.php (revision 80ac552f15be014c3406f65bcaa71e07c7c359c8)
11078ec26SAndreas Gohr<?php
21078ec26SAndreas Gohr
31078ec26SAndreas Gohrnamespace dokuwiki\plugin\pureldap\classes;
41078ec26SAndreas Gohr
5*80ac552fSAndreas Gohruse dokuwiki\Utf8\PhpString;
61078ec26SAndreas Gohruse FreeDSx\Ldap\Entry\Attribute;
71078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\BindException;
81078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\ConnectionException;
91078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\OperationException;
101078ec26SAndreas Gohruse FreeDSx\Ldap\LdapClient;
111078ec26SAndreas Gohr
121078ec26SAndreas Gohrrequire_once __DIR__ . '/../vendor/autoload.php';
131078ec26SAndreas Gohr
141078ec26SAndreas Gohrabstract class Client
151078ec26SAndreas Gohr{
161078ec26SAndreas Gohr    /** @var array the configuration */
171078ec26SAndreas Gohr    protected $config;
181078ec26SAndreas Gohr
191078ec26SAndreas Gohr    /** @var LdapClient */
201078ec26SAndreas Gohr    protected $ldap;
211078ec26SAndreas Gohr
221078ec26SAndreas Gohr    /** @var bool is this client authenticated already? */
231078ec26SAndreas Gohr    protected $isAuthenticated = false;
241078ec26SAndreas Gohr
251078ec26SAndreas Gohr    /** @var array cached user info */
261078ec26SAndreas Gohr    protected $userCache = [];
271078ec26SAndreas Gohr
285a3b9122SAndreas Gohr    /** @var array cached group list */
295a3b9122SAndreas Gohr    protected $groupCache = [];
305a3b9122SAndreas Gohr
311078ec26SAndreas Gohr    /**
321078ec26SAndreas Gohr     * Client constructor.
331078ec26SAndreas Gohr     * @param array $config
341078ec26SAndreas Gohr     */
351078ec26SAndreas Gohr    public function __construct($config)
361078ec26SAndreas Gohr    {
371078ec26SAndreas Gohr        $this->config = $this->prepareConfig($config);
381078ec26SAndreas Gohr        $this->ldap = new LdapClient($this->config);
391078ec26SAndreas Gohr    }
401078ec26SAndreas Gohr
411078ec26SAndreas Gohr    /**
421078ec26SAndreas Gohr     * Setup sane config defaults
431078ec26SAndreas Gohr     *
441078ec26SAndreas Gohr     * @param array $config
451078ec26SAndreas Gohr     * @return array
461078ec26SAndreas Gohr     */
471078ec26SAndreas Gohr    protected function prepareConfig($config)
481078ec26SAndreas Gohr    {
491078ec26SAndreas Gohr        $defaults = [
501078ec26SAndreas Gohr            'defaultgroup' => 'user', // we expect this to be passed from global conf
51*80ac552fSAndreas Gohr            'domain' => '',
521078ec26SAndreas Gohr            'use_tls' => false,
531078ec26SAndreas Gohr            'use_ssl' => false,
541078ec26SAndreas Gohr            'port' => '',
551078ec26SAndreas Gohr            'admin_username' => '',
561078ec26SAndreas Gohr            'admin_password' => '',
575a3b9122SAndreas Gohr            'page_size' => 1000,
581078ec26SAndreas Gohr        ];
591078ec26SAndreas Gohr
601078ec26SAndreas Gohr        $config = array_merge($defaults, $config);
611078ec26SAndreas Gohr
621078ec26SAndreas Gohr        // default port depends on SSL setting
631078ec26SAndreas Gohr        if (!$config['port']) {
641078ec26SAndreas Gohr            $config['port'] = $config['use_ssl'] ? 636 : 389;
651078ec26SAndreas Gohr        }
661078ec26SAndreas Gohr
67*80ac552fSAndreas Gohr        $config['domain'] = PhpString::strtolower($config['domain']);
68*80ac552fSAndreas Gohr
691078ec26SAndreas Gohr        return $config;
701078ec26SAndreas Gohr    }
711078ec26SAndreas Gohr
721078ec26SAndreas Gohr    /**
731078ec26SAndreas Gohr     * Authenticate as admin
741078ec26SAndreas Gohr     */
751078ec26SAndreas Gohr    public function autoAuth()
761078ec26SAndreas Gohr    {
771078ec26SAndreas Gohr        if ($this->isAuthenticated) return true;
781078ec26SAndreas Gohr        return $this->authenticate($this->config['admin_username'], $this->config['admin_password']);
791078ec26SAndreas Gohr    }
801078ec26SAndreas Gohr
811078ec26SAndreas Gohr    /**
821078ec26SAndreas Gohr     * Authenticates a given user. This client will remain authenticated
831078ec26SAndreas Gohr     *
841078ec26SAndreas Gohr     * @param string $user
851078ec26SAndreas Gohr     * @param string $pass
861078ec26SAndreas Gohr     * @return bool was the authentication successful?
875a3b9122SAndreas Gohr     * @noinspection PhpRedundantCatchClauseInspection
881078ec26SAndreas Gohr     */
891078ec26SAndreas Gohr    public function authenticate($user, $pass)
901078ec26SAndreas Gohr    {
91*80ac552fSAndreas Gohr        $user = $this->qualifiedUser($user);
92*80ac552fSAndreas Gohr
931078ec26SAndreas Gohr        if ($this->config['use_tls']) {
941078ec26SAndreas Gohr            try {
951078ec26SAndreas Gohr                $this->ldap->startTls();
961078ec26SAndreas Gohr            } catch (OperationException $e) {
97da369b60SAndreas Gohr                $this->fatal($e);
981078ec26SAndreas Gohr            }
991078ec26SAndreas Gohr        }
1001078ec26SAndreas Gohr
1011078ec26SAndreas Gohr        try {
1021078ec26SAndreas Gohr            $this->ldap->bind($user, $pass);
1031078ec26SAndreas Gohr        } catch (BindException $e) {
1041078ec26SAndreas Gohr            return false;
1051078ec26SAndreas Gohr        } catch (ConnectionException $e) {
106da369b60SAndreas Gohr            $this->fatal($e);
1071078ec26SAndreas Gohr            return false;
1081078ec26SAndreas Gohr        } catch (OperationException $e) {
109da369b60SAndreas Gohr            $this->fatal($e);
1101078ec26SAndreas Gohr            return false;
1111078ec26SAndreas Gohr        }
1121078ec26SAndreas Gohr
1131078ec26SAndreas Gohr        $this->isAuthenticated = true;
1141078ec26SAndreas Gohr        return true;
1151078ec26SAndreas Gohr    }
1161078ec26SAndreas Gohr
1171078ec26SAndreas Gohr    /**
1181078ec26SAndreas Gohr     * Get info for a single user, use cache if available
1191078ec26SAndreas Gohr     *
1201078ec26SAndreas Gohr     * @param string $username
1211078ec26SAndreas Gohr     * @param bool $fetchgroups Are groups needed?
1221078ec26SAndreas Gohr     * @return array|null
1231078ec26SAndreas Gohr     */
1241078ec26SAndreas Gohr    public function getCachedUser($username, $fetchgroups = true)
1251078ec26SAndreas Gohr    {
1261078ec26SAndreas Gohr        if (isset($this->userCache[$username])) {
1271078ec26SAndreas Gohr            if (!$fetchgroups || is_array($this->userCache[$username]['grps'])) {
1281078ec26SAndreas Gohr                return $this->userCache[$username];
1291078ec26SAndreas Gohr            }
1301078ec26SAndreas Gohr        }
1311078ec26SAndreas Gohr
1321078ec26SAndreas Gohr        // fetch fresh data
1331078ec26SAndreas Gohr        $info = $this->getUser($username, $fetchgroups);
1341078ec26SAndreas Gohr
1351078ec26SAndreas Gohr        // store in cache
1361078ec26SAndreas Gohr        if ($info !== null) {
1371078ec26SAndreas Gohr            $this->userCache[$username] = $info;
1381078ec26SAndreas Gohr        }
1391078ec26SAndreas Gohr
1401078ec26SAndreas Gohr        return $info;
1411078ec26SAndreas Gohr    }
1421078ec26SAndreas Gohr
1431078ec26SAndreas Gohr    /**
1441078ec26SAndreas Gohr     * Fetch a single user
1451078ec26SAndreas Gohr     *
1461078ec26SAndreas Gohr     * @param string $username
1471078ec26SAndreas Gohr     * @param bool $fetchgroups Shall groups be fetched, too?
1481078ec26SAndreas Gohr     * @return null|array
1491078ec26SAndreas Gohr     */
1501078ec26SAndreas Gohr    abstract public function getUser($username, $fetchgroups = true);
1511078ec26SAndreas Gohr
1521078ec26SAndreas Gohr    /**
1535a3b9122SAndreas Gohr     * Return a list of all available groups, use cache if available
1545a3b9122SAndreas Gohr     *
1555a3b9122SAndreas Gohr     * @return string[]
1565a3b9122SAndreas Gohr     */
1575a3b9122SAndreas Gohr    public function getCachedGroups()
1585a3b9122SAndreas Gohr    {
1595a3b9122SAndreas Gohr        if (empty($this->groupCache)) {
1605a3b9122SAndreas Gohr            $this->groupCache = $this->getGroups();
1615a3b9122SAndreas Gohr        }
1625a3b9122SAndreas Gohr
1635a3b9122SAndreas Gohr        return $this->groupCache;
1645a3b9122SAndreas Gohr    }
1655a3b9122SAndreas Gohr
1665a3b9122SAndreas Gohr    /**
1675a3b9122SAndreas Gohr     * Return a list of all available groups
1685a3b9122SAndreas Gohr     *
169b21740b4SAndreas Gohr     * Optionally filter the list
170b21740b4SAndreas Gohr     *
171b21740b4SAndreas Gohr     * @param null|string $match Filter for this, null for all groups
172b21740b4SAndreas Gohr     * @param string $filtermethod How to match the groups
1735a3b9122SAndreas Gohr     * @return string[]
1745a3b9122SAndreas Gohr     */
175b21740b4SAndreas Gohr    abstract public function getGroups($match = null, $filtermethod = 'equal');
1765a3b9122SAndreas Gohr
177*80ac552fSAndreas Gohr
178*80ac552fSAndreas Gohr    /**
179*80ac552fSAndreas Gohr     * Construst the fully qualified name to identify a user
180*80ac552fSAndreas Gohr     *
181*80ac552fSAndreas Gohr     * @param string $username
182*80ac552fSAndreas Gohr     * @return string
183*80ac552fSAndreas Gohr     */
184*80ac552fSAndreas Gohr    abstract public function qualifiedUser($username);
185*80ac552fSAndreas Gohr
186*80ac552fSAndreas Gohr    /**
187*80ac552fSAndreas Gohr     * Simplify the username if possible
188*80ac552fSAndreas Gohr     *
189*80ac552fSAndreas Gohr     * @param string $username
190*80ac552fSAndreas Gohr     * @return string
191*80ac552fSAndreas Gohr     */
192*80ac552fSAndreas Gohr    abstract public function simpleUser($username);
193*80ac552fSAndreas Gohr
1945a3b9122SAndreas Gohr    /**
1951078ec26SAndreas Gohr     * Helper method to get the first value of the given attribute
1961078ec26SAndreas Gohr     *
1971078ec26SAndreas Gohr     * The given attribute may be null, an empty string is returned then
1981078ec26SAndreas Gohr     *
1991078ec26SAndreas Gohr     * @param Attribute|null $attribute
2001078ec26SAndreas Gohr     * @return string
2011078ec26SAndreas Gohr     */
2025a3b9122SAndreas Gohr    protected function attr2str($attribute)
2035a3b9122SAndreas Gohr    {
2041078ec26SAndreas Gohr        if ($attribute !== null) {
2051078ec26SAndreas Gohr            return $attribute->firstValue();
2061078ec26SAndreas Gohr        }
2071078ec26SAndreas Gohr        return '';
2081078ec26SAndreas Gohr    }
2091078ec26SAndreas Gohr
2101078ec26SAndreas Gohr    /**
211da369b60SAndreas Gohr     * Handle fatal exceptions
2121078ec26SAndreas Gohr     *
2131078ec26SAndreas Gohr     * @param \Exception $e
2141078ec26SAndreas Gohr     */
215da369b60SAndreas Gohr    protected function fatal(\Exception $e)
2161078ec26SAndreas Gohr    {
2171078ec26SAndreas Gohr        if (defined('DOKU_UNITTEST')) {
2188595f73eSAndreas Gohr            throw new \RuntimeException('', 0, $e);
2191078ec26SAndreas Gohr        }
220da369b60SAndreas Gohr        msg('[pureldap] ' . hsc($e->getMessage()) . ' at ' . $e->getFile() . ':' . $e->getLine(), -1);
221da369b60SAndreas Gohr    }
2221078ec26SAndreas Gohr
223da369b60SAndreas Gohr    /**
224da369b60SAndreas Gohr     * Handle debug output
225da369b60SAndreas Gohr     *
226da369b60SAndreas Gohr     * @param string $msg
227da369b60SAndreas Gohr     * @param string $file
228da369b60SAndreas Gohr     * @param int $line
229da369b60SAndreas Gohr     */
230da369b60SAndreas Gohr    protected function debug($msg, $file, $line)
231da369b60SAndreas Gohr    {
23285916a2dSAndreas Gohr        msg('[pureldap] ' . hsc($msg) . ' at ' . $file . ':' . $line, 0);
2331078ec26SAndreas Gohr    }
2341078ec26SAndreas Gohr}
235