N°5324 - repair profiles by default or if configured for customer with multiple profiles + move dedicated test in UserProfilesEventListenerTest.php

This commit is contained in:
odain
2023-06-14 15:13:29 +02:00
parent d8c7888eac
commit b8d04e40e4
3 changed files with 523 additions and 341 deletions

View File

@@ -24,11 +24,20 @@ define('POWER_USER_PORTAL_PROFILE_NAME', 'Portal power user');
*/
class UserProfilesEventListener implements iEventServiceSetup
{
private $oUserPortalProfile;
private $bIsRepairmentEnabled = false;
/**
* @inheritDoc
*/
public function RegisterEventsAndListeners()
{
$this->Init();
if (false === $this->bIsRepairmentEnabled){
return;
}
$callback = [$this, 'OnUserProfileLinkChange'];
$aEventSource = [\User::class, \UserExternal::class, \UserInternal::class];
@@ -45,23 +54,82 @@ class UserProfilesEventListener implements iEventServiceSetup
);
}
public function IsRepairmentEnabled() : bool
{
return $this->bIsRepairmentEnabled;
}
public function OnUserProfileLinkChange(EventData $oEventData): void {
/** @var \User $oObject */
$oUser = $oEventData->Get('object');
try {
self::RepairProfiles($oUser);
} catch (Exception $oException) {
IssueLog::Error('Exception occurred on OnUserProfileLinkChange', LogChannels::DM_CRUD, [
$this->RepairProfiles($oUser);
} catch (Exception $e) {
IssueLog::Error('Exception occurred on RepairProfiles', LogChannels::DM_CRUD, [
'user_class' => get_class($oUser),
'user_id' => $oUser->GetKey(),
'exception_message' => $oException->getMessage(),
'exception_stacktrace' => $oException->getTraceAsString(),
'exception_message' => $e->getMessage(),
'exception_stacktrace' => $e->getTraceAsString(),
]);
}
}
public static function RepairProfiles(\User $oUser) : void
/**
* @param $aPortalDispatcherData: passed only for testing purpose
*
* @return void
* @throws \ConfigException
* @throws \CoreException
*/
public function Init($aPortalDispatcherData=null) : void {
if (is_null($aPortalDispatcherData)){
$aPortalDispatcherData = \PortalDispatcherData::GetData();
}
$sRepairmentProfile = \utils::GetConfig()->GetModuleSetting('itop-profiles-itil', 'poweruserportal-repair-profile', null);
if (is_null($sRepairmentProfile) && sizeof($aPortalDispatcherData) > 2){
//when there are further portals we dont want to force a specific portal by repairing the associated profiles to a user
$this->bIsRepairmentEnabled = false;
return;
}
if (is_null($sRepairmentProfile)){
$sRepairmentProfile = PORTAL_PROFILE_NAME;
}
try {
$sOQL = sprintf("SELECT URP_Profiles WHERE name = '%s'", $sRepairmentProfile);
$oSearch = \DBSearch::FromOQL($sOQL);
$oSearch->AllowAllData();
$oSet = new \DBObjectSet($oSearch);
if ($oSet->Count() !== 1) {
//user portal profile does not exist
//current iTop is customized enough to disable repairment
$this->bIsRepairmentEnabled = false;
return;
}
$this->oUserPortalProfile = $oSet->Fetch();
if (is_null($this->oUserPortalProfile)){
//may be not required. preventive code to disable repairment
$this->bIsRepairmentEnabled = false;
return;
}
} catch (\Exception $e) {
IssueLog::Error('Exception when searching user portal profile', LogChannels::DM_CRUD, [
'exception_message' => $e->getMessage(),
'exception_stacktrace' => $e->getTraceAsString(),
]);
$this->bIsRepairmentEnabled = false;
return;
}
$this->bIsRepairmentEnabled = true;
}
public function RepairProfiles(\User $oUser) : void
{
if (!is_null($oUser))
{
@@ -72,17 +140,8 @@ class UserProfilesEventListener implements iEventServiceSetup
if (POWER_USER_PORTAL_PROFILE_NAME === $oProfile->Get('profile')){
//add portal user
// power portal user is not a standalone profile (it will break console UI)
$sOQL = sprintf("SELECT URP_Profiles WHERE name = '%s'", PORTAL_PROFILE_NAME);
$oSearch = \DBSearch::FromOQL($sOQL);
$oSearch->AllowAllData();
$oSet = new \DBObjectSet($oSearch);
if ($oSet->Count() !==1){
return;
}
$oUserPortalProfile = $oSet->Fetch();
$oUserProfile = new \URP_UserProfile();
$oUserProfile->Set('profileid', $oUserPortalProfile->GetKey());
$oUserProfile->Set('profileid', $this->oUserPortalProfile->GetKey());
$oCurrentUserProfileSet->AddItem($oUserProfile);
$oUser->Set('profile_list', $oCurrentUserProfileSet);
}