Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow numbers in custom objects system names #18802

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3295,12 +3295,6 @@
'count' => 1,
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 2 on array\\{0\\: string, 1\\: string, 2\\: string, 3\\: numeric\\-string, 4\\?\\: string, 5\\?\\: non\\-empty\\-string\\} in isset\\(\\) always exists and is not nullable\\.$#',
'identifier' => 'isset.offset',
'count' => 1,
'path' => __DIR__ . '/src/Glpi/Search/Provider/SQLProvider.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 2 on array\\{string, string, string\\} in isset\\(\\) always exists and is not nullable\\.$#',
'identifier' => 'isset.offset',
Expand Down
14 changes: 14 additions & 0 deletions phpunit/functional/Glpi/Asset/AssetDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public static function addInputProvider(): iterable
if (
($char >= "A" && $char <= "Z") // A -> Z
|| ($char >= "a" && $char <= "z") // a -> z
|| ($char >= "0" && $char <= "9") // 0 -> 9
) {
yield [
'input' => [
Expand Down Expand Up @@ -335,6 +336,19 @@ public static function addInputProvider(): iterable
];
}

// System name must not start with a number
yield [
'input' => [
'system_name' => '3DPrinter',
],
'output' => false,
'messages' => [
ERROR => [
'The following field has an incorrect value: &quot;System name&quot;.',
],
],
];

// System name must not end with `Model` suffix
yield [
'input' => [
Expand Down
14 changes: 14 additions & 0 deletions phpunit/functional/Glpi/Dropdown/DropdownDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public static function addInputProvider(): iterable
if (
($char >= "A" && $char <= "Z") // A -> Z
|| ($char >= "a" && $char <= "z") // a -> z
|| ($char >= "0" && $char <= "9") // 0 -> 9
) {
yield [
'input' => [
Expand Down Expand Up @@ -280,6 +281,19 @@ public static function addInputProvider(): iterable
];
}

// System name must not start with a number
yield [
'input' => [
'system_name' => '7Test',
],
'output' => false,
'messages' => [
ERROR => [
'The following field has an incorrect value: &quot;System name&quot;.',
],
],
];

// System name must not end with `Model` suffix
yield [
'input' => [
Expand Down
6 changes: 5 additions & 1 deletion src/Glpi/CustomObject/AbstractDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ protected function prepareInput(array $input): array|bool
$has_errors = false;

if (array_key_exists('system_name', $input)) {
if (!is_string($input['system_name']) || preg_match('/^[a-z]+$/i', $input['system_name']) !== 1) {
// 1. Must start with a letter.
// 2. Must contain only letters or numbers.
$system_name_pattern = '/^[a-z][a-z0-9]*$/i';

if (!is_string($input['system_name']) || preg_match($system_name_pattern, $input['system_name']) !== 1) {
Session::addMessageAfterRedirect(
htmlescape(sprintf(
__('The following field has an incorrect value: "%s".'),
Expand Down
13 changes: 4 additions & 9 deletions src/Glpi/Search/Provider/SQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4789,15 +4789,10 @@ public static function constructData(array &$data, $onlycount = false)

// Parse data
foreach ($newrow['raw'] as $key => $val) {
if (preg_match('/ITEM(_(\w[^\d]+))?_(\d+)(_(.+))?/', $key, $matches)) {
$j = $matches[3];
if (isset($matches[2]) && !empty($matches[2])) {
$j = $matches[2] . '_' . $matches[3];
}
$fieldname = 'name';
if (isset($matches[5])) {
$fieldname = $matches[5];
}
$matches = [];
if (preg_match('/^ITEM(_(?<itemtype>[a-z][\w\\\]*))?_(?<num>\d+)(_(?<fieldname>.+))?$/i', $key, $matches)) {
$j = (!empty($matches['itemtype']) ? $matches['itemtype'] . '_' : '') . $matches['num'];
$fieldname = $matches['fieldname'] ?? 'name';

// No Group_concat case
if ($fieldname == 'content' || !is_string($val) || strpos($val, \Search::LONGSEP) === false) {
Expand Down
24 changes: 16 additions & 8 deletions templates/pages/admin/customobjects/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,38 @@
</script>
{% endif %}
<script type="module">
$('#mainformtable input[name="system_name"]').on('input', () => {
$('#mainformtable input[name="system_name"]').on('change', () => {
if ($('input[name="system_name"]').val() === '') {
$('input[name="system_name"]').data('manually_changed', false);
} else {
$('input[name="system_name"]').data('manually_changed', true);
}
});
function autoUpdateNameField() {
if ({{ not item.isNewItem() ? 'true' : 'false' }} || $('#mainformtable input[name="system_name"]').data('manually_changed')) {
return;
}
$('#mainformtable input[name="system_name"]').on('input', () => {
const reserved_names = {{ reserved_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
const existing_names = {{ existing_system_names|json_encode()|raw }}.map((n) => n.toLowerCase());
$('#mainformtable input[name="system_name"]').val(
$('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z]/gi, '')
);
const system_name = $('#mainformtable input[name="system_name"]').val().toLowerCase();

if (reserved_names.includes(system_name) || system_name.endsWith('type') || system_name.endsWith('model')) {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is a reserved name. Please enter a different label or manually change the system name.'));
} else if (existing_names.includes(system_name)) {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is already in use. Please enter a different label or manually change the system name.'));
} else if (/^[a-z][a-z0-9]*$/i.test(system_name) === false) {
{# See pattern used in \Glpi\CustomObject\AbstractDefinition::prepareInput() #}
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity(__('The system name is invalid. It must start with a letter and contain only alphanumeric chars.'));
} else {
$('#mainformtable input[name="system_name"]').get(0).setCustomValidity('');
}
});
function autoUpdateNameField() {
if ({{ not item.isNewItem() ? 'true' : 'false' }} || $('#mainformtable input[name="system_name"]').data('manually_changed')) {
return;
}

$('#mainformtable input[name="system_name"]').val(
$('#mainformtable input[name="label"]').val().normalize('NFD').replace(/[^a-z0-9]/gi, '')
);
$('#mainformtable input[name="system_name"]').trigger('input');
};
$('#mainformtable input[name="label"]').on('input', () => {
autoUpdateNameField();
Expand Down
Loading