Skip to content

Commit

Permalink
pkp#10571 Allow restriction to be set during template installation
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Dec 19, 2024
1 parent 422d4cf commit 755c55b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
11 changes: 11 additions & 0 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 7 additions & 8 deletions classes/emailTemplate/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion dtd/emailTemplates.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
name CDATA #REQUIRED
subject CDATA #REQUIRED
body CDATA #REQUIRED
alternateTo CDATA #IMPLIED>
alternateTo CDATA #IMPLIED
isUnrestricted (1 | 0) CDATA #IMPLIED>

0 comments on commit 755c55b

Please sign in to comment.