UsersLdapHelper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. if(V::get('DBG_L', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">attrs(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($attrs);echo'</pre>';}
  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. //if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">ldap_search (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('ldaprdn'=>'cn=groups,' . $ldap->getBaseDN(), 'filter'=>$filter, 'attributes'=>$attributes));echo'</pre>';}
  102. $attributes = array();
  103. $res = $ldap->search($filter, 'cn=groups', $attributes);
  104. if(V::get('DBG_L', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">search(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($filter);echo'</pre>';}
  105. $entry = $ldap->first_entry($res);
  106. while ($entry) {
  107. $attrs = $ldap->get_attributes($entry);
  108. if(V::get('DBG_L', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">user('.$userName.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($attrs);echo'</pre>';}
  109. $groupObj = new stdClass();
  110. foreach ($attrMap as $kAttrName => $vField) {
  111. $vAttrVal = V::get($kAttrName, '', $attrs);
  112. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  113. $groupObj->{$vField} = $vAttrVal[0];
  114. }
  115. }
  116. if ($groupObj->cn && $groupObj->gidNumber) {
  117. $userLdapGroupsAdd[$groupObj->gidNumber] = $groupObj;
  118. } else {
  119. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Error: brak cn lub apple-generateduid (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($attrs);echo'</pre>';
  120. }
  121. $entry = $ldap->next_entry($entry);
  122. }
  123. $ldap->free_result($res);
  124. if (empty($userLdapGroupsAdd)) {
  125. break;
  126. } else {
  127. if(V::get('DBG_L', '', $_GET) > 0){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">userLdapGroupsAdd('.count($userLdapGroupsAdd).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userLdapGroupsAdd);echo'</pre>';}
  128. $lastLoopFound = array();
  129. foreach ($userLdapGroupsAdd as $kAppleID => $vGroup) {
  130. $userLdapGroups[$kAppleID] = $vGroup;
  131. $lastLoopFound[] = $vGroup->appleUID;
  132. }
  133. //$lastLoopFound = array_keys($userLdapGroupsAdd);
  134. }
  135. }
  136. return $userLdapGroups;
  137. }
  138. public static function getGroupsByAppleUids($appleUids, $allAttrs = false) {
  139. $allLdapGroups = array();
  140. if (empty($appleUids)) return $allLdapGroups;
  141. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  142. $attrMap['apple-group-realname'] = 'realName';
  143. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  144. Lib::loadClass('LDAP');
  145. $ldap = LDAP::getInstance();
  146. $filters = array();
  147. foreach ($appleUids as $vAppleUid) $filters[] = "(apple-generateduid={$vAppleUid})";
  148. $filter = "(&(objectClass=apple-group)(|" . implode("", $filters) . "))";
  149. $attributes = array();
  150. $res = $ldap->search($filter, 'cn=groups', $attributes);
  151. $entry = $ldap->first_entry($res);
  152. while ($entry) {
  153. $attrs = $ldap->get_attributes($entry);
  154. $groupObj = new stdClass();
  155. if ($allAttrs) {
  156. for ($i = 0; $i < $attrs['count']; $i++) {
  157. $vAttrName = $attrs[$i];
  158. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  159. $vAttrVal = $attrs[$vAttrName];
  160. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  161. if ($vAttrVal['count'] > 1) {
  162. $groupObj->{$vFldName} = array();
  163. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  164. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  165. }
  166. } else {
  167. $groupObj->{$vFldName} = $vAttrVal[0];
  168. }
  169. }
  170. }
  171. } else {
  172. foreach ($attrMap as $kAttrName => $vFldName) {
  173. $vAttrVal = V::get($kAttrName, '', $attrs);
  174. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  175. if ($vAttrVal['count'] > 1) {
  176. $groupObj->{$vFldName} = array();
  177. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  178. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  179. }
  180. } else {
  181. $groupObj->{$vFldName} = $vAttrVal[0];
  182. }
  183. }
  184. }
  185. }
  186. if ($groupObj->cn && $groupObj->appleUID) {
  187. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  188. } else {
  189. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Error: brak cn lub apple-generateduid (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('attrs'=>$attrs, 'groupObj'=>$groupObj));echo'</pre>';
  190. }
  191. $entry = $ldap->next_entry($entry);
  192. }
  193. $ldap->free_result($res);
  194. return $allLdapGroups;
  195. }
  196. public static function getParentGroupsByAppleUID($appleUid, $allAttrs = false) {
  197. $allLdapGroups = array();
  198. if (!$appleUid) return $allLdapGroups;
  199. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  200. $attrMap['apple-group-realname'] = 'realName';
  201. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  202. Lib::loadClass('LDAP');
  203. $ldap = LDAP::getInstance();
  204. $filters = array();
  205. $filter = "(&(objectClass=apple-group)(apple-group-nestedgroup={$appleUid}))";
  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. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Error: brak cn lub apple-generateduid (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('attrs'=>$attrs, 'groupObj'=>$groupObj));echo'</pre>';
  247. }
  248. $entry = $ldap->next_entry($entry);
  249. }
  250. $ldap->free_result($res);
  251. return $allLdapGroups;
  252. }
  253. public static function getGroupsByID($groupID, $allAttrs = false) {
  254. $allLdapGroups = array();
  255. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  256. $attrMap['apple-group-realname'] = 'realName';
  257. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  258. Lib::loadClass('LDAP');
  259. $ldap = LDAP::getInstance();
  260. $filter = "(&(objectClass=apple-group)(|(cn={$groupID}-*)(cn={$groupID}_*)))";
  261. $attributes = array();
  262. $res = $ldap->search($filter, 'cn=groups', $attributes);
  263. $entry = $ldap->first_entry($res);
  264. while ($entry) {
  265. $attrs = $ldap->get_attributes($entry);
  266. $groupObj = new stdClass();
  267. if ($allAttrs) {
  268. for ($i = 0; $i < $attrs['count']; $i++) {
  269. $vAttrName = $attrs[$i];
  270. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  271. $vAttrVal = $attrs[$vAttrName];
  272. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  273. if ($vAttrVal['count'] > 1) {
  274. $groupObj->{$vFldName} = array();
  275. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  276. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  277. }
  278. } else {
  279. $groupObj->{$vFldName} = $vAttrVal[0];
  280. }
  281. }
  282. }
  283. } else {
  284. foreach ($attrMap as $kAttrName => $vFldName) {
  285. $vAttrVal = V::get($kAttrName, '', $attrs);
  286. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  287. if ($vAttrVal['count'] > 1) {
  288. $groupObj->{$vFldName} = array();
  289. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  290. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  291. }
  292. } else {
  293. $groupObj->{$vFldName} = $vAttrVal[0];
  294. }
  295. }
  296. }
  297. }
  298. if ($groupObj->cn && $groupObj->appleUID) {
  299. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  300. } else {
  301. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Error: brak cn lub apple-generateduid (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('attrs'=>$attrs, 'groupObj'=>$groupObj));echo'</pre>';
  302. }
  303. $entry = $ldap->next_entry($entry);
  304. }
  305. $ldap->free_result($res);
  306. return $allLdapGroups;
  307. }
  308. public static function getGroupsAll($allAttrs = false) {
  309. $allLdapGroups = array();
  310. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  311. $attrMap['apple-group-realname'] = 'realName';
  312. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  313. Lib::loadClass('LDAP');
  314. $ldap = LDAP::getInstance();
  315. $filter = "(objectClass=apple-group)";
  316. $attributes = array();
  317. $res = $ldap->search($filter, 'cn=groups', $attributes);
  318. $entry = $ldap->first_entry($res);
  319. while ($entry) {
  320. $attrs = $ldap->get_attributes($entry);
  321. $groupObj = new stdClass();
  322. if ($allAttrs) {
  323. for ($i = 0; $i < $attrs['count']; $i++) {
  324. $vAttrName = $attrs[$i];
  325. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  326. $vAttrVal = $attrs[$vAttrName];
  327. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  328. if ($vAttrVal['count'] > 1) {
  329. $groupObj->{$vFldName} = array();
  330. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  331. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  332. }
  333. } else {
  334. $groupObj->{$vFldName} = $vAttrVal[0];
  335. }
  336. }
  337. }
  338. } else {
  339. foreach ($attrMap as $kAttrName => $vFldName) {
  340. $vAttrVal = V::get($kAttrName, '', $attrs);
  341. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  342. if ($vAttrVal['count'] > 1) {
  343. $groupObj->{$vFldName} = array();
  344. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  345. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  346. }
  347. } else {
  348. $groupObj->{$vFldName} = $vAttrVal[0];
  349. }
  350. }
  351. }
  352. }
  353. if ($groupObj->cn && $groupObj->appleUID) {
  354. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  355. } else {
  356. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Error: brak cn lub apple-generateduid (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('attrs'=>$attrs, 'groupObj'=>$groupObj));echo'</pre>';
  357. }
  358. $entry = $ldap->next_entry($entry);
  359. }
  360. $ldap->free_result($res);
  361. return $allLdapGroups;
  362. }
  363. }