xref: /plugin/pureldap/classes/ADClient.php (revision fce018da7ff57fa3fbbabe650e4a6afd1da5aab2)
11078ec26SAndreas Gohr<?php
21078ec26SAndreas Gohr
31078ec26SAndreas Gohrnamespace dokuwiki\plugin\pureldap\classes;
41078ec26SAndreas Gohr
580ac552fSAndreas Gohruse dokuwiki\Utf8\PhpString;
61078ec26SAndreas Gohruse FreeDSx\Ldap\Entry\Entries;
71078ec26SAndreas Gohruse FreeDSx\Ldap\Entry\Entry;
81078ec26SAndreas Gohruse FreeDSx\Ldap\Exception\OperationException;
95a3b9122SAndreas Gohruse FreeDSx\Ldap\Exception\ProtocolException;
101078ec26SAndreas Gohruse FreeDSx\Ldap\Operations;
111078ec26SAndreas Gohruse FreeDSx\Ldap\Search\Filters;
121078ec26SAndreas Gohr
131078ec26SAndreas Gohrclass ADClient extends Client
141078ec26SAndreas Gohr{
151078ec26SAndreas Gohr
161078ec26SAndreas Gohr    /** @inheritDoc */
171078ec26SAndreas Gohr    public function getUser($username, $fetchgroups = true)
181078ec26SAndreas Gohr    {
191078ec26SAndreas Gohr        if (!$this->autoAuth()) return null;
201078ec26SAndreas Gohr
211078ec26SAndreas Gohr        $filter = Filters::and(
221078ec26SAndreas Gohr            Filters::equal('objectClass', 'user'),
2380ac552fSAndreas Gohr            Filters::equal('userPrincipalName', $this->qualifiedUser($username))
241078ec26SAndreas Gohr        );
25b21740b4SAndreas Gohr        $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__);
261078ec26SAndreas Gohr
271078ec26SAndreas Gohr        try {
281078ec26SAndreas Gohr            /** @var Entries $entries */
291078ec26SAndreas Gohr            $entries = $this->ldap->search(Operations::search($filter));
301078ec26SAndreas Gohr        } catch (OperationException $e) {
31b21740b4SAndreas Gohr            $this->fatal($e);
321078ec26SAndreas Gohr            return null;
331078ec26SAndreas Gohr        }
341078ec26SAndreas Gohr        if ($entries->count() !== 1) return null;
351078ec26SAndreas Gohr        $entry = $entries->first();
36b21740b4SAndreas Gohr        return $this->entry2User($entry);
37b21740b4SAndreas Gohr    }
381078ec26SAndreas Gohr
39b21740b4SAndreas Gohr    /** @inheritDoc */
40b21740b4SAndreas Gohr    public function getGroups($match = null, $filtermethod = 'equal')
41b21740b4SAndreas Gohr    {
42b21740b4SAndreas Gohr        if (!$this->autoAuth()) return [];
43b21740b4SAndreas Gohr
44b21740b4SAndreas Gohr        $filter = Filters::and(
45b21740b4SAndreas Gohr            Filters::equal('objectClass', 'group')
46b21740b4SAndreas Gohr        );
47b21740b4SAndreas Gohr        if ($match !== null) {
48*fce018daSAndreas Gohr            // FIXME this is a workaround that removes regex anchors as passed by the groupuser plugin
49*fce018daSAndreas Gohr            // a proper fix requires splitbrain/dokuwiki#3028 to be properly fixed
50*fce018daSAndreas Gohr            $match = ltrim($match, '^');
51*fce018daSAndreas Gohr            $match = rtrim($match, '$');
52*fce018daSAndreas Gohr
53b21740b4SAndreas Gohr            $filter->add(Filters::$filtermethod('cn', $match));
54b21740b4SAndreas Gohr        }
55b21740b4SAndreas Gohr
56b21740b4SAndreas Gohr        $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__);
57b21740b4SAndreas Gohr        $search = Operations::search($filter, 'cn');
58b21740b4SAndreas Gohr        $paging = $this->ldap->paging($search);
59b21740b4SAndreas Gohr
60b21740b4SAndreas Gohr        $groups = [];
61b21740b4SAndreas Gohr        while ($paging->hasEntries()) {
62b21740b4SAndreas Gohr            try {
63b21740b4SAndreas Gohr                $entries = $paging->getEntries();
64b21740b4SAndreas Gohr            } catch (ProtocolException $e) {
65b21740b4SAndreas Gohr                $this->fatal($e);
66b21740b4SAndreas Gohr                return $groups; // we return what we got so far
67b21740b4SAndreas Gohr            }
68b21740b4SAndreas Gohr
69b21740b4SAndreas Gohr            foreach ($entries as $entry) {
70b21740b4SAndreas Gohr                /** @var Entry $entry */
71b21740b4SAndreas Gohr                $groups[$entry->getDn()->toString()] = $this->attr2str($entry->get('cn'));
72b21740b4SAndreas Gohr            }
73b21740b4SAndreas Gohr        }
74b21740b4SAndreas Gohr
751b0eb9b3SAndreas Gohr        asort($groups);
76b21740b4SAndreas Gohr        return $groups;
77b21740b4SAndreas Gohr    }
78b21740b4SAndreas Gohr
79b21740b4SAndreas Gohr    /**
80b21740b4SAndreas Gohr     * Fetch users matching the given filters
81b21740b4SAndreas Gohr     *
82b21740b4SAndreas Gohr     * @param array $match
83b21740b4SAndreas Gohr     * @param string $filtermethod The method to use for filtering
84b21740b4SAndreas Gohr     * @return array
85b21740b4SAndreas Gohr     */
86b21740b4SAndreas Gohr    public function getFilteredUsers($match, $filtermethod = 'equal')
87b21740b4SAndreas Gohr    {
88b21740b4SAndreas Gohr        if (!$this->autoAuth()) return [];
89b21740b4SAndreas Gohr
90b21740b4SAndreas Gohr        $filter = Filters::and(Filters::equal('objectClass', 'user'));
91b21740b4SAndreas Gohr        if (isset($match['user'])) {
92b21740b4SAndreas Gohr            $filter->add(Filters::$filtermethod('userPrincipalName', $match['user']));
93b21740b4SAndreas Gohr        }
94b21740b4SAndreas Gohr        if (isset($match['name'])) {
95b21740b4SAndreas Gohr            $filter->add(Filters::$filtermethod('displayName', $match['name']));
96b21740b4SAndreas Gohr        }
97b21740b4SAndreas Gohr        if (isset($match['mail'])) {
98b21740b4SAndreas Gohr            $filter->add(Filters::$filtermethod('mail', $match['mail']));
99b21740b4SAndreas Gohr        }
100b21740b4SAndreas Gohr        if (isset($match['grps'])) {
101b21740b4SAndreas Gohr            // memberOf can not be checked with a substring match, so we need to get the right groups first
102b21740b4SAndreas Gohr            $groups = $this->getGroups($match['grps'], $filtermethod);
103b21740b4SAndreas Gohr            $or = Filters::or();
104b21740b4SAndreas Gohr            foreach ($groups as $dn => $group) {
105b21740b4SAndreas Gohr                $or->add(Filters::equal('memberOf', $dn));
106b21740b4SAndreas Gohr            }
107b21740b4SAndreas Gohr            $filter->add($or);
108b21740b4SAndreas Gohr        }
109b21740b4SAndreas Gohr        $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__);
110b21740b4SAndreas Gohr        $search = Operations::search($filter);
111b21740b4SAndreas Gohr        $paging = $this->ldap->paging($search);
112b21740b4SAndreas Gohr
113b21740b4SAndreas Gohr        $users = [];
114b21740b4SAndreas Gohr        while ($paging->hasEntries()) {
115b21740b4SAndreas Gohr            try {
116b21740b4SAndreas Gohr                $entries = $paging->getEntries();
117b21740b4SAndreas Gohr            } catch (ProtocolException $e) {
118b21740b4SAndreas Gohr                $this->fatal($e);
11980ac552fSAndreas Gohr                break; // we abort and return what we have so far
120b21740b4SAndreas Gohr            }
121b21740b4SAndreas Gohr
122b21740b4SAndreas Gohr            foreach ($entries as $entry) {
12380ac552fSAndreas Gohr                $userinfo = $this->entry2User($entry);
12480ac552fSAndreas Gohr                $users[$userinfo['user']] = $this->entry2User($entry);
125b21740b4SAndreas Gohr            }
126b21740b4SAndreas Gohr        }
127b21740b4SAndreas Gohr
1281b0eb9b3SAndreas Gohr        ksort($users);
129b21740b4SAndreas Gohr        return $users;
130b21740b4SAndreas Gohr    }
131b21740b4SAndreas Gohr
132b21740b4SAndreas Gohr    /**
13380ac552fSAndreas Gohr     * @inheritDoc
13480ac552fSAndreas Gohr     * userPrincipalName in the form <user>@<domain>
13580ac552fSAndreas Gohr     */
13680ac552fSAndreas Gohr    public function qualifiedUser($user)
13780ac552fSAndreas Gohr    {
13880ac552fSAndreas Gohr        $user = PhpString::strtolower($user);
13980ac552fSAndreas Gohr        if (!$this->config['domain']) return $user;
14080ac552fSAndreas Gohr
14180ac552fSAndreas Gohr        list($user, $domain) = explode('@', $user, 2);
14280ac552fSAndreas Gohr        if (!$domain) {
14380ac552fSAndreas Gohr            $domain = $this->config['domain'];
14480ac552fSAndreas Gohr        }
14580ac552fSAndreas Gohr
14680ac552fSAndreas Gohr        return $user . '@' . $domain;
14780ac552fSAndreas Gohr    }
14880ac552fSAndreas Gohr
14980ac552fSAndreas Gohr    /**
15080ac552fSAndreas Gohr     * @inheritDoc
15180ac552fSAndreas Gohr     * Removes the account suffix from the given user
15280ac552fSAndreas Gohr     */
15380ac552fSAndreas Gohr    public function simpleUser($user)
15480ac552fSAndreas Gohr    {
15580ac552fSAndreas Gohr        $user = PhpString::strtolower($user);
15680ac552fSAndreas Gohr        if (!$this->config['domain']) return $user;
15780ac552fSAndreas Gohr
15880ac552fSAndreas Gohr        // strip account suffix
15980ac552fSAndreas Gohr        list($luser, $suffix) = explode('@', $user, 2);
16080ac552fSAndreas Gohr        if ($suffix === $this->config['domain']) return $luser;
16180ac552fSAndreas Gohr
16280ac552fSAndreas Gohr        return $user;
16380ac552fSAndreas Gohr    }
16480ac552fSAndreas Gohr
16580ac552fSAndreas Gohr    /**
166b21740b4SAndreas Gohr     * Transform an LDAP entry to a user info array
167b21740b4SAndreas Gohr     *
168b21740b4SAndreas Gohr     * @param Entry $entry
169b21740b4SAndreas Gohr     * @return array
170b21740b4SAndreas Gohr     */
171b21740b4SAndreas Gohr    protected function entry2User(Entry $entry)
172b21740b4SAndreas Gohr    {
1731078ec26SAndreas Gohr        return [
17480ac552fSAndreas Gohr            'user' => $this->simpleUser($this->attr2str($entry->get('UserPrincipalName'))),
1751078ec26SAndreas Gohr            'name' => $this->attr2str($entry->get('DisplayName')) ?: $this->attr2str($entry->get('Name')),
1761078ec26SAndreas Gohr            'mail' => $this->attr2str($entry->get('mail')),
1771078ec26SAndreas Gohr            'dn' => $entry->getDn()->toString(),
1781078ec26SAndreas Gohr            'grps' => $this->getUserGroups($entry), // we always return groups because its currently inexpensive
1791078ec26SAndreas Gohr        ];
1801078ec26SAndreas Gohr    }
1811078ec26SAndreas Gohr
1821078ec26SAndreas Gohr    /**
1831078ec26SAndreas Gohr     * Get the list of groups the given user is member of
1841078ec26SAndreas Gohr     *
1851078ec26SAndreas Gohr     * This method currently does no LDAP queries and thus is inexpensive.
1861078ec26SAndreas Gohr     *
1871078ec26SAndreas Gohr     * @param Entry $userentry
1881078ec26SAndreas Gohr     * @return array
1896d90d5c8SAndreas Gohr     * @todo implement nested group memberships FIXME already correct?
1901078ec26SAndreas Gohr     */
1911078ec26SAndreas Gohr    protected function getUserGroups(Entry $userentry)
1921078ec26SAndreas Gohr    {
1931078ec26SAndreas Gohr        $groups = [$this->config['defaultgroup']]; // always add default
1941078ec26SAndreas Gohr
1951078ec26SAndreas Gohr        // we simply take the first CN= part of the group DN and return it as the group name
1961078ec26SAndreas Gohr        // this should be correct for ActiveDirectory and saves us additional LDAP queries
1971078ec26SAndreas Gohr        if ($userentry->has('memberOf')) {
198b21740b4SAndreas Gohr            foreach ($userentry->get('memberOf')->getValues() as $dn) {
199b21740b4SAndreas Gohr                list($cn) = explode(',', $dn, 2);
20080ac552fSAndreas Gohr                $groups[] = PhpString::strtolower(substr($cn, 3));
2011078ec26SAndreas Gohr            }
2021078ec26SAndreas Gohr        }
2031078ec26SAndreas Gohr
2041078ec26SAndreas Gohr        // resolving the primary group in AD is complicated but basically never needed
2051078ec26SAndreas Gohr        // http://support.microsoft.com/?kbid=321360
2061078ec26SAndreas Gohr        $gid = $userentry->get('primaryGroupID')->firstValue();
2071078ec26SAndreas Gohr        if ($gid == 513) {
20880ac552fSAndreas Gohr            $groups[] = 'domain users';
2091078ec26SAndreas Gohr        }
2101078ec26SAndreas Gohr
2111b0eb9b3SAndreas Gohr        sort($groups);
2121078ec26SAndreas Gohr        return $groups;
2131078ec26SAndreas Gohr    }
2141078ec26SAndreas Gohr}
215