use new method GetOwnerOrganizationAttCode in Attachment

+ test
This commit is contained in:
Anne-Cath
2024-12-16 15:42:47 +01:00
parent add7743b6f
commit 3ff97be963
4 changed files with 66 additions and 56 deletions

View File

@@ -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 @@
<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>

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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');
}
}