Skip to content

Commit

Permalink
Fix autoload
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Jan 30, 2025
1 parent 99934f8 commit 47d641b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,23 @@ public function bootstrapDefinition(AbstractDefinition $definition): void
*/
public function autoloadClass(string $classname): void
{
$ns = self::getDefinitionClass()::getCustomObjectNamespace() . '\\';
$definition_class = self::getDefinitionClass();
$ns = $definition_class::getCustomObjectNamespace() . '\\';

if (!\str_starts_with($classname, $ns)) {
return;
}

$system_name_pattern = $definition_class::SYSTEM_NAME_PATTERN;

$patterns = [
'/^' . preg_quote($ns, '/') . 'RuleDictionary([A-Za-z]+)ModelCollection$/' => 'loadConcreteModelDictionaryCollectionClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary([A-Za-z]+)TypeCollection$/' => 'loadConcreteTypeDictionaryCollectionClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary([A-Za-z]+)Model$/' => 'loadConcreteModelDictionaryClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary([A-Za-z]+)Type$/' => 'loadConcreteTypeDictionaryClass',
'/^' . preg_quote($ns, '/') . '([A-Za-z]+)Model$/' => 'loadConcreteModelClass',
'/^' . preg_quote($ns, '/') . '([A-Za-z]+)Type$/' => 'loadConcreteTypeClass',
'/^' . preg_quote($ns, '/') . '([A-Za-z]+)$/' => 'loadConcreteClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary(' . $system_name_pattern . ')ModelCollection$/' => 'loadConcreteModelDictionaryCollectionClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary(' . $system_name_pattern . ')TypeCollection$/' => 'loadConcreteTypeDictionaryCollectionClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary(' . $system_name_pattern . ')Model$/' => 'loadConcreteModelDictionaryClass',
'/^' . preg_quote($ns, '/') . 'RuleDictionary(' . $system_name_pattern . ')Type$/' => 'loadConcreteTypeDictionaryClass',
'/^' . preg_quote($ns, '/') . '(' . $system_name_pattern . ')Model$/' => 'loadConcreteModelClass',
'/^' . preg_quote($ns, '/') . '(' . $system_name_pattern . ')Type$/' => 'loadConcreteTypeClass',
'/^' . preg_quote($ns, '/') . '(' . $system_name_pattern . ')$/' => 'loadConcreteClass',
];

foreach ($patterns as $pattern => $load_function) {
Expand Down
18 changes: 12 additions & 6 deletions src/Glpi/CustomObject/AbstractDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use Gettext\Languages\CldrData as Language_CldrData;
use Gettext\Languages\Language;
use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\AssetDefinition;
use Glpi\Asset\CustomFieldDefinition;
use Profile;
use ProfileRight;
Expand All @@ -52,6 +51,14 @@
*/
abstract class AbstractDefinition extends CommonDBTM
{
/**
* System name regex pattern.
*
* 1. Must start with a letter.
* 2. Must contain only letters or numbers.
*/
public const SYSTEM_NAME_PATTERN = '[A-Za-z][A-Za-z0-9]*';

public static $rightname = 'config';

/**
Expand Down Expand Up @@ -434,11 +441,10 @@ protected function prepareInput(array $input): array|bool
$has_errors = false;

if (array_key_exists('system_name', $input)) {
// 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) {
if (
!is_string($input['system_name'])
|| preg_match('/^' . self::SYSTEM_NAME_PATTERN . '$/', $input['system_name']) !== 1
) {
Session::addMessageAfterRedirect(
htmlescape(sprintf(
__('The following field has an incorrect value: "%s".'),
Expand Down
5 changes: 3 additions & 2 deletions src/Glpi/Dropdown/DropdownDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ public function getReservedSystemNames(): array

public function autoloadClass(string $classname): void
{
$ns = static::getDefinitionClass()::getCustomObjectNamespace() . '\\';
$definition_class = self::getDefinitionClass();
$ns = $definition_class::getCustomObjectNamespace() . '\\';

if (!\str_starts_with($classname, $ns)) {
return;
}

$pattern = '/^' . preg_quote($ns, '/') . '([A-Za-z]+)$/';
$pattern = '/^' . preg_quote($ns, '/') . '(' . $definition_class::SYSTEM_NAME_PATTERN . ')$/';

if (preg_match($pattern, $classname) === 1) {
$system_name = preg_replace($pattern, '$1', $classname);
Expand Down
3 changes: 2 additions & 1 deletion templates/pages/admin/customobjects/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@
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());
const system_name = $('#mainformtable input[name="system_name"]').val().toLowerCase();
const system_name_pattern = /^{{ constant('Glpi\\CustomObject\\AbstractDefinition::SYSTEM_NAME_PATTERN')|raw }}$/;
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) {
} else if (system_name_pattern.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 {
Expand Down

0 comments on commit 47d641b

Please sign in to comment.