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) { 48b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('cn', $match)); 49b21740b4SAndreas Gohr } 50b21740b4SAndreas Gohr 51b21740b4SAndreas Gohr $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__); 52b21740b4SAndreas Gohr $search = Operations::search($filter, 'cn'); 53b21740b4SAndreas Gohr $paging = $this->ldap->paging($search); 54b21740b4SAndreas Gohr 55b21740b4SAndreas Gohr $groups = []; 56b21740b4SAndreas Gohr while ($paging->hasEntries()) { 57b21740b4SAndreas Gohr try { 58b21740b4SAndreas Gohr $entries = $paging->getEntries(); 59b21740b4SAndreas Gohr } catch (ProtocolException $e) { 60b21740b4SAndreas Gohr $this->fatal($e); 61b21740b4SAndreas Gohr return $groups; // we return what we got so far 62b21740b4SAndreas Gohr } 63b21740b4SAndreas Gohr 64b21740b4SAndreas Gohr foreach ($entries as $entry) { 65b21740b4SAndreas Gohr /** @var Entry $entry */ 66b21740b4SAndreas Gohr $groups[$entry->getDn()->toString()] = $this->attr2str($entry->get('cn')); 67b21740b4SAndreas Gohr } 68b21740b4SAndreas Gohr } 69b21740b4SAndreas Gohr 70*1b0eb9b3SAndreas Gohr asort($groups); 71b21740b4SAndreas Gohr return $groups; 72b21740b4SAndreas Gohr } 73b21740b4SAndreas Gohr 74b21740b4SAndreas Gohr /** 75b21740b4SAndreas Gohr * Fetch users matching the given filters 76b21740b4SAndreas Gohr * 77b21740b4SAndreas Gohr * @param array $match 78b21740b4SAndreas Gohr * @param string $filtermethod The method to use for filtering 79b21740b4SAndreas Gohr * @return array 80b21740b4SAndreas Gohr */ 81b21740b4SAndreas Gohr public function getFilteredUsers($match, $filtermethod = 'equal') 82b21740b4SAndreas Gohr { 83b21740b4SAndreas Gohr if (!$this->autoAuth()) return []; 84b21740b4SAndreas Gohr 85b21740b4SAndreas Gohr $filter = Filters::and(Filters::equal('objectClass', 'user')); 86b21740b4SAndreas Gohr if (isset($match['user'])) { 87b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('userPrincipalName', $match['user'])); 88b21740b4SAndreas Gohr } 89b21740b4SAndreas Gohr if (isset($match['name'])) { 90b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('displayName', $match['name'])); 91b21740b4SAndreas Gohr } 92b21740b4SAndreas Gohr if (isset($match['mail'])) { 93b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('mail', $match['mail'])); 94b21740b4SAndreas Gohr } 95b21740b4SAndreas Gohr if (isset($match['grps'])) { 96b21740b4SAndreas Gohr // memberOf can not be checked with a substring match, so we need to get the right groups first 97b21740b4SAndreas Gohr $groups = $this->getGroups($match['grps'], $filtermethod); 98b21740b4SAndreas Gohr $or = Filters::or(); 99b21740b4SAndreas Gohr foreach ($groups as $dn => $group) { 100b21740b4SAndreas Gohr $or->add(Filters::equal('memberOf', $dn)); 101b21740b4SAndreas Gohr } 102b21740b4SAndreas Gohr $filter->add($or); 103b21740b4SAndreas Gohr } 104b21740b4SAndreas Gohr $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__); 105b21740b4SAndreas Gohr $search = Operations::search($filter); 106b21740b4SAndreas Gohr $paging = $this->ldap->paging($search); 107b21740b4SAndreas Gohr 108b21740b4SAndreas Gohr $users = []; 109b21740b4SAndreas Gohr while ($paging->hasEntries()) { 110b21740b4SAndreas Gohr try { 111b21740b4SAndreas Gohr $entries = $paging->getEntries(); 112b21740b4SAndreas Gohr } catch (ProtocolException $e) { 113b21740b4SAndreas Gohr $this->fatal($e); 11480ac552fSAndreas Gohr break; // we abort and return what we have so far 115b21740b4SAndreas Gohr } 116b21740b4SAndreas Gohr 117b21740b4SAndreas Gohr foreach ($entries as $entry) { 11880ac552fSAndreas Gohr $userinfo = $this->entry2User($entry); 11980ac552fSAndreas Gohr $users[$userinfo['user']] = $this->entry2User($entry); 120b21740b4SAndreas Gohr } 121b21740b4SAndreas Gohr } 122b21740b4SAndreas Gohr 123*1b0eb9b3SAndreas Gohr ksort($users); 124b21740b4SAndreas Gohr return $users; 125b21740b4SAndreas Gohr } 126b21740b4SAndreas Gohr 127b21740b4SAndreas Gohr /** 12880ac552fSAndreas Gohr * @inheritDoc 12980ac552fSAndreas Gohr * userPrincipalName in the form <user>@<domain> 13080ac552fSAndreas Gohr */ 13180ac552fSAndreas Gohr public function qualifiedUser($user) 13280ac552fSAndreas Gohr { 13380ac552fSAndreas Gohr $user = PhpString::strtolower($user); 13480ac552fSAndreas Gohr if (!$this->config['domain']) return $user; 13580ac552fSAndreas Gohr 13680ac552fSAndreas Gohr list($user, $domain) = explode('@', $user, 2); 13780ac552fSAndreas Gohr if (!$domain) { 13880ac552fSAndreas Gohr $domain = $this->config['domain']; 13980ac552fSAndreas Gohr } 14080ac552fSAndreas Gohr 14180ac552fSAndreas Gohr return $user . '@' . $domain; 14280ac552fSAndreas Gohr } 14380ac552fSAndreas Gohr 14480ac552fSAndreas Gohr /** 14580ac552fSAndreas Gohr * @inheritDoc 14680ac552fSAndreas Gohr * Removes the account suffix from the given user 14780ac552fSAndreas Gohr */ 14880ac552fSAndreas Gohr public function simpleUser($user) 14980ac552fSAndreas Gohr { 15080ac552fSAndreas Gohr $user = PhpString::strtolower($user); 15180ac552fSAndreas Gohr if (!$this->config['domain']) return $user; 15280ac552fSAndreas Gohr 15380ac552fSAndreas Gohr // strip account suffix 15480ac552fSAndreas Gohr list($luser, $suffix) = explode('@', $user, 2); 15580ac552fSAndreas Gohr if ($suffix === $this->config['domain']) return $luser; 15680ac552fSAndreas Gohr 15780ac552fSAndreas Gohr return $user; 15880ac552fSAndreas Gohr } 15980ac552fSAndreas Gohr 16080ac552fSAndreas Gohr /** 161b21740b4SAndreas Gohr * Transform an LDAP entry to a user info array 162b21740b4SAndreas Gohr * 163b21740b4SAndreas Gohr * @param Entry $entry 164b21740b4SAndreas Gohr * @return array 165b21740b4SAndreas Gohr */ 166b21740b4SAndreas Gohr protected function entry2User(Entry $entry) 167b21740b4SAndreas Gohr { 1681078ec26SAndreas Gohr return [ 16980ac552fSAndreas Gohr 'user' => $this->simpleUser($this->attr2str($entry->get('UserPrincipalName'))), 1701078ec26SAndreas Gohr 'name' => $this->attr2str($entry->get('DisplayName')) ?: $this->attr2str($entry->get('Name')), 1711078ec26SAndreas Gohr 'mail' => $this->attr2str($entry->get('mail')), 1721078ec26SAndreas Gohr 'dn' => $entry->getDn()->toString(), 1731078ec26SAndreas Gohr 'grps' => $this->getUserGroups($entry), // we always return groups because its currently inexpensive 1741078ec26SAndreas Gohr ]; 1751078ec26SAndreas Gohr } 1761078ec26SAndreas Gohr 1771078ec26SAndreas Gohr /** 1781078ec26SAndreas Gohr * Get the list of groups the given user is member of 1791078ec26SAndreas Gohr * 1801078ec26SAndreas Gohr * This method currently does no LDAP queries and thus is inexpensive. 1811078ec26SAndreas Gohr * 1821078ec26SAndreas Gohr * @param Entry $userentry 1831078ec26SAndreas Gohr * @return array 1841078ec26SAndreas Gohr * @todo implement nested group memberships 1851078ec26SAndreas Gohr */ 1861078ec26SAndreas Gohr protected function getUserGroups(Entry $userentry) 1871078ec26SAndreas Gohr { 1881078ec26SAndreas Gohr $groups = [$this->config['defaultgroup']]; // always add default 1891078ec26SAndreas Gohr 1901078ec26SAndreas Gohr // we simply take the first CN= part of the group DN and return it as the group name 1911078ec26SAndreas Gohr // this should be correct for ActiveDirectory and saves us additional LDAP queries 1921078ec26SAndreas Gohr if ($userentry->has('memberOf')) { 193b21740b4SAndreas Gohr foreach ($userentry->get('memberOf')->getValues() as $dn) { 194b21740b4SAndreas Gohr list($cn) = explode(',', $dn, 2); 19580ac552fSAndreas Gohr $groups[] = PhpString::strtolower(substr($cn, 3)); 1961078ec26SAndreas Gohr } 1971078ec26SAndreas Gohr } 1981078ec26SAndreas Gohr 1991078ec26SAndreas Gohr // resolving the primary group in AD is complicated but basically never needed 2001078ec26SAndreas Gohr // http://support.microsoft.com/?kbid=321360 2011078ec26SAndreas Gohr $gid = $userentry->get('primaryGroupID')->firstValue(); 2021078ec26SAndreas Gohr if ($gid == 513) { 20380ac552fSAndreas Gohr $groups[] = 'domain users'; 2041078ec26SAndreas Gohr } 2051078ec26SAndreas Gohr 206*1b0eb9b3SAndreas Gohr sort($groups); 2071078ec26SAndreas Gohr return $groups; 2081078ec26SAndreas Gohr } 2091078ec26SAndreas Gohr} 210