SyncUsers.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. class SyncUsers {
  3. private $_fromStorage;
  4. private $_toStorage;
  5. private $_errors = array();
  6. public function __construct($fromStorage, $toStorage) {
  7. $this->_fromStorage = $fromStorage;
  8. $this->_toStorage = $toStorage;
  9. }
  10. /**
  11. * Sync user.
  12. *
  13. * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
  14. */
  15. public function syncUser($usrLogin) {
  16. $syncGroups = true;
  17. $syncDisabled = true;
  18. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  19. $usrTo = $this->_toStorage->getUser($usrLogin);
  20. $usrFromDisabled = null;
  21. $usrToDisabeld = null;
  22. if (!$usrFrom) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie danych");
  23. DBG::_('DBG_SU', true, 'usrFrom', $usrFrom, __CLASS__, __FUNCTION__, __LINE__);
  24. DBG::_('DBG_SU', true, 'usrTo', $usrTo, __CLASS__, __FUNCTION__, __LINE__);
  25. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  26. if (!$usrTo && true === $usrFromDisabled) {
  27. throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP. Jest zablokowany bazie danych, więc nie ma potrzeby tworzenia go w bazie LDAP.");
  28. }
  29. if (!$usrTo) {
  30. $this->_toStorage->createUser($usrFrom);
  31. $usrTo = $this->_toStorage->getUser($usrLogin);
  32. if (!$usrTo) throw new Exception("Nie udało się utworzyć użytkownika '{$usrLogin}' w bazie LDAP");
  33. $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
  34. }
  35. else if (true !== $usrFromDisabled) {
  36. $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
  37. }
  38. {// $syncDisabled
  39. $usrTo = $this->_toStorage->getUser($usrLogin);
  40. if (!$usrTo) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP");
  41. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  42. if (null === $usrFromDisabled) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie danych");
  43. if (null === $usrToDisabeld) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie LDAP");
  44. if ($usrFromDisabled !== $usrToDisabeld) {
  45. if (!$this->_toStorage->setDisabled($usrLogin, $usrFromDisabled)) {
  46. throw new Exception("Nie udało się ustawić statusu blokady dla użytkownika '{$usrLogin}'");
  47. }
  48. }
  49. }
  50. {// $syncGroups
  51. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  52. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  53. $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
  54. DBG::_('DBG_SU', '>0', 'groupsTodo', $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  55. DBG::_('DBG_SU', '>0', 'groupsFrom', $groupsFrom, __CLASS__, __FUNCTION__, __LINE__);
  56. DBG::_('DBG_SU', '>0', 'groupsTo', $groupsTo, __CLASS__, __FUNCTION__, __LINE__);
  57. if (!empty($groupsTodo)) {
  58. foreach ($groupsTodo as $kGroupID => $vBool) {
  59. if ($vBool) {
  60. $this->_toStorage->addUserGroup($usrLogin, $groupsFrom[$kGroupID]);
  61. }
  62. else {
  63. $this->_toStorage->removeUserGroup($usrLogin, $groupsTo[$kGroupID]);
  64. }
  65. }
  66. }
  67. }
  68. {// clean up members by apple-generateduid
  69. $groupsTo = $this->_toStorage->getGroupsByUserUid($usrLogin);
  70. $groupsTodo = $this->getCleanupSyncUserGroupsByUidTodoList($usrLogin);
  71. DBG::_('DBG_SU', '>0', "groupsTodo Cleanup member uid's usrFromDisabeld(" . (($usrFromDisabled)? 'true' : 'false') . ")", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  72. if (!empty($groupsTodo)) {
  73. foreach ($groupsTodo as $kGroupID => $vBool) {
  74. if ($vBool) {
  75. //$syncTodoList[] = "Dodaj '{$usrLogin}' do grupy {$kGroupID}";
  76. }
  77. else {
  78. $this->_toStorage->removeUserUidFromGroup($usrLogin, $groupsTo[$kGroupID]);
  79. }
  80. }
  81. }
  82. }
  83. $this->_fromStorage->setSyncUserDate($usrLogin);
  84. $this->_toStorage->setSyncUserDate($usrLogin);
  85. if ($this->hasErrors()) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. public function syncExistingUser($usrLogin, ObjectUser $usrFrom, ObjectUser $usrTo) {
  91. if (!$usrFrom) return false;
  92. if (!$usrTo) return false;
  93. $updateData = array();
  94. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  95. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  96. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  97. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  98. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  99. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  100. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = $usrFrom->password;
  101. $updated = $this->_toStorage->updateUser($usrLogin, $updateData);
  102. if (!$updated) {
  103. $errors = $this->_toStorage->getRawErrorsList();
  104. foreach ($errors as $vErr) {
  105. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  106. }
  107. throw new Exception("TODO: update user {$usrLogin} from Database to Ldap");
  108. }
  109. }
  110. public function getSyncUserTodoList($usrLogin) {
  111. $syncGroups = true;
  112. $syncDisabled = true;
  113. $syncTodoList = array();
  114. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  115. $usrTo = $this->_toStorage->getUser($usrLogin);
  116. DBG::_('DBG_SU', true, 'usrFrom', ($usrFrom)? $usrFrom->exportData() : null, __CLASS__, __FUNCTION__, __LINE__);
  117. DBG::_('DBG_SU', true, 'usrTo', ($usrTo)? $usrTo->exportData() : null, __CLASS__, __FUNCTION__, __LINE__);
  118. if (!$usrFrom) {
  119. $syncTodoList[] = "Użytkownik {$usrLogin} nie istnieje w bazie danych";
  120. return $syncTodoList;
  121. }
  122. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  123. if (!$usrTo && true === $usrFromDisabled) {
  124. throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP. Jest zablokowany bazie danych, więc nie ma potrzeby tworzenia go w bazie LDAP.");
  125. }
  126. if (!$usrTo) {
  127. $syncDisabled = false;
  128. $syncTodoList[] = "Utwórz użytkownika '{$usrLogin}' w bazie LDAP";
  129. }
  130. else if (true !== $usrFromDisabled) {
  131. //throw new Exception("Użytkownik '{$usrLogin}' jest zablokowany bazie danych, więc nie ma potrzeby aktualizacji jego danych w bazie LDAP.");
  132. $updateData = array();
  133. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  134. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  135. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  136. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  137. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  138. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  139. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = '*****';
  140. foreach ($updateData as $key => $val) {
  141. $syncTodoList[] = "Aktualizuj {$key}: {$val}";
  142. }
  143. }
  144. if ($syncDisabled && $usrTo && $usrFrom) {
  145. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  146. if ($usrFromDisabled === null || $usrToDisabeld === null) {
  147. $syncTodoList[] = "Status blokady '{$usrLogin}' nieznany w bazie danych lub LDAP";
  148. return $syncTodoList;
  149. }
  150. if ($usrFromDisabled !== $usrToDisabeld) {
  151. $syncTodoList[] = "Ustawienie statusu blokady '{$usrLogin}' na '" . (($usrFromDisabled)? 'true' : 'false') . "'";
  152. }
  153. }
  154. {// $syncGroups
  155. $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
  156. DBG::_('DBG_SU', '>0', "groupsTodo usrFromDisabeld(" . (($usrFromDisabled)? 'true' : 'false') . ")", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  157. if (!empty($groupsTodo)) {
  158. foreach ($groupsTodo as $kGroupID => $vBool) {
  159. if ($vBool) {
  160. $syncTodoList[] = "Dodaj '{$usrLogin}' do grupy {$kGroupID}";
  161. }
  162. else {
  163. $syncTodoList[] = "Usuń '{$usrLogin}' z grupy {$kGroupID}";
  164. }
  165. }
  166. }
  167. }
  168. {// clean up members by apple-generateduid
  169. $groupsTodo = $this->getCleanupSyncUserGroupsByUidTodoList($usrLogin);
  170. DBG::_('DBG_SU', '>0', "groupsTodo Cleanup member uid's usrFromDisabeld(" . (($usrFromDisabled)? 'true' : 'false') . ")", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  171. if (!empty($groupsTodo)) {
  172. foreach ($groupsTodo as $kGroupID => $vBool) {
  173. if ($vBool) {
  174. //$syncTodoList[] = "Dodaj '{$usrLogin}' do grupy {$kGroupID}";
  175. }
  176. else {
  177. $syncTodoList[] = "Usuń `uid` usera '{$usrLogin}' z grupy {$kGroupID}";
  178. }
  179. }
  180. }
  181. }
  182. return $syncTodoList;
  183. }
  184. /**
  185. * @returns array $groupsTodo - groups todo list:
  186. * 'com.apple.access_mail' => true - add to this group
  187. * 'com.apple.access_mail' => false - remove from this group
  188. */
  189. public function getSyncUserGroupsTodoList($usrLogin) {
  190. $groupsTodo = array();// `guid` => true (add), false (remove)
  191. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  192. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  193. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  194. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  195. foreach ($groupsTo as $kUid => $vName) {
  196. $groupsTodo[$kUid] = false;
  197. }
  198. if (true === $usrFromDisabled) {
  199. // remove all groups
  200. } else {
  201. foreach ($groupsFrom as $kUid => $vGroup) {
  202. if (isset($groupsTodo[$kUid])) {
  203. unset($groupsTodo[$kUid]);
  204. } else {
  205. $groupsTodo[$kUid] = true;
  206. }
  207. }
  208. }
  209. return $groupsTodo;
  210. }
  211. public function getCleanupSyncUserGroupsByUidTodoList($usrLogin) {
  212. $groupsTodo = array();// `guid` => true (add), false (remove)
  213. $groupsTo = $this->_toStorage->getGroupsByUserUid($usrLogin);
  214. DBG::_('DBG_SU', '>0', "CleanupAppleMemberUidTodoList user groups by apple-generateduid({$usrAppleUid})", $userGroupsLdap, __CLASS__, __FUNCTION__, __LINE__);
  215. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  216. $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
  217. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  218. foreach ($groupsTo as $kUid => $vName) {
  219. $groupsTodo[$kUid] = false;
  220. }
  221. if (true === $usrFromDisabled) {
  222. // remove all groups
  223. } else {
  224. foreach ($groupsFrom as $kUid => $vGroup) {
  225. if (isset($groupsTodo[$kUid])) {
  226. unset($groupsTodo[$kUid]);
  227. } else {
  228. $groupsTodo[$kUid] = true;
  229. }
  230. }
  231. }
  232. DBG::_('DBG_SU', '>0', "CleanupAppleMemberUidTodoList groupsTodo apple-generateduid({$usrAppleUid})", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  233. return $groupsTodo;
  234. }
  235. public function getSyncGroupTodoList($idGroup, $syncNestedGroups = false) {
  236. $syncTodoList = array();
  237. $groupFrom = $this->_fromStorage->getGroup($idGroup);
  238. $groupTo = $this->_toStorage->getGroup($idGroup);
  239. if (!$groupFrom) {
  240. $syncTodoList[] = "Grupa {$idGroup} nie istnieje w bazie danych";
  241. return $syncTodoList;
  242. }
  243. if (!$groupTo) {
  244. $syncTodoList[] = "Utwórz grupę {$idGroup} w bazie LDAP";
  245. return $syncTodoList;
  246. }
  247. $updateData = array();
  248. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  249. $updateData['realName'] = $groupFrom->realName;
  250. }
  251. foreach ($updateData as $key => $val) {
  252. $syncTodoList[] = "Aktualizuj {$key}: {$val}";
  253. }
  254. if ($syncNestedGroups) {
  255. if (!empty($groupTo->nestedGroups) || !empty($groupFrom->nestedGroups)) {
  256. $groupsTodo = array();
  257. if (!empty($groupTo->nestedGroups)) {
  258. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  259. $groupsTodo[$kUid] = false;
  260. }
  261. }
  262. if (!empty($groupFrom->nestedGroups)) {
  263. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  264. if (isset($groupsTodo[$kUid])) {
  265. unset($groupsTodo[$kUid]);
  266. } else {
  267. $groupsTodo[$kUid] = true;
  268. }
  269. }
  270. }
  271. if (!empty($groupsTodo)) {
  272. foreach ($groupsTodo as $kIdGroup => $vBool) {
  273. if ($vBool) {
  274. $syncTodoList[] = "Dodaj grupę '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";
  275. }
  276. else {
  277. $syncTodoList[] = "Usuń grupę '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";
  278. }
  279. }
  280. }
  281. }
  282. $fromParentGroups = $groupFrom->getParentGroups();
  283. $toParentGroups = $groupTo->getParentGroups();
  284. {
  285. $groupsTodo = array();
  286. if (!empty($toParentGroups)) {
  287. foreach ($toParentGroups as $kUid => $vGroup) {
  288. $groupsTodo[$kUid] = false;
  289. }
  290. }
  291. if (!empty($fromParentGroups)) {
  292. foreach ($fromParentGroups as $kUid => $vGroup) {
  293. if (isset($groupsTodo[$kUid])) {
  294. unset($groupsTodo[$kUid]);
  295. } else {
  296. $groupsTodo[$kUid] = true;
  297. }
  298. }
  299. }
  300. if (!empty($groupsTodo)) {
  301. foreach ($groupsTodo as $kIdGroup => $vBool) {
  302. if ($vBool) {
  303. $syncTodoList[] = "Dodaj grupę nadrzędną '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";// == add $idGroup to $kIdGroup
  304. }
  305. else {
  306. $groupTest = $this->_fromStorage->getGroup($kIdGroup);
  307. if ($groupTest) {
  308. $syncTodoList[] = "Usuń grupę nadrzędną '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";// == remove $idGroup from $kIdGroup
  309. } else {
  310. //$syncTodoList[] = "Keep parent group '{$kIdGroup}' in group '{$idGroup}' in toStorage, because that group dont exists in fromStorage";
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. return $syncTodoList;
  318. }
  319. /**
  320. * Sync user.
  321. *
  322. * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
  323. */
  324. public function syncGroup($idGroup, $syncNestedGroups = false) {
  325. $groupFrom = $this->_fromStorage->getGroup($idGroup);
  326. $groupTo = $this->_toStorage->getGroup($idGroup);
  327. DBG::_('DBG_SU', '>0', 'groupFrom', $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
  328. DBG::_('DBG_SU', '>0', 'groupTo', $groupTo, __CLASS__, __FUNCTION__, __LINE__);
  329. if (!$groupFrom) throw new Exception("Grupa [{$idGroup}] nie istnieje w bazie danych");
  330. if (!$groupTo) {
  331. $this->_toStorage->createGroup($groupFrom);
  332. $groupTo = $this->_toStorage->getGroup($idGroup);
  333. if (!$groupTo) throw new Exception("Nie udało się utworzyć grupy [{$idGroup}] w bazie LDAP");
  334. $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
  335. }
  336. else {// $groupFrom && $groupTo
  337. $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
  338. }
  339. $this->_fromStorage->setSyncGroupDate($idGroup);
  340. $this->_toStorage->setSyncGroupDate($idGroup);
  341. return true;
  342. }
  343. public function syncExistingGroup($groupID, ObjectGroup $groupFrom, ObjectGroup $groupTo, $syncNestedGroups = false) {
  344. if (!$groupFrom) throw new Exception("Nie podano grupy z bazy danych!");
  345. if (!$groupTo) throw new Exception("Nie podano grupy z bazy LDAP!");
  346. $updateData = array();
  347. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  348. $updateData['realName'] = $groupFrom->realName;
  349. }
  350. //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
  351. $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
  352. if (!$updated) {
  353. $errors = $this->_toStorage->getRawErrorsList();
  354. foreach ($errors as $vErr) {
  355. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  356. }
  357. throw new Exception("TODO: update group [{$groupID}] from fromStorage to toStorage");
  358. }
  359. if ($syncNestedGroups) {
  360. $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
  361. if (!$synced) {
  362. $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  363. return false;
  364. }
  365. $synced = $this->syncParentGroups($groupID, $groupFrom, $groupTo);
  366. if (!$synced) {
  367. $this->setError(1, "Error: sync parent groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
  374. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  375. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  376. DBG::_('DBG_SU', '>0', "groupFrom (hasNestedGroups:" . (!empty($groupFrom->nestedGroups)) . ")", $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
  377. DBG::_('DBG_SU', '>0', "groupTo (hasNestedGroups:" . (!empty($groupTo->nestedGroups)) . ")", $groupTo, __CLASS__, __FUNCTION__, __LINE__);
  378. if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
  379. // nothing to do
  380. return true;
  381. }
  382. else {
  383. /*
  384. [nestedGroups] => Array(
  385. [2981] => stdClass Object(
  386. [primaryKey] => 2981
  387. [type] => STANOWISKO
  388. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  389. [zasobID] => 2981
  390. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  391. */
  392. /**
  393. * $groupsTodo - groups todo list:
  394. * 'com.apple.access_mail' => true - add to this group
  395. * 'com.apple.access_mail' => false - remove from this group
  396. */
  397. $groupsTodo = array();
  398. if (!empty($groupTo->nestedGroups)) {
  399. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  400. $groupsTodo[$kUid] = false;
  401. }
  402. }
  403. if (!empty($groupFrom->nestedGroups)) {
  404. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  405. if (isset($groupsTodo[$kUid])) {
  406. unset($groupsTodo[$kUid]);
  407. } else {
  408. $groupsTodo[$kUid] = true;
  409. }
  410. }
  411. }
  412. DBG::_('DBG_SU', '>0', "groupsTodo", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
  413. if (!empty($groupsTodo)) {
  414. foreach ($groupsTodo as $kGroupID => $vBool) {
  415. if ($vBool) {
  416. $this->_toStorage->addNestedGroup($groupID, $kGroupID);
  417. }
  418. else {
  419. $this->_toStorage->removeNestedGroup($groupID, $kGroupID);
  420. }
  421. }
  422. }
  423. }
  424. if ($this->hasErrors()) {
  425. return false;
  426. }
  427. return true;
  428. }
  429. public function syncParentGroups($groupID, $groupFrom = null, $groupTo = null) {
  430. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  431. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  432. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  433. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupFrom (hasNestedGroups:'.(!empty($groupFrom->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupFrom);echo'</pre>';
  434. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupTo (hasNestedGroups:'.(!empty($groupTo->nestedGroups)).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupTo);echo'</pre>';
  435. }
  436. $fromParentGroups = $groupFrom->getParentGroups();
  437. $toParentGroups = $groupTo->getParentGroups();
  438. if (empty($fromParentGroups) && empty($toParentGroups)) {
  439. return true;
  440. }
  441. else {
  442. /*
  443. [nestedGroups] => Array(
  444. [2981] => stdClass Object(
  445. [primaryKey] => 2981
  446. [type] => STANOWISKO
  447. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  448. [zasobID] => 2981
  449. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  450. */
  451. /**
  452. * $groupsTodo - groups todo list:
  453. * 'com.apple.access_mail' => true - add to this group
  454. * 'com.apple.access_mail' => false - remove from this group
  455. */
  456. $groupsTodo = array();
  457. if (!empty($toParentGroups)) {
  458. foreach ($toParentGroups as $kUid => $vGroup) {
  459. $groupsTodo[$kUid] = false;
  460. }
  461. }
  462. if (!empty($fromParentGroups)) {
  463. foreach ($fromParentGroups as $kUid => $vGroup) {
  464. if (isset($groupsTodo[$kUid])) {
  465. unset($groupsTodo[$kUid]);
  466. } else {
  467. $groupsTodo[$kUid] = true;
  468. }
  469. }
  470. }
  471. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  472. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  473. }
  474. if (!empty($groupsTodo)) {
  475. foreach ($groupsTodo as $kGroupID => $vBool) {
  476. if ($vBool) {
  477. //$syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
  478. $this->_toStorage->addNestedGroup($kGroupID, $groupID);
  479. // TODO: add try catch to prevent resend exception
  480. if (0) {
  481. $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  482. }
  483. }
  484. else {
  485. $groupTest = $this->_fromStorage->getGroup($kGroupID);
  486. if ($groupTest) {
  487. //$syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
  488. $this->_toStorage->removeNestedGroup($kGroupID, $groupID);
  489. // TODO: add try catch to prevent resend exception
  490. if (0) {
  491. $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  492. }
  493. } else {
  494. //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
  495. }
  496. }
  497. }
  498. }
  499. }
  500. if ($this->hasErrors()) {
  501. return false;
  502. }
  503. return true;
  504. }
  505. private function setError($code, $msg, $dbgMsg) {
  506. $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
  507. }
  508. public function hasErrors() {
  509. return !empty($this->_errors);
  510. }
  511. public function getErrorsMsgList() {
  512. $msgList = array();
  513. foreach ($this->_errors as $vErr) {
  514. $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
  515. }
  516. return $msgList;
  517. }
  518. public function getErrorsMsgListWithDbg() {
  519. $msgList = array();
  520. foreach ($this->_errors as $vErr) {
  521. $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
  522. }
  523. return $msgList;
  524. }
  525. }