11078ec26SAndreas Gohr<?php 21078ec26SAndreas Gohr 31078ec26SAndreas Gohrnamespace dokuwiki\plugin\pureldap\classes; 41078ec26SAndreas Gohr 5*80ac552fSAndreas 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'), 23*80ac552fSAndreas 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 70b21740b4SAndreas Gohr return $groups; 71b21740b4SAndreas Gohr } 72b21740b4SAndreas Gohr 73b21740b4SAndreas Gohr /** 74b21740b4SAndreas Gohr * Fetch users matching the given filters 75b21740b4SAndreas Gohr * 76b21740b4SAndreas Gohr * @param array $match 77b21740b4SAndreas Gohr * @param string $filtermethod The method to use for filtering 78b21740b4SAndreas Gohr * @return array 79b21740b4SAndreas Gohr */ 80b21740b4SAndreas Gohr public function getFilteredUsers($match, $filtermethod = 'equal') 81b21740b4SAndreas Gohr { 82b21740b4SAndreas Gohr if (!$this->autoAuth()) return []; 83b21740b4SAndreas Gohr 84b21740b4SAndreas Gohr $filter = Filters::and(Filters::equal('objectClass', 'user')); 85b21740b4SAndreas Gohr if (isset($match['user'])) { 86b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('userPrincipalName', $match['user'])); 87b21740b4SAndreas Gohr } 88b21740b4SAndreas Gohr if (isset($match['name'])) { 89b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('displayName', $match['name'])); 90b21740b4SAndreas Gohr } 91b21740b4SAndreas Gohr if (isset($match['mail'])) { 92b21740b4SAndreas Gohr $filter->add(Filters::$filtermethod('mail', $match['mail'])); 93b21740b4SAndreas Gohr } 94b21740b4SAndreas Gohr if (isset($match['grps'])) { 95b21740b4SAndreas Gohr // memberOf can not be checked with a substring match, so we need to get the right groups first 96b21740b4SAndreas Gohr $groups = $this->getGroups($match['grps'], $filtermethod); 97b21740b4SAndreas Gohr $or = Filters::or(); 98b21740b4SAndreas Gohr foreach ($groups as $dn => $group) { 99b21740b4SAndreas Gohr $or->add(Filters::equal('memberOf', $dn)); 100b21740b4SAndreas Gohr } 101b21740b4SAndreas Gohr $filter->add($or); 102b21740b4SAndreas Gohr } 103b21740b4SAndreas Gohr $this->debug('Searching ' . $filter->toString(), __FILE__, __LINE__); 104b21740b4SAndreas Gohr $search = Operations::search($filter); 105b21740b4SAndreas Gohr $paging = $this->ldap->paging($search); 106b21740b4SAndreas Gohr 107b21740b4SAndreas Gohr $users = []; 108b21740b4SAndreas Gohr while ($paging->hasEntries()) { 109b21740b4SAndreas Gohr try { 110b21740b4SAndreas Gohr $entries = $paging->getEntries(); 111b21740b4SAndreas Gohr } catch (ProtocolException $e) { 112b21740b4SAndreas Gohr $this->fatal($e); 113*80ac552fSAndreas Gohr break; // we abort and return what we have so far 114b21740b4SAndreas Gohr } 115b21740b4SAndreas Gohr 116b21740b4SAndreas Gohr foreach ($entries as $entry) { 117*80ac552fSAndreas Gohr $userinfo = $this->entry2User($entry); 118*80ac552fSAndreas Gohr $users[$userinfo['user']] = $this->entry2User($entry); 119b21740b4SAndreas Gohr } 120b21740b4SAndreas Gohr } 121b21740b4SAndreas Gohr 122b21740b4SAndreas Gohr return $users; 123b21740b4SAndreas Gohr } 124b21740b4SAndreas Gohr 125b21740b4SAndreas Gohr /** 126*80ac552fSAndreas Gohr * @inheritDoc 127*80ac552fSAndreas Gohr * userPrincipalName in the form <user>@<domain> 128*80ac552fSAndreas Gohr */ 129*80ac552fSAndreas Gohr public function qualifiedUser($user) 130*80ac552fSAndreas Gohr { 131*80ac552fSAndreas Gohr $user = PhpString::strtolower($user); 132*80ac552fSAndreas Gohr if (!$this->config['domain']) return $user; 133*80ac552fSAndreas Gohr 134*80ac552fSAndreas Gohr list($user, $domain) = explode('@', $user, 2); 135*80ac552fSAndreas Gohr if (!$domain) { 136*80ac552fSAndreas Gohr $domain = $this->config['domain']; 137*80ac552fSAndreas Gohr } 138*80ac552fSAndreas Gohr 139*80ac552fSAndreas Gohr return $user . '@' . $domain; 140*80ac552fSAndreas Gohr } 141*80ac552fSAndreas Gohr 142*80ac552fSAndreas Gohr /** 143*80ac552fSAndreas Gohr * @inheritDoc 144*80ac552fSAndreas Gohr * Removes the account suffix from the given user 145*80ac552fSAndreas Gohr */ 146*80ac552fSAndreas Gohr public function simpleUser($user) 147*80ac552fSAndreas Gohr { 148*80ac552fSAndreas Gohr $user = PhpString::strtolower($user); 149*80ac552fSAndreas Gohr if (!$this->config['domain']) return $user; 150*80ac552fSAndreas Gohr 151*80ac552fSAndreas Gohr // strip account suffix 152*80ac552fSAndreas Gohr list($luser, $suffix) = explode('@', $user, 2); 153*80ac552fSAndreas Gohr if ($suffix === $this->config['domain']) return $luser; 154*80ac552fSAndreas Gohr 155*80ac552fSAndreas Gohr return $user; 156*80ac552fSAndreas Gohr } 157*80ac552fSAndreas Gohr 158*80ac552fSAndreas Gohr /** 159b21740b4SAndreas Gohr * Transform an LDAP entry to a user info array 160b21740b4SAndreas Gohr * 161b21740b4SAndreas Gohr * @param Entry $entry 162b21740b4SAndreas Gohr * @return array 163b21740b4SAndreas Gohr */ 164b21740b4SAndreas Gohr protected function entry2User(Entry $entry) 165b21740b4SAndreas Gohr { 1661078ec26SAndreas Gohr return [ 167*80ac552fSAndreas Gohr 'user' => $this->simpleUser($this->attr2str($entry->get('UserPrincipalName'))), 1681078ec26SAndreas Gohr 'name' => $this->attr2str($entry->get('DisplayName')) ?: $this->attr2str($entry->get('Name')), 1691078ec26SAndreas Gohr 'mail' => $this->attr2str($entry->get('mail')), 1701078ec26SAndreas Gohr 'dn' => $entry->getDn()->toString(), 1711078ec26SAndreas Gohr 'grps' => $this->getUserGroups($entry), // we always return groups because its currently inexpensive 1721078ec26SAndreas Gohr ]; 1731078ec26SAndreas Gohr } 1741078ec26SAndreas Gohr 1751078ec26SAndreas Gohr /** 1761078ec26SAndreas Gohr * Get the list of groups the given user is member of 1771078ec26SAndreas Gohr * 1781078ec26SAndreas Gohr * This method currently does no LDAP queries and thus is inexpensive. 1791078ec26SAndreas Gohr * 1801078ec26SAndreas Gohr * @param Entry $userentry 1811078ec26SAndreas Gohr * @return array 1821078ec26SAndreas Gohr * @todo implement nested group memberships 1831078ec26SAndreas Gohr */ 1841078ec26SAndreas Gohr protected function getUserGroups(Entry $userentry) 1851078ec26SAndreas Gohr { 1861078ec26SAndreas Gohr $groups = [$this->config['defaultgroup']]; // always add default 1871078ec26SAndreas Gohr 1881078ec26SAndreas Gohr // we simply take the first CN= part of the group DN and return it as the group name 1891078ec26SAndreas Gohr // this should be correct for ActiveDirectory and saves us additional LDAP queries 1901078ec26SAndreas Gohr if ($userentry->has('memberOf')) { 191b21740b4SAndreas Gohr foreach ($userentry->get('memberOf')->getValues() as $dn) { 192b21740b4SAndreas Gohr list($cn) = explode(',', $dn, 2); 193*80ac552fSAndreas Gohr $groups[] = PhpString::strtolower(substr($cn, 3)); 1941078ec26SAndreas Gohr } 1951078ec26SAndreas Gohr } 1961078ec26SAndreas Gohr 1971078ec26SAndreas Gohr // resolving the primary group in AD is complicated but basically never needed 1981078ec26SAndreas Gohr // http://support.microsoft.com/?kbid=321360 1991078ec26SAndreas Gohr $gid = $userentry->get('primaryGroupID')->firstValue(); 2001078ec26SAndreas Gohr if ($gid == 513) { 201*80ac552fSAndreas Gohr $groups[] = 'domain users'; 2021078ec26SAndreas Gohr } 2031078ec26SAndreas Gohr 2041078ec26SAndreas Gohr return $groups; 2051078ec26SAndreas Gohr } 2061078ec26SAndreas Gohr} 207