Skip to content

Commit 3e3a62c

Browse files
authored
fix(model): getting models created on parent entites on child entites (#600)
* fix(model): getting models created on parent entites on child entites * building the models query with an array to use the GLPI DB iterator * refactoring the db query
1 parent 09b2fe9 commit 3e3a62c

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fix crash when injection model has no mandatory fields defined
13+
- Fix models created on parent entities can't be used on child entites
1314

1415
## [2.15.4] - 2026-03-16
1516

inc/model.class.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -382,29 +382,52 @@ public static function getModels($user_id, $order = "name", $entity = -1, $all =
382382
global $DB;
383383

384384
$models = [];
385-
$query = "SELECT `id`, `name`, `is_private`, `entities_id`, `is_recursive`, `itemtype`,
386-
`step`, `comment`
387-
FROM `glpi_plugin_datainjection_models` ";
388385

386+
$where = [];
389387
if (!$all) {
390-
$query .= " WHERE `step` = '" . self::READY_TO_USE_STEP . "' AND (";
391-
} else {
392-
$query .= " WHERE (";
388+
$where['step'] = self::READY_TO_USE_STEP;
393389
}
394390

395-
$query .= "(`is_private` = '" . self::MODEL_PUBLIC . "'" .
396-
getEntitiesRestrictRequest(
397-
" AND",
398-
"glpi_plugin_datainjection_models",
399-
"entities_id",
400-
$entity,
401-
false,
402-
) . ")
403-
OR (`is_private` = '" . self::MODEL_PRIVATE . "' AND `users_id` = '$user_id'))
404-
ORDER BY `is_private` DESC,
405-
`entities_id`, " . ($order == "`name`" ? "`name`" : $order);
406-
407-
foreach ($DB->doQuery($query) as $data) {
391+
$restrict = getEntitiesRestrictCriteria(
392+
'glpi_plugin_datainjection_models',
393+
'entities_id',
394+
$entity,
395+
true,
396+
);
397+
398+
$where[] = [
399+
'OR' => [
400+
[
401+
'is_private' => self::MODEL_PUBLIC,
402+
] + $restrict,
403+
[
404+
'is_private' => self::MODEL_PRIVATE,
405+
'users_id' => $user_id,
406+
],
407+
],
408+
];
409+
410+
$query = [
411+
'SELECT' => [
412+
'id',
413+
'name',
414+
'is_private',
415+
'entities_id',
416+
'is_recursive',
417+
'itemtype',
418+
'step',
419+
'comment',
420+
],
421+
'FROM' => 'glpi_plugin_datainjection_models',
422+
'WHERE' => $where,
423+
'ORDER' => [
424+
'is_private DESC',
425+
'entities_id',
426+
$order == "`name`" ? "name" : $order,
427+
],
428+
];
429+
430+
foreach ($DB->request($query) as $data) {
408431
if (
409432
self::checkRightOnModel($data['id'])
410433
&& class_exists($data['itemtype'])

0 commit comments

Comments
 (0)