From 76426fc1d92c9c8e79d1027c6c331f951c8bc8f9 Mon Sep 17 00:00:00 2001 From: Taslan Graham Date: Fri, 15 Nov 2024 15:50:21 -0500 Subject: [PATCH] pkp/pkp-lib#10571 Allow restriction to be set during template installation --- classes/emailTemplate/DAO.php | 11 +++++++++++ classes/emailTemplate/Repository.php | 15 +++++++-------- dtd/emailTemplates.dtd | 3 ++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/classes/emailTemplate/DAO.php b/classes/emailTemplate/DAO.php index cf6dce96712..17bb2f614a3 100644 --- a/classes/emailTemplate/DAO.php +++ b/classes/emailTemplate/DAO.php @@ -282,6 +282,17 @@ public function installEmailTemplates( $this->installAlternateEmailTemplates($contextId, $attrs['key']); } } + + if (isset($attrs['isUnrestricted'])) { + if ($attrs['isUnrestricted'] !== '1' && $attrs['isUnrestricted'] !== '0') { + throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template'); + } + + $contextIds = app()->get('context')->getIds(); + foreach ($contextIds as $contextId) { + Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$attrs['isUnrestricted'], $contextId); + } + } } return true; } diff --git a/classes/emailTemplate/Repository.php b/classes/emailTemplate/Repository.php index 7efc7e0a58b..ac3a388e397 100644 --- a/classes/emailTemplate/Repository.php +++ b/classes/emailTemplate/Repository.php @@ -332,7 +332,7 @@ public function setEmailTemplateAccess(EmailTemplate $emailTemplate, int $contex } if($isUnrestricted !== null) { - $this->markTemplateAsUnrestricted($emailTemplate, $isUnrestricted, $contextId); + $this->markTemplateAsUnrestricted($emailTemplate->getData('key'), $isUnrestricted, $contextId); } } @@ -342,30 +342,29 @@ public function setEmailTemplateAccess(EmailTemplate $emailTemplate, int $contex * An unrestricted email template is available to all user groups associated with the Roles linked to the mailable that the template belongs to. * Mailable roles are stored in the $fromRoleIds property of a mailable */ - private function markTemplateAsUnrestricted(EmailTemplate $emailTemplate, bool $isUnrestricted, int $contextId): void + public function markTemplateAsUnrestricted(string $emailKey, bool $isUnrestricted, int $contextId): void { // Unrestricted emails are represented by an entry with a `null` value for the user group ID if ($isUnrestricted) { EmailTemplateAccessGroup::updateOrCreate( [ // The where conditions (keys that should match) - 'email_key' => $emailTemplate->getData('key'), + 'email_key' => $emailKey, 'user_group_id' => null, 'context_id' => $contextId, ], [ // The data to insert or update (values to set) - 'emailKey' => $emailTemplate->getData('key'), + 'emailKey' => $emailKey, 'userGroupId' => null, 'contextId' => $contextId, ] ); } else { - // Remove entry with a `null` value for the user group ID to reflect that it is no longer unrestricted - EmailTemplateAccessGroup::withEmailKey([$emailTemplate->getData('key')]) - ->withContextId($contextId) - ->withGroupIds([null]) + EmailTemplateAccessGroup::where('email_key', $emailKey) + ->where('context_id', $contextId) + ->whereNull('user_group_id') ->delete(); } } diff --git a/dtd/emailTemplates.dtd b/dtd/emailTemplates.dtd index 58b1be72443..1c54a17fc2b 100644 --- a/dtd/emailTemplates.dtd +++ b/dtd/emailTemplates.dtd @@ -16,4 +16,5 @@ name CDATA #REQUIRED subject CDATA #REQUIRED body CDATA #REQUIRED - alternateTo CDATA #IMPLIED> + alternateTo CDATA #IMPLIED + isUnrestricted (1 | 0) CDATA #IMPLIED>