SyncUsers.php 22 KB

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