Skip to content

Commit

Permalink
use new method GetOwnerOrganizationAttCode in Attachment
Browse files Browse the repository at this point in the history
+ test
  • Loading branch information
accognet committed Jan 31, 2025
1 parent a2a5c93 commit 724d283
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
37 changes: 12 additions & 25 deletions datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,16 @@
$this->Set('item_class', $sClass);
$this->Set('item_id', $iItemId);
$sAttCode = UserRights::GetOwnerOrganizationAttCode( $sClass);
if (is_null($sAttCode)) {
// No need for silos
return;
}
$iOrgId = $oItem->Get($sAttCode);
if ($iOrgId > 0)
{
if ($iOrgId != $this->Get('item_org_id'))
{
if ($iOrgId > 0) {
if ($iOrgId != $this->Get('item_org_id')) {
$this->Set('item_org_id', $iOrgId);
if ($bUpdateOnChange)
{
if ($bUpdateOnChange) {
$this->DBUpdate();
}
}
Expand All @@ -191,24 +187,15 @@
<code><![CDATA[ public function SetDefaultOrgId()
{
// Check that the organization CAN be fetched from the current user
//
if (MetaModel::IsValidClass('Person'))
{
$aCallSpec = array('Person', 'MapContextParam');
if (is_callable($aCallSpec))
{
$sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
if (MetaModel::IsValidAttCode('Person', $sAttCode))
{
// OK - try it
//
$oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
if ($oCurrentPerson)
{
$this->Set('item_org_id', $oCurrentPerson->Get($sAttCode));
}
}
}
$sOrgAttrCodeForPerson = UserRights::GetOwnerOrganizationAttCode('Person');
if (is_null($sOrgAttrCodeForPerson)) {
// No need for silos
return;
}
$oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
if ($oCurrentPerson) {
$this->Set('item_org_id', $oCurrentPerson->Get($sOrgAttrCodeForPerson));
}
}]]></code>
</method>
Expand Down
33 changes: 33 additions & 0 deletions tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1465,4 +1465,37 @@ static protected function StopStopwatchInTheFuture(DBObject $oObject, string $sS
$oObject->Set($sStopwatchAttCode, $oStopwatch);
}


protected function GivenUserLoggedInWithContact(int $iContactOrgId)
{
$iContactId = $this->GivenObjectInDB('Person', [
'first_name' => 'TestContact',
'name' => 'TestContact',
'org_id' => $iContactOrgId]);
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
$iUser = $this->GivenObjectInDB('UserLocal', [
'login' => $sLogin,
'password' => 'tagada-Secret,007',
'language' => 'EN US',
'contactid' => $iContactId,
'profile_list' => [
'profileid:'.self::$aURP_Profiles['Configuration Manager']
]
]);
\UserRights::Login($sLogin);
}

protected function GivenUserLoggedInWithoutContact()
{
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
$iUser = $this->GivenObjectInDB('UserLocal', [
'login' => $sLogin,
'password' => 'tagada-Secret,007',
'language' => 'EN US',
'profile_list' => [
'profileid:'.self::$aURP_Profiles['Configuration Manager']
]
]);
\UserRights::Login($sLogin);
}
}
31 changes: 0 additions & 31 deletions tests/php-unit-tests/unitary-tests/core/InlineImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,4 @@ public function testSetDefaultOrgIdWhenLoggedInWithoutContact()
$this->assertEquals(0, $oInlineImage->Get('item_org_id'),'The org_id should be left undefined');
}

private function GivenUserLoggedInWithContact(int $iContactOrgId)
{
$iContactId = $this->GivenObjectInDB('Person', [
'first_name' => 'TestContact',
'name' => 'TestContact',
'org_id' => $iContactOrgId]);
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
$iUser = $this->GivenObjectInDB('UserLocal', [
'login' => $sLogin,
'password' => 'tagada-Secret,007',
'language' => 'EN US',
'contactid' => $iContactId,
'profile_list' => [
'profileid:'.self::$aURP_Profiles['Configuration Manager']
]
]);
\UserRights::Login($sLogin);
}
private function GivenUserLoggedInWithoutContact()
{
$sLogin = 'demo_test_'.uniqid(__CLASS__, true);
$iUser = $this->GivenObjectInDB('UserLocal', [
'login' => $sLogin,
'password' => 'tagada-Secret,007',
'language' => 'EN US',
'profile_list' => [
'profileid:'.self::$aURP_Profiles['Configuration Manager']
]
]);
\UserRights::Login($sLogin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,25 @@ public function testSetItemOnObjectWithoutDefinedOrganization()
$oAttachment->SetItem($oUserRequest);
$this->assertEquals(0, $oAttachment->Get('item_org_id'),'The org_id should be the one of the contact');
}


public function testSetDefaultOrgIdWhenLoggedInWithContact()
{
$iContactOrgId = $this->GivenObjectInDB('Organization', ['name' => 'TestOrg']);
$this->GivenUserLoggedInWithContact($iContactOrgId);

$oAttachment = new \Attachment();
$oAttachment->SetDefaultOrgId();
$this->assertEquals($iContactOrgId, $oAttachment->Get('item_org_id'),'The org_id should be the one of the contact');
}


public function testSetDefaultOrgIdWhenLoggedInWithoutContact()
{
$this->GivenUserLoggedInWithoutContact();

$oAttachment = new \Attachment();
$oAttachment->SetDefaultOrgId();
$this->assertEquals(0, $oAttachment->Get('item_org_id'),'The org_id should be left undefined');
}
}

0 comments on commit 724d283

Please sign in to comment.