From c2fcadd54d3c32b297ec412d6c993975ea76fc70 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Fri, 28 Aug 2020 19:46:40 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B02847=20-=20Add=20optional=20user=5Fid=20?= =?UTF-8?q?to=20CMDBChange?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Important: This can make the setup / toolkit to take a very long time on large databases as the "priv_change" table is one of the largest. SQL queries to run and/or a migration tool will be provided when 2.8.0 will be released. --- core/cmdbchange.class.inc.php | 14 ++++++++++ core/cmdbchangeop.class.inc.php | 1 + core/cmdbobject.class.inc.php | 48 +++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/core/cmdbchange.class.inc.php b/core/cmdbchange.class.inc.php index e560789e7..aea63d58b 100644 --- a/core/cmdbchange.class.inc.php +++ b/core/cmdbchange.class.inc.php @@ -52,6 +52,7 @@ class CMDBChange extends DBObject //MetaModel::Init_InheritAttributes(); MetaModel::Init_AddAttribute(new AttributeDateTime("date", array("allowed_values"=>null, "sql"=>"date", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeString("userinfo", array("allowed_values"=>null, "sql"=>"userinfo", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array()))); + MetaModel::Init_AddAttribute(new AttributeInteger("user_id", array("allowed_values" => null, "sql" => "user_id", "default_value" => null, "is_null_allowed" => true, "depends_on" => array(),))); MetaModel::Init_AddAttribute(new AttributeEnum("origin", array("allowed_values"=>new ValueSetEnum('interactive,csv-interactive,csv-import.php,webservice-soap,webservice-rest,synchro-data-source,email-processing,custom-extension'), "sql"=>"origin", "default_value"=>"interactive", "is_null_allowed"=>true, "depends_on"=>array()))); } @@ -75,6 +76,19 @@ class CMDBChange extends DBObject return $sUserString; } + /** + * Return the current user + * + * @return string|null + * @throws \OQLException + * @since 2.8.0 + */ + public static function GetCurrentUserId() + { + // Note: We might have use only UserRights::GetRealUserId() as it would have done the same thing in the end + return UserRights::IsImpersonated() ? UserRights::GetRealUserId() : UserRights::GetUserId(); + } + public function GetUserName() { if (preg_match('/^(.*)\\(CSV\\)$/i', $this->Get('userinfo'), $aMatches)) diff --git a/core/cmdbchangeop.class.inc.php b/core/cmdbchangeop.class.inc.php index c87d6c739..aee542a7f 100644 --- a/core/cmdbchangeop.class.inc.php +++ b/core/cmdbchangeop.class.inc.php @@ -69,6 +69,7 @@ class CMDBChangeOp extends DBObject implements iCMDBChangeOp MetaModel::Init_AddAttribute(new AttributeExternalKey("change", array("allowed_values"=>null, "sql"=>"changeid", "targetclass"=>"CMDBChange", "is_null_allowed"=>false, "on_target_delete"=>DEL_MANUAL, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeExternalField("date", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"date"))); MetaModel::Init_AddAttribute(new AttributeExternalField("userinfo", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"userinfo"))); + MetaModel::Init_AddAttribute(new AttributeExternalField("user_id", array("allowed_values"=>null, "extkey_attcode"=>"change", "target_attcode"=>"user_id"))); MetaModel::Init_AddAttribute(new AttributeString("objclass", array("allowed_values"=>null, "sql"=>"objclass", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array()))); MetaModel::Init_AddAttribute(new AttributeObjectKey("objkey", array("allowed_values"=>null, "class_attcode"=>"objclass", "sql"=>"objkey", "is_null_allowed"=>false, "depends_on"=>array()))); diff --git a/core/cmdbobject.class.inc.php b/core/cmdbobject.class.inc.php index bee7d7a41..48919429e 100644 --- a/core/cmdbobject.class.inc.php +++ b/core/cmdbobject.class.inc.php @@ -94,6 +94,7 @@ abstract class CMDBObject extends DBObject // Note: this value is static, but that could be changed because it is sometimes a real issue (see update of interfaces / connected_to protected static $m_oCurrChange = null; protected static $m_sInfo = null; // null => the information is built in a standard way + protected static $m_sUserId = null; // null => the user doing the change is unknown protected static $m_sOrigin = null; // null => the origin is 'interactive' /** @@ -146,6 +147,21 @@ abstract class CMDBObject extends DBObject self::$m_sInfo = $sInfo; } + /** + * Provide information about the user doing the change + * + * @see static::SetTrackInfo + * @see static::SetCurrentChange + * + * @param string $sId ID of the user doing the change, null if not done by a user (eg. background task) + * + * @since 2.8.0 + */ + public static function SetTrackUserId($sId) + { + self::$m_sUserId = $sId; + } + /** * Provides information about the origin of the change * @@ -174,6 +190,25 @@ abstract class CMDBObject extends DBObject return self::$m_sInfo; } } + + /** + * Get the ID of the user doing the change (defaulting to null) + * + * @return string|null + * @throws \OQLException + * @since 2.8.0 + */ + protected static function GetTrackUserId() + { + if (is_null(self::$m_sUserId)) + { + return CMDBChange::GetCurrentUserId(); + } + else + { + return self::$m_sUserId; + } + } /** * Get the 'origin' information (defaulting to 'interactive') @@ -189,15 +224,24 @@ abstract class CMDBObject extends DBObject return self::$m_sOrigin; } } - + /** * Create a standard change record (done here 99% of the time, and nearly once per page) - */ + * + * @throws \ArchivedObjectException + * @throws \CoreCannotSaveObjectException + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \CoreWarning + * @throws \MySQLException + * @throws \OQLException + */ protected static function CreateChange() { self::$m_oCurrChange = MetaModel::NewObject("CMDBChange"); self::$m_oCurrChange->Set("date", time()); self::$m_oCurrChange->Set("userinfo", self::GetTrackInfo()); + self::$m_oCurrChange->Set("user_id", self::GetTrackUserId()); self::$m_oCurrChange->Set("origin", self::GetTrackOrigin()); self::$m_oCurrChange->DBInsert(); }