Skip to content

Commit

Permalink
fix(Rule): do not import as refused equipement if rule denied with no…
Browse files Browse the repository at this point in the history
… log
  • Loading branch information
stonebuzz authored Oct 20, 2023
1 parent e8aa95e commit 1bffc15
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/Inventory/Asset/MainAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use NetworkEquipment;
use Printer;
use RefusedEquipment;
use RuleImportAsset;
use RuleImportAssetCollection;
use RuleImportEntityCollection;
use RuleLocationCollection;
Expand Down Expand Up @@ -584,8 +585,10 @@ public function handle()
//Only main item is stored as refused, not all APs
unset($this->data[$key]);
} else {
$input['rules_id'] = $datarules['rules_id'];
$this->addRefused($input);
if ($datarules['action'] != RuleImportAsset::LINK_RESULT_DENIED) {
$input['rules_id'] = $datarules['rules_id'];
$this->addRefused($input);
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/RuleImportAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -927,11 +927,16 @@ public function executeActions($output, $params, array $input = [])

if (count($this->actions)) {
foreach ($this->actions as $action) {
if ($action->fields['field'] == '_ignore_import' || $action->fields["value"] == self::RULE_ACTION_DENIED) {
if ($action->fields["value"] == self::RULE_ACTION_DENIED) {
$output['action'] = self::LINK_RESULT_DENIED;
return $output;
}

if ($action->fields['field'] == '_ignore_import') {
$output['action'] = self::LINK_RESULT_CREATE;
return $output;
}

if ($action->fields['field'] != '_inventory' && $action->fields['field'] != '_fusion') {
if (count($this->criterias)) {
foreach ($this->criterias as $criterion) {
Expand Down
21 changes: 13 additions & 8 deletions templates/pages/admin/inventory/upload_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@
</span>
</td>
<td>
{% for item in result['items'] %}
<div>
<span>
<i class="{{ item.getIcon() }}"></i>
{{ get_item_link(item) }}
</span>
</div>
{% endfor %}
{% if result['items'] is empty %}
{{ __('Import denied (no log)') }}
{% else %}
{% for item in result['items'] %}
<div>
<span>
<i class="{{ item.getIcon() }}"></i>
{{ get_item_link(item) }}
</span>
</div>
{% endfor %}
{% endif %}

</td>
</tr>
{% endfor %}
Expand Down
94 changes: 92 additions & 2 deletions tests/functional/Glpi/Inventory/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4036,7 +4036,7 @@ public function testImportNetworkEquipmentWAggregatedPorts()
$this->integer(count($unmanageds))->isIdenticalTo(36);
}

public function testImportRefusedFromAssetRules()
public function testImportRefusedFromAssetRulesWithNoLog()
{
$rule = new \Rule();

Expand Down Expand Up @@ -4107,6 +4107,96 @@ public function testImportRefusedFromAssetRules()
$computers_id = $agent['items_id'];
$this->integer($computers_id)->isIdenticalTo(0);

$iterator = $DB->request([
'FROM' => \RefusedEquipment::getTable(),
]);
$this->integer(count($iterator))->isIdenticalTo(0);
}

public function testImportRefusedFromAssetRulesWithLog()
{
$rule = new \Rule();

//prepares needed rules id
$this->boolean(
$rule->getFromDBByCrit(['name' => 'Computer constraint (name)'])
)->isTrue();
$rules_id_torefuse = $rule->fields['id'];


$this->boolean(
$rule->getFromDBByCrit(['name' => 'Computer import denied'])
)->isTrue();
$rules_id_refuse = $rule->fields['id'];

//update ruleAction to refused import with log
$ruleaction = new \RuleAction();
$this->boolean($ruleaction->getFromDBByCrit(['rules_id' => $rules_id_refuse]))->isTrue();
$this->boolean(
$ruleaction->update([
'id' => $ruleaction->fields['id'],
'field' => '_ignore_import',
'action_type' => 'assign',
'value' => 1
])
)->isTrue();

$this->boolean(
$rule->getFromDBByCrit(['name' => 'Computer import (by name)'])
)->isTrue();
$rules_id_toaccept = $rule->fields['id'];

//move rule to refuse computer inventory
$rulecollection = new \RuleImportAssetCollection();
$this->boolean(
$rulecollection->moveRule(
$rules_id_refuse,
$rules_id_torefuse,
\RuleCollection::MOVE_BEFORE
)
)->isTrue();

//do inventory
$json = json_decode(file_get_contents(self::INV_FIXTURES . 'computer_1.json'));
$inventory = $this->doInventory($json);

//move rule back to accept computer inventory
$this->boolean(
$rulecollection->moveRule(
$rules_id_refuse,
$rules_id_toaccept,
\RuleCollection::MOVE_AFTER
)
)->isTrue();

//check inventory metadata
$metadata = $inventory->getMetadata();
$this->array($metadata)->hasSize(7)
->string['deviceid']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['version']->isIdenticalTo('FusionInventory-Agent_v2.5.2-1.fc31')
->string['itemtype']->isIdenticalTo('Computer')
->string['tag']->isIdenticalTo('000005')
->variable['port']->isIdenticalTo(null)
->string['action']->isIdenticalTo('inventory');
$this->array($metadata['provider'])->hasSize(10);

global $DB;
//check created agent
$agenttype = $DB->request(['FROM' => \AgentType::getTable(), 'WHERE' => ['name' => 'Core']])->current();
$agents = $DB->request(['FROM' => \Agent::getTable()]);
$this->integer(count($agents))->isIdenticalTo(1);
$agent = $agents->current();
$this->array($agent)
->string['deviceid']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['name']->isIdenticalTo('glpixps-2018-07-09-09-07-13')
->string['version']->isIdenticalTo('2.5.2-1.fc31')
->string['itemtype']->isIdenticalTo('Computer')
->string['tag']->isIdenticalTo('000005')
->integer['agenttypes_id']->isIdenticalTo($agenttype['id']);

$computers_id = $agent['items_id'];
$this->integer($computers_id)->isIdenticalTo(0);

$iterator = $DB->request([
'FROM' => \RefusedEquipment::getTable(),
]);
Expand Down Expand Up @@ -4229,7 +4319,7 @@ public function testImportRefusedFromEntitiesRules()
$this->integer(
$rulecriteria->add([
'rules_id' => $rules_id,
'criteria' => "name",
'criteria' => "deviceid",
'pattern' => "/^glpixps.*/",
'condition' => \RuleImportEntity::REGEX_MATCH
])
Expand Down

0 comments on commit 1bffc15

Please sign in to comment.