From 4872c19a9c4b3c5440a83d7a4d437a0f34cd2f40 Mon Sep 17 00:00:00 2001 From: Omkar Pednekar Date: Sun, 6 Jun 2021 16:14:26 +0530 Subject: [PATCH] DEG-152: Added machine name validation. --- src/BaseEntityGenerate.php | 35 +++++++++++++++++++++++++++++--- src/Commands/ContentType.php | 3 +++ src/Commands/CustomBlockType.php | 3 +++ src/Commands/ImageStyle.php | 3 +++ src/Commands/Media.php | 3 +++ src/Commands/Menu.php | 3 +++ src/Commands/Paragraph.php | 3 +++ src/Commands/UserRole.php | 3 +++ src/Commands/Vocabulary.php | 3 +++ src/Commands/Workflow.php | 3 +++ src/Services/GeneralApi.php | 26 ++++++++++++++++++++++++ 11 files changed, 85 insertions(+), 3 deletions(-) diff --git a/src/BaseEntityGenerate.php b/src/BaseEntityGenerate.php index db6a62f..c6678a8 100644 --- a/src/BaseEntityGenerate.php +++ b/src/BaseEntityGenerate.php @@ -342,12 +342,15 @@ public function updateEntityType(EntityInterface $entity_type, array $data) { /** * Function to check data having required fields to create entity. * - * @param $data + * @param array $data * Entity data from sheet. + * @param string $type + * Entity type. + * * @return bool * Return status based on fields availability. */ - public function requiredFieldsCheck($data, $type = 'Entity type') { + public function requiredFieldsCheck(array $data, string $type = 'Entity type') { $missing_fields = []; foreach ($this->requiredFields as $requiredField) { if (array_key_exists($requiredField, $data) && $data[$requiredField] == '') { @@ -355,7 +358,7 @@ public function requiredFieldsCheck($data, $type = 'Entity type') { } } if (!empty($missing_fields)) { - $this->io()->warning("$type can not be created with empty " . implode(', ', $missing_fields) .". Skipping creation..."); + $this->io()->warning("$type can not be created with empty " . implode(', ', $missing_fields) . ". Skipping creation..."); $status = FALSE; } else { @@ -364,4 +367,30 @@ public function requiredFieldsCheck($data, $type = 'Entity type') { return $status; } + /** + * Helper function to validate machine name. + * + * @param string $machine_name + * Machine name string to validate. + * @param int $length + * Expected maximum length. + * @param string $separator + * Separator to match in string. + * + * @return bool + * Returns true or false based on matching result. + */ + public function validateMachineName(string $machine_name, int $length = 32, string $separator = '_') { + $result = FALSE; + $pattern = "/^[a-z0-9$separator]+$/"; + if (strlen($machine_name) <= $length && preg_match($pattern, $machine_name)) { + $result = TRUE; + } + else { + $message = "The machine-readable name must contain only lowercase letters, numbers, and underscores with maximum length of $length. Skipping bundle creation with machine name $machine_name"; + $this->io()->warning($message); + } + return $result; + } + } diff --git a/src/Commands/ContentType.php b/src/Commands/ContentType.php index 01b827f..85b1ac4 100644 --- a/src/Commands/ContentType.php +++ b/src/Commands/ContentType.php @@ -152,6 +152,9 @@ private function getNodeTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Content type')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $node = []; $node['name'] = $item['name']; $node['type'] = $item['machine_name']; diff --git a/src/Commands/CustomBlockType.php b/src/Commands/CustomBlockType.php index a5f0c68..0969402 100644 --- a/src/Commands/CustomBlockType.php +++ b/src/Commands/CustomBlockType.php @@ -130,6 +130,9 @@ private function getCustomBlockTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Custom block type')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $block_content = []; $block_content['label'] = $item['name']; $block_content['id'] = $item['machine_name']; diff --git a/src/Commands/ImageStyle.php b/src/Commands/ImageStyle.php index 0c10c1d..6d5a32f 100644 --- a/src/Commands/ImageStyle.php +++ b/src/Commands/ImageStyle.php @@ -127,6 +127,9 @@ private function getImageStyleData(array $styles, array $effects) { if (!$this->requiredFieldsCheck($item, 'Image style')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $image_style = []; $image_style['label'] = $item['style_name']; $image_style['name'] = $item['machine_name']; diff --git a/src/Commands/Media.php b/src/Commands/Media.php index 602b9c0..358e8f1 100644 --- a/src/Commands/Media.php +++ b/src/Commands/Media.php @@ -202,6 +202,9 @@ private function getMediaTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Media type')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $source_option = $item['settings/notes']; if (!\in_array($source_option, $source_options)) { $implode_source_options = \implode(',', $source_options); diff --git a/src/Commands/Menu.php b/src/Commands/Menu.php index a4da353..4968200 100644 --- a/src/Commands/Menu.php +++ b/src/Commands/Menu.php @@ -105,6 +105,9 @@ private function getMenuData(array $data) { if (!$this->requiredFieldsCheck($item, 'Menu')) { continue; } + if (!$this->validateMachineName($item['machine_name'], 32, '-_')) { + continue; + } $menu = []; $description = isset($item['description']) ? $item['description'] : $item['name'] . ' menu.'; $menu['id'] = str_replace('_', '-', $item['machine_name']); diff --git a/src/Commands/Paragraph.php b/src/Commands/Paragraph.php index 61a7550..b06e857 100644 --- a/src/Commands/Paragraph.php +++ b/src/Commands/Paragraph.php @@ -129,6 +129,9 @@ private function getParagraphTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Paragraph type')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $paragraph = []; $paragraph['label'] = $item['name']; $paragraph['id'] = $item['machine_name']; diff --git a/src/Commands/UserRole.php b/src/Commands/UserRole.php index 5f23e00..ad335be 100644 --- a/src/Commands/UserRole.php +++ b/src/Commands/UserRole.php @@ -105,6 +105,9 @@ private function getUserRoleData(array $data) { if (!$this->requiredFieldsCheck($item, 'User role')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $user_role = []; $user_role['id'] = $item['machine_name']; $user_role['label'] = $item['name']; diff --git a/src/Commands/Vocabulary.php b/src/Commands/Vocabulary.php index 4e35199..fa11e90 100644 --- a/src/Commands/Vocabulary.php +++ b/src/Commands/Vocabulary.php @@ -133,6 +133,9 @@ private function getVocabTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Vocabulary')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $vocabs = []; $description = isset($item['description']) ? $item['description'] : $item['name'] . ' vocabulary.'; $vocabs['vid'] = $item['machine_name']; diff --git a/src/Commands/Workflow.php b/src/Commands/Workflow.php index 43fe94c..27a3543 100644 --- a/src/Commands/Workflow.php +++ b/src/Commands/Workflow.php @@ -168,6 +168,9 @@ private function getWorkflowTypeData(array $data) { if (!$this->requiredFieldsCheck($item, 'Workflow')) { continue; } + if (!$this->validateMachineName($item['machine_name'])) { + continue; + } $workflow = []; $workflow['label'] = $item['label']; $workflow['id'] = $item['machine_name']; diff --git a/src/Services/GeneralApi.php b/src/Services/GeneralApi.php index 9cb3fc1..ec969b5 100644 --- a/src/Services/GeneralApi.php +++ b/src/Services/GeneralApi.php @@ -452,6 +452,12 @@ public function generateEntityFields(string $bundle_type, array $fields_data, ar $bundleVal = ''; $bundle = $field['bundle']; $field_machine_name = $field['machine_name']; + if (!$this->validateMachineName($field_machine_name)) { + $message = $this->t("The machine-readable name must contain only lowercase letters, numbers, and underscores with maximum length of 32. Skipping bundle creation with machine name @machine_name", + ['@machine_name' => $field_machine_name]); + $this->logger->warning($message); + continue; + } $bundle_name = trim(substr($bundle, 0, strpos($bundle, "("))); if (array_key_exists($bundle_name, $bundles_data)) { $bundleVal = $bundles_data[$bundle_name]; @@ -667,4 +673,24 @@ public function fieldDependencyCheck(array $field_meta, array $field) { return $field; } + /** + * Helper function to validate machine name. + * + * @param string $machine_name + * Machine name string to validate. + * @param int $length + * Expected maximum length. + * + * @return bool + * Returns true or false based on matching result. + */ + public function validateMachineName(string $machine_name, int $length = 32) { + $result = FALSE; + $pattern = "/^[a-z0-9_]+$/"; + if (strlen($machine_name) <= $length && preg_match($pattern, $machine_name)) { + $result = TRUE; + } + return $result; + } + }