Skip to content

Commit 0fcbf99

Browse files
committed
feat(sccm): Works with multiple configurations
1 parent 5044f22 commit 0fcbf99

File tree

5 files changed

+401
-214
lines changed

5 files changed

+401
-214
lines changed

front/config.form.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@
3232
include ('../../../inc/includes.php');
3333
require_once('../inc/config.class.php');
3434

35+
global $CFG_GLPI;
36+
37+
$urlConfigList = PluginSccmConfig::configUrl();
3538

3639
Session::checkRight("config", UPDATE);
3740

3841
$PluginSccmConfig = new PluginSccmConfig();
42+
global $DB;
3943

4044
if (isset($_POST["update"])) {
4145
if (array_key_exists('sccmdb_password', $_POST)) {
@@ -45,18 +49,36 @@
4549

4650
$PluginSccmConfig->update($_POST);
4751

48-
$sccmDB = new PluginSccmSccmdb();
49-
if ($sccmDB->connect()) {
50-
Session::addMessageAfterRedirect("Connexion réussie !.", false, INFO, false);
52+
Toolbox::logInFile('sccm', "Updating configuration ".$_POST['sccm_config_name']." ".$_POST['id']." ...\n", true);
53+
54+
$sccmDB = new PluginSccmSccmdb();
55+
$sccmDB->testConfiguration($_POST['id']);
56+
57+
Html::redirect($urlConfigList);
58+
}
59+
if (isset($_POST["add"])) {
60+
Toolbox::logInFile('sccm', "Inserting configuration ".$_POST['sccm_config_name']." ...\n", true);
61+
$insertedId = $PluginSccmConfig->add($_POST);
62+
63+
if ($insertedId) {
64+
$sccmDB = new PluginSccmSccmdb();
65+
$sccmDB->testConfiguration($insertedId);
5166
} else {
52-
Session::addMessageAfterRedirect("Connexion incorrecte.", false, ERROR, false);
67+
Toolbox::logInFile('sccm', "Error inserting configuration ".$_POST['sccm_config_name']." ".$DB->error()." ...\n", true);
68+
Session::addMessageAfterRedirect("Error inserting configuration.", false, ERROR, false);
5369
}
5470

55-
56-
Html::back();
71+
Html::redirect($urlConfigList);
5772
}
5873

5974
Html::header(__("Setup - SCCM", "sccm"), $_SERVER["PHP_SELF"],
60-
"plugins", "sccm", "configuration");
61-
$PluginSccmConfig->showConfigForm($PluginSccmConfig);
75+
"config", "sccm", "configuration");
76+
$configId = isset($_GET['id']) ? intval($_GET['id']) : null;
77+
78+
if ($configId !== null) {
79+
$PluginSccmConfig->showConfigForm($PluginSccmConfig, $configId);
80+
} else {
81+
Search::show('PluginSccmConfig');
82+
}
83+
6284
Html::footer();

inc/config.class.php

Lines changed: 174 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,83 @@ static function getInstance() {
6161

6262
if (!isset(self::$_instance)) {
6363
self::$_instance = new self();
64-
if (!self::$_instance->getFromDB(1)) {
65-
self::$_instance->getEmpty();
66-
}
6764
}
6865
return self::$_instance;
6966
}
7067

68+
public function rawSearchOptions()
69+
{
70+
$tab = [];
71+
$tab[] = [
72+
'id' => 'common',
73+
'name' => self::getTypeName(2)
74+
];
75+
76+
$tab[] = [
77+
'id' => '1',
78+
'table' => $this->getTable(),
79+
'field' => 'sccm_config_name',
80+
'name' => __('Config name'),
81+
'massiveaction' => false,
82+
'datatype' => 'itemlink'
83+
];
84+
85+
$tab[] = [
86+
'id' => '2',
87+
'table' => $this->getTable(),
88+
'field' => 'active_sync',
89+
'name' => __('Enabled'),
90+
'massiveaction' => false,
91+
'datatype' => 'bool'
92+
];
93+
$tab[] = [
94+
'id' => '3',
95+
'table' => $this->getTable(),
96+
'field' => 'sccm_collection_name',
97+
'name' => __('Collection'),
98+
'massiveaction' => false,
99+
'datatype' => 'string'
100+
];
101+
$tab[] = [
102+
'id' => '4',
103+
'table' => $this->getTable(),
104+
'field' => 'sccmdb_host',
105+
'name' => __('Db Host'),
106+
'massiveaction' => false,
107+
'datatype' => 'string'
108+
];
109+
$tab[] = [
110+
'id' => '5',
111+
'table' => $this->getTable(),
112+
'field' => 'sccmdb_dbname',
113+
'name' => __('DB Name'),
114+
'massiveaction' => false,
115+
'datatype' => 'string'
116+
];
117+
$tab[] = [
118+
'id' => '6',
119+
'table' => $this->getTable(),
120+
'field' => 'sccmdb_user',
121+
'name' => __('DB User'),
122+
'massiveaction' => false,
123+
'datatype' => 'string'
124+
];
125+
return $tab;
126+
}
127+
128+
function getAllConfigurations() {
129+
return getAllDataFromTable(self::getTable());
130+
}
131+
132+
function loadFirstConfiguration() {
133+
$configurations = $this->getAllConfigurations();
134+
if (empty($configurations)) {
135+
return false;
136+
}
137+
$this->getFromDB(array_values($configurations)[0]['id']);
138+
139+
return true;
140+
}
71141

72142
function prepareInputForUpdate($input) {
73143
if (isset($input["sccmdb_password"]) AND !empty($input["sccmdb_password"])) {
@@ -81,6 +151,28 @@ function prepareInputForUpdate($input) {
81151
return $input;
82152
}
83153

154+
function prepareInputForAdd($input) {
155+
if (isset($input["sccmdb_password"]) AND !empty($input["sccmdb_password"])) {
156+
$input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]);
157+
}
158+
159+
if (array_key_exists('inventory_server_url', $input) && !empty($input['inventory_server_url'])) {
160+
$input['inventory_server_url'] = trim($input['inventory_server_url'], '/ ');
161+
}
162+
163+
return $input;
164+
}
165+
166+
static function isIdAutoIncrement()
167+
{
168+
global $DB;
169+
170+
$columns = $DB->query("SHOW COLUMNS FROM glpi_plugin_sccm_configs WHERE FIELD = 'id'");
171+
$data = $columns->fetch_assoc();
172+
Toolbox::logInFile('sccm', "Auto increment ... " . $data["Extra"] . " \n", true);
173+
return str_contains($data["Extra"], "auto_increment");
174+
}
175+
84176
static function install(Migration $migration) {
85177
global $CFG_GLPI, $DB;
86178

@@ -89,15 +181,20 @@ static function install(Migration $migration) {
89181
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();
90182

91183
$table = 'glpi_plugin_sccm_configs';
184+
Toolbox::logInFile('sccm', "Installing ...\n", true);
92185

93186
if (!$DB->tableExists($table)) {
94187

188+
Toolbox::logInFile('sccm', "Table not exists, creating ...\n", true);
189+
95190
$query = "CREATE TABLE `". $table."`(
96-
`id` int {$default_key_sign} NOT NULL,
191+
`id` int {$default_key_sign} NOT NULL AUTO_INCREMENT,
192+
`sccm_config_name` VARCHAR(255) NULL,
97193
`sccmdb_host` VARCHAR(255) NULL,
98194
`sccmdb_dbname` VARCHAR(255) NULL,
99195
`sccmdb_user` VARCHAR(255) NULL,
100196
`sccmdb_password` VARCHAR(255) NULL,
197+
`sccm_collection_name` VARCHAR(255) NULL,
101198
`inventory_server_url` VARCHAR(255) NULL,
102199
`active_sync` tinyint NOT NULL default '0',
103200
`verify_ssl_cert` tinyint NOT NULL default '0',
@@ -116,15 +213,30 @@ static function install(Migration $migration) {
116213
. "<br />".$DB->error());
117214

118215
$query = "INSERT INTO `$table`
119-
(id, date_mod, sccmdb_host, sccmdb_dbname,
216+
(date_mod, sccmdb_host, sccmdb_dbname,
120217
sccmdb_user, sccmdb_password, inventory_server_url)
121-
VALUES (1, NOW(), 'srv_sccm','bdd_sccm','user_sccm','',
218+
VALUES (NOW(), 'srv_sccm','bdd_sccm','user_sccm','',
122219
NULL)";
123220

124221
$DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm")
125222
. "<br />" . $DB->error());
126223

127224
} else {
225+
if (!self::isIdAutoIncrement())
226+
{
227+
Toolbox::logInFile('sccm', "Cambiando a Auto increment ... \n", true);
228+
$migration->changeField("glpi_plugin_sccm_configs", "id", "id", "autoincrement");
229+
$migration->migrationOneTable('glpi_plugin_sccm_configs');
230+
}
231+
if (!$DB->fieldExists($table, 'sccm_config_name')) {
232+
$migration->addField("glpi_plugin_sccm_configs", "sccm_config_name", "VARCHAR(255)");
233+
$migration->migrationOneTable('glpi_plugin_sccm_configs');
234+
}
235+
236+
if (!$DB->fieldExists($table, 'sccm_collection_name')) {
237+
$migration->addField("glpi_plugin_sccm_configs", "sccm_collection_name", "VARCHAR(255)");
238+
$migration->migrationOneTable('glpi_plugin_sccm_configs');
239+
}
128240

129241
if (!$DB->fieldExists($table, 'verify_ssl_cert')) {
130242
$migration->addField("glpi_plugin_sccm_configs", "verify_ssl_cert", "tinyint NOT NULL default '0'");
@@ -153,24 +265,28 @@ static function install(Migration $migration) {
153265

154266
if (!$DB->fieldExists($table, 'is_password_sodium_encrypted')) {
155267
$config = self::getInstance();
156-
if (!empty($config->fields['sccmdb_password'])) {
157-
$key = new GLPIKey();
158-
$migration->addPostQuery(
159-
$DB->buildUpdate(
160-
'glpi_plugin_sccm_configs',
161-
[
162-
'sccmdb_password' => $key->encrypt(
163-
$key->decryptUsingLegacyKey(
164-
$config->fields['sccmdb_password']
268+
$configurations = $config->getAllConfigurations();
269+
foreach ($configurations as $data) {
270+
$config->getFromDB($data['id']);
271+
if (!empty($config->fields['sccmdb_password'])) {
272+
$key = new GLPIKey();
273+
$migration->addPostQuery(
274+
$DB->buildUpdate(
275+
'glpi_plugin_sccm_configs',
276+
[
277+
'sccmdb_password' => $key->encrypt(
278+
$key->decryptUsingLegacyKey(
279+
$config->fields['sccmdb_password']
280+
)
165281
)
282+
],
283+
[
284+
'id' => $data['id'],
285+
]
166286
)
167-
],
168-
[
169-
'id' => 1,
170-
]
171-
)
172-
);
173-
}
287+
);
288+
}
289+
}
174290
$migration->addField("glpi_plugin_sccm_configs", "is_password_sodium_encrypted", "tinyint NOT NULL default '1'");
175291
$migration->migrationOneTable('glpi_plugin_sccm_configs');
176292
}
@@ -219,6 +335,7 @@ static function install(Migration $migration) {
219335
static function uninstall() {
220336
global $DB;
221337

338+
Toolbox::logInFile('sccm', "Uninstalling ...\n", true);
222339
if ($DB->tableExists('glpi_plugin_sccm_configs')) {
223340

224341
$query = "DROP TABLE `glpi_plugin_sccm_configs`";
@@ -227,14 +344,43 @@ static function uninstall() {
227344
return true;
228345
}
229346

347+
static function configUrl() {
348+
global $CFG_GLPI;
349+
return $CFG_GLPI['url_base'] . "/plugins/sccm/front/config.form.php";;
350+
}
351+
352+
static function showConfigList() {
353+
global $DB;
354+
355+
$configUrl = self::configUrl();
230356

231-
static function showConfigForm($item) {
357+
echo "<p>SCCM Configuration list: </p>";
358+
echo "<ul>";
359+
360+
$configs = $DB->query("select * from glpi_plugin_sccm_configs");
361+
while ($data = $configs->fetch_assoc()) {
362+
echo " <li> <a href='" . $configUrl . "?id=" . $data['id'] . "'>".$data['sccm_config_name']."</a>";
363+
}
364+
echo " <li> <a href='" . $configUrl . "?id=-1'>Add new ...</a>";
365+
echo "</ul>";
366+
}
367+
368+
static function showConfigForm($item, $configId) {
232369
global $CFG_GLPI;
233370

234371
$config = self::getInstance();
235372

373+
if (!$config->getFromDB($configId)) {
374+
$config->getEmpty();
375+
}
376+
236377
$config->showFormHeader();
237378

379+
echo "<tr class='tab_bg_1'>";
380+
echo "<td>".__("SCCM configuration name", "sccm")." (Id: ".$config->getField('id').")</td><td>";
381+
echo Html::input('sccm_config_name', ['value' => $config->getField('sccm_config_name')]);
382+
echo "</td></tr>\n";
383+
238384
echo "<tr class='tab_bg_1'>";
239385
echo "<td>".__("Enable SCCM synchronization", "sccm")."</td><td>";
240386
Dropdown::showYesNo("active_sync", $config->getField('active_sync'));
@@ -262,6 +408,11 @@ static function showConfigForm($item) {
262408
echo "<input type='password' name='sccmdb_password' value='$password' autocomplete='off'>";
263409
echo "</td></tr>\n";
264410

411+
echo "<tr class='tab_bg_1'>";
412+
echo "<td>".__("SCCM collection name", "sccm")."</td><td>";
413+
echo Html::input('sccm_collection_name', ['value' => $config->getField('sccm_collection_name')]);
414+
echo "</td></tr>\n";
415+
265416
echo "<tr class='tab_bg_1'>";
266417
echo "<td>".__("Inventory server base URL", "sccm")."</td><td>";
267418
echo Html::input(

0 commit comments

Comments
 (0)