Skip to content

Commit 8fa1344

Browse files
committed
fix(itil): Readonly logic is now only applied on form controller not on all add/update logic
1 parent 17f19a9 commit 8fa1344

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

front/change.form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
if (isset($_POST["add"])) {
5454
$change->check(-1, CREATE, $_POST);
5555

56+
$_POST = $change->enforceReadonlyFields($_POST, true);
5657
$newID = $change->add($_POST);
5758
Event::log(
5859
$newID,
@@ -109,6 +110,7 @@
109110
} elseif (isset($_POST["update"])) {
110111
$change->check($_POST["id"], UPDATE);
111112

113+
$_POST = $change->enforceReadonlyFields($_POST);
112114
$change->update($_POST);
113115
Event::log(
114116
$_POST["id"],

front/problem.form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
if (isset($_POST["add"])) {
5454
$problem->check(-1, CREATE, $_POST);
5555

56+
$_POST = $problem->enforceReadonlyFields($_POST, true);
5657
if ($newID = $problem->add($_POST)) {
5758
Event::log(
5859
$newID,
@@ -108,6 +109,7 @@
108109
} elseif (isset($_POST["update"])) {
109110
$problem->check($_POST["id"], UPDATE);
110111

112+
$_POST = $problem->enforceReadonlyFields($_POST);
111113
$problem->update($_POST);
112114
Event::log(
113115
$_POST["id"],

front/ticket.form.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474

7575
if (isset($_POST["add"])) {
7676
$track->check(-1, CREATE, $_POST);
77+
$_POST = $track->enforceReadonlyFields($_POST, true);
7778

7879
if ($track->add($_POST)) {
7980
if ($_SESSION['glpibackcreated']) {
@@ -85,6 +86,7 @@
8586
if (!$track::canUpdate()) {
8687
throw new AccessDeniedHttpException();
8788
}
89+
$_POST = $track->enforceReadonlyFields($_POST);
8890
$track->update($_POST);
8991

9092
if (isset($_POST['kb_linked_id'])) {

src/CommonITILObject.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,8 +1770,6 @@ public function cleanDBonPurge()
17701770
protected function handleTemplateFields(array $input, bool $show_error_message = true)
17711771
{
17721772
//// check mandatory fields
1773-
// First get ticket template associated: entity and type/category
1774-
$tt = $this->getITILTemplateFromInput($input);
17751773

17761774
$check_allowed_fields_for_template = false;
17771775
$allowed_fields = [];
@@ -1858,7 +1856,9 @@ class_exists($validation_class)
18581856
}
18591857
}
18601858

1861-
if (count($tt->mandatory)) {
1859+
// First get ticket template associated: entity and type/category
1860+
$tt = $this->getITILTemplateFromInput($input);
1861+
if ($tt && count($tt->mandatory)) {
18621862
$mandatory_missing = [];
18631863
$fieldsname = $tt->getAllowedFieldsNames(true);
18641864
foreach ($tt->mandatory as $key => $val) {
@@ -1967,7 +1967,6 @@ public function prepareInputForUpdate($input)
19671967
if (!$this->checkFieldsConsistency($input)) {
19681968
return false;
19691969
}
1970-
$input = $this->handleReadonlyFields($input);
19711970

19721971
// Add document if needed
19731972
$this->getFromDB($input["id"]); // entities_id field required
@@ -2332,9 +2331,25 @@ public function prepareInputForUpdate($input)
23322331

23332332
return $input;
23342333
}
2335-
private function handleReadonlyFields(array $input, bool $isAdd = false): array
2334+
2335+
/**
2336+
* Processes readonly fields in the input array based on the ITIL template data.
2337+
*
2338+
* @param array $input The user input data to process (often $_POST).
2339+
* @param bool $isAdd true if we are in a creation, will force to apply the template predefined field.
2340+
*
2341+
* @return array The modified user input array after processing readonly fields.
2342+
*
2343+
* @since 11.0.2
2344+
*/
2345+
public function enforceReadonlyFields(array $input, bool $isAdd = false): array
23362346
{
23372347
$tt = $this->getITILTemplateFromInput($input);
2348+
if (!$tt) {
2349+
dump('Template not found');
2350+
return $input;
2351+
}
2352+
23382353
$tt->getFromDBWithData($tt->getID()); // We load the fields (predefined and readonly)
23392354

23402355
foreach (array_keys($tt->readonly) as $read_only_field) {
@@ -2824,7 +2839,6 @@ public function prepareInputForAdd($input)
28242839
if (!$this->checkFieldsConsistency($input)) {
28252840
return false;
28262841
}
2827-
$input = $this->handleReadonlyFields($input, true);
28282842

28292843
$input = $this->transformActorsInput($input);
28302844

@@ -8269,13 +8283,16 @@ public function getITILTemplateToUse(
82698283
* If the input is not defined, it will get it from the object fields datas
82708284
*
82718285
* @param array $input
8272-
* @return ITILTemplate
8286+
* @return ITILTemplate|null
82738287
*
82748288
* @since 11.0.2
82758289
*/
8276-
public function getITILTemplateFromInput(array $input = []): ITILTemplate
8290+
public function getITILTemplateFromInput(array $input = []): ?ITILTemplate
82778291
{
8278-
$entid = $input['entities_id'] ?? $this->fields['entities_id'];
8292+
$entid = $input['entities_id'] ?? $this->fields['entities_id'] ?? $input['id'] ?? null;
8293+
if (is_null($entid)) {
8294+
return null;
8295+
}
82798296

82808297
$type = null;
82818298
if (isset($input['type'])) {
@@ -8285,6 +8302,9 @@ public function getITILTemplateFromInput(array $input = []): ITILTemplate
82858302
}
82868303

82878304
$categid = $input['itilcategories_id'] ?? $this->fields['itilcategories_id'];
8305+
if (is_null($categid)) {
8306+
return null;
8307+
}
82888308
return $this->getITILTemplateToUse(0, $type, $categid, $entid);
82898309
}
82908310

tests/src/AbstractITILTemplateReadonlyFieldTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testHandleReadonlyFieldsOnAddWithPredefined(): void
165165
$input['type'] = Ticket::INCIDENT_TYPE;
166166
}
167167

168-
$processed_input = $itil_object->prepareInputForAdd($input);
168+
$processed_input = $itil_object->enforceReadonlyFields($input, true);
169169

170170
$this->assertEquals(Urgency::HIGH->value, $processed_input['urgency']);
171171
$this->assertEquals('Some content', $processed_input['name']);
@@ -187,9 +187,9 @@ public function testHandleReadonlyFieldsOnAddWithoutPredefined(): void
187187
$input['type'] = Ticket::INCIDENT_TYPE;
188188
}
189189

190-
$processed_input = $itil_object->prepareInputForAdd($input);
190+
$processed_input = $itil_object->enforceReadonlyFields($input, true);
191191

192-
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']); // Default value
192+
$this->assertArrayNotHasKey('urgency', $processed_input); // Default value
193193
$this->assertEquals('Some content', $processed_input['name']);
194194
}
195195

@@ -224,7 +224,7 @@ public function testHandleReadonlyFieldsOnUpdateWithExistingValue(): void
224224
$update_input['type'] = Ticket::INCIDENT_TYPE;
225225
}
226226

227-
$processed_input = $itil_object->prepareInputForUpdate($update_input);
227+
$processed_input = $itil_object->enforceReadonlyFields($update_input);
228228

229229
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']);
230230
$this->assertEquals('Updated content', $processed_input['name']);
@@ -261,7 +261,7 @@ public function testHandleReadonlyFieldsOnUpdateWithoutExistingValue(): void
261261
$update_input['type'] = Ticket::INCIDENT_TYPE;
262262
}
263263

264-
$processed_input = $itil_object->prepareInputForUpdate($update_input);
264+
$processed_input = $itil_object->enforceReadonlyFields($update_input);
265265

266266
$this->assertEquals(Urgency::MEDIUM->value, $processed_input['urgency']); // Default value
267267
$this->assertEquals('Updated content', $processed_input['name']);

0 commit comments

Comments
 (0)