UsersLdapHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. if (!Config::getConfFile('default_ldap')) {
  91. return array_merge(
  92. array_map(function ($group) {
  93. return (object)[
  94. 'appleUID' => $group->ID, // 'EBE2DE46-1B11-4793-BBAB-A47486B60E6C',
  95. 'gidNumber' => $group->ID, // '1025',
  96. 'cn' => "{$group->ID}_{$group->DESC}", // 'workgroup',
  97. 'name' => "[{$group->ID}] {$group->DESC}", // 'Workgroup',
  98. ];
  99. }, User::getGroups(),
  100. [ (object)[
  101. 'appleUID' => '1025', // 'EBE2DE46-1B11-4793-BBAB-A47486B60E6C',
  102. 'gidNumber' => '1025',
  103. 'cn' => 'workgroup',
  104. 'name' => 'Workgroup',
  105. ] ]
  106. ));
  107. // array (
  108. // 'appleUID' => 'EBE2DE46-1B11-4793-BBAB-A47486B60E6C',
  109. // 'gidNumber' => '1025',
  110. // 'cn' => 'workgroup',
  111. // 'name' => 'Workgroup',
  112. // ),
  113. }
  114. Lib::loadClass('LDAP');
  115. $ldap = LDAP::getInstance();
  116. $lastLoopFound = array();
  117. for ($i = 0; $i <= $authLDAPSubGroupDepth; $i++) {
  118. $userLdapGroupsAdd = array();
  119. if ($i == 0) {
  120. $filter = "(&(objectClass=apple-group)(memberUid={$userName}))";
  121. } else {
  122. $queryOrArr = array();
  123. foreach ($lastLoopFound as $vAppleUid) {
  124. $queryOrArr[] = "apple-group-nestedgroup={$vAppleUid}";
  125. }
  126. if (!empty($queryOrArr)) {
  127. $queryOr = '(|(' . implode(')(', $queryOrArr) . '))';
  128. $filter = "(&(objectClass=apple-group){$queryOr})";
  129. } else {
  130. break;
  131. }
  132. }
  133. $attributes = array();
  134. $res = $ldap->search($filter, 'cn=groups', $attributes);
  135. DBG::_('DBG_L', '>0', "search", $filter, __CLASS__, __FUNCTION__, __LINE__);
  136. $entry = $ldap->first_entry($res);
  137. while ($entry) {
  138. $attrs = $ldap->get_attributes($entry);
  139. DBG::_('DBG_L', '>0', "user({$userName})", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  140. $groupObj = new stdClass();
  141. foreach ($attrMap as $kAttrName => $vField) {
  142. $vAttrVal = V::get($kAttrName, '', $attrs);
  143. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  144. $groupObj->{$vField} = $vAttrVal[0];
  145. }
  146. }
  147. if ($groupObj->cn && $groupObj->gidNumber) {
  148. $userLdapGroupsAdd[$groupObj->gidNumber] = $groupObj;
  149. } else {
  150. DBG::_(true, true, "Error: brak cn lub apple-generateduid", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  151. }
  152. $entry = $ldap->next_entry($entry);
  153. }
  154. $ldap->free_result($res);
  155. if (empty($userLdapGroupsAdd)) {
  156. break;
  157. } else {
  158. DBG::_('DBG_L', '>0', "userLdapGroupsAdd(".count($userLdapGroupsAdd).")", $userLdapGroupsAdd, __CLASS__, __FUNCTION__, __LINE__);
  159. $lastLoopFound = array();
  160. foreach ($userLdapGroupsAdd as $kAppleID => $vGroup) {
  161. $userLdapGroups[$kAppleID] = $vGroup;
  162. $lastLoopFound[] = $vGroup->appleUID;
  163. }
  164. //$lastLoopFound = array_keys($userLdapGroupsAdd);
  165. }
  166. }
  167. DBG::log($userLdapGroups, 'array', '$userLdapGroups');
  168. return $userLdapGroups;
  169. }
  170. public static function getUserGroupsByAppleUid($userUid, $authLDAPSubGroupDepth = 3) {
  171. $userLdapGroups = array();
  172. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  173. Lib::loadClass('LDAP');
  174. $ldap = LDAP::getInstance();
  175. $lastLoopFound = array();
  176. for ($i = 0; $i <= $authLDAPSubGroupDepth; $i++) {
  177. $userLdapGroupsAdd = array();
  178. if ($i == 0) {
  179. $filter = "(&(objectClass=apple-group)(apple-group-memberguid={$userUid}))";
  180. } else {
  181. $queryOrArr = array();
  182. foreach ($lastLoopFound as $vAppleUid) {
  183. $queryOrArr[] = "apple-group-nestedgroup={$vAppleUid}";
  184. }
  185. if (!empty($queryOrArr)) {
  186. $queryOr = '(|(' . implode(')(', $queryOrArr) . '))';
  187. $filter = "(&(objectClass=apple-group){$queryOr})";
  188. } else {
  189. break;
  190. }
  191. }
  192. $attributes = array();
  193. $res = $ldap->search($filter, 'cn=groups', $attributes);
  194. DBG::_('DBG_L', '>0', "search", $filter, __CLASS__, __FUNCTION__, __LINE__);
  195. $entry = $ldap->first_entry($res);
  196. while ($entry) {
  197. $attrs = $ldap->get_attributes($entry);
  198. DBG::_('DBG_L', '>0', "user({$userUid})", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  199. $groupObj = new stdClass();
  200. foreach ($attrMap as $kAttrName => $vField) {
  201. $vAttrVal = V::get($kAttrName, '', $attrs);
  202. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  203. $groupObj->{$vField} = $vAttrVal[0];
  204. }
  205. }
  206. if ($groupObj->cn && $groupObj->gidNumber) {
  207. $userLdapGroupsAdd[$groupObj->gidNumber] = $groupObj;
  208. } else {
  209. DBG::_(true, true, "Error: brak cn lub apple-generateduid", $attrs, __CLASS__, __FUNCTION__, __LINE__);
  210. }
  211. $entry = $ldap->next_entry($entry);
  212. }
  213. $ldap->free_result($res);
  214. if (empty($userLdapGroupsAdd)) {
  215. break;
  216. } else {
  217. DBG::_('DBG_L', '>0', "userLdapGroupsAdd(".count($userLdapGroupsAdd).")", $userLdapGroupsAdd, __CLASS__, __FUNCTION__, __LINE__);
  218. $lastLoopFound = array();
  219. foreach ($userLdapGroupsAdd as $kAppleID => $vGroup) {
  220. $userLdapGroups[$kAppleID] = $vGroup;
  221. $lastLoopFound[] = $vGroup->appleUID;
  222. }
  223. //$lastLoopFound = array_keys($userLdapGroupsAdd);
  224. }
  225. }
  226. return $userLdapGroups;
  227. }
  228. public static function getGroupsByAppleUids($appleUids, $allAttrs = false) {
  229. $allLdapGroups = array();
  230. if (empty($appleUids)) return $allLdapGroups;
  231. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  232. $attrMap['apple-group-realname'] = 'realName';
  233. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  234. Lib::loadClass('LDAP');
  235. $ldap = LDAP::getInstance();
  236. $filters = array();
  237. foreach ($appleUids as $vAppleUid) $filters[] = "(apple-generateduid={$vAppleUid})";
  238. $filter = "(&(objectClass=apple-group)(|" . implode("", $filters) . "))";
  239. $attributes = array();
  240. $res = $ldap->search($filter, 'cn=groups', $attributes);
  241. $entry = $ldap->first_entry($res);
  242. while ($entry) {
  243. $attrs = $ldap->get_attributes($entry);
  244. $groupObj = new stdClass();
  245. if ($allAttrs) {
  246. for ($i = 0; $i < $attrs['count']; $i++) {
  247. $vAttrName = $attrs[$i];
  248. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  249. $vAttrVal = $attrs[$vAttrName];
  250. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  251. if ($vAttrVal['count'] > 1) {
  252. $groupObj->{$vFldName} = array();
  253. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  254. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  255. }
  256. } else {
  257. $groupObj->{$vFldName} = $vAttrVal[0];
  258. }
  259. }
  260. }
  261. } else {
  262. foreach ($attrMap as $kAttrName => $vFldName) {
  263. $vAttrVal = V::get($kAttrName, '', $attrs);
  264. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  265. if ($vAttrVal['count'] > 1) {
  266. $groupObj->{$vFldName} = array();
  267. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  268. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  269. }
  270. } else {
  271. $groupObj->{$vFldName} = $vAttrVal[0];
  272. }
  273. }
  274. }
  275. }
  276. if ($groupObj->cn && $groupObj->appleUID) {
  277. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  278. } else {
  279. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  280. }
  281. $entry = $ldap->next_entry($entry);
  282. }
  283. $ldap->free_result($res);
  284. return $allLdapGroups;
  285. }
  286. public static function getParentGroupsByAppleUID($appleUid, $allAttrs = false) {
  287. $allLdapGroups = array();
  288. if (!$appleUid) return $allLdapGroups;
  289. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  290. $attrMap['apple-group-realname'] = 'realName';
  291. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  292. Lib::loadClass('LDAP');
  293. $ldap = LDAP::getInstance();
  294. $filters = array();
  295. $filter = "(&(objectClass=apple-group)(apple-group-nestedgroup={$appleUid}))";
  296. $attributes = array();
  297. $res = $ldap->search($filter, 'cn=groups', $attributes);
  298. $entry = $ldap->first_entry($res);
  299. while ($entry) {
  300. $attrs = $ldap->get_attributes($entry);
  301. $groupObj = new stdClass();
  302. if ($allAttrs) {
  303. for ($i = 0; $i < $attrs['count']; $i++) {
  304. $vAttrName = $attrs[$i];
  305. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  306. $vAttrVal = $attrs[$vAttrName];
  307. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  308. if ($vAttrVal['count'] > 1) {
  309. $groupObj->{$vFldName} = array();
  310. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  311. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  312. }
  313. } else {
  314. $groupObj->{$vFldName} = $vAttrVal[0];
  315. }
  316. }
  317. }
  318. } else {
  319. foreach ($attrMap as $kAttrName => $vFldName) {
  320. $vAttrVal = V::get($kAttrName, '', $attrs);
  321. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  322. if ($vAttrVal['count'] > 1) {
  323. $groupObj->{$vFldName} = array();
  324. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  325. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  326. }
  327. } else {
  328. $groupObj->{$vFldName} = $vAttrVal[0];
  329. }
  330. }
  331. }
  332. }
  333. if ($groupObj->cn && $groupObj->appleUID) {
  334. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  335. } else {
  336. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  337. }
  338. $entry = $ldap->next_entry($entry);
  339. }
  340. $ldap->free_result($res);
  341. return $allLdapGroups;
  342. }
  343. public static function getGroupsByID($groupID, $allAttrs = false) {
  344. $allLdapGroups = array();
  345. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  346. $attrMap['apple-group-realname'] = 'realName';
  347. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  348. Lib::loadClass('LDAP');
  349. $ldap = LDAP::getInstance();
  350. if (!$ldap) {
  351. return []; // TODO: fetch user groups from zasoby
  352. }
  353. $filter = "(&(objectClass=apple-group)(|(cn={$groupID}-*)(cn={$groupID}_*)))";
  354. $attributes = array();
  355. $res = $ldap->search($filter, 'cn=groups', $attributes);
  356. $entry = $ldap->first_entry($res);
  357. while ($entry) {
  358. $attrs = $ldap->get_attributes($entry);
  359. $groupObj = new stdClass();
  360. if ($allAttrs) {
  361. for ($i = 0; $i < $attrs['count']; $i++) {
  362. $vAttrName = $attrs[$i];
  363. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  364. $vAttrVal = $attrs[$vAttrName];
  365. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  366. if ($vAttrVal['count'] > 1) {
  367. $groupObj->{$vFldName} = array();
  368. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  369. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  370. }
  371. } else {
  372. $groupObj->{$vFldName} = $vAttrVal[0];
  373. }
  374. }
  375. }
  376. } else {
  377. foreach ($attrMap as $kAttrName => $vFldName) {
  378. $vAttrVal = V::get($kAttrName, '', $attrs);
  379. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  380. if ($vAttrVal['count'] > 1) {
  381. $groupObj->{$vFldName} = array();
  382. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  383. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  384. }
  385. } else {
  386. $groupObj->{$vFldName} = $vAttrVal[0];
  387. }
  388. }
  389. }
  390. }
  391. if ($groupObj->cn && $groupObj->appleUID) {
  392. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  393. } else {
  394. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  395. }
  396. $entry = $ldap->next_entry($entry);
  397. }
  398. $ldap->free_result($res);
  399. return $allLdapGroups;
  400. }
  401. public static function getGroupsAll($allAttrs = false) { // used only in TypespecialVariable for field 'DEFAULT_ACL_GROUP'
  402. $allLdapGroups = array();
  403. $attrMap = array('apple-generateduid'=>'appleUID', 'gidNumber'=>'gidNumber', 'cn'=>'cn');// (givenName, sn) = cn
  404. $attrMap['apple-group-realname'] = 'realName';
  405. $attrMap['apple-group-nestedgroup'] = 'nestedGroups';
  406. Lib::loadClass('LDAP');
  407. $ldap = LDAP::getInstance();
  408. if (!$ldap) {
  409. return []; // TODO: fetch all groups from zasoby
  410. }
  411. $filter = "(objectClass=apple-group)";
  412. $attributes = array();
  413. $res = $ldap->search($filter, 'cn=groups', $attributes);
  414. $entry = $ldap->first_entry($res);
  415. while ($entry) {
  416. $attrs = $ldap->get_attributes($entry);
  417. $groupObj = new stdClass();
  418. if ($allAttrs) {
  419. for ($i = 0; $i < $attrs['count']; $i++) {
  420. $vAttrName = $attrs[$i];
  421. $vFldName = V::get($vAttrName, $vAttrName, $attrMap);
  422. $vAttrVal = $attrs[$vAttrName];
  423. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  424. if ($vAttrVal['count'] > 1) {
  425. $groupObj->{$vFldName} = array();
  426. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  427. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  428. }
  429. } else {
  430. $groupObj->{$vFldName} = $vAttrVal[0];
  431. }
  432. }
  433. }
  434. } else {
  435. foreach ($attrMap as $kAttrName => $vFldName) {
  436. $vAttrVal = V::get($kAttrName, '', $attrs);
  437. if (is_array($vAttrVal) && !empty($vAttrVal)) {
  438. if ($vAttrVal['count'] > 1) {
  439. $groupObj->{$vFldName} = array();
  440. for ($j = 0; $j < $vAttrVal['count']; $j++) {
  441. $groupObj->{$vFldName}[] = $vAttrVal[$j];
  442. }
  443. } else {
  444. $groupObj->{$vFldName} = $vAttrVal[0];
  445. }
  446. }
  447. }
  448. }
  449. if ($groupObj->cn && $groupObj->appleUID) {
  450. $allLdapGroups[$groupObj->appleUID] = $groupObj;
  451. } else {
  452. DBG::_(true, true, "Error: brak cn lub apple-generateduid", array('attrs'=>$attrs, 'groupObj'=>$groupObj), __CLASS__, __FUNCTION__, __LINE__);
  453. }
  454. $entry = $ldap->next_entry($entry);
  455. }
  456. $ldap->free_result($res);
  457. return $allLdapGroups;
  458. }
  459. }