SyncUsers.php 17 KB

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