UsersLdapHelper.php 15 KB

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