UsersLdapHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. class UsersLdapHelper {
  3. public static function getUser($userName, $allAttrs = false) {
  4. $ldapUsers = array();
  5. $fetchAttrs = 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 ($fetchAttrs 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. $fetchAttrs = 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 ($fetchAttrs 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. $fetchAttrs = 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 ($fetchAttrs 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. $fetchAttrs = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  127. $fetchAttrs['apple-group-realname'] = 'realName';
  128. $fetchAttrs['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, $fetchAttrs);
  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 ($fetchAttrs 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 getGroupsByID($groupID, $allAttrs = false) {
  182. $allLdapGroups = array();
  183. $fetchAttrs = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  184. $fetchAttrs['apple-group-realname'] = 'realName';
  185. $fetchAttrs['apple-group-nestedgroup'] = 'nestedGroups';
  186. Lib::loadClass('LDAP');
  187. $ldap = LDAP::getInstance();
  188. $filter = "(&(objectClass=apple-group)(|(cn={$groupID}-*)(cn={$groupID}_*)))";
  189. $attributes = array();
  190. $res = $ldap->search($filter, 'cn=groups', $attributes);
  191. $entry = $ldap->first_entry($res);
  192. while ($entry) {
  193. $attrs = $ldap->get_attributes($entry);
  194. $groupObj = new stdClass();
  195. if ($allAttrs) {
  196. for ($i = 0; $i < $attrs['count']; $i++) {
  197. $vAttrName = $attrs[$i];
  198. $vFldName = V::get($vAttrName, $vAttrName, $fetchAttrs);
  199. $vAttrVal = $attrs[$vAttrName];
  200. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  201. if ($vAttrVal['count'] > 1) {
  202. $groupObj->{$vFldName} = array();
  203. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  204. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  205. }
  206. } else {
  207. $groupObj->{$vFldName} = $vAttrVal[0];
  208. }
  209. }
  210. }
  211. } else {
  212. foreach ($fetchAttrs as $kAttrName => $vFldName) {
  213. $vAttrVal = V::get($kAttrName, '', $attrs);
  214. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  215. if ($vAttrVal['count'] > 1) {
  216. $groupObj->{$vFldName} = array();
  217. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  218. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  219. }
  220. } else {
  221. $groupObj->{$vFldName} = $vAttrVal[0];
  222. }
  223. }
  224. }
  225. }
  226. if ($groupObj->cn && $groupObj->appleUID) {
  227. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  228. } else {
  229. 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>';
  230. }
  231. $entry = $ldap->next_entry($entry);
  232. }
  233. $ldap->free_result($res);
  234. return $allLdapGroups;
  235. }
  236. public static function getGroupsAll($allAttrs = false) {
  237. $allLdapGroups = array();
  238. $fetchAttrs = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  239. $fetchAttrs['apple-group-realname'] = 'realName';
  240. $fetchAttrs['apple-group-nestedgroup'] = 'nestedGroups';
  241. Lib::loadClass('LDAP');
  242. $ldap = LDAP::getInstance();
  243. $filter = "(objectClass=apple-group)";
  244. $attributes = array();
  245. $res = $ldap->search($filter, 'cn=groups', $attributes);
  246. $entry = $ldap->first_entry($res);
  247. while ($entry) {
  248. $attrs = $ldap->get_attributes($entry);
  249. $groupObj = new stdClass();
  250. if ($allAttrs) {
  251. for ($i = 0; $i < $attrs['count']; $i++) {
  252. $vAttrName = $attrs[$i];
  253. $vFldName = V::get($vAttrName, $vAttrName, $fetchAttrs);
  254. $vAttrVal = $attrs[$vAttrName];
  255. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  256. if ($vAttrVal['count'] > 1) {
  257. $groupObj->{$vFldName} = array();
  258. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  259. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  260. }
  261. } else {
  262. $groupObj->{$vFldName} = $vAttrVal[0];
  263. }
  264. }
  265. }
  266. } else {
  267. foreach ($fetchAttrs as $kAttrName => $vFldName) {
  268. $vAttrVal = V::get($kAttrName, '', $attrs);
  269. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  270. if ($vAttrVal['count'] > 1) {
  271. $groupObj->{$vFldName} = array();
  272. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  273. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  274. }
  275. } else {
  276. $groupObj->{$vFldName} = $vAttrVal[0];
  277. }
  278. }
  279. }
  280. }
  281. if ($groupObj->cn && $groupObj->appleUID) {
  282. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  283. } else {
  284. 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>';
  285. }
  286. $entry = $ldap->next_entry($entry);
  287. }
  288. $ldap->free_result($res);
  289. return $allLdapGroups;
  290. }
  291. }