SyncUsers.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. * @returns 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. }
  37. else {// $usrFrom && $usrTo
  38. $updateData = array();
  39. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  40. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  41. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  42. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  43. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  44. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  45. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = $usrFrom->password;
  46. $updated = $this->_toStorage->updateUser($usrLogin, $updateData);
  47. if (!$updated) {
  48. $errors = $this->_toStorage->getRawErrorsList();
  49. foreach ($errors as $vErr) {
  50. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  51. }
  52. $this->setError(1, "TODO: update user {$usrLogin} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  53. return false;
  54. }
  55. }
  56. if ($syncGroups) {
  57. $synced = $this->syncUserGroups($usrLogin);
  58. if (!$synced) {
  59. $this->setError(1, "Error: sync groups for user '{$usrLogin}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  60. return false;
  61. }
  62. }
  63. if ($syncDisabled) {
  64. $synced = $this->syncDisabled($usrLogin);
  65. if (!$synced) {
  66. $this->setError(1, "Error: sync groups for user '{$usrLogin}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  67. return false;
  68. }
  69. }
  70. $this->_fromStorage->setSyncUserDate($usrLogin);
  71. $this->_toStorage->setSyncUserDate($usrLogin);
  72. return true;
  73. }
  74. public function getSyncUserTodoList($usrLogin, $syncGroups = false, $syncDisabled = false) {
  75. $syncTodoList = array();
  76. $usrFrom = $this->_fromStorage->getUser($usrLogin);
  77. $usrTo = $this->_toStorage->getUser($usrLogin);
  78. if (!$usrFrom) {
  79. $syncTodoList[] = "User {$usrLogin} not exists in fromStorage";
  80. return $syncTodoList;
  81. }
  82. else if (!$usrTo) {
  83. $syncDisabled = false;
  84. $syncTodoList[] = "Create user {$usrLogin} in toStorage";
  85. }
  86. else {// $usrFrom && $usrTo
  87. $updateData = array();
  88. if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
  89. if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
  90. if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
  91. if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
  92. if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
  93. if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
  94. if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = '*****';
  95. foreach ($updateData as $key => $val) {
  96. $syncTodoList[] = "Update {$key}: {$val}";
  97. }
  98. }
  99. if ($syncDisabled) {
  100. $usrFromDisabeld = $this->_fromStorage->isDisabled($usrFrom);
  101. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  102. if ($usrFromDisabeld === null || $usrToDisabeld === null) {
  103. $syncTodoList[] = "Error: isDisabled '{$usrLogin}' not set in fromStorage or toStorage";
  104. return $syncTodoList;
  105. }
  106. if ($usrFromDisabeld !== $usrToDisabeld) {
  107. $syncTodoList[] = "Set isDisabled '{$usrLogin}' to " . (($usrFromDisabeld)? '1' : '0');
  108. }
  109. }
  110. if ($syncGroups) {
  111. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  112. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  113. $groupsTodo = array();
  114. foreach ($groupsTo as $kUid => $vName) {
  115. $groupsTodo[$kUid] = false;
  116. }
  117. foreach ($groupsFrom as $kUid => $vGroup) {
  118. if (isset($groupsTodo[$kUid])) {
  119. unset($groupsTodo[$kUid]);
  120. } else {
  121. $groupsTodo[$kUid] = true;
  122. }
  123. }
  124. if (!empty($groupsTodo)) {
  125. foreach ($groupsTodo as $kGroupID => $vBool) {
  126. if ($vBool) {
  127. $syncTodoList[] = "Add user '{$usrLogin}' to group {$kGroupID}";
  128. }
  129. else {
  130. $syncTodoList[] = "Remove user '{$usrLogin}' from group {$kGroupID}";
  131. }
  132. }
  133. }
  134. }
  135. return $syncTodoList;
  136. }
  137. public function syncDisabled($usrLogin, $usrFrom = null, $usrTo = null) {
  138. if (!$usrFrom) $usrFrom = $this->_fromStorage->getUser($usrLogin);
  139. if (!$usrTo) $usrTo = $this->_toStorage->getUser($usrLogin);
  140. if (!$usrFrom || !$usrTo) {
  141. $this->setError(1, "Error: user '{$usrLogin}' not exists in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  142. return false;
  143. }
  144. $usrFromDisabeld = $this->_fromStorage->isDisabled($usrFrom);
  145. $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
  146. if ($usrFromDisabeld === null || $usrToDisabeld === null) {
  147. $this->setError(1, "Error: isDisabled '{$usrLogin}' not set in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  148. return false;
  149. }
  150. if ($usrFromDisabeld !== $usrToDisabeld) {
  151. $synced = $this->_toStorage->setDisabled($usrLogin, $usrFromDisabeld);
  152. if (!$synced) {
  153. $this->setError(1, "Error: sync isDisabled '{$usrLogin}' failed ", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. public function syncUserGroups($usrLogin, $usrFrom = null, $usrTo = null) {
  160. if (!$usrFrom) $usrFrom = $this->_fromStorage->getUser($usrLogin);
  161. if (!$usrTo) $usrTo = $this->_toStorage->getUser($usrLogin);
  162. if (!$usrFrom || !$usrTo) {
  163. $this->setError(1, "Error: user '{$usrLogin}' not exists in fromStorage or toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  164. return false;
  165. }
  166. $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
  167. $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
  168. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  169. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsFrom (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsFrom);echo'</pre>';
  170. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTo);echo'</pre>';
  171. }
  172. /**
  173. * $groupsTodo - groups todo list:
  174. * 'com.apple.access_mail' => true - add to this group
  175. * 'com.apple.access_mail' => false - remove from this group
  176. */
  177. $groupsTodo = array();
  178. foreach ($groupsTo as $kUid => $vName) {
  179. $groupsTodo[$kUid] = false;
  180. }
  181. foreach ($groupsFrom as $kUid => $vGroup) {
  182. if (isset($groupsTodo[$kUid])) {
  183. unset($groupsTodo[$kUid]);
  184. } else {
  185. $groupsTodo[$kUid] = true;
  186. }
  187. }
  188. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  189. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  190. }
  191. if (!empty($groupsTodo)) {
  192. foreach ($groupsTodo as $kGroupID => $vBool) {
  193. if ($vBool) {
  194. $added = $this->_toStorage->addUserGroup($usrLogin, $groupsFrom[$kGroupID]);
  195. if (!$added) {
  196. $this->setError(1, "Error: user '{$usrLogin}' add to group '{$kGroupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  197. }
  198. }
  199. else {
  200. $removed = $this->_toStorage->removeUserGroup($usrLogin, $groupsTo[$kGroupID]);
  201. if (!$removed) {
  202. $this->setError(1, "Error: user '{$usrLogin}' remove from group '{$kGroupID}/{$groupsTo[$kGroupID]->name}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  203. }
  204. }
  205. }
  206. }
  207. if ($this->hasErrors()) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. public function getSyncGroupTodoList($groupID, $syncNestedGroups = false) {
  213. $syncTodoList = array();
  214. $groupFrom = $this->_fromStorage->getGroup($groupID);
  215. $groupTo = $this->_toStorage->getGroup($groupID);
  216. if (!$groupFrom) {
  217. $syncTodoList[] = "Group {$groupID} not exists in fromStorage";
  218. return $syncTodoList;
  219. }
  220. else if (!$groupTo) {
  221. $syncTodoList[] = "Create group {$groupID} in toStorage";
  222. }
  223. else {
  224. $updateData = array();
  225. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  226. $updateData['realName'] = $groupFrom->realName;
  227. }
  228. foreach ($updateData as $key => $val) {
  229. $syncTodoList[] = "Update {$key}: {$val}";
  230. }
  231. }
  232. if ($syncNestedGroups) {
  233. if (!empty($groupTo->nestedGroups) && !empty($groupFrom->nestedGroups)) {
  234. $groupsTodo = array();
  235. if (!empty($groupTo->nestedGroups)) {
  236. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  237. $groupsTodo[$kUid] = false;
  238. }
  239. }
  240. if (!empty($groupFrom->nestedGroups)) {
  241. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  242. if (isset($groupsTodo[$kUid])) {
  243. unset($groupsTodo[$kUid]);
  244. } else {
  245. $groupsTodo[$kUid] = true;
  246. }
  247. }
  248. }
  249. if (!empty($groupsTodo)) {
  250. foreach ($groupsTodo as $kGroupID => $vBool) {
  251. if ($vBool) {
  252. $syncTodoList[] = "Add group '{$kGroupID}' to group '{$groupID}' in toStorage";
  253. }
  254. else {
  255. $syncTodoList[] = "Remove group '{$kGroupID}' from group '{$groupID}' in toStorage";
  256. }
  257. }
  258. }
  259. }
  260. }
  261. return $syncTodoList;
  262. }
  263. /**
  264. * Sync user.
  265. *
  266. * @returns bool or -int if error @see getSyncUserErrorMsg($errCode);
  267. */
  268. public function syncGroup($groupID, $syncNestedGroups = false) {
  269. $groupFrom = $this->_fromStorage->getGroup($groupID);
  270. $groupTo = $this->_toStorage->getGroup($groupID);
  271. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  272. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupFrom (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupFrom);echo'</pre>';
  273. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupTo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupTo);echo'</pre>';
  274. }
  275. if (!$groupFrom) {
  276. $this->setError(1, "Group {$groupID} not exists in fromStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  277. return false;
  278. }
  279. else if (!$groupTo) {
  280. $created = $this->_toStorage->createGroup($groupFrom);
  281. if (!$created) {
  282. $errors = $this->_toStorage->getRawErrorsList();
  283. foreach ($errors as $vErr) {
  284. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  285. }
  286. $this->setError(1, "Error: create group {$groupID} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  287. return false;
  288. }
  289. }
  290. else {// $groupFrom && $groupTo
  291. $updateData = array();
  292. if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
  293. $updateData['realName'] = $groupFrom->realName;
  294. }
  295. //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
  296. $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
  297. if (!$updated) {
  298. $errors = $this->_toStorage->getRawErrorsList();
  299. foreach ($errors as $vErr) {
  300. $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
  301. }
  302. $this->setError(1, "TODO: update group {$groupID} from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  303. return false;
  304. }
  305. }
  306. if ($syncNestedGroups) {
  307. $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
  308. if (!$synced) {
  309. $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  310. return false;
  311. }
  312. }
  313. $this->_fromStorage->setSyncGroupDate($groupID);
  314. $this->_toStorage->setSyncGroupDate($groupID);
  315. return true;
  316. }
  317. public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
  318. if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
  319. if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
  320. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  321. 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>';
  322. 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>';
  323. }
  324. if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
  325. // nothing to do
  326. return true;
  327. }
  328. else {
  329. /*
  330. [nestedGroups] => Array(
  331. [2981] => stdClass Object(
  332. [primaryKey] => 2981
  333. [type] => STANOWISKO
  334. [realName] => [2981] Kierownik ds. Rozwoju Biznesu
  335. [zasobID] => 2981
  336. [zasobDESC] => Kierownik ds. Rozwoju Biznesu
  337. */
  338. /**
  339. * $groupsTodo - groups todo list:
  340. * 'com.apple.access_mail' => true - add to this group
  341. * 'com.apple.access_mail' => false - remove from this group
  342. */
  343. $groupsTodo = array();
  344. if (!empty($groupTo->nestedGroups)) {
  345. foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
  346. $groupsTodo[$kUid] = false;
  347. }
  348. }
  349. if (!empty($groupFrom->nestedGroups)) {
  350. foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
  351. if (isset($groupsTodo[$kUid])) {
  352. unset($groupsTodo[$kUid]);
  353. } else {
  354. $groupsTodo[$kUid] = true;
  355. }
  356. }
  357. }
  358. if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
  359. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
  360. }
  361. if (!empty($groupsTodo)) {
  362. foreach ($groupsTodo as $kGroupID => $vBool) {
  363. if ($vBool) {
  364. $added = $this->_toStorage->addNestedGroup($groupID, $groupFrom->nestedGroups[$kGroupID]);
  365. if (!$added) {
  366. $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  367. }
  368. }
  369. else {
  370. $removed = $this->_toStorage->removeNestedGroup($groupID, $groupTo->nestedGroups[$kGroupID]);
  371. if (!$removed) {
  372. $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
  373. }
  374. }
  375. }
  376. }
  377. }
  378. if ($this->hasErrors()) {
  379. return false;
  380. }
  381. return true;
  382. }
  383. private function setError($code, $msg, $dbgMsg) {
  384. $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
  385. }
  386. public function hasErrors() {
  387. return !empty($this->_errors);
  388. }
  389. public function getErrorsMsgList() {
  390. $msgList = array();
  391. foreach ($this->_errors as $vErr) {
  392. $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
  393. }
  394. return $msgList;
  395. }
  396. public function getErrorsMsgListWithDbg() {
  397. $msgList = array();
  398. foreach ($this->_errors as $vErr) {
  399. $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
  400. }
  401. return $msgList;
  402. }
  403. }