| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- class SyncUsers {
- private $_fromStorage;
- private $_toStorage;
- private $_errors = array();
- public function __construct($fromStorage, $toStorage) {
- $this->_fromStorage = $fromStorage;
- $this->_toStorage = $toStorage;
- }
- /**
- * Sync user.
- *
- * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
- */
- public function syncUser($usrLogin) {
- $syncGroups = true;
- $syncDisabled = true;
- $usrFrom = $this->_fromStorage->getUser($usrLogin);
- $usrTo = $this->_toStorage->getUser($usrLogin);
- $usrFromDisabled = null;
- $usrToDisabeld = null;
- if (!$usrFrom) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie danych");
- DBG::_('DBG_SU', true, 'usrFrom', $usrFrom, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', true, 'usrTo', $usrTo, __CLASS__, __FUNCTION__, __LINE__);
- $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
- if (!$usrTo && true === $usrFromDisabled) {
- 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.");
- }
- if (!$usrTo) {
- $this->_toStorage->createUser($usrFrom);
- $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrTo) throw new Exception("Nie udało się utworzyć użytkownika '{$usrLogin}' w bazie LDAP");
- $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
- }
- else {// $usrFrom && $usrTo
- $this->syncExistingUser($usrLogin, $usrFrom, $usrTo);
- }
- {// $syncDisabled
- $usrTo = $this->_toStorage->getUser($usrLogin);
- if (!$usrTo) throw new Exception("Użytkownik '{$usrLogin}' nie istnieje w bazie LDAP");
- $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
- if (null === $usrFromDisabled) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie danych");
- if (null === $usrToDisabeld) throw new Exception("Nieznany status blokady dla użytkownika '{$usrLogin}' w bazie LDAP");
- if ($usrFromDisabled !== $usrToDisabeld) {
- if (!$this->_toStorage->setDisabled($usrLogin, $usrFromDisabled)) {
- throw new Exception("Nie udało się ustawić statusu blokady dla użytkownika '{$usrLogin}'");
- }
- }
- }
- {// $syncGroups
- $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
- $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
- $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
- DBG::_('DBG_SU', '>0', 'groupsTodo', $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', '>0', 'groupsFrom', $groupsFrom, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', '>0', 'groupsTo', $groupsTo, __CLASS__, __FUNCTION__, __LINE__);
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $this->_toStorage->addUserGroup($usrLogin, $groupsFrom[$kGroupID]);
- }
- else {
- $this->_toStorage->removeUserGroup($usrLogin, $groupsTo[$kGroupID]);
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- $this->_fromStorage->setSyncUserDate($usrLogin);
- $this->_toStorage->setSyncUserDate($usrLogin);
- return true;
- }
- public function syncExistingUser($usrLogin, ObjectUser $usrFrom, ObjectUser $usrTo) {
- if (!$usrFrom) return false;
- if (!$usrTo) return false;
- $updateData = array();
- if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
- if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
- if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
- if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
- if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
- if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
- if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = $usrFrom->password;
- $updated = $this->_toStorage->updateUser($usrLogin, $updateData);
- if (!$updated) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- throw new Exception("TODO: update user {$usrLogin} from Database to Ldap");
- }
- }
- public function getSyncUserTodoList($usrLogin) {
- $syncGroups = true;
- $syncDisabled = true;
- $syncTodoList = array();
- $usrFrom = $this->_fromStorage->getUser($usrLogin);
- $usrTo = $this->_toStorage->getUser($usrLogin);
- DBG::_('DBG_SU', true, 'usrFrom', $usrFrom, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', true, 'usrTo', $usrTo, __CLASS__, __FUNCTION__, __LINE__);
- if (!$usrFrom) {
- $syncTodoList[] = "Użytkownik {$usrLogin} nie istnieje w bazie danych";
- return $syncTodoList;
- }
- $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
- if (!$usrTo && true === $usrFromDisabled) {
- 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.");
- }
- if (!$usrTo) {
- $syncDisabled = false;
- $syncTodoList[] = "Utwórz użytkownika '{$usrLogin}' w bazie LDAP";
- }
- else {// $usrFrom && $usrTo
- $updateData = array();
- if ($usrFrom->name != $usrTo->name) $updateData['name'] = $usrFrom->name;
- if ($usrFrom->email != $usrTo->email) $updateData['email'] = $usrFrom->email;
- if ($usrFrom->phone != $usrTo->phone) $updateData['phone'] = $usrFrom->phone;
- if ($usrFrom->homeEmail != $usrTo->homeEmail) $updateData['homeEmail'] = $usrFrom->homeEmail;
- if ($usrFrom->homePhone != $usrTo->homePhone) $updateData['homePhone'] = $usrFrom->homePhone;
- if ($usrFrom->employeeType != $usrTo->employeeType) $updateData['employeeType'] = $usrFrom->employeeType;
- if ($this->_fromStorage->isPasswordChanged($usrLogin)) $updateData['password'] = '*****';
- foreach ($updateData as $key => $val) {
- $syncTodoList[] = "Aktualizuj {$key}: {$val}";
- }
- }
- $usrToDisabeld = $this->_toStorage->isDisabled($usrTo);
- if ($syncDisabled) {
- if ($usrFromDisabled === null || $usrToDisabeld === null) {
- $syncTodoList[] = "Status blokady '{$usrLogin}' nieznany w bazie danych lub LDAP";
- return $syncTodoList;
- }
- if ($usrFromDisabled !== $usrToDisabeld) {
- $syncTodoList[] = "Ustawienie statusu blokady '{$usrLogin}' na '" . (($usrFromDisabled)? 'true' : 'false') . "'";
- }
- }
- {// $syncGroups
- $groupsTodo = $this->getSyncUserGroupsTodoList($usrLogin);
- DBG::_('DBG_SU', '>0', "groupsTodo usrFromDisabeld(" . (($usrFromDisabled)? 'true' : 'false') . ")", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Dodaj '{$usrLogin}' do grupy {$kGroupID}";
- }
- else {
- $syncTodoList[] = "Usuń '{$usrLogin}' z grupy {$kGroupID}";
- }
- }
- }
- }
- return $syncTodoList;
- }
- /**
- * @returns array $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- public function getSyncUserGroupsTodoList($usrLogin) {
- $groupsTodo = array();// `guid` => true (add), false (remove)
- $usrFrom = $this->_fromStorage->getUser($usrLogin);
- $usrFromDisabled = $this->_fromStorage->isDisabled($usrFrom);
- $groupsFrom = $this->_fromStorage->getUserGroups($usrLogin);
- $groupsTo = $this->_toStorage->getUserGroups($usrLogin);
- foreach ($groupsTo as $kUid => $vName) {
- $groupsTodo[$kUid] = false;
- }
- if (true === $usrFromDisabled) {
- // remove all groups
- } else {
- foreach ($groupsFrom as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- return $groupsTodo;
- }
- public function getSyncGroupTodoList($idGroup, $syncNestedGroups = false) {
- $syncTodoList = array();
- $groupFrom = $this->_fromStorage->getGroup($idGroup);
- $groupTo = $this->_toStorage->getGroup($idGroup);
- if (!$groupFrom) {
- $syncTodoList[] = "Grupa {$idGroup} nie istnieje w bazie danych";
- return $syncTodoList;
- }
- if (!$groupTo) {
- $syncTodoList[] = "Utwórz grupę {$idGroup} w bazie LDAP";
- return $syncTodoList;
- }
- $updateData = array();
- if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
- $updateData['realName'] = $groupFrom->realName;
- }
- foreach ($updateData as $key => $val) {
- $syncTodoList[] = "Aktualizuj {$key}: {$val}";
- }
- if ($syncNestedGroups) {
- if (!empty($groupTo->nestedGroups) || !empty($groupFrom->nestedGroups)) {
- $groupsTodo = array();
- if (!empty($groupTo->nestedGroups)) {
- foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($groupFrom->nestedGroups)) {
- foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kIdGroup => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Dodaj grupę '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";
- }
- else {
- $syncTodoList[] = "Usuń grupę '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";
- }
- }
- }
- }
- $fromParentGroups = $groupFrom->getParentGroups();
- $toParentGroups = $groupTo->getParentGroups();
- {
- $groupsTodo = array();
- if (!empty($toParentGroups)) {
- foreach ($toParentGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($fromParentGroups)) {
- foreach ($fromParentGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kIdGroup => $vBool) {
- if ($vBool) {
- $syncTodoList[] = "Dodaj grupę nadrzędną '{$kIdGroup}' do grupy '{$idGroup}' w bazie LDAP";// == add $idGroup to $kIdGroup
- }
- else {
- $groupTest = $this->_fromStorage->getGroup($kIdGroup);
- if ($groupTest) {
- $syncTodoList[] = "Usuń grupę nadrzędną '{$kIdGroup}' z grupy '{$idGroup}' w bazie LDAP";// == remove $idGroup from $kIdGroup
- } else {
- //$syncTodoList[] = "Keep parent group '{$kIdGroup}' in group '{$idGroup}' in toStorage, because that group dont exists in fromStorage";
- }
- }
- }
- }
- }
- }
- return $syncTodoList;
- }
- /**
- * Sync user.
- *
- * @return bool or -int if error @see getSyncUserErrorMsg($errCode);
- */
- public function syncGroup($idGroup, $syncNestedGroups = false) {
- $groupFrom = $this->_fromStorage->getGroup($idGroup);
- $groupTo = $this->_toStorage->getGroup($idGroup);
- DBG::_('DBG_SU', '>0', 'groupFrom', $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', '>0', 'groupTo', $groupTo, __CLASS__, __FUNCTION__, __LINE__);
- if (!$groupFrom) throw new Exception("Grupa [{$idGroup}] nie istnieje w bazie danych");
- if (!$groupTo) {
- $this->_toStorage->createGroup($groupFrom);
- $groupTo = $this->_toStorage->getGroup($idGroup);
- if (!$groupTo) throw new Exception("Nie udało się utworzyć grupy [{$idGroup}] w bazie LDAP");
- $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
- }
- else {// $groupFrom && $groupTo
- $this->syncExistingGroup($idGroup, $groupFrom, $groupTo, $syncNestedGroups);
- }
- $this->_fromStorage->setSyncGroupDate($idGroup);
- $this->_toStorage->setSyncGroupDate($idGroup);
- return true;
- }
- public function syncExistingGroup($groupID, ObjectGroup $groupFrom, ObjectGroup $groupTo, $syncNestedGroups = false) {
- if (!$groupFrom) throw new Exception("Nie podano grupy z bazy danych!");
- if (!$groupTo) throw new Exception("Nie podano grupy z bazy LDAP!");
- $updateData = array();
- if (empty($groupTo->realName) && $groupFrom->realName != $groupTo->realName) {
- $updateData['realName'] = $groupFrom->realName;
- }
- //if ($groupFrom->employeeType != $groupTo->employeeType) $updateData['employeeType'] = $groupFrom->employeeType;
- $updated = $this->_toStorage->updateGroup($groupTo, $updateData);
- if (!$updated) {
- $errors = $this->_toStorage->getRawErrorsList();
- foreach ($errors as $vErr) {
- $this->setError($vErr->code, $vErr->msg, $vErr->dbgMsg);
- }
- throw new Exception("TODO: update group [{$groupID}] from fromStorage to toStorage");
- }
- if ($syncNestedGroups) {
- $synced = $this->syncNestedGroups($groupID, $groupFrom, $groupTo);
- if (!$synced) {
- $this->setError(1, "Error: sync nested groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- $synced = $this->syncParentGroups($groupID, $groupFrom, $groupTo);
- if (!$synced) {
- $this->setError(1, "Error: sync parent groups for group '{$groupID}' from fromStorage to toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- return false;
- }
- }
- return true;
- }
- public function syncNestedGroups($groupID, $groupFrom = null, $groupTo = null) {
- if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
- if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
- DBG::_('DBG_SU', '>0', "groupFrom (hasNestedGroups:" . (!empty($groupFrom->nestedGroups)) . ")", $groupFrom, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_SU', '>0', "groupTo (hasNestedGroups:" . (!empty($groupTo->nestedGroups)) . ")", $groupTo, __CLASS__, __FUNCTION__, __LINE__);
- if (empty($groupFrom->nestedGroups) && empty($groupTo->nestedGroups)) {
- // nothing to do
- return true;
- }
- else {
- /*
- [nestedGroups] => Array(
- [2981] => stdClass Object(
- [primaryKey] => 2981
- [type] => STANOWISKO
- [realName] => [2981] Kierownik ds. Rozwoju Biznesu
- [zasobID] => 2981
- [zasobDESC] => Kierownik ds. Rozwoju Biznesu
- */
- /**
- * $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- $groupsTodo = array();
- if (!empty($groupTo->nestedGroups)) {
- foreach ($groupTo->nestedGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($groupFrom->nestedGroups)) {
- foreach ($groupFrom->nestedGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- DBG::_('DBG_SU', '>0', "groupsTodo", $groupsTodo, __CLASS__, __FUNCTION__, __LINE__);
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- $this->_toStorage->addNestedGroup($groupID, $kGroupID);
- }
- else {
- $this->_toStorage->removeNestedGroup($groupID, $kGroupID);
- }
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- public function syncParentGroups($groupID, $groupFrom = null, $groupTo = null) {
- if (!$groupFrom) $groupFrom = $this->_fromStorage->getGroup($groupID);
- if (!$groupTo) $groupTo = $this->_toStorage->getGroup($groupID);
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- 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>';
- 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>';
- }
- $fromParentGroups = $groupFrom->getParentGroups();
- $toParentGroups = $groupTo->getParentGroups();
- if (empty($fromParentGroups) && empty($toParentGroups)) {
- return true;
- }
- else {
- /*
- [nestedGroups] => Array(
- [2981] => stdClass Object(
- [primaryKey] => 2981
- [type] => STANOWISKO
- [realName] => [2981] Kierownik ds. Rozwoju Biznesu
- [zasobID] => 2981
- [zasobDESC] => Kierownik ds. Rozwoju Biznesu
- */
- /**
- * $groupsTodo - groups todo list:
- * 'com.apple.access_mail' => true - add to this group
- * 'com.apple.access_mail' => false - remove from this group
- */
- $groupsTodo = array();
- if (!empty($toParentGroups)) {
- foreach ($toParentGroups as $kUid => $vGroup) {
- $groupsTodo[$kUid] = false;
- }
- }
- if (!empty($fromParentGroups)) {
- foreach ($fromParentGroups as $kUid => $vGroup) {
- if (isset($groupsTodo[$kUid])) {
- unset($groupsTodo[$kUid]);
- } else {
- $groupsTodo[$kUid] = true;
- }
- }
- }
- if (V::get('DBG_SU', 0, $_GET, 'int') > 0) {
- echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">groupsTodo (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($groupsTodo);echo'</pre>';
- }
- if (!empty($groupsTodo)) {
- foreach ($groupsTodo as $kGroupID => $vBool) {
- if ($vBool) {
- //$syncTodoList[] = "Add parent group '{$kGroupID}' to group '{$groupID}' in toStorage";// == add $groupID to $kGroupID
- $this->_toStorage->addNestedGroup($kGroupID, $groupID);
- // TODO: add try catch to prevent resend exception
- if (0) {
- $this->setError(1, "Error: group '{$kGroupID}' add to group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- }
- else {
- $groupTest = $this->_fromStorage->getGroup($kGroupID);
- if ($groupTest) {
- //$syncTodoList[] = "Remove parent group '{$kGroupID}' from group '{$groupID}' in toStorage";// == remove $groupID from $kGroupID
- $this->_toStorage->removeNestedGroup($kGroupID, $groupID);
- // TODO: add try catch to prevent resend exception
- if (0) {
- $this->setError(1, "Error: group '{$kGroupID}' remove from group '{$groupID}' in toStorage", '(' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . ')');
- }
- } else {
- //$syncTodoList[] = "Keep parent group '{$kGroupID}' in group '{$groupID}' in toStorage, because that group dont exists in fromStorage";
- }
- }
- }
- }
- }
- if ($this->hasErrors()) {
- return false;
- }
- return true;
- }
- private function setError($code, $msg, $dbgMsg) {
- $this->_errors[] = (object)array('code'=>$code, 'msg'=>$msg, 'dbgMsg'=>$dbgMsg);
- }
- public function hasErrors() {
- return !empty($this->_errors);
- }
- public function getErrorsMsgList() {
- $msgList = array();
- foreach ($this->_errors as $vErr) {
- $msgList[] = "Error {$vErr->code}: {$vErr->msg}";
- }
- return $msgList;
- }
- public function getErrorsMsgListWithDbg() {
- $msgList = array();
- foreach ($this->_errors as $vErr) {
- $msgList[] = "Error {$vErr->code}: {$vErr->msg} (DBG:{$vErr->dbgMsg})";
- }
- return $msgList;
- }
- }
|