From 083c3d861363248ec8657d513e7662c1700d693a Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Tue, 21 Aug 2012 13:25:32 +0000 Subject: [PATCH] Profiles defined in XML: reviewed the internal extension capability (GetReadOnlyAttributes and GetPredefinedObjects) SVN:trunk[2152] --- .../userrightsprofile.class.inc.php | 4 +-- core/dbobject.class.php | 28 +++++++++++++++++-- core/userrights.class.inc.php | 3 +- setup/ajax.dataloader.php | 19 ++++++------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/addons/userrights/userrightsprofile.class.inc.php b/addons/userrights/userrightsprofile.class.inc.php index 6268fc516..4c5787dc0 100644 --- a/addons/userrights/userrightsprofile.class.inc.php +++ b/addons/userrights/userrightsprofile.class.inc.php @@ -187,14 +187,14 @@ class URP_Profiles extends UserRightsBaseClassGUI } } - public static function GetConstantColumns() + public static function GetReadOnlyAttributes() { return array('name', 'description'); } // returns an array of id => array of column => php value(so-called "real value") - public static function GetConstantValues() + public static function GetPredefinedObjects() { return ProfilesConfig::GetProfilesValues(); } diff --git a/core/dbobject.class.php b/core/dbobject.class.php index a6d62555f..8150e8ac9 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -778,6 +778,29 @@ abstract class DBObject return MetaModel::GetStateDescription(get_class($this), $sStateValue); } } + + /** + * Overridable - Define attributes read-only from the end-user perspective + * + * @return array List of attcodes + */ + public static function GetReadOnlyAttributes() + { + return null; + } + + + /** + * Overridable - Get predefined objects (could be hardcoded) + * The predefined objects will be synchronized with the DB at each install/upgrade + * As soon as a class has predefined objects, then nobody can create nor delete objects + * @return array An array of id => array of attcode => php value(so-called "real value": integer, string, ormDocument, DBObjectSet, etc.) + */ + public static function GetPredefinedObjects() + { + return null; + } + /** * Returns the set of flags (OPT_ATT_HIDDEN, OPT_ATT_READONLY, OPT_ATT_MANDATORY...) * for the given attribute in the current state of the object @@ -790,9 +813,10 @@ abstract class DBObject { $iFlags = 0; // By default (if no life cycle) no flag at all - if (method_exists(get_class($this), 'GetConstantColumns')) + $aReadOnlyAtts = $this->GetReadOnlyAttributes(); + if ($aReadOnlyAtts != null) { - if (in_array($sAttCode, $this->GetConstantColumns())) + if (in_array($sAttCode, $aReadOnlyAtts)) { return OPT_ATT_READONLY; } diff --git a/core/userrights.class.inc.php b/core/userrights.class.inc.php index 8ef04d29b..6bead0ec7 100644 --- a/core/userrights.class.inc.php +++ b/core/userrights.class.inc.php @@ -680,7 +680,8 @@ class UserRights if ($iActionCode == UR_ACTION_BULK_DELETE) return false; } - if (method_exists($sClass, 'GetConstantColumns')) + $aPredefinedObjects = call_user_func(array($sClass, 'GetPredefinedObjects')); + if ($aPredefinedObjects != null) { // As opposed to the read-only DB, modifying an object is allowed // (the constant columns will be marked as read-only) diff --git a/setup/ajax.dataloader.php b/setup/ajax.dataloader.php index e2260ddbe..225081300 100644 --- a/setup/ajax.dataloader.php +++ b/setup/ajax.dataloader.php @@ -284,7 +284,8 @@ try // foreach (MetaModel::GetClasses() as $sClass) { - if (method_exists($sClass, 'GetConstantColumns')) + $aPredefinedObjects = call_user_func(array($sClass, 'GetPredefinedObjects')); + if ($aPredefinedObjects != null) { // Temporary... until this get really encapsulated as the default and transparent behavior $oMyChange = MetaModel::NewObject("CMDBChange"); @@ -296,18 +297,16 @@ try // Create/Delete/Update objects of this class, // according to the given constant values // - $aAttList = call_user_func(array($sClass, 'GetConstantColumns')); - $aRefValues = call_user_func(array($sClass, 'GetConstantValues')); $aDBIds = array(); $oAll = new DBObjectSet(new DBObjectSearch($sClass)); while ($oObj = $oAll->Fetch()) { - if (array_key_exists($oObj->GetKey(), $aRefValues)) + if (array_key_exists($oObj->GetKey(), $aPredefinedObjects)) { - $aObjValues = $aRefValues[$oObj->GetKey()]; - foreach ($aAttList as $sAttCode) + $aObjValues = $aPredefinedObjects[$oObj->GetKey()]; + foreach ($aObjValues as $sAttCode => $value) { - $oObj->Set($sAttCode, $aObjValues[$sAttCode]); + $oObj->Set($sAttCode, $value); } $oObj->DBUpdateTracked($oMyChange); $aDBIds[$oObj->GetKey()] = true; @@ -317,15 +316,15 @@ try $oObj->DBDeleteTracked($oMyChange); } } - foreach ($aRefValues as $iRefId => $aObjValues) + foreach ($aPredefinedObjects as $iRefId => $aObjValues) { if (!array_key_exists($iRefId, $aDBIds)) { $oNewObj = MetaModel::NewObject($sClass); $oNewObj->SetKey($iRefId); - foreach ($aAttList as $sAttCode) + foreach ($aObjValues as $sAttCode => $value) { - $oNewObj->Set($sAttCode, $aObjValues[$sAttCode]); + $oNewObj->Set($sAttCode, $value); } $oNewObj->DBInsertTracked($oMyChange); }