From 3d05606ad6f2d4dca72136dd3ed4717ea2dea0b1 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 21 Jul 2020 14:51:02 +0300 Subject: [PATCH] formula functions changes --- .../Core/Formula/Functions/BaseFunction.php | 2 +- .../Functions/EnvGroup/UserAttributeType.php | 17 +++++--- .../ExtGroup/EmailGroup/ApplyTemplateType.php | 41 +++++++++++------- .../ExtGroup/EmailGroup/SendType.php | 30 ++++++++----- .../ExtGroup/PdfGroup/GenerateType.php | 43 +++++++++++-------- .../Functions/PasswordGroup/GenerateType.php | 13 ++++-- .../Functions/PasswordGroup/HashType.php | 15 ++++--- .../Espo/Core/Formula/FormulaTest.php | 11 ++++- 8 files changed, 109 insertions(+), 63 deletions(-) diff --git a/application/Espo/Core/Formula/Functions/BaseFunction.php b/application/Espo/Core/Formula/Functions/BaseFunction.php index cf6adf4088..36b342258d 100644 --- a/application/Espo/Core/Formula/Functions/BaseFunction.php +++ b/application/Espo/Core/Formula/Functions/BaseFunction.php @@ -159,6 +159,6 @@ abstract class BaseFunction protected function log(string $msg, string $level = 'notice') { if (!$this->log) return; - $this->log->log($level, 'function: ' . $this->name . ', ' . $msg); + $this->log->log($level, 'Formula function: ' . $this->name . ', ' . $msg); } } diff --git a/application/Espo/Core/Formula/Functions/EnvGroup/UserAttributeType.php b/application/Espo/Core/Formula/Functions/EnvGroup/UserAttributeType.php index d877a71700..cbf6b62714 100644 --- a/application/Espo/Core/Formula/Functions/EnvGroup/UserAttributeType.php +++ b/application/Espo/Core/Formula/Functions/EnvGroup/UserAttributeType.php @@ -29,25 +29,28 @@ namespace Espo\Core\Formula\Functions\EnvGroup; -use Espo\Core\Exceptions\Error; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; use Espo\Core\Di; -class UserAttributeType extends \Espo\Core\Formula\Functions\AttributeType implements +class UserAttributeType extends BaseFunction implements Di\UserAware { use Di\UserSetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { - if (count($item->value) < 1) { - throw new Error("userAttribute: too few arguments."); + if (count($args) < 1) { + $this->throwTooFewArguments(); } - $attribute = $this->evaluate($item->value[0]); + $attribute = $this->evaluate($args[0]); if (!is_string($attribute)) { - throw new Error(); + $this->throwBadArgumentType(1, 'string'); } return $this->user->get($attribute); diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php index 662773e068..e44fa3907c 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/ApplyTemplateType.php @@ -29,39 +29,48 @@ namespace Espo\Core\Formula\Functions\ExtGroup\EmailGroup; -use Espo\Core\Exceptions\Error; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; use Espo\Core\Di; -class ApplyTemplateType extends \Espo\Core\Formula\Functions\Base implements +class ApplyTemplateType extends BaseFunction implements Di\EntityManagerAware, Di\ServiceFactoryAware { use Di\EntityManagerSetter; use Di\ServiceFactorySetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { - $args = $this->fetchArguments($item); + if (count($args) < 2) { + $this->throwTooFewArguments(2); + } - if (count($args) < 2) throw new Error("Formula ext\email\applyTemplate: Too few arguments."); + $args = $this->evaluate($args); $id = $args[0]; $templateId = $args[1]; $parentType = $args[2] ?? null; $parentId = $args[3] ?? null; - if (!$id || !is_string($id)) - throw new Error("Formula ext\\email\applyTemplate: 1st argument should be string and not be empty."); + if (!$id || !is_string($id)) { + $this->throwBadArgumentType(1, 'string'); + } - if (!$templateId || !is_string($templateId)) - throw new Error("Formula ext\\email\applyTemplate: 2nd argument should be string and not be empty."); + if (!$templateId || !is_string($templateId)) { + $this->throwBadArgumentType(2, 'string'); + } - if ($parentType && !is_string($parentType)) - throw new Error("Formula ext\\email\applyTemplate: 3st argument should be string."); + if ($parentType && !is_string($parentType)) { + $this->throwBadArgumentType(3, 'string'); + } - if ($parentId && !is_string($parentId)) - throw new Error("Formula ext\\email\applyTemplate: 4th argument should be string."); + if ($parentId && !is_string($parentId)) { + $this->throwBadArgumentType(4, 'string'); + } $em = $this->entityManager; @@ -69,18 +78,18 @@ class ApplyTemplateType extends \Espo\Core\Formula\Functions\Base implements $emailTemplate = $em->getEntity('EmailTemplate', $templateId); if (!$email) { - $GLOBALS['log']->warning("Formula ext\\email\applyTemplate: Email {$id} does not exist."); + $this->log("Email {$id} does not exist."); return false; } if (!$emailTemplate) { - $GLOBALS['log']->warning("Formula ext\\email\applyTemplate: EmailTemplate {$templateId} does not exist."); + $this->log("EmailTemplate {$templateId} does not exist."); return false; } $status = $email->get('status'); if ($status && in_array($status, ['Sent'])) { - $GLOBALS['log']->warning("Formula ext\\email\applyTemplate: Can't apply template to email with 'Sent' status."); + $this->log("Can't apply template to email with 'Sent' status."); return false; } diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php index 6e4191ac28..622f14e107 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/EmailGroup/SendType.php @@ -29,42 +29,49 @@ namespace Espo\Core\Formula\Functions\ExtGroup\EmailGroup; -use Espo\Core\Exceptions\Error; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; use Espo\Core\Di; -class SendType extends \Espo\Core\Formula\Functions\Base implements +class SendType extends BaseFunction implements Di\EntityManagerAware, Di\ServiceFactoryAware, Di\ConfigAware { use Di\EntityManagerSetter; use Di\ServiceFactorySetter; - use Di\ConfigSettr; + use Di\ConfigSetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { - $args = $this->fetchArguments($item); + if (count($args) < 1) { + $this->throwTooFewArguments(1); + } + + $args = $this->evaluate($args); - if (count($args) < 1) throw new Error("Formula ext\email\send: Too few arguments."); $id = $args[0]; - if (!$id) throw new Error("Formula ext\\email\send: First argument should not be empty."); - if (!is_string($id)) throw new Error("Formula ext\\email\send: First argument should be a string."); + if (!$id || !is_string($id)) { + $this->throwBadArgumentType(1, 'string'); + } $em = $this->entityManager; $email = $em->getEntity('Email', $id); if (!$email) { - $GLOBALS['log']->warning("Formula ext\\email\send: Email {$id} does not exist."); + $this->log("Email '{$id}' does not exist."); return false; } $status = $email->get('status'); if ($status && in_array($status, ['Sent'])) { - $GLOBALS['log']->warning("Formula ext\\email\send: Can't send email that has 'Sent' status."); + $this->log("Can't send email that has 'Sent' status."); return false; } @@ -96,7 +103,8 @@ class SendType extends \Espo\Core\Formula\Functions\Base implements try { $service->sendEntity($email); } catch (\Exception $e) { - $GLOBALS['log']->error("Formula ext\\email\send: Error while sending. Message: " . $e->getMessage()); + $message = $e->getMessage(); + $this->log("Error while sending. Message: {$message}." , 'error'); return false; } diff --git a/application/Espo/Core/Formula/Functions/ExtGroup/PdfGroup/GenerateType.php b/application/Espo/Core/Formula/Functions/ExtGroup/PdfGroup/GenerateType.php index 16206a2c34..c574aa17d5 100644 --- a/application/Espo/Core/Formula/Functions/ExtGroup/PdfGroup/GenerateType.php +++ b/application/Espo/Core/Formula/Functions/ExtGroup/PdfGroup/GenerateType.php @@ -29,57 +29,66 @@ namespace Espo\Core\Formula\Functions\ExtGroup\PdfGroup; -use Espo\Core\Exceptions\Error; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; use Espo\Core\Utils\Util; use Espo\Core\Di; -class GenerateType extends \Espo\Core\Formula\Functions\Base implements +class GenerateType extends BaseFunction implements Di\EntityManagerAware, Di\ServiceFactoryAware { use Di\EntityManagerSetter; use Di\ServiceFactorySetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { - $args = $this->fetchArguments($item); + if (count($args) < 3) { + $this->throwTooFewArguments(3); + } - if (count($args) < 3) throw new Error("Formula ext\\pdf\\generate: Too few arguments."); + $args = $this->evaluate($args); $entityType = $args[0]; $id = $args[1]; $templateId = $args[2]; $fileName = $args[3]; - if (!$entityType || !is_string($entityType)) - throw new Error("Formula ext\\pdf\\generate: 1st argument should be string and not be empty."); - if (!$id || !is_string($id)) - throw new Error("Formula ext\\pdf\\generate: 2nd argument should be string and not be empty."); - if (!$templateId || !is_string($templateId)) - throw new Error("Formula ext\\pdf\\generate: 3rd argument should be string and not be empty."); - if ($fileName && !is_string($fileName)) - throw new Error("Formula ext\\pdf\\generate: 4rd argument should be string."); + if (!$entityType || !is_string($entityType)) { + $this->throwBadArgumentType(1, 'string'); + } + if (!$id || !is_string($id)) { + $this->throwBadArgumentType(2, 'string'); + } + if (!$templateId || !is_string($templateId)) { + $this->throwBadArgumentType(3, 'string'); + } + if ($fileName && !is_string($fileName)) { + $this->throwBadArgumentType(4, 'string'); + } $em = $this->entityManager; try { $entity = $em->getEntity($entityType, $id); } catch (\Exception $e) { - $GLOBALS['log']->error("Formula ext\\pdf\\generate: Message: " . $e->getMessage()); + $this->log("Message: " . $e->getMessage() . "."); return null; } if (!$entity) { - $GLOBALS['log']->warning("Formula ext\\pdf\\generate: Record {$entityType} {$id} does not exist."); + $this->log("Record {$entityType} {$id} does not exist."); return null; } $template = $em->getEntity('Template', $templateId); if (!$template) { - $GLOBALS['log']->warning("Formula ext\\pdf\\generate: Template {$templateId} does not exist."); + $this->log("Template {$templateId} does not exist."); return null; } @@ -94,7 +103,7 @@ class GenerateType extends \Espo\Core\Formula\Functions\Base implements try { $contents = $this->serviceFactory->create('Pdf')->buildFromTemplate($entity, $template); } catch (\Exception $e) { - $GLOBALS['log']->error("Formula ext\\pdf\\generate: Error while generating. Message: " . $e->getMessage()); + $this->log("Error while generating. Message: " . $e->getMessage() . ".", 'error'); return null; } diff --git a/application/Espo/Core/Formula/Functions/PasswordGroup/GenerateType.php b/application/Espo/Core/Formula/Functions/PasswordGroup/GenerateType.php index 5b986d0113..da99ccc70e 100644 --- a/application/Espo/Core/Formula/Functions/PasswordGroup/GenerateType.php +++ b/application/Espo/Core/Formula/Functions/PasswordGroup/GenerateType.php @@ -29,14 +29,21 @@ namespace Espo\Core\Formula\Functions\PasswordGroup; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; + +use Espo\Core\Utils\Util; + use Espo\Core\Di; -class GenerateType extends \Espo\Core\Formula\Functions\Base implements +class GenerateType extends BaseFunction implements Di\ConfigAware { use Di\ConfigSetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { $config = $this->config; @@ -44,7 +51,7 @@ class GenerateType extends \Espo\Core\Formula\Functions\Base implements $letterCount = $config->get('passwordGenerateLetterCount', 4); $numberCount = $config->get('passwordGenerateNumberCount', 2); - $password = \Espo\Core\Utils\Util::generatePassword($length, $letterCount, $numberCount, true); + $password = Util::generatePassword($length, $letterCount, $numberCount, true); return $password; } diff --git a/application/Espo/Core/Formula/Functions/PasswordGroup/HashType.php b/application/Espo/Core/Formula/Functions/PasswordGroup/HashType.php index 0b6cd62958..7a7f7d9b1d 100644 --- a/application/Espo/Core/Formula/Functions/PasswordGroup/HashType.php +++ b/application/Espo/Core/Formula/Functions/PasswordGroup/HashType.php @@ -29,29 +29,30 @@ namespace Espo\Core\Formula\Functions\PasswordGroup; -use Espo\Core\Exceptions\Error; +use Espo\Core\Formula\{ + Functions\BaseFunction, + ArgumentList, +}; use Espo\Core\Utils\PasswordHash; use Espo\Core\Di; -class HashType extends \Espo\Core\Formula\Functions\Base implements +class HashType extends BaseFunction implements Di\ConfigAware { use Di\ConfigSetter; - public function process(\StdClass $item) + public function process(ArgumentList $args) { - $args = $item->value ?? []; - if (count($args) < 1) { - throw new Error("Formula: password\\hash: no argument."); + $this->throwTooFewArguments(); } $password = $this->evaluate($args[0]); if (!is_string($password)) { - throw new Error("Formula: password\\hash: bad argument."); + $this->throwBadArgumentType(1, 'string'); } $passwordHash = new PasswordHash($this->config); diff --git a/tests/integration/Espo/Core/Formula/FormulaTest.php b/tests/integration/Espo/Core/Formula/FormulaTest.php index 65adf774bc..196bbc41c9 100644 --- a/tests/integration/Espo/Core/Formula/FormulaTest.php +++ b/tests/integration/Espo/Core/Formula/FormulaTest.php @@ -676,7 +676,6 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase $this->assertEquals(false, $email->get('isHtml')); $this->assertEquals(2, count($attachmentsIds)); - $case = $em->createEntity('Case', [ 'name' => 'Case 1', ]); @@ -744,4 +743,14 @@ class FormulaTest extends \tests\integration\Core\BaseTestCase $this->assertEquals('1.pdf', $attachment->get('name')); } + + public function testEnvUserAttribute() + { + $fm = $this->getContainer()->get('formulaManager'); + $user = $this->getContainer()->get('user'); + + $script = "env\\userAttribute('id')"; + $id = $fm->run($script); + $this->assertEquals($id, $user->id); + } }