UsersLdapHelper.php 14 KB

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