try { $oDefinitionFilter = DBObjectSearch::FromOQL($oAuditCategory->Get('definition_set')); FilterByContext($oDefinitionFilter, $oAppContext); $aObjectsWithErrors = array(); if (!empty($currentOrganization)) { if (MetaModel::IsValidFilterCode($oDefinitionFilter->GetClass(), 'org_id')) { $oDefinitionFilter->AddCondition('org_id', $currentOrganization, '='); } } $aResults = array(); $oDefinitionSet = new CMDBObjectSet($oDefinitionFilter); $iCount = $oDefinitionSet->Count(); $oRulesFilter = new DBObjectSearch('AuditRule'); $oRulesFilter->AddCondition('category_id', $oAuditCategory->GetKey(), '='); $oRulesSet = new DBObjectSet($oRulesFilter); while ($oAuditRule = $oRulesSet->fetch()) { $aRow = array(); $aRow['description'] = $oAuditRule->GetName(); if ($iCount == 0) { // nothing to check, really ! $aRow['nb_errors'] = "<a href=\"audit.php?operation=errors&category=" . $oAuditCategory->GetKey() . "&rule=" . $oAuditRule->GetKey() . "\">0</a>"; $aRow['percent_ok'] = '100.00'; $aRow['class'] = GetReportColor($iCount, 0); } else { try { $oFilter = GetRuleResultFilter($oAuditRule->GetKey(), $oDefinitionFilter, $oAppContext); $aErrors = $oFilter->ToDataArray(array('id')); $iErrorsCount = count($aErrors); foreach ($aErrors as $aErrorRow) { $aObjectsWithErrors[$aErrorRow['id']] = true; }
public static function GetObjectByColumn($sClass, $sAttCode, $value, $bMustBeFoundUnique = true) { if (!isset(self::$m_aCacheObjectByColumn[$sClass][$sAttCode][$value])) { self::_check_subclass($sClass); $oObjSearch = new DBObjectSearch($sClass); $oObjSearch->AddCondition($sAttCode, $value, '='); $oSet = new DBObjectSet($oObjSearch); if ($oSet->Count() == 1) { self::$m_aCacheObjectByColumn[$sClass][$sAttCode][$value] = $oSet->fetch(); } else { if ($bMustBeFoundUnique) { throw new CoreException('Failed to get an object by column', array('class' => $sClass, 'attcode' => $sAttCode, 'value' => $value, 'matches' => $oSet->Count())); } self::$m_aCacheObjectByColumn[$sClass][$sAttCode][$value] = null; } } return self::$m_aCacheObjectByColumn[$sClass][$sAttCode][$value]; }
public static function GetProperty($sName, $default = null) { try { $oSearch = DBObjectSearch::FromOQL('SELECT DBProperty WHERE name = :name'); $oSet = new DBObjectSet($oSearch, array(), array('name' => $sName)); $iCount = $oSet->Count(); if ($iCount == 0) { //throw new CoreException('unknown db property', array('name' => $sName)); $sValue = $default; } elseif ($iCount == 1) { $oProp = $oSet->fetch(); $sValue = $oProp->Get('value'); } else { // $iCount > 1 // Houston... throw new CoreException('duplicate db property', array('name' => $sName, 'count' => $iCount)); } } catch (MySQLException $e) { // This might be because the table could not be found, // let's check it and discard silently if this is really the case if (self::IsInstalled()) { throw $e; } $sValue = $default; } return $sValue; }
/** * Find a user based on its login and its type of authentication * @param string $sLogin Login/identifier of the user * @param string $sAuthentication Type of authentication used: internal|external|any * @return User The found user or null */ protected static function FindUser($sLogin, $sAuthentication = 'any') { if ($sAuthentication == 'any') { $oUser = self::FindUser($sLogin, 'internal'); if ($oUser == null) { $oUser = self::FindUser($sLogin, 'external'); } } else { if (!isset(self::$m_aCacheUsers)) { self::$m_aCacheUsers = array('internal' => array(), 'external' => array()); } if (!isset(self::$m_aCacheUsers[$sAuthentication][$sLogin])) { switch ($sAuthentication) { case 'external': $sBaseClass = 'UserExternal'; break; case 'internal': $sBaseClass = 'UserInternal'; break; default: echo "<p>sAuthentication = {$sAuthentication}</p>\n"; assert(false); // should never happen } $oSearch = DBObjectSearch::FromOQL("SELECT {$sBaseClass} WHERE login = :login"); $oSet = new DBObjectSet($oSearch, array(), array('login' => $sLogin)); $oUser = $oSet->fetch(); self::$m_aCacheUsers[$sAuthentication][$sLogin] = $oUser; } $oUser = self::$m_aCacheUsers[$sAuthentication][$sLogin]; } return $oUser; }
protected function DoExecute() { CMDBSource::Query('START TRANSACTION'); //CMDBSource::Query('ROLLBACK'); automatique ! //////////////////////////////////////////////////////////////////////////////// // Set the stage // $oServer = MetaModel::NewObject('Server'); $oServer->Set('name', 'unit test linkset'); $oServer->Set('org_id', 3); $oServer->DBInsert(); $iServer = $oServer->GetKey(); $oTypes = new DBObjectSet(DBObjectSearch::FromOQL('SELECT NetworkDeviceType WHERE name = "Router"')); $oType = $oTypes->fetch(); $oDevice = MetaModel::NewObject('NetworkDevice'); $oDevice->Set('name', 'test device A'); $oDevice->Set('org_id', 3); $oDevice->Set('networkdevicetype_id', $oType->GetKey()); $oDevice->DBInsert(); $iDev1 = $oDevice->GetKey(); $oDevice = MetaModel::NewObject('NetworkDevice'); $oDevice->Set('name', 'test device B'); $oDevice->Set('org_id', 3); $oDevice->Set('networkdevicetype_id', $oType->GetKey()); $oDevice->DBInsert(); $iDev2 = $oDevice->GetKey(); //////////////////////////////////////////////////////////////////////////////// // Scenarii // $aScenarii = array(array('description' => 'Add the first item', 'links' => array(array('networkdevice_id' => $iDev1, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => '')), 'expected-res' => array("{$iDev1}, test device A, unit test linkset, , , downlink, test device A"), 'history_added' => 1, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Modify the unique item', 'links' => array(array('networkdevice_id' => $iDev1, 'connectableci_id' => $iServer, 'network_port' => 'devTagada', 'device_port' => '')), 'expected-res' => array("{$iDev1}, test device A, unit test linkset, devTagada, , downlink, test device A"), 'history_added' => 1, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'Modify again the original item and add a second item', 'links' => array(array('networkdevice_id' => $iDev1, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => ''), array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => '')), 'expected-res' => array("{$iDev1}, test device A, unit test linkset, , , downlink, test device A", "{$iDev2}, test device B, unit test linkset, , , downlink, test device B"), 'history_added' => 2, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'No change, the links are added in the reverse order', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => ''), array('networkdevice_id' => $iDev1, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => '')), 'expected-res' => array("{$iDev1}, test device A, unit test linkset, , , downlink, test device A", "{$iDev2}, test device B, unit test linkset, , , downlink, test device B"), 'history_added' => 0, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Change on attribute on both links at the same time', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'PortDev B', 'device_port' => ''), array('networkdevice_id' => $iDev1, 'connectableci_id' => $iServer, 'network_port' => 'PortDev A', 'device_port' => '')), 'expected-res' => array("{$iDev1}, test device A, unit test linkset, PortDev A, , downlink, test device A", "{$iDev2}, test device B, unit test linkset, PortDev B, , downlink, test device B"), 'history_added' => 2, 'history_removed' => 2, 'history_modified' => 0), array('description' => 'Removing A', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'PortDev B', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, PortDev B, , downlink, test device B"), 'history_added' => 0, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'Adding B again - with a different port (duplicate!)', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_123', 'device_port' => ''), array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_456', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, port_123, , downlink, test device B", "{$iDev2}, test device B, unit test linkset, port_456, , downlink, test device B"), 'history_added' => 2, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'No change (creating a set with the reloaded links, like in the UI)', 'links' => array(array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer} AND network_port = 'port_123'", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_123', 'device_port' => ''), array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer} AND network_port = 'port_456'", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_456', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, port_123, , downlink, test device B", "{$iDev2}, test device B, unit test linkset, port_456, , downlink, test device B"), 'history_added' => 0, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Change an attribute on one link (based on reloaded links, like in the UI)', 'links' => array(array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer} AND network_port = 'port_123'", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_123_modified', 'device_port' => ''), array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer} AND network_port = 'port_456'", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_456', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, port_123_modified, , downlink, test device B", "{$iDev2}, test device B, unit test linkset, port_456, , downlink, test device B"), 'history_added' => 0, 'history_removed' => 0, 'history_modified' => 1), array('description' => 'Remove the second link (set based on reloaded links, like in the UI)', 'links' => array(array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer} AND network_port = 'port_123_modified'", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'port_123_modified', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, port_123_modified, , downlink, test device B"), 'history_added' => 0, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'Remove all', 'links' => array(), 'expected-res' => array(), 'history_added' => 0, 'history_removed' => 1, 'history_modified' => 0), array('description' => 'Create one link from scratch, no port, to prepare for the next test case', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, , , downlink, test device B"), 'history_added' => 1, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Create a second link from scratch, pointing to the same server and no port, to prepare for the next test case', 'links' => array(array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer}", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => ''), array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'X', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, , , downlink, test device B", "{$iDev2}, test device B, unit test linkset, X, , downlink, test device B"), 'history_added' => 1, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Create a second link from scratch, pointing to the same server and no port, to prepare for the next test case', 'links' => array(array('id' => "SELECT lnkConnectableCIToNetworkDevice WHERE networkdevice_id = {$iDev2} AND connectableci_id = {$iServer}", 'networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => '', 'device_port' => ''), array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'X', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, , , downlink, test device B", "{$iDev2}, test device B, unit test linkset, X, , downlink, test device B"), 'history_added' => 1, 'history_removed' => 0, 'history_modified' => 0), array('description' => 'Device B twice (same characteristics) - known issue #1145', 'links' => array(array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'portX', 'device_port' => ''), array('networkdevice_id' => $iDev2, 'connectableci_id' => $iServer, 'network_port' => 'portX', 'device_port' => '')), 'expected-res' => array("{$iDev2}, test device B, unit test linkset, portX, , downlink, test device B", "{$iDev2}, test device B, unit test linkset, portX, , downlink, test device B"), 'history_added' => 1, 'history_removed' => 0, 'history_modified' => 0)); foreach ($aScenarii as $aScenario) { echo "<h4>" . $aScenario['description'] . "</h4>\n"; $oChange = MetaModel::NewObject("CMDBChange"); $oChange->Set("date", time()); $oChange->Set("userinfo", CMDBChange::GetCurrentUserName()); $oChange->Set("origin", 'custom-extension'); $oChange->DBInsert(); CMDBObject::SetCurrentChange($oChange); $iChange = $oChange->GetKey(); // Prepare set $oLinkset = DBObjectSet::FromScratch('lnkConnectableCIToNetworkDevice'); foreach ($aScenario['links'] as $aLinkData) { if (array_key_exists('id', $aLinkData)) { $sOQL = $aLinkData['id']; $oSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL)); $oLink1 = $oSet->Fetch(); if (!is_object($oLink1)) { throw new Exception('Failed to find the lnkConnectableCIToNetworkDevice: ' . $sOQL); } } else { $oLink1 = MetaModel::NewObject('lnkConnectableCIToNetworkDevice'); } foreach ($aLinkData as $sAttCode => $value) { if ($sAttCode == 'id') { continue; } $oLink1->Set($sAttCode, $value); } $oLinkset->AddObject($oLink1); } // Write $oServer = MetaModel::GetObject('Server', $iServer); $oServer->Set('networkdevice_list', $oLinkset); $oServer->DBWrite(); // Check Results $bFoundIssue = false; $oServer = MetaModel::GetObject('Server', $iServer); $oLinkset = $oServer->Get('networkdevice_list'); $aRes = $this->StandardizedDump($oLinkset, 'connectableci_id'); $sRes = var_export($aRes, true); echo "Found: <pre>" . $sRes . "</pre>\n"; $sExpectedRes = var_export($aScenario['expected-res'], true); if ($sRes != $sExpectedRes) { $bFoundIssue = true; echo "NOT COMPLIANT!!! Expecting: <pre>" . $sExpectedRes . "</pre>\n"; } // Check History $aQueryParams = array('change' => $iChange, 'objclass' => get_class($oServer), 'objkey' => $oServer->GetKey()); $oAdded = new DBObjectSet(DBSearch::FromOQL("SELECT CMDBChangeOpSetAttributeLinksAddRemove WHERE objclass = :objclass AND objkey = :objkey AND change = :change AND type = 'added'"), array(), $aQueryParams); echo "added: " . $oAdded->Count() . "<br/>\n"; if ($aScenario['history_added'] != $oAdded->Count()) { $bFoundIssue = true; echo "NOT COMPLIANT!!! Expecting: " . $aScenario['history_added'] . "<br/>\n"; } $oRemoved = new DBObjectSet(DBSearch::FromOQL("SELECT CMDBChangeOpSetAttributeLinksAddRemove WHERE objclass = :objclass AND objkey = :objkey AND change = :change AND type = 'removed'"), array(), $aQueryParams); echo "removed: " . $oRemoved->Count() . "<br/>\n"; if ($aScenario['history_removed'] != $oRemoved->Count()) { $bFoundIssue = true; echo "NOT COMPLIANT!!! Expecting: " . $aScenario['history_removed'] . "<br/>\n"; } $oModified = new DBObjectSet(DBSearch::FromOQL("SELECT CMDBChangeOpSetAttributeLinksTune WHERE objclass = :objclass AND objkey = :objkey AND change = :change"), array(), $aQueryParams); echo "modified: " . $oModified->Count() . "<br/>\n"; if ($aScenario['history_modified'] != $oModified->Count()) { $bFoundIssue = true; echo "NOT COMPLIANT!!! Expecting: " . $aScenario['history_modified'] . "<br/>\n"; } if ($bFoundIssue) { throw new Exception('Stopping on failed scenario'); } } }