Skip to content

Commit

Permalink
Adding new user by field rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Lainow committed Apr 23, 2024
1 parent 5aa4cb1 commit 804a8e0
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Inventory/Asset/MainAsset.php
Expand Up @@ -351,7 +351,9 @@ protected function prepareForUsers($val)
}
}

$val->_user_inventory = $_user_inventory;
if (!empty($_user_inventory)) {
$val->_user_inventory = $_user_inventory;
}
}

protected function prepareForBios($val)
Expand Down
35 changes: 29 additions & 6 deletions src/RuleAsset.php
Expand Up @@ -234,9 +234,13 @@ public function getActions()
$actions['_affect_user_by_email_and_regex']['type'] = 'text';
$actions['_affect_user_by_email_and_regex']['force_actions'] = ['regex_result'];

$actions['_affect_user_by_administrativenumber_and_regex']['name'] = __('Assign user from phone');
$actions['_affect_user_by_administrativenumber_and_regex']['type'] = 'text';
$actions['_affect_user_by_administrativenumber_and_regex']['force_actions'] = ['regex_result'];
$actions['_affect_user_by_registration_number_and_regex']['name'] = __('Assign user from registration number');
$actions['_affect_user_by_registration_number_and_regex']['type'] = 'text';
$actions['_affect_user_by_registration_number_and_regex']['force_actions'] = ['regex_result'];

$actions['_affect_user_by_sync_field_and_regex']['name'] = __('Assign user from sync field');
$actions['_affect_user_by_sync_field_and_regex']['type'] = 'text';
$actions['_affect_user_by_sync_field_and_regex']['force_actions'] = ['regex_result'];

return $actions;
}
Expand All @@ -256,7 +260,6 @@ public function getRights($interface = 'central')

public function executeActions($output, $params, array $input = [])
{

if (count($this->actions)) {
foreach ($this->actions as $action) {
switch ($action->fields["action_type"]) {
Expand Down Expand Up @@ -304,12 +307,14 @@ public function executeActions($output, $params, array $input = [])
if ($res != null) {
$user = User::getUsersIdByEmails($res);
if ($user) {
$output['users_id'] = $user;
$output['users_id'] = $user[0];
} else {
$output['users_id'] = 0;
}
}
}
break;
case "_affect_user_by_administrativenumber_and_regex":
case "_affect_user_by_registration_number_and_regex":
foreach ($this->regex_results as $regex_result) {
$res = RuleAction::getRegexResultById(
$action->fields["value"],
Expand All @@ -319,6 +324,24 @@ public function executeActions($output, $params, array $input = [])
$user = User::getIdByField('registration_number', $res);
if ($user) {
$output['users_id'] = $user;
} else {
$output['users_id'] = 0;
}
}
}
break;
case "_affect_user_by_sync_field_and_regex":
foreach ($this->regex_results as $regex_result) {
$res = RuleAction::getRegexResultById(
$action->fields["value"],
$regex_result
);
if ($res != null) {
$user = User::getIdByField('sync_field', $res);
if ($user) {
$output['users_id'] = $user;
} else {
$output['users_id'] = 0;
}
}
}
Expand Down
134 changes: 134 additions & 0 deletions tests/functional/Glpi/Inventory/Inventory.php
Expand Up @@ -44,6 +44,7 @@
use OperatingSystemServicePack;
use OperatingSystemVersion;
use RuleCriteria;
use UserEmail;
use wapmorgan\UnifiedArchive\UnifiedArchive;

class Inventory extends InventoryTestCase
Expand Down Expand Up @@ -7851,4 +7852,137 @@ public function testStatusIfInventoryOnAddUpdate()
$this->boolean($computer->getFromDB($agent['items_id']))->isTrue();
$this->integer($computer->fields['states_id'])->isIdenticalTo($states_id);
}

protected function getAssignUserByFieldAndRegexRules()
{
$user_id = \User::getIDByName('glpi');
$this->createItem(UserEmail::class, [
'users_id' => $user_id,
'is_default' => 1,
'email' => 'glpi@teclib.com'
]);
$user = $this->updateItem(\User::class, $user_id, [
'registration_number' => 'glpi1234567890',
'realname' => 'glpi',
'authtype' => \Auth::EXTERNAL,
'sync_field' => 'glpi@toto'
]);
yield [
'rules_fields' => [
'name' => 'Assign user by name',
'action' => '_affect_user_by_name_and_regex',
'value' => '#0i',
],
'result' => [
'users_id' => $user->fields['id'],
]
];
yield [
'rules_fields' => [
'name' => 'Assign user by email',
'action' => '_affect_user_by_email_and_regex',
'value' => '#0i@teclib.com',
],
'result' => [
'users_id' => $user->fields['id'],
]
];
yield [
'rules_fields' => [
'name' => 'Assign user by registration number',
'action' => '_affect_user_by_registration_number_and_regex',
'value' => '#0i1234567890',
],
'result' => [
'users_id' => $user->fields['id'],
]
];
yield [
'rules_fields' => [
'name' => 'Assign user by sync field',
'action' => '_affect_user_by_sync_field_and_regex',
'value' => '#0i@toto',
],
'result' => [
'users_id' => $user->fields['id'],
]
];
}

/**
* @dataProvider getAssignUserByFieldAndRegexRules
*/
public function testAssignUserByFieldAndRegex($rules_fields, $result)
{
global $DB;

//create rule
$input_rule = [
'is_active' => 1,
'name' => $rules_fields['name'],
'match' => 'AND',
'sub_type' => 'RuleAsset',
'condition' => \RuleAsset::ONADD + \RuleAsset::ONUPDATE
];

$rule = new \Rule();
$rules_id = $rule->add($input_rule);
$this->integer($rules_id)->isGreaterThan(0);

//create criteria
$input_criteria = [
'rules_id' => $rules_id,
'criteria' => 'contact',
'condition' => \Rule::REGEX_MATCH,
'pattern' => '/^log([^@]+)/'
];
$rule_criteria = new \RuleCriteria();
$rule_criteria_id = $rule_criteria->add($input_criteria);
$this->integer($rule_criteria_id)->isGreaterThan(0);

//create action
$input_action = [
'rules_id' => $rules_id,
'action_type' => 'regex_result',
'field' => $rules_fields['action'],
'value' => $rules_fields['value']
];
$rule_action = new \RuleAction();
$rule_action_id = $rule_action->add($input_action);
$this->integer($rule_action_id)->isGreaterThan(0);

$xml_source = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
<HARDWARE>
<NAME>glpixps</NAME>
<UUID>25C1BB60-5BCB-11D9-B18F-5404A6A534C4</UUID>
</HARDWARE>
<BIOS>
<MSN>640HP72</MSN>
</BIOS>
<USERS>
<DOMAIN>TECLIB</DOMAIN>
<LOGIN>logglp</LOGIN>
</USERS>
<VERSIONCLIENT>GLPI-Agent_v1.6.18</VERSIONCLIENT>
</CONTENT>
<DEVICEID>test_assign_user_by_field_and_regex</DEVICEID>
<QUERY>INVENTORY</QUERY>
</REQUEST>";

\SingletonRuleList::getInstance("RuleAsset", 0)->load = 0;
\SingletonRuleList::getInstance("RuleAsset", 0)->list = [];
$this->doInventory($xml_source, true);

//check created agent
$agents = $DB->request(['FROM' => \Agent::getTable(), "WHERE" => ['deviceid' => 'test_assign_user_by_field_and_regex']]);
$this->integer(count($agents))->isIdenticalTo(1);
$agent = $agents->current();

//check created computer
$computer = new \Computer();
$this->boolean($computer->getFromDB($agent['items_id']))->isTrue();
$this->integer($computer->fields['users_id'])->isIdenticalTo($result['users_id']);
}
}

0 comments on commit 804a8e0

Please sign in to comment.