Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N°7963 - Inlineimage::SetDefaultOrgId blend field name between Person and linked class #680

Open
wants to merge 11 commits into
base: support/3.2
Choose a base branch
from
28 changes: 4 additions & 24 deletions addons/userrights/userrightsprofile.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public function GetSelectFilter($oUser, $sClass, $aSettings = array())
$aConditions = array();

// Determine if this class is part of a silo and build the filter for it
$sAttCode = self::GetOwnerOrganizationAttCode($sClass);
$sAttCode = UserRights::GetOwnerOrganizationAttCode($sClass);
if (!is_null($sAttCode))
{
$aUserOrgs = $this->GetUserOrgs($oUser, $sClass);
Expand Down Expand Up @@ -835,7 +835,7 @@ public function IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet = n
/** @var \DBObjectSet $oInstanceSet */
throw new CoreException(__FUNCTION__.': Expecting object set to be of class '.$sClass.' but it is of class '.$oInstanceSet->GetClass(), ['OQL_Query' => $oInstanceSet->GetFilter()->ToOQL(), 'classes' => $oInstanceSet->GetSelectedClasses()]);
}
$sOrgAttCode = self::GetOwnerOrganizationAttCode($sClass);
$sOrgAttCode = UserRights::GetOwnerOrganizationAttCode($sClass);
if (!is_null($sOrgAttCode)) {
$aUserOrgs = $this->GetUserOrgs($oUser, $sClass);
if (!is_null($aUserOrgs) && count($aUserOrgs) > 0) {
Expand Down Expand Up @@ -927,31 +927,11 @@ public function FlushPrivileges()
* @param string $sClass
* @return string|null Find out which attribute is corresponding the dimension 'owner org'
* returns null if no such attribute has been found (no filtering should occur)
* @deprecated 3.3.0 use @UserRights::GetOwnerOrganizationAttCode instead
*/
public static function GetOwnerOrganizationAttCode($sClass)
{
$sAttCode = null;

$aCallSpec = array($sClass, 'MapContextParam');
if (($sClass == 'Organization') || is_subclass_of($sClass, 'Organization'))
{
$sAttCode = 'id';
}
elseif (is_callable($aCallSpec))
{
$sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
{
// Skip silently. The data model checker will tell you something about this...
$sAttCode = null;
}
}
elseif(MetaModel::IsValidAttCode($sClass, 'org_id'))
{
$sAttCode = 'org_id';
}

return $sAttCode;
return UserRights::GetOwnerOrganizationAttCode($sClass);
}

/**
Expand Down
27 changes: 3 additions & 24 deletions addons/userrights/userrightsprofile.db.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function GetSelectFilter($oUser, $sClass, $aSettings = array())

// Determine how to position the objects of this class
//
$sAttCode = self::GetOwnerOrganizationAttCode($sClass);
$sAttCode = UserRights::GetOwnerOrganizationAttCode($sClass);
if (is_null($sAttCode))
{
// No filtering for this object
Expand Down Expand Up @@ -909,7 +909,7 @@ public function IsActionAllowed($oUser, $sClass, $iActionCode, $oInstanceSet = n
// But currently we are checking wether the objects might be written...
// Let's exclude the objects based on the relevant criteria

$sOrgAttCode = self::GetOwnerOrganizationAttCode($sClass);
$sOrgAttCode = UserRights::GetOwnerOrganizationAttCode($sClass);
if (!is_null($sOrgAttCode))
{
$aUserOrgs = $this->GetUserOrgs($oUser, $sClass);
Expand Down Expand Up @@ -1015,28 +1015,7 @@ public function FlushPrivileges()
*/
public static function GetOwnerOrganizationAttCode($sClass)
{
$sAttCode = null;

$aCallSpec = array($sClass, 'MapContextParam');
if (($sClass == 'Organization') || is_subclass_of($sClass, 'Organization'))
{
$sAttCode = 'id';
}
elseif (is_callable($aCallSpec))
{
$sAttCode = call_user_func($aCallSpec, 'org_id'); // Returns null when there is no mapping for this parameter
if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
{
// Skip silently. The data model checker will tell you something about this...
$sAttCode = null;
}
}
elseif(MetaModel::IsValidAttCode($sClass, 'org_id'))
{
$sAttCode = 'org_id';
}

return $sAttCode;
return UserRights::GetOwnerOrganizationAttCode($sClass);;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions core/dbobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,7 @@ final public function DoComputeValues()
$oKPI = new ExecutionKPI();
$this->ComputeValues();
$oKPI->ComputeStatsForExtension($this, 'ComputeValues');

}

/**
Expand Down Expand Up @@ -3663,6 +3664,8 @@ public function DBUpdate()
return $this->m_iKey;
}

$this->UpdateOrganizationInInlineImages($aChanges);

[$bRes, $aIssues] = $this->CheckToWrite(false);
if (!$bRes) {
throw new CoreCannotSaveObjectException(['issues' => $aIssues, 'class' => $sClass, 'id' => $this->GetKey()]);
Expand Down Expand Up @@ -3850,6 +3853,22 @@ public function DBUpdate()
return $this->m_iKey;
}

public function UpdateOrganizationInInlineImages($aChanges){
$sClass = get_class($this);
$sOrgAttrCodeForObject = UserRights::GetOwnerOrganizationAttCode( $sClass );
if (is_null($sOrgAttrCodeForObject)) {
return;
}
if (in_array($sOrgAttrCodeForObject, array_keys($aChanges)) ) {
// Get all current inlineImages
$oSearch = DBObjectSearch::FromOQL("SELECT InlineImage WHERE item_class = :class AND item_id = :item_id");
$oSet = new DBObjectSet($oSearch, array(), array('class' => $sClass, 'item_id' => $this->GetKey()));
while ($oInlineImage = $oSet->Fetch()) {
$oInlineImage->SetItem($this, true /*updateonchange*/);
}
}
}

/**
* @param array $aChanges
*
Expand Down
85 changes: 31 additions & 54 deletions core/inlineimage.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,24 @@ public static function MapContextParam($sContextParam)
*/
public function SetItem(DBObject $oItem, $bUpdateOnChange = false)
{
$sClass = get_class($oItem);
$iItemId = $oItem->GetKey();
$sClass = get_class($oItem);
$iItemId = $oItem->GetKey();

$this->Set('item_class', $sClass);
$this->Set('item_id', $iItemId);
$this->Set('item_class', $sClass);
$this->Set('item_id', $iItemId);

$aCallSpec = array($sClass, '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($sClass, $sAttCode))
{
$iOrgId = $oItem->Get($sAttCode);
if ($iOrgId > 0)
{
if ($iOrgId != $this->Get('item_org_id'))
{
$this->Set('item_org_id', $iOrgId);
if ($bUpdateOnChange)
{
$this->DBUpdate();
}
}
}
}
}
$sAttCode = UserRights::GetOwnerOrganizationAttCode( $sClass);
if (is_null($sAttCode)) {
// No need for silos
return;
}
$iOrgId = $oItem->Get($sAttCode);
if ($iOrgId > 0 && $iOrgId != $this->Get('item_org_id')) {
$this->Set('item_org_id', $iOrgId);
if ($bUpdateOnChange) {
$this->DBUpdate();
}
}
}

/**
Expand All @@ -140,36 +132,21 @@ public function SetItem(DBObject $oItem, $bUpdateOnChange = false)
*/
public function SetDefaultOrgId()
{
// First check that the organization CAN be fetched from the target class
//
$sClass = $this->Get('item_class');
$aCallSpec = array($sClass, '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($sClass, $sAttCode))
{
// Second: check that the organization CAN be fetched from the current user
//
if (MetaModel::IsValidClass('Person'))
{
$aCallSpec = array($sClass, '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($sClass, $sAttCode))
{
// OK - try it
//
$oCurrentPerson = MetaModel::GetObject('Person', UserRights::GetContactId(), false);
if ($oCurrentPerson)
{
$this->Set('item_org_id', $oCurrentPerson->Get($sAttCode));
}
}
}
}
}
// If the item class has no organization attribute, then no need to set the organization id
if (is_null(UserRights::GetOwnerOrganizationAttCode( $this->Get('item_class')))) {
// No need for silos
return;
}
// get organization attribute code for the person class
$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));
}
}

Expand Down
28 changes: 27 additions & 1 deletion core/userrights.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public function MakeSelectFilter($sClass, $aAllowedOrgs, $aSettings = array(), $
$oSearchShares = new DBObjectSearch($sShareClass);
$oSearchShares->AllowAllData();

$sHierarchicalKeyCode = MetaModel::IsHierarchicalClass('Organization');
$oOrgField = new FieldExpression('org_id', $sShareClass);
$oSearchShares->AddConditionExpression(new BinaryExpression($oOrgField, 'IN', $oListExpr));
$aShared = array();
Expand Down Expand Up @@ -2028,6 +2027,33 @@ public static function GetLastLoginStatus()
{
return self::$m_sLastLoginStatus;
}


/**
* @param string $sClass
* @return string|null Find out which attribute is corresponding the dimension 'owner org'
* returns null if no such attribute has been found (no filtering should occur)
* @since 3.3.0
*/
public static function GetOwnerOrganizationAttCode($sClass)
{
$sAttCode = null;

$aCallSpec = array($sClass, '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($sClass, $sAttCode)) {
// Skip silently. The data model checker will tell you something about this...
$sAttCode = null;
}
}
elseif(MetaModel::IsValidAttCode($sClass, 'org_id')) {
$sAttCode = 'org_id';
}

return $sAttCode;
}

}

/**
Expand Down
59 changes: 21 additions & 38 deletions datamodels/2.x/itop-attachments/datamodel.itop-attachments.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,18 @@
$this->Set('item_class', $sClass);
$this->Set('item_id', $iItemId);

$aCallSpec = array($sClass, '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($sClass, $sAttCode))
{
$iOrgId = $oItem->Get($sAttCode);
if ($iOrgId > 0)
{
if ($iOrgId != $this->Get('item_org_id'))
{
$this->Set('item_org_id', $iOrgId);
if ($bUpdateOnChange)
{
$this->DBUpdate();
}
}
}
}
$sAttCode = UserRights::GetOwnerOrganizationAttCode( $sClass);
if (is_null($sAttCode)) {
// No need for silos
return;
}
$iOrgId = $oItem->Get($sAttCode);
if ($iOrgId > 0 && $iOrgId != $this->Get('item_org_id')) {
$this->Set('item_org_id', $iOrgId);
if ($bUpdateOnChange) {
$this->DBUpdate();
}
}
}]]></code>
</method>
<method id="SetDefaultOrgId">
Expand All @@ -192,25 +184,16 @@
<type>Overload-ExNihilo</type>
<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));
}
}
}
// Check that the organization CAN be fetched from the current user
$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
24 changes: 23 additions & 1 deletion datamodels/2.x/itop-structure/datamodel.itop-structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,29 @@
</definition>
</field>
</fields>
<methods/>
<methods>
<method id="MapContextParam">
<comment><![CDATA[/**
* Maps the given context parameter name to the appropriate filter/search code for this class
* @param string $sContextParam Name of the context parameter, e.g. 'org_id'
* @return string Filter code, e.g. 'id' for class Organization
*/]]></comment>
<static>true</static>
<access>public</access>
<type>Overload-ExNihilo</type>
<code><![CDATA[ public static function MapContextParam($sContextParam)
{
if ($sContextParam == 'org_id')
{
return 'id';
}
else
{
return null;
}
}]]></code>
</method>
</methods>
<presentation>
<details>
<items>
Expand Down
Loading