Import a dropdown - check if already exists
static public import ( $itemtype, $input ) : the | ||
$itemtype | string name of the class | |
$input | array of value to import | |
Результат | the | ID of the new |
/** * Add a software. If already exist in dustbin restore it * * @param name the software's name * @param manufacturer the software's manufacturer * @param entity the entity in which the software must be added * @param comment comment (default '') * @param is_recursive boolean must the software be recursive (false by default) * @param is_helpdesk_visible show in helpdesk, default = config value (false by default) */ function addOrRestoreFromTrash($name, $manufacturer, $entity, $comment = '', $is_recursive = false, $is_helpdesk_visible = NULL) { global $DB; //Look for the software by his name in GLPI for a specific entity $manufacturer_id = 0; if ($manufacturer != '') { $manufacturer_id = Dropdown::import('Manufacturer', array('name' => $manufacturer)); } $query_search = "SELECT `glpi_softwares`.`id`, `glpi_softwares`.`is_deleted`\n FROM `glpi_softwares`\n WHERE `name` = '{$name}'\n AND `manufacturers_id` = '{$manufacturer_id}'\n AND `is_template` = '0' " . getEntitiesRestrictRequest('AND', 'glpi_softwares', 'entities_id', $entity, true); $result_search = $DB->query($query_search); if ($DB->numrows($result_search) > 0) { //Software already exists for this entity, get his ID $data = $DB->fetch_assoc($result_search); $ID = $data["id"]; // restore software if ($data['is_deleted']) { $this->removeFromTrash($ID); } } else { $ID = 0; } if (!$ID) { $ID = $this->addSoftware($name, $manufacturer_id, $entity, $comment, $is_recursive, $is_helpdesk_visible); } return $ID; }
/** * Function that try to load from LDAP the user information... * * @param $ldap_connection ldap connection descriptor * @param $ldap_method LDAP method * @param $userdn Basedn of the user * @param $login User Login * @param $import boolean true for import, false for update (true by default) * * @return boolean : true if found / false if not founded **/ function getFromLDAP($ldap_connection, $ldap_method, $userdn, $login, $import = true) { global $DB, $CFG_GLPI; // we prevent some delay... if (empty($ldap_method["host"])) { return false; } if ($ldap_connection) { //Set all the search fields $this->fields['password'] = ""; $fields = AuthLDAP::getSyncFields($ldap_method); //Hook to allow plugin to request more attributes from ldap $fields = Plugin::doHookFunction("retrieve_more_field_from_ldap", $fields); $fields = array_filter($fields); $f = self::getLdapFieldNames($fields); $sr = @ldap_read($ldap_connection, $userdn, "objectClass=*", $f); $v = AuthLDAP::get_entries_clean($ldap_connection, $sr); if (!is_array($v) || count($v) == 0 || empty($v[0][$fields['name']][0])) { return false; } //Store user's dn $this->fields['user_dn'] = addslashes($userdn); //Store date_sync $this->fields['date_sync'] = $_SESSION['glpi_currenttime']; // Empty array to ensure than syncDynamicEmails will be done $this->fields["_emails"] = array(); foreach ($fields as $k => $e) { $val = self::getLdapFieldValue($e, $v); if (empty($val)) { switch ($k) { case "language": // Not set value : managed but user class break; case "usertitles_id": case "usercategories_id": case 'locations_id': $this->fields[$k] = 0; break; default: $this->fields[$k] = ""; } } else { switch ($k) { case "email1": case "email2": case "email3": case "email4": // Manage multivaluable fields if (!empty($v[0][$e])) { foreach ($v[0][$e] as $km => $m) { if (!preg_match('/count/', $km)) { $this->fields["_emails"][] = addslashes($m); } } // Only get them once if duplicated $this->fields["_emails"] = array_unique($this->fields["_emails"]); } break; case "language": $language = Config::getLanguage($val); if ($language != '') { $this->fields[$k] = $language; } break; case "usertitles_id": $this->fields[$k] = Dropdown::importExternal('UserTitle', $val); break; case 'locations_id': // use import to build the location tree $this->fields[$k] = Dropdown::import('Location', ['completename' => $val, 'entities_id' => 0, 'is_recursive' => 1]); break; case "usercategories_id": $this->fields[$k] = Dropdown::importExternal('UserCategory', $val); break; default: $this->fields[$k] = $val; } } } // Empty array to ensure than syncLdapGroups will be done $this->fields["_groups"] = array(); ///The groups are retrieved by looking into an ldap user object if ($ldap_method["group_search_type"] == 0 || $ldap_method["group_search_type"] == 2) { $this->getFromLDAPGroupVirtual($ldap_connection, $ldap_method, $userdn, $login); } ///The groups are retrived by looking into an ldap group object if ($ldap_method["group_search_type"] == 1 || $ldap_method["group_search_type"] == 2) { $this->getFromLDAPGroupDiscret($ldap_connection, $ldap_method, $userdn, $login); } ///Only process rules if working on the master database if (!$DB->isSlave()) { //Instanciate the affectation's rule $rule = new RuleRightCollection(); //Process affectation rules : //we don't care about the function's return because all //the datas are stored in session temporary if (isset($this->fields["_groups"])) { $groups = $this->fields["_groups"]; } else { $groups = array(); } $this->fields = $rule->processAllRules($groups, Toolbox::stripslashes_deep($this->fields), array('type' => 'LDAP', 'ldap_server' => $ldap_method["id"], 'connection' => $ldap_connection, 'userdn' => $userdn)); $this->fields['_ruleright_process'] = true; //If rule action is ignore import if ($import && isset($this->fields["_stop_import"])) { return false; } //or no rights found & do not import users with no rights if ($import && !$CFG_GLPI["use_noright_users_add"]) { $ok = false; if (isset($this->fields["_ldap_rules"]) && count($this->fields["_ldap_rules"])) { if (isset($this->fields["_ldap_rules"]["rules_entities_rights"]) && count($this->fields["_ldap_rules"]["rules_entities_rights"])) { $ok = true; } if (!$ok) { $entity_count = 0; $right_count = 0; if (Profile::getDefault()) { $right_count++; } if (isset($this->fields["_ldap_rules"]["rules_entities"])) { $entity_count += count($this->fields["_ldap_rules"]["rules_entities"]); } if (isset($this->input["_ldap_rules"]["rules_rights"])) { $right_count += count($this->fields["_ldap_rules"]["rules_rights"]); } if ($entity_count && $right_count) { $ok = true; } } } if (!$ok) { $this->fields["_stop_import"] = true; return false; } } // Add ldap result to data send to the hook $this->fields['_ldap_result'] = $v; $this->fields['_ldap_conn'] = $ldap_connection; //Hook to retrieve more information for ldap $this->fields = Plugin::doHookFunction("retrieve_more_data_from_ldap", $this->fields); unset($this->fields['_ldap_result']); } return true; } return false; }
/** * Test software category Rule and putInTrash / removeFromTrash */ public function testSoftwareCategory() { global $CFG_GLPI; $ent0 = $this->sharedFixture['entity'][0]; // Clean preload rules $tmp = SingletonRuleList::getInstance('RuleSoftwareCategory'); $tmp->load = 0; $this->assertArrayHasKey('softwarecategories_id_ondelete', $CFG_GLPI, "Fail: no softwarecategories_id_ondelete"); $idcat[0] = Dropdown::import('SoftwareCategory', array('name' => 'Trashed')); $this->assertGreaterThan(0, $idcat[0], "Fail: can't create SoftwareCategory"); $idcat[1] = Dropdown::import('SoftwareCategory', array('name' => 'OpenSource')); $this->assertGreaterThan(0, $idcat[1], "Fail: can't create SoftwareCategory"); $rule = new RuleSoftwareCategory(); $crit = new RuleCriteria(); $acte = new RuleAction(); $idr[0] = $rule->add(array('name' => 'OSS', 'sub_type' => 'RuleSoftwareCategory', 'match' => 'AND', 'is_active' => 1)); $this->assertGreaterThan(0, $idr[0], "Fail: can't create rule 1"); $this->assertTrue($rule->getFromDB($idr[0])); $this->assertEquals(1, $rule->fields['ranking'], "Fail: ranking not set"); $idc[0] = $crit->add(array('rules_id' => $idr[0], 'criteria' => 'manufacturer', 'condition' => Rule::PATTERN_IS, 'pattern' => 'Indepnet')); $this->assertGreaterThan(0, $idc[0], "Fail: can't create rule 1 criteria"); $ida[0] = $acte->add(array('rules_id' => $idr[0], 'action_type' => 'assign', 'field' => 'softwarecategories_id', 'value' => $idcat[1])); $this->assertGreaterThan(0, $ida[0], "Fail: can't create rule 1 action"); // Createthe software $soft = new Software(); $id[0] = $soft->addOrRestoreFromTrash('GLPI', 'Indepnet', $ent0); $this->assertGreaterThan(0, $id[0], "Fail: can't create software 1"); // Check name $this->assertTrue($soft->getFromDB($id[0]), "Fail: can't read new soft"); $this->assertEquals('GLPI', $soft->getField('name'), "Fail: name not set"); // Check category $catid = $soft->getField('softwarecategories_id'); $this->assertEquals($idcat[1], $catid, "Fail: category not set"); // Change configuration $CFG_GLPI["softwarecategories_id_ondelete"] = $idcat[0]; // Delete $this->assertTrue($soft->putInTrash($id[0]), "Fail: can't put soft in trash"); $this->assertTrue($soft->getFromDB($id[0]), "Fail: can't read new soft"); $catid = $soft->getField('softwarecategories_id'); $this->assertEquals($idcat[0], $catid, "Fail: category not set"); $this->assertEquals(1, $soft->getField('is_deleted'), "Fail: soft not deleted"); // Restore $this->assertTrue($soft->removeFromTrash($id[0]), "Fail: can't put soft in trash"); $this->assertTrue($soft->getFromDB($id[0]), "Fail: can't read new soft"); $catid = $soft->getField('softwarecategories_id'); $this->assertEquals($idcat[1], $catid, "Fail: category not set"); $this->assertEquals(0, $soft->getField('is_deleted'), "Fail: soft not restored"); // Clean $this->assertTrue($soft->delete(array('id' => $id[0]), true), "Fail: can't delete software 1)"); }
function computerSoftwareTransformation($a_inventory, $entities_id) { /* * Sometimes we can have 2 same software, but one without manufacturer and * one with. So in this case, delete the software without manufacturer */ $softwareWithManufacturer = array(); $softwareWithoutManufacturer = array(); $entities_id_software = Entity::getUsedConfig('entities_id_software', $entities_id); $is_software_recursive = 0; $nb_RuleDictionnarySoftware = countElementsInTable("glpi_rules", "`sub_type`='RuleDictionnarySoftware'\n AND `is_active`='1'"); //Configuration says that software can be created in the computer's entity if ($entities_id_software < 0) { $entities_id_software = $entities_id; } else { //Software will be created in an entity which is not the computer's entity. //It should be set as recursive $is_software_recursive = 1; } $a_inventory['software'] = array(); $rulecollection = new RuleDictionnarySoftwareCollection(); foreach ($a_inventory['SOFTWARES'] as $a_softwares) { if (isset($a_softwares['PUBLISHER']) && gettype($a_softwares['PUBLISHER']) == 'array') { $a_softwares['PUBLISHER'] = current($a_softwares['PUBLISHER']); } $array_tmp = $this->addValues($a_softwares, array('PUBLISHER' => 'manufacturers_id', 'NAME' => 'name', 'VERSION' => 'version')); if (!isset($array_tmp['name']) || $array_tmp['name'] == '') { if (isset($a_softwares['GUID']) && $a_softwares['GUID'] != '') { $array_tmp['name'] = $a_softwares['GUID']; } } if (!(!isset($array_tmp['name']) || $array_tmp['name'] == '')) { if (count($array_tmp) > 0) { $res_rule = array(); if ($nb_RuleDictionnarySoftware > 0) { $res_rule = $rulecollection->processAllRules(array("name" => $array_tmp['name'], "manufacturer" => $array_tmp['manufacturers_id'], "old_version" => $array_tmp['version'], "entities_id" => $entities_id_software)); } if (isset($res_rule['_ignore_import']) && $res_rule['_ignore_import'] == 1) { } else { if (isset($res_rule["name"])) { $array_tmp['name'] = $res_rule["name"]; } if (isset($res_rule["version"])) { $array_tmp['version'] = $res_rule["version"]; } if (isset($res_rule["manufacturer"])) { $array_tmp['manufacturers_id'] = Dropdown::import("Manufacturer", array('name' => $res_rule["manufacturer"])); } else { if (isset($array_tmp['manufacturers_id']) && $array_tmp['manufacturers_id'] != '' && $array_tmp['manufacturers_id'] != '0') { if (!isset($this->manufacturer_cache[$array_tmp['manufacturers_id']])) { $new_value = Dropdown::importExternal('Manufacturer', $array_tmp['manufacturers_id']); $this->manufacturer_cache[$array_tmp['manufacturers_id']] = $new_value; } $array_tmp['manufacturers_id'] = $this->manufacturer_cache[$array_tmp['manufacturers_id']]; } else { $array_tmp['manufacturers_id'] = 0; } } if (isset($res_rule['new_entities_id'])) { $array_tmp['entities_id'] = $res_rule['new_entities_id']; $is_software_recursive = 1; } if (!isset($array_tmp['entities_id']) || $array_tmp['entities_id'] == '') { $array_tmp['entities_id'] = $entities_id_software; } if (!isset($array_tmp['version'])) { $array_tmp['version'] = ""; } $array_tmp['is_template_computer'] = 0; $array_tmp['is_deleted_computer'] = 0; $array_tmp['is_recursive'] = $is_software_recursive; $comp_key = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['manufacturers_id'] . "\$\$\$\$" . $array_tmp['entities_id']; $comp_key_simple = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['entities_id']; if ($array_tmp['manufacturers_id'] == 0) { $softwareWithoutManufacturer[$comp_key_simple] = $array_tmp; } else { if (!isset($a_inventory['software'][$comp_key])) { $softwareWithManufacturer[$comp_key_simple] = 1; $a_inventory['software'][$comp_key] = $array_tmp; } } } } } } foreach ($softwareWithoutManufacturer as $key => $array_tmp) { if (!isset($softwareWithManufacturer[$key])) { $comp_key = strtolower($array_tmp['name']) . "\$\$\$\$" . strtolower($array_tmp['version']) . "\$\$\$\$" . $array_tmp['manufacturers_id'] . "\$\$\$\$" . $array_tmp['entities_id']; if (!isset($a_inventory['software'][$comp_key])) { $a_inventory['software'][$comp_key] = $array_tmp; } } } unset($a_inventory['SOFTWARES']); return $a_inventory; }
/** * @covers Dropdown::import * @covers CommonTreeDropdown::import * @dataProvider dataTestTreeImport */ public function testTreeImport($input, $result, $complete, $msg) { $id = Dropdown::import('Location', $input); if ($result) { $this->assertGreaterThan(0, $id, $msg); $ut = new Location(); $this->assertTrue($ut->getFromDB($id), $msg); $this->assertEquals($result, $ut->getField('name'), $msg); $this->assertEquals($complete, $ut->getField('completename'), $msg); } else { $this->assertLessThanOrEqual(0, $id, $msg); } }
/** * @covers Dropdown::import * @covers CommonTreeDropdown::import * @dataProvider dataTestTreeImport */ public function testTreeImport($input, $result, $complete, $msg) { $input['entities_id'] = getItemByTypeName('Entity', '_test_root_entity', true); $id = Dropdown::import('Location', $input); if ($result) { $this->assertGreaterThan(0, $id, $msg); $ut = new Location(); $this->assertTrue($ut->getFromDB($id), $msg); $this->assertEquals($result, $ut->getField('name'), $msg); $this->assertEquals($complete, $ut->getField('completename'), $msg); } else { $this->assertLessThanOrEqual(0, $id, $msg); } }