From e9a1167da6fa4ecb9a9aadc5c4c0efcbe3deb171 Mon Sep 17 00:00:00 2001 From: acognet Date: Tue, 30 Jun 2020 09:02:42 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B02363=20-=20API=20:=20deprecate=20old=20l?= =?UTF-8?q?inkedset=20update=20pattern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/loginwebpage.class.inc.php | 48 +++++++++++++++---- .../2.x/authent-cas/src/CASLoginExtension.php | 13 ++--- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/application/loginwebpage.class.inc.php b/application/loginwebpage.class.inc.php index 363406000..dc7a09d84 100644 --- a/application/loginwebpage.class.inc.php +++ b/application/loginwebpage.class.inc.php @@ -100,6 +100,44 @@ class LoginWebPage extends NiceWebPage self::$m_sLoginFailedMessage = $sMessage; } + /** + * @param $oUser + * @param array $aProfiles + * + * @return array + * @throws \CoreException + * @throws \CoreUnexpectedValue + */ + public static function SynchroniseProfiles(&$oUser, array $aProfiles, $sOrigin) + { + $oProfilesSet = $oUser->Get(‘profile_list’); + //delete old profiles + $aExistingProfiles = []; + while ($oProfile = $oProfilesSet->Fetch()) + { + array_push($aExistingProfiles, $oProfile->Get('profileid')); + $iArrayKey = array_search($oProfile->Get('profileid'), $aProfiles); + if (!$iArrayKey) + { + $oProfilesSet->RemoveItem($oProfile->Get('profileid')); + } + else + { + unset($aProfiles[$iArrayKey]); + } + } + //add profiles not already linked with user + foreach ($aProfiles as $iProfileId) + { + $oLink = new URP_UserProfile(); + $oLink->Set('profileid', $iProfileId); + $oLink->Set('reason', $sOrigin); + + $oProfilesSet->AddItem(MetaModel::NewObject('URP_UserProfile', array('profileid' => $iProfileId, 'reason' => $sOrigin))); + } + $oUser->Set('profile_list', $oProfilesSet); + } + public function DisplayLoginHeader($bMainAppLogo = false) { $sLogo = 'itop-logo-external.png'; @@ -886,20 +924,12 @@ class LoginWebPage extends NiceWebPage } // Now synchronize the profiles - $oProfilesSet = DBObjectSet::FromScratch('URP_UserProfile'); $sOrigin = 'External User provisioning'; if (isset($_SESSION['login_mode'])) { $sOrigin .= " ({$_SESSION['login_mode']})"; } - foreach ($aProfiles as $iProfileId) - { - $oLink = new URP_UserProfile(); - $oLink->Set('profileid', $iProfileId); - $oLink->Set('reason', $sOrigin); - $oProfilesSet->AddObject($oLink); - } - $oUser->Set('profile_list', $oProfilesSet); + $aExistingProfiles = self::SynchroniseProfiles($oUser, $aProfiles, $sOrigin); if ($oUser->IsModified()) { $oUser->DBWrite(); diff --git a/datamodels/2.x/authent-cas/src/CASLoginExtension.php b/datamodels/2.x/authent-cas/src/CASLoginExtension.php index cefc54b56..4c5da3434 100644 --- a/datamodels/2.x/authent-cas/src/CASLoginExtension.php +++ b/datamodels/2.x/authent-cas/src/CASLoginExtension.php @@ -156,7 +156,7 @@ class CASLoginExtension extends AbstractLoginFSMExtension implements iLogoutExte { phpCAS::setDebug(APPROOT.'log/cas.log'); } - + // Initialize phpCAS $sCASVersion = Config::Get('cas_version'); $sCASHost = Config::Get('cas_host'); @@ -500,15 +500,8 @@ class CASUserProvisioning } // Now synchronize the profiles - $oProfilesSet = DBObjectSet::FromScratch('URP_UserProfile'); - foreach($aProfiles as $iProfileId) - { - $oLink = new URP_UserProfile(); - $oLink->Set('profileid', $iProfileId); - $oLink->Set('reason', 'CAS/LDAP Synchro'); - $oProfilesSet->AddObject($oLink); - } - $oUser->Set('profile_list', $oProfilesSet); + LoginWebPage::SynchroniseProfiles($oUser, $aProfiles, 'CAS/LDAP Synchro'); + phpCAS::log("Info: the user '".$oUser->GetName()."' (id=".$oUser->GetKey().") now has the following profiles: '".implode("', '", $aProfiles)."'."); if ($oUser->IsModified()) {