UsersLdapHelper.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. class UsersLdapHelper {
  3. public static function getUser($userName, $allAttrs = false) {
  4. $ldapUsers = array();
  5. //$attrMap = array('uid', 'apple-generateduid', 'givenName', 'uidNumber', 'cn', 'mail', 'apple-user-mailattribute');// (givenName, sn) = cn
  6. $attrMap = array();
  7. $attrMap['uid'] = 'uid';
  8. $attrMap['apple-generateduid'] = 'apple-generateduid';
  9. $attrMap['givenName'] = 'givenName';
  10. $attrMap['uidNumber'] = 'uidNumber';
  11. $attrMap['cn'] = 'cn';
  12. $attrMap['mail'] = 'mail';
  13. $attrMap['carLicense'] = 'carLicense';
  14. Lib::loadClass('LDAP');
  15. $ldap = LDAP::getInstance();
  16. $filter = (false !== strpos($userName, '@'))? "(mail={$userName})" : "(uid={$userName})";
  17. $filter = "(&(objectClass=apple-user){$filter})";// apple-user posixAccount inetOrgPerson
  18. //$filter = "(&(objectClass=inetOrgPerson){$filter})";
  19. $attributes = array();
  20. $res = $ldap->search($filter, 'cn=users', $attributes);
  21. $entry = $ldap->first_entry($res);
  22. while ($entry) {
  23. $attrs = $ldap->get_attributes($entry);
  24. DBG::_('DBG_L', '>0', "attrs", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  25. $userObj = new stdClass();
  26. if ($allAttrs) {
  27. for ($i = 0; $i < $attrs['count']; $i++) {
  28. $vAttrName = $attrs[$i];
  29. $vAttrVal = $attrs[$vAttrName];
  30. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  31. $userObj->{$vAttrName} = $vAttrVal[0];
  32. }
  33. }
  34. } else {
  35. foreach ($attrMap as $kAttrName => $vFldName) {
  36. $vAttrVal = V::get($kAttrName, '', $attrs);
  37. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  38. if ($vAttrVal['count'] > 1) {
  39. $userObj->{$vFldName} = array();
  40. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  41. $userObj->{$vFldName}[] = $vAttrVal[$j];
  42. }
  43. } else {
  44. $userObj->{$vFldName} = $vAttrVal[0];
  45. }
  46. }
  47. }
  48. }
  49. $ldapUsers[] = $userObj;
  50. $entry = $ldap->next_entry($entry);
  51. }
  52. $ldap->free_result($res);
  53. return $ldapUsers;
  54. }
  55. public static function getUsersAll() {
  56. $allLdapUsers = array();
  57. $attrMap = array('uid', 'apple-generateduid', 'givenName', 'uidNumber', 'cn', 'mail', 'carLicense');// (givenName, sn) = cn
  58. Lib::loadClass('LDAP');
  59. $ldap = LDAP::getInstance();
  60. $filter = "(objectClass=apple-user)";// apple-user posixAccount inetOrgPerson
  61. $attributes = array();
  62. $res = $ldap->search($filter, 'cn=users', $attributes);
  63. $entry = $ldap->first_entry($res);
  64. while ($entry) {
  65. $attrs = $ldap->get_attributes($entry);
  66. $userObj = new stdClass();
  67. foreach ($attrMap as $vAttrName) {
  68. $vAttrVal = V::get($vAttrName, '', $attrs);
  69. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  70. $userObj->{$vAttrName} = $vAttrVal[0];
  71. }
  72. }
  73. $allLdapUsers[] = $userObj;
  74. $entry = $ldap->next_entry($entry);
  75. }
  76. $ldap->free_result($res);
  77. return $allLdapUsers;
  78. }
  79. public static function getUserGroups($userName, $authLDAPSubGroupDepth = 3) {
  80. $userLdapGroups = array();
  81. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  82. Lib::loadClass('LDAP');
  83. $ldap = LDAP::getInstance();
  84. $lastLoopFound = array();
  85. for ($i = 0; $i <= $authLDAPSubGroupDepth; $i++) {
  86. $userLdapGroupsAdd = array();
  87. if ($i == 0) {
  88. $filter = "(&(objectClass=apple-group)(memberUid={$userName}))";
  89. } else {
  90. $queryOrArr = array();
  91. foreach ($lastLoopFound as $vAppleUid) {
  92. $queryOrArr[] = "apple-group-nestedgroup={$vAppleUid}";
  93. }
  94. if (!empty($queryOrArr)) {
  95. $queryOr = '(|(' . implode(')(', $queryOrArr) . '))';
  96. $filter = "(&(objectClass=apple-group){$queryOr})";
  97. } else {
  98. break;
  99. }
  100. }
  101. $attributes = array();
  102. $res = $ldap->search($filter, 'cn=groups', $attributes);
  103. DBG::_('DBG_L', '>0', "search", $filter, __CLASS__, __FUNCTION__, __LINE__);
  104. $entry = $ldap->first_entry($res);
  105. while ($entry) {
  106. $attrs = $ldap->get_attributes($entry);
  107. DBG::_('DBG_L', '>0', "user({$userName})", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  108. $groupObj = new stdClass();
  109. foreach ($attrMap as $kAttrName => $vField) {
  110. $vAttrVal = V::get($kAttrName, '', $attrs);
  111. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  112. $groupObj->{$vField} = $vAttrVal[0];
  113. }
  114. }
  115. if ($groupObj->cn && $groupObj->gidNumber) {
  116. $userLdapGroupsAdd[$groupObj->gidNumber] = $groupObj;
  117. } else {
  118. DBG::_(true, true, "Error: brak cn lub apple-generateduid", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  119. }
  120. $entry = $ldap->next_entry($entry);
  121. }
  122. $ldap->free_result($res);
  123. if (empty($userLdapGroupsAdd)) {
  124. break;
  125. } else {
  126. DBG::_('DBG_L', '>0', "userLdapGroupsAdd(".count($userLdapGroupsAdd).")", $userLdapGroupsAdd, __CLASS__, __FUNCTION__, __LINE__);
  127. $lastLoopFound = array();
  128. foreach ($userLdapGroupsAdd as $kAppleID => $vGroup) {
  129. $userLdapGroups[$kAppleID] = $vGroup;
  130. $lastLoopFound[] = $vGroup->appleUID;
  131. }
  132. //$lastLoopFound = array_keys($userLdapGroupsAdd);
  133. }
  134. }
  135. return $userLdapGroups;
  136. }
  137. public static function getUserGroupsByAppleUid($userUid, $authLDAPSubGroupDepth = 3) {
  138. $userLdapGroups = array();
  139. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  140. Lib::loadClass('LDAP');
  141. $ldap = LDAP::getInstance();
  142. $lastLoopFound = array();
  143. for ($i = 0; $i <= $authLDAPSubGroupDepth; $i++) {
  144. $userLdapGroupsAdd = array();
  145. if ($i == 0) {
  146. $filter = "(&(objectClass=apple-group)(apple-group-memberguid={$userUid}))";
  147. } else {
  148. $queryOrArr = array();
  149. foreach ($lastLoopFound as $vAppleUid) {
  150. $queryOrArr[] = "apple-group-nestedgroup={$vAppleUid}";
  151. }
  152. if (!empty($queryOrArr)) {
  153. $queryOr = '(|(' . implode(')(', $queryOrArr) . '))';
  154. $filter = "(&(objectClass=apple-group){$queryOr})";
  155. } else {
  156. break;
  157. }
  158. }
  159. $attributes = array();
  160. $res = $ldap->search($filter, 'cn=groups', $attributes);
  161. DBG::_('DBG_L', '>0', "search", $filter, __CLASS__, __FUNCTION__, __LINE__);
  162. $entry = $ldap->first_entry($res);
  163. while ($entry) {
  164. $attrs = $ldap->get_attributes($entry);
  165. DBG::_('DBG_L', '>0', "user({$userUid})", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  166. $groupObj = new stdClass();
  167. foreach ($attrMap as $kAttrName => $vField) {
  168. $vAttrVal = V::get($kAttrName, '', $attrs);
  169. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  170. $groupObj->{$vField} = $vAttrVal[0];
  171. }
  172. }
  173. if ($groupObj->cn && $groupObj->gidNumber) {
  174. $userLdapGroupsAdd[$groupObj->gidNumber] = $groupObj;
  175. } else {
  176. DBG::_(true, true, "Error: brak cn lub apple-generateduid", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  177. }
  178. $entry = $ldap->next_entry($entry);
  179. }
  180. $ldap->free_result($res);
  181. if (empty($userLdapGroupsAdd)) {
  182. break;
  183. } else {
  184. DBG::_('DBG_L', '>0', "userLdapGroupsAdd(".count($userLdapGroupsAdd).")", $userLdapGroupsAdd, __CLASS__, __FUNCTION__, __LINE__);
  185. $lastLoopFound = array();
  186. foreach ($userLdapGroupsAdd as $kAppleID => $vGroup) {
  187. $userLdapGroups[$kAppleID] = $vGroup;
  188. $lastLoopFound[] = $vGroup->appleUID;
  189. }
  190. //$lastLoopFound = array_keys($userLdapGroupsAdd);
  191. }
  192. }
  193. return $userLdapGroups;
  194. }
  195. public static function getGroupsByAppleUids($appleUids, $allAttrs = false) {
  196. $allLdapGroups = array();
  197. if (empty($appleUids)) return $allLdapGroups;
  198. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  199. $attrMap['apple-group-realname'] = 'realName';
  200. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  201. Lib::loadClass('LDAP');
  202. $ldap = LDAP::getInstance();
  203. $filters = array();
  204. foreach ($appleUids as $vAppleUid) $filters[] = "(apple-generateduid={$vAppleUid})";
  205. $filter = "(&(objectClass=apple-group)(|" . implode("", $filters) . "))";
  206. $attributes = array();
  207. $res = $ldap->search($filter, 'cn=groups', $attributes);
  208. $entry = $ldap->first_entry($res);
  209. while ($entry) {
  210. $attrs = $ldap->get_attributes($entry);
  211. $groupObj = new stdClass();
  212. if ($allAttrs) {
  213. for ($i = 0; $i < $attrs['count']; $i++) {
  214. $vAttrName = $attrs[$i];
  215. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  216. $vAttrVal = $attrs[$vAttrName];
  217. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  218. if ($vAttrVal['count'] > 1) {
  219. $groupObj->{$vFldName} = array();
  220. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  221. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  222. }
  223. } else {
  224. $groupObj->{$vFldName} = $vAttrVal[0];
  225. }
  226. }
  227. }
  228. } else {
  229. foreach ($attrMap as $kAttrName => $vFldName) {
  230. $vAttrVal = V::get($kAttrName, '', $attrs);
  231. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  232. if ($vAttrVal['count'] > 1) {
  233. $groupObj->{$vFldName} = array();
  234. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  235. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  236. }
  237. } else {
  238. $groupObj->{$vFldName} = $vAttrVal[0];
  239. }
  240. }
  241. }
  242. }
  243. if ($groupObj->cn && $groupObj->appleUID) {
  244. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  245. } else {
  246. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  247. }
  248. $entry = $ldap->next_entry($entry);
  249. }
  250. $ldap->free_result($res);
  251. return $allLdapGroups;
  252. }
  253. public static function getParentGroupsByAppleUID($appleUid, $allAttrs = false) {
  254. $allLdapGroups = array();
  255. if (!$appleUid) return $allLdapGroups;
  256. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  257. $attrMap['apple-group-realname'] = 'realName';
  258. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  259. Lib::loadClass('LDAP');
  260. $ldap = LDAP::getInstance();
  261. $filters = array();
  262. $filter = "(&(objectClass=apple-group)(apple-group-nestedgroup={$appleUid}))";
  263. $attributes = array();
  264. $res = $ldap->search($filter, 'cn=groups', $attributes);
  265. $entry = $ldap->first_entry($res);
  266. while ($entry) {
  267. $attrs = $ldap->get_attributes($entry);
  268. $groupObj = new stdClass();
  269. if ($allAttrs) {
  270. for ($i = 0; $i < $attrs['count']; $i++) {
  271. $vAttrName = $attrs[$i];
  272. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  273. $vAttrVal = $attrs[$vAttrName];
  274. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  275. if ($vAttrVal['count'] > 1) {
  276. $groupObj->{$vFldName} = array();
  277. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  278. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  279. }
  280. } else {
  281. $groupObj->{$vFldName} = $vAttrVal[0];
  282. }
  283. }
  284. }
  285. } else {
  286. foreach ($attrMap as $kAttrName => $vFldName) {
  287. $vAttrVal = V::get($kAttrName, '', $attrs);
  288. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  289. if ($vAttrVal['count'] > 1) {
  290. $groupObj->{$vFldName} = array();
  291. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  292. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  293. }
  294. } else {
  295. $groupObj->{$vFldName} = $vAttrVal[0];
  296. }
  297. }
  298. }
  299. }
  300. if ($groupObj->cn && $groupObj->appleUID) {
  301. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  302. } else {
  303. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  304. }
  305. $entry = $ldap->next_entry($entry);
  306. }
  307. $ldap->free_result($res);
  308. return $allLdapGroups;
  309. }
  310. public static function getGroupsByID($groupID, $allAttrs = false) {
  311. $allLdapGroups = array();
  312. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  313. $attrMap['apple-group-realname'] = 'realName';
  314. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  315. Lib::loadClass('LDAP');
  316. $ldap = LDAP::getInstance();
  317. $filter = "(&(objectClass=apple-group)(|(cn={$groupID}-*)(cn={$groupID}_*)))";
  318. $attributes = array();
  319. $res = $ldap->search($filter, 'cn=groups', $attributes);
  320. $entry = $ldap->first_entry($res);
  321. while ($entry) {
  322. $attrs = $ldap->get_attributes($entry);
  323. $groupObj = new stdClass();
  324. if ($allAttrs) {
  325. for ($i = 0; $i < $attrs['count']; $i++) {
  326. $vAttrName = $attrs[$i];
  327. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  328. $vAttrVal = $attrs[$vAttrName];
  329. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  330. if ($vAttrVal['count'] > 1) {
  331. $groupObj->{$vFldName} = array();
  332. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  333. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  334. }
  335. } else {
  336. $groupObj->{$vFldName} = $vAttrVal[0];
  337. }
  338. }
  339. }
  340. } else {
  341. foreach ($attrMap as $kAttrName => $vFldName) {
  342. $vAttrVal = V::get($kAttrName, '', $attrs);
  343. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  344. if ($vAttrVal['count'] > 1) {
  345. $groupObj->{$vFldName} = array();
  346. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  347. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  348. }
  349. } else {
  350. $groupObj->{$vFldName} = $vAttrVal[0];
  351. }
  352. }
  353. }
  354. }
  355. if ($groupObj->cn && $groupObj->appleUID) {
  356. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  357. } else {
  358. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  359. }
  360. $entry = $ldap->next_entry($entry);
  361. }
  362. $ldap->free_result($res);
  363. return $allLdapGroups;
  364. }
  365. public static function getGroupsAll($allAttrs = false) {
  366. $allLdapGroups = array();
  367. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  368. $attrMap['apple-group-realname'] = 'realName';
  369. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  370. Lib::loadClass('LDAP');
  371. $ldap = LDAP::getInstance();
  372. $filter = "(objectClass=apple-group)";
  373. $attributes = array();
  374. $res = $ldap->search($filter, 'cn=groups', $attributes);
  375. $entry = $ldap->first_entry($res);
  376. while ($entry) {
  377. $attrs = $ldap->get_attributes($entry);
  378. $groupObj = new stdClass();
  379. if ($allAttrs) {
  380. for ($i = 0; $i < $attrs['count']; $i++) {
  381. $vAttrName = $attrs[$i];
  382. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  383. $vAttrVal = $attrs[$vAttrName];
  384. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  385. if ($vAttrVal['count'] > 1) {
  386. $groupObj->{$vFldName} = array();
  387. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  388. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  389. }
  390. } else {
  391. $groupObj->{$vFldName} = $vAttrVal[0];
  392. }
  393. }
  394. }
  395. } else {
  396. foreach ($attrMap as $kAttrName => $vFldName) {
  397. $vAttrVal = V::get($kAttrName, '', $attrs);
  398. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  399. if ($vAttrVal['count'] > 1) {
  400. $groupObj->{$vFldName} = array();
  401. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  402. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  403. }
  404. } else {
  405. $groupObj->{$vFldName} = $vAttrVal[0];
  406. }
  407. }
  408. }
  409. }
  410. if ($groupObj->cn && $groupObj->appleUID) {
  411. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  412. } else {
  413. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  414. }
  415. $entry = $ldap->next_entry($entry);
  416. }
  417. $ldap->free_result($res);
  418. return $allLdapGroups;
  419. }
  420. }