Skip to content

Commit

Permalink
Adding options for importing data with the user fix #127
Browse files Browse the repository at this point in the history
  • Loading branch information
amma35 committed Dec 28, 2017
1 parent 627c6bc commit 58edce2
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 43 deletions.
10 changes: 10 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,16 @@ function plugin_ocsinventoryng_install()
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->queryOrDie($query, "1.3.4 create table glpi_plugin_ocsinventoryng_winusers");
}

if (TableExists('glpi_plugin_ocsinventoryng_ocsservers')
&& !FieldExists('glpi_plugin_ocsinventoryng_ocsservers', 'import_user')) {

$query = "ALTER TABLE `glpi_plugin_ocsinventoryng_ocsservers`
ADD `import_user` tinyint(1) NOT NULL DEFAULT '1',
ADD `import_user_location` tinyint(1) NOT NULL DEFAULT '1',
ADD `import_user_group` tinyint(1) NOT NULL DEFAULT '1';";
$DB->queryOrDie($query, "1.3.4 update table glpi_plugin_ocsinventoryng_ocsservers add user fields");
}

$cron = new CronTask();
if (!$cron->getFromDBbyName('PluginOcsinventoryngThread', 'CleanOldThreads')) {
Expand Down
202 changes: 160 additions & 42 deletions inc/ocsserver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,16 +688,11 @@ function accordion(id, openall) {
Dropdown::showYesNo("import_general_domain", $this->fields["import_general_domain"]);
echo "</td></tr>\n";

echo "<tr class='tab_bg_2'><td class='center'>" . __('Alternate username') . "</td>\n<td>";
Dropdown::showYesNo("import_general_contact", $this->fields["import_general_contact"]);
echo "</td>\n";
echo "<tr class='tab_bg_2'>";
echo "<td class='center'>" . __('Comments') . "</td>\n<td>";
Dropdown::showYesNo("import_general_comment", $this->fields["import_general_comment"]);
echo "</td></tr>\n";

echo "<tr class='tab_bg_2'><td class='center'>" . __('IP') . "</td>\n<td>";
Dropdown::showYesNo("import_ip", $this->fields["import_ip"]);
echo "</td>\n";

if (self::checkOCSconnection($ID) && self::checkVersion($ID)) {
echo "<td class='center'>" . __('UUID') . "</td>\n<td>";
Dropdown::showYesNo("import_general_uuid", $this->fields["import_general_uuid"]);
Expand All @@ -706,6 +701,37 @@ function accordion(id, openall) {
echo "<input type='hidden' name='import_general_uuid' value='0'>";
}
echo "</td></tr>\n";

echo "<tr class='tab_bg_2'><td class='center'>" . __('IP') . "</td>\n<td>";
Dropdown::showYesNo("import_ip", $this->fields["import_ip"]);
echo "</td>\n";

echo "<td colspan='2'></td></tr>\n";

echo "<tr class='tab_bg_2'>";
echo "<th colspan='4'>" . __('User informations', 'ocsinventoryng');
echo "</th></tr>\n";

echo "<tr class='tab_bg_2'><td class='center'>" . __('Alternate username') . "</td>\n<td>";
Dropdown::showYesNo("import_general_contact", $this->fields["import_general_contact"]);
echo "</td>\n";
echo "<td class='center'>" . __('Affect user from contact') . "</td>\n<td>";
Dropdown::showYesNo("import_user", $this->fields["import_user"]);
echo "&nbsp;";
Html::showToolTip(nl2br(__('Depends on contact import', 'ocsinventoryng')));
echo "</td></tr>\n";

echo "<tr class='tab_bg_2'><td class='center'>" . __('Affect user location by default') . "</td>\n<td>";
Dropdown::showYesNo("import_user_location", $this->fields["import_user_location"]);
echo "&nbsp;";
Html::showToolTip(nl2br(__('Depends on contact import', 'ocsinventoryng')));
echo "</td>\n";
echo "<td class='center'>" . __('Affect first group of user by default') . "</td>\n<td>";
Dropdown::showYesNo("import_user_group", $this->fields["import_user_group"]);
echo "&nbsp;";
Html::showToolTip(nl2br(__('Depends on contact import', 'ocsinventoryng')));
echo "</td></tr>\n";

echo "</table>";
echo "</div>";

Expand Down Expand Up @@ -2387,8 +2413,7 @@ static function getOcsFieldsMatching()
* @param int $locations_id
* @return array
*/
static function getComputerInformations($ocs_fields = array(), $cfg_ocs, $entities_id, $locations_id = 0)
{
static function getComputerInformations($ocs_fields = array(), $cfg_ocs, $entities_id, $locations_id = 0, $groups_id = 0) {
$input = array();
$input["is_dynamic"] = 1;

Expand All @@ -2402,6 +2427,10 @@ static function getComputerInformations($ocs_fields = array(), $cfg_ocs, $entiti
$input["locations_id"] = $locations_id;
}

if ($groups_id) {
$input["groups_id"] = $groups_id;
}

$input['ocsid'] = $ocs_fields['META']['ID'];
$ocs_fields_matching = self::getOcsFieldsMatching();
foreach ($ocs_fields_matching as $glpi_field => $ocs_field) {
Expand Down Expand Up @@ -2507,6 +2536,7 @@ static function getComputerInformations($ocs_fields = array(), $cfg_ocs, $entiti

if (intval($cfg_ocs["import_general_contact"]) == 0) {
unset($input["contact"]);
unset($input["users_id"]);
}

if (intval($cfg_ocs["import_general_domain"]) == 0) {
Expand All @@ -2524,11 +2554,11 @@ static function getComputerInformations($ocs_fields = array(), $cfg_ocs, $entiti
* @param int $defaultlocation
* @return array
*/
static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lock = 0, $defaultentity = -1, $defaultlocation = -1)
{
static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lock = 0, $defaultentity = -1, $defaultlocation = -1) {
global $DB;

self::checkOCSconnection($plugin_ocsinventoryng_ocsservers_id);
$cfg_ocs = self::getConfig($plugin_ocsinventoryng_ocsservers_id);
$comp = new Computer();

$rules_matched = array();
Expand All @@ -2543,8 +2573,9 @@ static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lo
));

$locations_id = 0;
$groups_id = 0;
$contact = (isset($ocsComputer['META']["USERID"])) ? $ocsComputer['META']["USERID"] : "";
if (!empty($contact)) {
if (!empty($contact) && $cfg_ocs["import_general_contact"] > 0) {
$query = "SELECT `id`
FROM `glpi_users`
WHERE `name` = '" . $contact . "';";
Expand All @@ -2554,7 +2585,12 @@ static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lo
$user_id = $DB->result($result, 0, 0);
$user = new User();
$user->getFromDB($user_id);
$locations_id = $user->fields["locations_id"];
if ($cfg_ocs["import_user_location"] > 0) {
$locations_id = $user->fields["locations_id"];
}
if ($cfg_ocs["import_user_group"] > 0) {
$groups_id = self::getUserGroup(0, $user_id, '`is_requester`', true);
}
}
}

Expand All @@ -2565,10 +2601,12 @@ static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lo
$data = array();
$data = $rule->processAllRules(array('ocsservers_id' => $plugin_ocsinventoryng_ocsservers_id,
'_source' => 'ocsinventoryng',
'locations_id' => $locations_id
), array(
'locations_id' => $locations_id
), array('ocsid' => $ocsid));
'locations_id' => $locations_id,
'groups_id' => $groups_id
), array(
'locations_id' => $locations_id,
'groups_id' => $groups_id
), array('ocsid' => $ocsid));

if (isset($data['_ignore_import']) && $data['_ignore_import'] == 1) {
//ELSE Return code to indicates that the machine was not imported because it doesn't matched rules
Expand Down Expand Up @@ -2598,7 +2636,9 @@ static function importComputer($ocsid, $plugin_ocsinventoryng_ocsservers_id, $lo
$computer = Toolbox::clean_cross_side_scripting_deep(Toolbox::addslashes_deep($ocsComputer));

$locations_id = (isset($data['locations_id']) ? $data['locations_id'] : 0);
$input = self::getComputerInformations($computer, self::getConfig($plugin_ocsinventoryng_ocsservers_id), $data['entities_id'], $locations_id);
$groups_id = (isset($data['groups_id']) ? $data['groups_id'] : 0);
$input = self::getComputerInformations($computer, self::getConfig($plugin_ocsinventoryng_ocsservers_id),
$data['entities_id'], $locations_id, $groups_id);
//Check if machine could be linked with another one already in DB
$rulelink = new RuleImportComputerCollection();
$rulelink_results = array();
Expand Down Expand Up @@ -2724,7 +2764,7 @@ static function updateComputer($ID, $plugin_ocsinventoryng_ocsservers_id, $dohis

$locations_id = 0;
$contact = (isset($computer['META']["USERID"])) ? $computer['META']["USERID"] : "";
if (!empty($contact)) {
if (!empty($contact) && $cfg_ocs["import_general_contact"] > 0) {
$query = "SELECT `id`
FROM `glpi_users`
WHERE `name` = '" . $contact . "';";
Expand All @@ -2734,16 +2774,25 @@ static function updateComputer($ID, $plugin_ocsinventoryng_ocsservers_id, $dohis
$user_id = $DB->result($result, 0, 0);
$user = new User();
$user->getFromDB($user_id);
$locations_id = $user->fields["locations_id"];
if ($cfg_ocs["import_user_location"] > 0) {
$locations_id = $user->fields["locations_id"];
}
if ($cfg_ocs["import_user_group"] > 0) {
$groups_id = self::getUserGroup($comp->fields["entities_id"], $user_id, '`is_requester`', true);
}
}
}
$rule = new RuleImportEntityCollection();

$data = array();
$data = $rule->processAllRules(array('ocsservers_id' => $line["plugin_ocsinventoryng_ocsservers_id"],
'_source' => 'ocsinventoryng',
'locations_id' => $locations_id), array('locations_id' => $locations_id), array('ocsid' => $line["ocsid"]));
self::updateLocation($line, $data, $cfg_ocs);
'locations_id' => $locations_id,
'groups_id' => $groups_id),
array('locations_id' => $locations_id,
'groups_id' => $groups_id),
array('ocsid' => $line["ocsid"]));
self::updateComputerFields($line, $data, $cfg_ocs);
}

// update last_update and and last_ocs_update
Expand Down Expand Up @@ -3282,19 +3331,20 @@ static function getComputerHardware($params = array())
}

if (intval($options['cfg_ocs']["import_general_contact"]) > 0
&& !in_array("contact", $options['computers_updates'])
) {
&& !in_array("contact", $options['computers_updates'])) {

$compupdate["contact"] = self::encodeOcsDataInUtf8($is_utf8, $hardware["USERID"]);
$query = "SELECT `id`

if (intval($options['cfg_ocs']["import_user"]) > 0) {
$query = "SELECT `id`
FROM `glpi_users`
WHERE `name` = '" . $hardware["USERID"] . "';";
$result = $DB->query($query);
$result = $DB->query($query);

if ($DB->numrows($result) == 1
&& !in_array("users_id", $options['computers_updates'])
) {
$compupdate["users_id"] = $DB->result($result, 0, 0);
if ($DB->numrows($result) == 1
&& !in_array("users_id", $options['computers_updates'])) {
$compupdate["users_id"] = $DB->result($result, 0, 0);
}
}
}

Expand Down Expand Up @@ -7326,17 +7376,17 @@ static function showStatistics($statistics = array(), $finished = false, $snmp =
* @internal param array $line_ocs : data from ocs tables
*
*/
static function transferComputer($line_links)
{
static function transferComputer($line_links) {
global $DB, $CFG_GLPI;

$ocsClient = self::getDBocs($line_links["plugin_ocsinventoryng_ocsservers_id"]);
$cfg_ocs = self::getConfig($line_links["plugin_ocsinventoryng_ocsservers_id"]);
$ocsComputer = $ocsClient->getComputer($line_links["ocsid"]);

$locations_id = 0;
$groups_id = 0;
$contact = (isset($ocsComputer['META']["USERID"])) ? $ocsComputer['META']["USERID"] : "";
if (!empty($contact)) {
if (!empty($contact) && $cfg_ocs["import_general_contact"] > 0) {
$query = "SELECT `id`
FROM `glpi_users`
WHERE `name` = '" . $contact . "';";
Expand All @@ -7346,20 +7396,29 @@ static function transferComputer($line_links)
$user_id = $DB->result($result, 0, 0);
$user = new User();
$user->getFromDB($user_id);
$locations_id = $user->fields["locations_id"];

if ($cfg_ocs["import_user_location"] > 0) {
$locations_id = $user->fields["locations_id"];
}
if ($cfg_ocs["import_user_group"] > 0) {
$comp = new Computer();
$comp->getFromDB($line_links["computers_id"]);
$groups_id = self::getUserGroup($comp->fields["entities_id"], $user_id, '`is_requester`', true);
}
}
}

// Get all rules for the current plugin_ocsinventoryng_ocsservers_id
$rule = new RuleImportEntityCollection();

$data = array();
$data = $rule->processAllRules(array('ocsservers_id' => $line_links["plugin_ocsinventoryng_ocsservers_id"],
'_source' => 'ocsinventoryng',
'locations_id' => $locations_id
), array(
'locations_id' => $locations_id
), array('ocsid' => $line_links["ocsid"]));
'locations_id' => $locations_id,
'groups_id' => $groups_id
), array(
'locations_id' => $locations_id,
'groups_id' => $groups_id
), array('ocsid' => $line_links["ocsid"]));

// If entity is changing move items to the new entities_id
if (isset($data['entities_id']) && $data['entities_id'] > -1 && $data['entities_id'] != $line_links['entities_id']) {
Expand All @@ -7378,11 +7437,41 @@ static function transferComputer($line_links)
}

//If location is update by a rule
self::updateLocation($line_links, $data, $cfg_ocs);
self::updateComputerFields($line_links, $data, $cfg_ocs);
}

/**
* @param $entity
* @param $userid
* @param string $filter
* @param bool $first
*
* @return array|int
*/
static private function getUserGroup ($entity, $userid, $filter='', $first=true) {
global $DB;

$query = "SELECT `glpi_groups`.`id`
FROM `glpi_groups_users`
INNER JOIN `glpi_groups` ON (`glpi_groups`.`id` = `glpi_groups_users`.`groups_id`)
WHERE `glpi_groups_users`.`users_id` = '".$userid."'".
getEntitiesRestrictRequest(' AND ', 'glpi_groups', '', $entity, true);

if ($filter) {
$query .= "AND (".$filter.")";
}
$rep = [];
foreach ($DB->request($query) as $data) {
if ($first) {
return $data['id'];
}
$rep[] = $data['id'];
}
return ($first ? 0 : $rep);
}

/**
* Update location for a computer if needed after rule processing
* Update fields : location / group for a computer if needed after rule processing
*
* @param $line_links
* @param $data
Expand All @@ -7392,7 +7481,7 @@ static function transferComputer($line_links)
* @internal param $data
*
*/
static function updateLocation($line_links, $data, $cfg_ocs)
static function updateComputerFields($line_links, $data, $cfg_ocs)
{

//If there's a location to update
Expand Down Expand Up @@ -7424,6 +7513,35 @@ static function updateLocation($line_links, $data, $cfg_ocs)
}
}
}

//If there's a Group to update
if (isset($data['groups_id'])) {
$computer = new Computer();
$computer->getFromDB($line_links['computers_id']);
$ancestors = getAncestorsOf('glpi_entities', $computer->fields['entities_id']);

$group = new Group();
if ($group->getFromDB($data['groups_id'])) {
//If group is in the same entity as the computer, or if the group is
//defined in a parent entity, but recursive
if ($group->fields['entities_id'] == $computer->fields['entities_id']
|| (in_array($group->fields['entities_id'], $ancestors)
&& $group->fields['is_recursive'])) {
$ko = 0;
$locks = self::getLocksForComputer($line_links['computers_id']);
if (is_array($locks) && count($locks)) {
if (in_array("groups_id", $locks)) {
$ko = 1;
}
}
if ($ko == 0) {
$tmp['groups_id'] = $data['groups_id'];
$tmp['id'] = $line_links['computers_id'];
$computer->update($tmp, $cfg_ocs['history_hardware']);
}
}
}
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions install/mysql/1.3.4-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ CREATE TABLE `glpi_plugin_ocsinventoryng_ocsservers` (
`import_general_type` tinyint(1) NOT NULL DEFAULT '1',
`import_general_domain` tinyint(1) NOT NULL DEFAULT '1',
`import_general_contact` tinyint(1) NOT NULL DEFAULT '1',
`import_user` tinyint(1) NOT NULL DEFAULT '1',
`import_user_location` tinyint(1) NOT NULL DEFAULT '1',
`import_user_group` tinyint(1) NOT NULL DEFAULT '1',
`import_general_comment` tinyint(1) NOT NULL DEFAULT '1',
`import_device_processor` tinyint(1) NOT NULL DEFAULT '1',
`import_device_memory` tinyint(1) NOT NULL DEFAULT '1',
Expand Down
Loading

0 comments on commit 58edce2

Please sign in to comment.