diff --git a/datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml b/datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml index 2e8670240..bc54d4d52 100755 --- a/datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml +++ b/datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml @@ -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(); } } @@ -191,24 +187,15 @@ 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)); } }]]> diff --git a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php index f9fc9fe41..1d0825ed4 100644 --- a/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php +++ b/tests/php-unit-tests/src/BaseTestCase/ItopDataTestCase.php @@ -1406,4 +1406,37 @@ abstract class ItopDataTestCase extends ItopTestCase self::markTestSkipped("Test skipped: module '$sModule' is not present"); } } + + 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); + } } diff --git a/tests/php-unit-tests/unitary-tests/core/InlineImageTest.php b/tests/php-unit-tests/unitary-tests/core/InlineImageTest.php index 04df96c4c..84189deaa 100644 --- a/tests/php-unit-tests/unitary-tests/core/InlineImageTest.php +++ b/tests/php-unit-tests/unitary-tests/core/InlineImageTest.php @@ -88,35 +88,4 @@ class InlineImageTest extends ItopDataTestCase $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); - } } diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-attachments/TestAttachment.php b/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-attachments/TestAttachment.php index 675e0abdc..9745b6d68 100644 --- a/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-attachments/TestAttachment.php +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/itop-attachments/TestAttachment.php @@ -90,4 +90,25 @@ class TestAttachment extends ItopDataTestCase $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'); + } }