diff --git a/CHANGELOG.md b/CHANGELOG.md index 8940b280..e7a8a6c8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 3.6.1 - 2023-09-19 +### Fixed +- Empty user id on draft creation + ## 3.6.0 - 2023-05-24 ### Added - Introduced new job and translation status "needs attention" diff --git a/composer.json b/composer.json index a2af5e2c..5f81c92d 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "lilt/craft-lilt-plugin", "description": "The Lilt plugin makes it easy for you to send content to Lilt for translation right from within Craft CMS.", "type": "craft-plugin", - "version": "3.6.0", + "version": "3.6.1", "keywords": [ "craft", "cms", diff --git a/src/controllers/job/AbstractJobController.php b/src/controllers/job/AbstractJobController.php index b2c55476..8c5da3b5 100644 --- a/src/controllers/job/AbstractJobController.php +++ b/src/controllers/job/AbstractJobController.php @@ -16,7 +16,9 @@ use lilthq\craftliltplugin\Craftliltplugin; use lilthq\craftliltplugin\elements\Job; use lilthq\craftliltplugin\parameters\CraftliltpluginParameters; +use RuntimeException; use yii\base\InvalidConfigException; +use yii\web\IdentityInterface; use yii\web\Response; class AbstractJobController extends Controller @@ -34,10 +36,20 @@ protected function convertRequestToJobModel(): Job $job->title = $bodyParams['title']; $job->sourceSiteId = (int)$bodyParams['sourceSite']; $job->versions = $bodyParams['versions'] ?? []; - $job->authorId = !empty($bodyParams['author'][0]) ? (int)$bodyParams['author'][0] : null; + $job->authorId = !empty($bodyParams['author'][0]) ? (int) $bodyParams['author'][0] : null; $job->translationWorkflow = $bodyParams['translationWorkflow']; $job->elementIds = json_decode($bodyParams['entries'], false) ?? []; + if (empty($job->authorId)) { + $userIdentity = Craft::$app->getUser()->getIdentity(); + + if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) { + throw new RuntimeException("can't create job: author id is empty"); + } + + $job->authorId = (int) $userIdentity->getId(); + } + //TODO: due date not using right now //$job->dueDate = DateTimeHelper::toDateTime($this->request->getBodyParam('dueDate')) ?: null; diff --git a/src/controllers/job/GetJobCreateFormController.php b/src/controllers/job/GetJobCreateFormController.php index 0b13bb25..cb44fa6c 100644 --- a/src/controllers/job/GetJobCreateFormController.php +++ b/src/controllers/job/GetJobCreateFormController.php @@ -16,7 +16,9 @@ use Craft; use craft\helpers\UrlHelper; use lilthq\craftliltplugin\elements\Job; +use RuntimeException; use yii\base\InvalidConfigException; +use yii\web\IdentityInterface; use yii\web\Response; class GetJobCreateFormController extends AbstractJobController @@ -46,20 +48,16 @@ public function actionInvoke(): Response $job->sourceSiteId = (int) $sourceSiteId; } + $userIdentity = Craft::$app->getUser()->getIdentity(); + if (!$userIdentity instanceof IdentityInterface || empty($userIdentity->getId())) { + throw new RuntimeException("can't create job: author id is empty"); + } + + $job->authorId = (int)$userIdentity->getId(); + return $this->renderJobForm( $job, [ - /*'formActions' => [ - [ - "label" => "Create and continue editing", - "redirect" => "{cpEditUrl}", - "shortcut" => true, - "retainScroll" => true, - "eventData" => [ - "autosave" => false - ] - ] - ],*/ 'crumbs' => [ [ 'label' => 'Lilt Plugin', @@ -70,7 +68,6 @@ public function actionInvoke(): Response 'url' => UrlHelper::cpUrl('admin/craft-lilt-plugin/jobs') ], ], - 'author' => Craft::$app->getUser()->getIdentity() ] ); } diff --git a/src/services/handlers/CopySourceTextHandler.php b/src/services/handlers/CopySourceTextHandler.php index e9c7442b..8915f4a0 100644 --- a/src/services/handlers/CopySourceTextHandler.php +++ b/src/services/handlers/CopySourceTextHandler.php @@ -53,7 +53,8 @@ public function __invoke(Job $job): void $job->title, $job->sourceSiteId, $targetSiteId, - $job->translationWorkflow + $job->translationWorkflow, + $job->authorId ) ); diff --git a/src/services/handlers/CreateDraftHandler.php b/src/services/handlers/CreateDraftHandler.php index 1cbc8a11..36162b52 100644 --- a/src/services/handlers/CreateDraftHandler.php +++ b/src/services/handlers/CreateDraftHandler.php @@ -58,14 +58,9 @@ public function create( $element->getCanonicalId() ) : $element; - $creatorId = Craft::$app->user->getId(); - if ($creatorId === null) { - $creatorId = $element->authorId; - } - $draft = Craft::$app->drafts->createDraft( $createFrom, - $creatorId ?? 0, + $command->getAuthorId(), sprintf( '%s [%s -> %s] ' . (new DateTime())->format('H:i:s'), $jobTitle, diff --git a/src/services/handlers/SendJobToLiltConnectorHandler.php b/src/services/handlers/SendJobToLiltConnectorHandler.php index 3750d0c3..ffb84866 100644 --- a/src/services/handlers/SendJobToLiltConnectorHandler.php +++ b/src/services/handlers/SendJobToLiltConnectorHandler.php @@ -76,7 +76,8 @@ public function __invoke(Job $job): void $job->title, $job->sourceSiteId, $targetSiteId, - $job->translationWorkflow + $job->translationWorkflow, + $job->authorId ) ); diff --git a/src/services/handlers/commands/CreateDraftCommand.php b/src/services/handlers/commands/CreateDraftCommand.php index 4aa8732f..77f3b9ad 100644 --- a/src/services/handlers/commands/CreateDraftCommand.php +++ b/src/services/handlers/commands/CreateDraftCommand.php @@ -18,19 +18,22 @@ class CreateDraftCommand private $sourceSiteId; private $targetSiteId; private $flow; + private $authorId; public function __construct( ElementInterface $element, string $jobTitle, int $sourceSiteId, int $targetSiteId, - string $flow + string $flow, + int $authorId ) { $this->element = $element; $this->jobTitle = $jobTitle; $this->sourceSiteId = $sourceSiteId; $this->targetSiteId = $targetSiteId; $this->flow = $flow; + $this->authorId = $authorId; } public function getElement(): ElementInterface @@ -57,4 +60,9 @@ public function getFlow(): string { return $this->flow; } + + public function getAuthorId(): int + { + return $this->authorId; + } } diff --git a/tests/_support/Helper/CraftLiltPluginHelper.php b/tests/_support/Helper/CraftLiltPluginHelper.php index b93c36fd..57a5db07 100644 --- a/tests/_support/Helper/CraftLiltPluginHelper.php +++ b/tests/_support/Helper/CraftLiltPluginHelper.php @@ -53,7 +53,8 @@ public function createJobWithTranslations(array $data): array $job->title, $job->sourceSiteId, $targetSiteId, - $job->translationWorkflow + $job->translationWorkflow, + $job->authorId ) ); } diff --git a/tests/integration/controllers/translation/PostTranslationPublishControllerCest.php b/tests/integration/controllers/translation/PostTranslationPublishControllerCest.php index 79301a13..6337f7be 100644 --- a/tests/integration/controllers/translation/PostTranslationPublishControllerCest.php +++ b/tests/integration/controllers/translation/PostTranslationPublishControllerCest.php @@ -89,7 +89,8 @@ public function testCopySlugSettingEnabled(IntegrationTester $I): void $job->title, $job->siteId, Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('ru-RU'), - 'instant' + 'instant', + $job->authorId ) ); @@ -206,7 +207,8 @@ public function testCopySlugSettingDisabled(IntegrationTester $I): void $job->title, $job->siteId, Craftliltplugin::getInstance()->languageMapper->getSiteIdByLanguage('ru-RU'), - 'instant' + 'instant', + $job->authorId ) );