group_ldap.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. <?php
  2. /**
  3. * @author Alexander Bergolth <leo@strike.wu.ac.at>
  4. * @author Andreas Fischer <bantu@owncloud.com>
  5. * @author Arthur Schiwon <blizzz@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\user_ldap;
  30. use OCA\user_ldap\lib\Access;
  31. use OCA\user_ldap\lib\BackendUtility;
  32. class GROUP_LDAP extends BackendUtility implements \OCP\GroupInterface {
  33. protected $enabled = false;
  34. /**
  35. * @var string[] $cachedGroupMembers array of users with gid as key
  36. */
  37. protected $cachedGroupMembers = array();
  38. /**
  39. * @var string[] $cachedGroupsByMember array of groups with uid as key
  40. */
  41. protected $cachedGroupsByMember = array();
  42. public function __construct(Access $access) {
  43. parent::__construct($access);
  44. $filter = $this->access->connection->ldapGroupFilter;
  45. $gassoc = $this->access->connection->ldapGroupMemberAssocAttr;
  46. if(!empty($filter) && !empty($gassoc)) {
  47. $this->enabled = true;
  48. }
  49. }
  50. /**
  51. * is user in group?
  52. * @param string $uid uid of the user
  53. * @param string $gid gid of the group
  54. * @return bool
  55. *
  56. * Checks whether the user is member of a group or not.
  57. */
  58. public function inGroup($uid, $gid) {
  59. if(!$this->enabled) {
  60. return false;
  61. }
  62. $cacheKey = 'inGroup'.$uid.':'.$gid;
  63. if($this->access->connection->isCached($cacheKey)) {
  64. return $this->access->connection->getFromCache($cacheKey);
  65. }
  66. $userDN = $this->access->username2dn($uid);
  67. if(isset($this->cachedGroupMembers[$gid])) {
  68. $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]);
  69. return $isInGroup;
  70. }
  71. $cacheKeyMembers = 'inGroup-members:'.$gid;
  72. if($this->access->connection->isCached($cacheKeyMembers)) {
  73. $members = $this->access->connection->getFromCache($cacheKeyMembers);
  74. $this->cachedGroupMembers[$gid] = $members;
  75. $isInGroup = in_array($userDN, $members);
  76. $this->access->connection->writeToCache($cacheKey, $isInGroup);
  77. return $isInGroup;
  78. }
  79. $groupDN = $this->access->groupname2dn($gid);
  80. // just in case
  81. if(!$groupDN || !$userDN) {
  82. $this->access->connection->writeToCache($cacheKey, false);
  83. return false;
  84. }
  85. //check primary group first
  86. if($gid === $this->getUserPrimaryGroup($userDN)) {
  87. $this->access->connection->writeToCache($cacheKey, true);
  88. return true;
  89. }
  90. //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course.
  91. $members = $this->_groupMembers($groupDN);
  92. $members = array_keys($members); // uids are returned as keys
  93. if(!is_array($members) || count($members) === 0) {
  94. $this->access->connection->writeToCache($cacheKey, false);
  95. return false;
  96. }
  97. //extra work if we don't get back user DNs
  98. if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
  99. $dns = array();
  100. $filterParts = array();
  101. $bytes = 0;
  102. foreach($members as $mid) {
  103. $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter);
  104. $filterParts[] = $filter;
  105. $bytes += strlen($filter);
  106. if($bytes >= 9000000) {
  107. // AD has a default input buffer of 10 MB, we do not want
  108. // to take even the chance to exceed it
  109. $filter = $this->access->combineFilterWithOr($filterParts);
  110. $bytes = 0;
  111. $filterParts = array();
  112. $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts));
  113. $dns = array_merge($dns, $users);
  114. }
  115. }
  116. if(count($filterParts) > 0) {
  117. $filter = $this->access->combineFilterWithOr($filterParts);
  118. $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts));
  119. $dns = array_merge($dns, $users);
  120. }
  121. $members = $dns;
  122. }
  123. $isInGroup = in_array($userDN, $members);
  124. $this->access->connection->writeToCache($cacheKey, $isInGroup);
  125. $this->access->connection->writeToCache($cacheKeyMembers, $members);
  126. $this->cachedGroupMembers[$gid] = $members;
  127. return $isInGroup;
  128. }
  129. /**
  130. * @param string $dnGroup
  131. * @param array|null &$seen
  132. * @return array|mixed|null
  133. */
  134. private function _groupMembers($dnGroup, &$seen = null) {
  135. if ($seen === null) {
  136. $seen = array();
  137. }
  138. $allMembers = array();
  139. if (array_key_exists($dnGroup, $seen)) {
  140. // avoid loops
  141. return array();
  142. }
  143. // used extensively in cron job, caching makes sense for nested groups
  144. $cacheKey = '_groupMembers'.$dnGroup;
  145. if($this->access->connection->isCached($cacheKey)) {
  146. return $this->access->connection->getFromCache($cacheKey);
  147. }
  148. $seen[$dnGroup] = 1;
  149. $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr,
  150. $this->access->connection->ldapGroupFilter);
  151. if (is_array($members)) {
  152. foreach ($members as $memberDN) {
  153. $allMembers[$memberDN] = 1;
  154. $nestedGroups = $this->access->connection->ldapNestedGroups;
  155. if (!empty($nestedGroups)) {
  156. $subMembers = $this->_groupMembers($memberDN, $seen);
  157. if ($subMembers) {
  158. $allMembers = array_merge($allMembers, $subMembers);
  159. }
  160. }
  161. }
  162. }
  163. $this->access->connection->writeToCache($cacheKey, $allMembers);
  164. return $allMembers;
  165. }
  166. /**
  167. * translates a primary group ID into an ownCloud internal name
  168. * @param string $gid as given by primaryGroupID on AD
  169. * @param string $dn a DN that belongs to the same domain as the group
  170. * @return string|bool
  171. */
  172. public function primaryGroupID2Name($gid, $dn) {
  173. $cacheKey = 'primaryGroupIDtoName';
  174. if($this->access->connection->isCached($cacheKey)) {
  175. $groupNames = $this->access->connection->getFromCache($cacheKey);
  176. if(isset($groupNames[$gid])) {
  177. return $groupNames[$gid];
  178. }
  179. }
  180. $domainObjectSid = $this->access->getSID($dn);
  181. if($domainObjectSid === false) {
  182. return false;
  183. }
  184. //we need to get the DN from LDAP
  185. $filter = $this->access->combineFilterWithAnd(array(
  186. $this->access->connection->ldapGroupFilter,
  187. 'objectsid=' . $domainObjectSid . '-' . $gid
  188. ));
  189. $result = $this->access->searchGroups($filter, array('dn'), 1);
  190. if(empty($result)) {
  191. return false;
  192. }
  193. $dn = $result[0];
  194. //and now the group name
  195. //NOTE once we have separate ownCloud group IDs and group names we can
  196. //directly read the display name attribute instead of the DN
  197. $name = $this->access->dn2groupname($dn);
  198. $this->access->connection->writeToCache($cacheKey, $name);
  199. return $name;
  200. }
  201. /**
  202. * returns the entry's primary group ID
  203. * @param string $dn
  204. * @param string $attribute
  205. * @return string|bool
  206. */
  207. private function getEntryGroupID($dn, $attribute) {
  208. $value = $this->access->readAttribute($dn, $attribute);
  209. if(is_array($value) && !empty($value)) {
  210. return $value[0];
  211. }
  212. return false;
  213. }
  214. /**
  215. * returns the group's primary ID
  216. * @param string $dn
  217. * @return string|bool
  218. */
  219. public function getGroupPrimaryGroupID($dn) {
  220. return $this->getEntryGroupID($dn, 'primaryGroupToken');
  221. }
  222. /**
  223. * returns the user's primary group ID
  224. * @param string $dn
  225. * @return string|bool
  226. */
  227. public function getUserPrimaryGroupIDs($dn) {
  228. return $this->getEntryGroupID($dn, 'primaryGroupID');
  229. }
  230. /**
  231. * returns a filter for a "users in primary group" search or count operation
  232. *
  233. * @param string $groupDN
  234. * @param string $search
  235. * @return string
  236. * @throws \Exception
  237. */
  238. private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') {
  239. $groupID = $this->getGroupPrimaryGroupID($groupDN);
  240. if($groupID === false) {
  241. throw new \Exception('Not a valid group');
  242. }
  243. $filterParts = [];
  244. $filterParts[] = $this->access->getFilterForUserCount();
  245. if(!empty($search)) {
  246. $filterParts[] = $this->access->getFilterPartForUserSearch($search);
  247. }
  248. $filterParts[] = 'primaryGroupID=' . $groupID;
  249. $filter = $this->access->combineFilterWithAnd($filterParts);
  250. return $filter;
  251. }
  252. /**
  253. * returns a list of users that have the given group as primary group
  254. *
  255. * @param string $groupDN
  256. * @param string $search
  257. * @param int $limit
  258. * @param int $offset
  259. * @return string[]
  260. */
  261. public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) {
  262. try {
  263. $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search);
  264. return $this->access->fetchListOfUsers(
  265. $filter,
  266. array($this->access->connection->ldapUserDisplayName, 'dn'),
  267. $limit,
  268. $offset
  269. );
  270. } catch (\Exception $e) {
  271. return array();
  272. }
  273. }
  274. /**
  275. * returns the number of users that have the given group as primary group
  276. *
  277. * @param string $groupDN
  278. * @param string $search
  279. * @param int $limit
  280. * @param int $offset
  281. * @return int
  282. */
  283. public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) {
  284. try {
  285. $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search);
  286. $users = $this->access->countUsers($filter, array('dn'), $limit, $offset);
  287. return (int)$users;
  288. } catch (\Exception $e) {
  289. return 0;
  290. }
  291. }
  292. /**
  293. * gets the primary group of a user
  294. * @param string $dn
  295. * @return string
  296. */
  297. public function getUserPrimaryGroup($dn) {
  298. $groupID = $this->getUserPrimaryGroupIDs($dn);
  299. if($groupID !== false) {
  300. $groupName = $this->primaryGroupID2Name($groupID, $dn);
  301. if($groupName !== false) {
  302. return $groupName;
  303. }
  304. }
  305. return false;
  306. }
  307. /**
  308. * Get all groups a user belongs to
  309. * @param string $uid Name of the user
  310. * @return array with group names
  311. *
  312. * This function fetches all groups a user belongs to. It does not check
  313. * if the user exists at all.
  314. */
  315. public function getUserGroups($uid) {
  316. if(!$this->enabled) {
  317. return array();
  318. }
  319. $cacheKey = 'getUserGroups'.$uid;
  320. if($this->access->connection->isCached($cacheKey)) {
  321. return $this->access->connection->getFromCache($cacheKey);
  322. }
  323. $userDN = $this->access->username2dn($uid);
  324. if(!$userDN) {
  325. $this->access->connection->writeToCache($cacheKey, array());
  326. return array();
  327. }
  328. //uniqueMember takes DN, memberuid the uid, so we need to distinguish
  329. if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember')
  330. || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member')
  331. ) {
  332. $uid = $userDN;
  333. } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') {
  334. $result = $this->access->readAttribute($userDN, 'uid');
  335. if ($result === false) {
  336. \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '.
  337. $this->access->connection->ldapHost, \OCP\Util::DEBUG);
  338. }
  339. $uid = $result[0];
  340. } else {
  341. // just in case
  342. $uid = $userDN;
  343. }
  344. if(isset($this->cachedGroupsByMember[$uid])) {
  345. $groups = $this->cachedGroupsByMember[$uid];
  346. } else {
  347. $groups = array_values($this->getGroupsByMember($uid));
  348. $groups = $this->access->ownCloudGroupNames($groups);
  349. $this->cachedGroupsByMember[$uid] = $groups;
  350. }
  351. $primaryGroup = $this->getUserPrimaryGroup($userDN);
  352. if($primaryGroup !== false) {
  353. $groups[] = $primaryGroup;
  354. }
  355. $groups = array_unique($groups, SORT_LOCALE_STRING);
  356. $this->access->connection->writeToCache($cacheKey, $groups);
  357. return $groups;
  358. }
  359. /**
  360. * @param string $dn
  361. * @param array|null &$seen
  362. * @return array
  363. */
  364. private function getGroupsByMember($dn, &$seen = null) {
  365. if ($seen === null) {
  366. $seen = array();
  367. }
  368. $allGroups = array();
  369. if (array_key_exists($dn, $seen)) {
  370. // avoid loops
  371. return array();
  372. }
  373. $seen[$dn] = true;
  374. $filter = $this->access->combineFilterWithAnd(array(
  375. $this->access->connection->ldapGroupFilter,
  376. $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn
  377. ));
  378. $groups = $this->access->fetchListOfGroups($filter,
  379. array($this->access->connection->ldapGroupDisplayName, 'dn'));
  380. if (is_array($groups)) {
  381. foreach ($groups as $groupobj) {
  382. $groupDN = $groupobj['dn'];
  383. $allGroups[$groupDN] = $groupobj;
  384. $nestedGroups = $this->access->connection->ldapNestedGroups;
  385. if (!empty($nestedGroups)) {
  386. $supergroups = $this->getGroupsByMember($groupDN, $seen);
  387. if (is_array($supergroups) && (count($supergroups)>0)) {
  388. $allGroups = array_merge($allGroups, $supergroups);
  389. }
  390. }
  391. }
  392. }
  393. return $allGroups;
  394. }
  395. /**
  396. * get a list of all users in a group
  397. *
  398. * @param string $gid
  399. * @param string $search
  400. * @param int $limit
  401. * @param int $offset
  402. * @return array with user ids
  403. */
  404. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  405. if(!$this->enabled) {
  406. return array();
  407. }
  408. if(!$this->groupExists($gid)) {
  409. return array();
  410. }
  411. $search = $this->access->escapeFilterPart($search, true);
  412. $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset;
  413. // check for cache of the exact query
  414. $groupUsers = $this->access->connection->getFromCache($cacheKey);
  415. if(!is_null($groupUsers)) {
  416. return $groupUsers;
  417. }
  418. // check for cache of the query without limit and offset
  419. $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search);
  420. if(!is_null($groupUsers)) {
  421. $groupUsers = array_slice($groupUsers, $offset, $limit);
  422. $this->access->connection->writeToCache($cacheKey, $groupUsers);
  423. return $groupUsers;
  424. }
  425. if($limit === -1) {
  426. $limit = null;
  427. }
  428. $groupDN = $this->access->groupname2dn($gid);
  429. if(!$groupDN) {
  430. // group couldn't be found, return empty resultset
  431. $this->access->connection->writeToCache($cacheKey, array());
  432. return array();
  433. }
  434. $members = array_keys($this->_groupMembers($groupDN));
  435. if(!$members) {
  436. //in case users could not be retrieved, return empty result set
  437. $this->access->connection->writeToCache($cacheKey, array());
  438. return array();
  439. }
  440. $groupUsers = array();
  441. $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid');
  442. foreach($members as $member) {
  443. if($isMemberUid) {
  444. //we got uids, need to get their DNs to 'translate' them to user names
  445. $filter = $this->access->combineFilterWithAnd(array(
  446. \OCP\Util::mb_str_replace('%uid', $member,
  447. $this->access->connection->ldapLoginFilter, 'UTF-8'),
  448. $this->access->getFilterPartForUserSearch($search)
  449. ));
  450. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  451. if(count($ldap_users) < 1) {
  452. continue;
  453. }
  454. $groupUsers[] = $this->access->dn2username($ldap_users[0]);
  455. } else {
  456. //we got DNs, check if we need to filter by search or we can give back all of them
  457. if(!empty($search)) {
  458. if(!$this->access->readAttribute($member,
  459. $this->access->connection->ldapUserDisplayName,
  460. $this->access->getFilterPartForUserSearch($search))) {
  461. continue;
  462. }
  463. }
  464. // dn2username will also check if the users belong to the allowed base
  465. if($ocname = $this->access->dn2username($member)) {
  466. $groupUsers[] = $ocname;
  467. }
  468. }
  469. }
  470. natsort($groupUsers);
  471. $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers);
  472. $groupUsers = array_slice($groupUsers, $offset, $limit);
  473. //and get users that have the group as primary
  474. $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset);
  475. $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers));
  476. $this->access->connection->writeToCache($cacheKey, $groupUsers);
  477. return $groupUsers;
  478. }
  479. /**
  480. * returns the number of users in a group, who match the search term
  481. * @param string $gid the internal group name
  482. * @param string $search optional, a search string
  483. * @return int|bool
  484. */
  485. public function countUsersInGroup($gid, $search = '') {
  486. $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search;
  487. if(!$this->enabled || !$this->groupExists($gid)) {
  488. return false;
  489. }
  490. $groupUsers = $this->access->connection->getFromCache($cacheKey);
  491. if(!is_null($groupUsers)) {
  492. return $groupUsers;
  493. }
  494. $groupDN = $this->access->groupname2dn($gid);
  495. if(!$groupDN) {
  496. // group couldn't be found, return empty result set
  497. $this->access->connection->writeToCache($cacheKey, false);
  498. return false;
  499. }
  500. $members = array_keys($this->_groupMembers($groupDN));
  501. if(!$members) {
  502. //in case users could not be retrieved, return empty result set
  503. $this->access->connection->writeToCache($cacheKey, false);
  504. return false;
  505. }
  506. if(empty($search)) {
  507. $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, '');
  508. $groupUsers = count($members) + $primaryUsers;
  509. $this->access->connection->writeToCache($cacheKey, $groupUsers);
  510. return $groupUsers;
  511. }
  512. $search = $this->access->escapeFilterPart($search, true);
  513. $isMemberUid =
  514. (strtolower($this->access->connection->ldapGroupMemberAssocAttr)
  515. === 'memberuid');
  516. //we need to apply the search filter
  517. //alternatives that need to be checked:
  518. //a) get all users by search filter and array_intersect them
  519. //b) a, but only when less than 1k 10k ?k users like it is
  520. //c) put all DNs|uids in a LDAP filter, combine with the search string
  521. // and let it count.
  522. //For now this is not important, because the only use of this method
  523. //does not supply a search string
  524. $groupUsers = array();
  525. foreach($members as $member) {
  526. if($isMemberUid) {
  527. //we got uids, need to get their DNs to 'translate' them to user names
  528. $filter = $this->access->combineFilterWithAnd(array(
  529. \OCP\Util::mb_str_replace('%uid', $member,
  530. $this->access->connection->ldapLoginFilter, 'UTF-8'),
  531. $this->access->getFilterPartForUserSearch($search)
  532. ));
  533. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  534. if(count($ldap_users) < 1) {
  535. continue;
  536. }
  537. $groupUsers[] = $this->access->dn2username($ldap_users[0]);
  538. } else {
  539. //we need to apply the search filter now
  540. if(!$this->access->readAttribute($member,
  541. $this->access->connection->ldapUserDisplayName,
  542. $this->access->getFilterPartForUserSearch($search))) {
  543. continue;
  544. }
  545. // dn2username will also check if the users belong to the allowed base
  546. if($ocname = $this->access->dn2username($member)) {
  547. $groupUsers[] = $ocname;
  548. }
  549. }
  550. }
  551. //and get users that have the group as primary
  552. $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search);
  553. return count($groupUsers) + $primaryUsers;
  554. }
  555. /**
  556. * get a list of all groups
  557. *
  558. * @param string $search
  559. * @param $limit
  560. * @param int $offset
  561. * @return array with group names
  562. *
  563. * Returns a list with all groups (used by getGroups)
  564. */
  565. protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) {
  566. if(!$this->enabled) {
  567. return array();
  568. }
  569. $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset;
  570. //Check cache before driving unnecessary searches
  571. \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG);
  572. $ldap_groups = $this->access->connection->getFromCache($cacheKey);
  573. if(!is_null($ldap_groups)) {
  574. return $ldap_groups;
  575. }
  576. // if we'd pass -1 to LDAP search, we'd end up in a Protocol
  577. // error. With a limit of 0, we get 0 results. So we pass null.
  578. if($limit <= 0) {
  579. $limit = null;
  580. }
  581. $filter = $this->access->combineFilterWithAnd(array(
  582. $this->access->connection->ldapGroupFilter,
  583. $this->access->getFilterPartForGroupSearch($search)
  584. ));
  585. \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG);
  586. $ldap_groups = $this->access->fetchListOfGroups($filter,
  587. array($this->access->connection->ldapGroupDisplayName, 'dn'),
  588. $limit,
  589. $offset);
  590. $ldap_groups = $this->access->ownCloudGroupNames($ldap_groups);
  591. $this->access->connection->writeToCache($cacheKey, $ldap_groups);
  592. return $ldap_groups;
  593. }
  594. /**
  595. * get a list of all groups using a paged search
  596. *
  597. * @param string $search
  598. * @param int $limit
  599. * @param int $offset
  600. * @return array with group names
  601. *
  602. * Returns a list with all groups
  603. * Uses a paged search if available to override a
  604. * server side search limit.
  605. * (active directory has a limit of 1000 by default)
  606. */
  607. public function getGroups($search = '', $limit = -1, $offset = 0) {
  608. if(!$this->enabled) {
  609. return array();
  610. }
  611. $search = $this->access->escapeFilterPart($search, true);
  612. $pagingSize = $this->access->connection->ldapPagingSize;
  613. if ((! $this->access->connection->hasPagedResultSupport)
  614. || empty($pagingSize)) {
  615. return $this->getGroupsChunk($search, $limit, $offset);
  616. }
  617. $maxGroups = 100000; // limit max results (just for safety reasons)
  618. if ($limit > -1) {
  619. $overallLimit = min($limit + $offset, $maxGroups);
  620. } else {
  621. $overallLimit = $maxGroups;
  622. }
  623. $chunkOffset = $offset;
  624. $allGroups = array();
  625. while ($chunkOffset < $overallLimit) {
  626. $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset);
  627. $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset);
  628. $nread = count($ldapGroups);
  629. \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG);
  630. if ($nread) {
  631. $allGroups = array_merge($allGroups, $ldapGroups);
  632. $chunkOffset += $nread;
  633. }
  634. if ($nread < $chunkLimit) {
  635. break;
  636. }
  637. }
  638. return $allGroups;
  639. }
  640. /**
  641. * @param string $group
  642. * @return bool
  643. */
  644. public function groupMatchesFilter($group) {
  645. return (strripos($group, $this->groupSearch) !== false);
  646. }
  647. /**
  648. * check if a group exists
  649. * @param string $gid
  650. * @return bool
  651. */
  652. public function groupExists($gid) {
  653. if($this->access->connection->isCached('groupExists'.$gid)) {
  654. return $this->access->connection->getFromCache('groupExists'.$gid);
  655. }
  656. //getting dn, if false the group does not exist. If dn, it may be mapped
  657. //only, requires more checking.
  658. $dn = $this->access->groupname2dn($gid);
  659. if(!$dn) {
  660. $this->access->connection->writeToCache('groupExists'.$gid, false);
  661. return false;
  662. }
  663. //if group really still exists, we will be able to read its objectclass
  664. if(!is_array($this->access->readAttribute($dn, ''))) {
  665. $this->access->connection->writeToCache('groupExists'.$gid, false);
  666. return false;
  667. }
  668. $this->access->connection->writeToCache('groupExists'.$gid, true);
  669. return true;
  670. }
  671. /**
  672. * Check if backend implements actions
  673. * @param int $actions bitwise-or'ed actions
  674. * @return boolean
  675. *
  676. * Returns the supported actions as int to be
  677. * compared with OC_USER_BACKEND_CREATE_USER etc.
  678. */
  679. public function implementsActions($actions) {
  680. return (bool)(OC_GROUP_BACKEND_COUNT_USERS & $actions);
  681. }
  682. }