group_ldap.php 21 KB

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