Skip to content

Commit 6a34afe

Browse files
stonebuzztrashercedric-anne
authored
Fix(Core): Add entities_id and is_recursive fields to correctly filter data from the API (#858)
* Fix(Core): check UPDATE / VIEW right from API * fix CS * check for access entity and prevent purge from API * fix purge * fix * move back to CommonDBTM * remove useless data * fix CS * another try * Move to CommonDBChild * fix * fix * move to sql instead of object * fix * fix CS * Update inc/abstractcontainerinstance.class.php Co-authored-by: Johan Cwiklinski <trasher@x-tnd.be> * revert * can't attach because of 'itemtype' fk field * add check Co-authored-by: Cédric Anne <cedric.anne@gmail.com> * add check Co-authored-by: Cédric Anne <cedric.anne@gmail.com> * Adapt changelog --------- Co-authored-by: Johan Cwiklinski <trasher@x-tnd.be> Co-authored-by: Cédric Anne <cedric.anne@gmail.com>
1 parent 68065cf commit 6a34afe

File tree

3 files changed

+166
-3
lines changed

3 files changed

+166
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [UNRELAESE]
9+
10+
### Fixed
11+
12+
- Fix `container` to prevent calls from `API` returning full container data
13+
814
## [1.21.15] - 2024-10-09
915

1016
### Fixed

inc/abstractcontainerinstance.class.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,56 @@
2828
* -------------------------------------------------------------------------
2929
*/
3030

31-
abstract class PluginFieldsAbstractContainerInstance extends CommonDBTM
31+
abstract class PluginFieldsAbstractContainerInstance extends CommonDBChild
3232
{
33+
public static $itemtype = 'itemtype';
34+
public static $items_id = 'items_id';
35+
36+
public static $mustBeAttached = false;
37+
38+
/**
39+
* This function relies on the static property `static::$plugins_forward_entity`,
40+
* which should be populated using the following method (from setup):
41+
*
42+
* Plugin::registerClass(
43+
* PluginFields<Itemtype><name>,
44+
* ['forwardentityfrom' => <Itemtype>]
45+
* );
46+
*
47+
* However, the order in which plugins are loaded can affect the behavior.
48+
* For example, if a container is defined on a `GenericObject` itemtype and
49+
* the `fields` plugin initializes before the `genericobject` plugin, the
50+
* `itemtype` for the container will not yet exist, leading to potential issues.
51+
*
52+
* Modification of this function to meet specific requirements.
53+
*/
54+
public function addNeededInfoToInput($input)
55+
{
56+
if ($this->tryEntityForwarding()) {
57+
$completeinput = array_merge($this->fields, $input);
58+
if (
59+
$itemToGetEntity = static::getItemFromArray(
60+
static::$itemtype,
61+
static::$items_id,
62+
$completeinput,
63+
)
64+
) {
65+
if (
66+
($itemToGetEntity instanceof CommonDBTM)
67+
) {
68+
if ($itemToGetEntity->isEntityAssign()) {
69+
$input['entities_id'] = $itemToGetEntity->getEntityID();
70+
}
71+
72+
if ($itemToGetEntity->maybeRecursive()) {
73+
$input['is_recursive'] = intval($itemToGetEntity->isRecursive());
74+
}
75+
}
76+
}
77+
}
78+
return $input;
79+
}
80+
3381
public static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = [])
3482
{
3583
if (!is_array($values)) {

templates/container.class.tpl

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance
1313
1414
$obj = new self();
1515
$table = $obj->getTable();
16+
$migration = new PluginFieldsMigration(0);
1617
1718
// create Table
1819
if (!$DB->tableExists($table)) {
@@ -32,10 +33,8 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance
3233
$result = $DB->query("SHOW COLUMNS FROM `$table`");
3334
if ($result && $DB->numrows($result) > 0) {
3435
$changed = false;
35-
$migration = new PluginFieldsMigration(0);
3636
while ($data = $DB->fetchAssoc($result)) {
3737
if (str_starts_with($data['Field'], 'itemtype_') && $data['Null'] !== 'YES') {
38-
Toolbox::logDebug($data);
3938
$migration->changeField($table, $data['Field'], $data['Field'], "varchar(100) DEFAULT NULL");
4039
$changed = true;
4140
}
@@ -45,6 +44,116 @@ class %%CLASSNAME%% extends PluginFieldsAbstractContainerInstance
4544
}
4645
}
4746
}
47+
48+
/**
49+
* Adds the 'entities_id' field to the database table and migrates existing data.
50+
*
51+
* This block ensures that the 'entities_id' field is created and populated if it
52+
* associated item type requires entity assignment
53+
*/
54+
if (getItemForItemtype("%%ITEMTYPE%%")->isEntityAssign() && !$DB->fieldExists($table, 'entities_id')) {
55+
$migration->addField($table, 'entities_id', 'fkey', ['after' => 'plugin_fields_containers_id']);
56+
$migration->addKey($table, 'entities_id');
57+
$migration->executeMigration();
58+
59+
// migrate data
60+
$query = $DB->buildUpdate(
61+
$table,
62+
['entities_id' => new QueryParam()],
63+
['id' => new QueryParam()]
64+
);
65+
$stmt = $DB->prepare($query);
66+
67+
//load all entries
68+
$data = $DB->request(
69+
[
70+
'SELECT' => '*',
71+
'FROM' => $table,
72+
]
73+
);
74+
75+
foreach ($data as $fields) {
76+
//load related item
77+
$related_item = $DB->request(
78+
[
79+
'SELECT' => '*',
80+
'FROM' => getTableForItemType($fields['itemtype']),
81+
'WHERE' => [
82+
'id' => $fields['items_id'],
83+
]
84+
]
85+
)->current();
86+
87+
if ($related_item === null) {
88+
continue;
89+
}
90+
91+
//update if needed
92+
if ($fields['entities_id'] != $related_item['entities_id']) {
93+
$stmt->bind_param(
94+
'ii',
95+
$related_item['entities_id'],
96+
$fields['id']
97+
);
98+
$stmt->execute();
99+
}
100+
}
101+
}
102+
103+
/**
104+
* Adds the 'is_recursive' field to the database table and migrates existing data.
105+
*
106+
* This block ensures that the 'is_recursive' field is created and populated if it
107+
* associated item type requires recursive assignment
108+
*/
109+
if (getItemForItemtype("%%ITEMTYPE%%")->maybeRecursive() && !$DB->fieldExists($table, 'is_recursive')) {
110+
$migration->addField($table, 'is_recursive', 'bool', ['after' => 'entities_id']);
111+
$migration->addKey($table, 'is_recursive');
112+
$migration->executeMigration();
113+
114+
//migrate data
115+
$query = $DB->buildUpdate(
116+
$table,
117+
['is_recursive' => new QueryParam()],
118+
['id' => new QueryParam()]
119+
);
120+
$stmt = $DB->prepare($query);
121+
122+
//load all entries
123+
$data = $DB->request(
124+
[
125+
'SELECT' => '*',
126+
'FROM' => $table,
127+
]
128+
);
129+
130+
foreach ($data as $fields) {
131+
//load related item
132+
$related_item = $DB->request(
133+
[
134+
'SELECT' => '*',
135+
'FROM' => getTableForItemType($fields['itemtype']),
136+
'WHERE' => [
137+
'id' => $fields['items_id'],
138+
]
139+
]
140+
)->current();
141+
142+
if ($related_item === null) {
143+
continue;
144+
}
145+
146+
//update if needed
147+
if ($fields['is_recursive'] != $related_item['is_recursive']) {
148+
$stmt->bind_param(
149+
'ii',
150+
$related_item['is_recursive'],
151+
$fields['id']
152+
);
153+
$stmt->execute();
154+
}
155+
}
156+
}
48157
}
49158

50159
static function uninstall() {

0 commit comments

Comments
 (0)