SyncUsers.php 16 KB

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