Skip to content

Commit

Permalink
#10571 Update access on template deletion and template restore/reset
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Nov 25, 2024
1 parent 2ae5004 commit 80068ac
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion api/v1/emailTemplates/PKPEmailTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public function delete(Request $illuminateRequest): JsonResponse

$props = Repo::emailTemplate()->getSchemaMap()->map($emailTemplate);
Repo::emailTemplate()->delete($emailTemplate);

Repo::emailTemplate()->deleteTemplateGroupAccess($requestContext->getId(), [$illuminateRequest->route('key')]);
return response()->json($props, Response::HTTP_OK);
}

Expand Down
61 changes: 33 additions & 28 deletions classes/emailTemplate/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,34 +360,6 @@ public function installEmailTemplateLocaleData(
return true;
}


/**
* Registers default templates as unrestricted for the specified context ID.
*/
public function registerDefaultTemplatesAsUnrestricted(int $contextId): void
{
$templates = Repo::emailTemplate()->getCollector($contextId)->getMany()->all();
$keys = array_map(fn (EmailTemplate $template) => $template->getData('key'), $templates);

$existingUnrestrictedTemplates = EmailTemplateAccessGroup::withContextId($contextId)
->whereNull('user_group_id')
->select('email_key')
->pluck('email_key')->all();

// Identify templates to add as unrestricted
$defaultKeysToRegister = array_diff($keys, $existingUnrestrictedTemplates);

$data = array_map(function ($key) use ($contextId) {
return [
'email_key' => $key,
'context_id' => $contextId,
'user_group_id' => null
];
}, $defaultKeysToRegister);

EmailTemplateAccessGroup::insert($data);
}

/**
* Installs the "extra" email templates for a context
*
Expand Down Expand Up @@ -494,4 +466,37 @@ protected function getUniqueKey(EmailTemplate $emailTemplate): string

return $key;
}


/**
* Sets email template's unrestricted status to their defaults
*/
public function setTemplateDefaultUnrestirctedSetting(int $contextId, ?array $emailKeys = null)
{
$xmlDao = new XMLDAO();
$data = $xmlDao->parseStruct($this->getMainEmailTemplatesFilename(), ['email']);

if (!isset($data['email'])) {
return false;
}

foreach ($data['email'] as $entry) {
$attrs = $entry['attributes'];

if ($emailKeys !== null && !in_array($attrs['key'], $emailKeys)) {
continue;
}

// Default to true if `isUnrestricted` is not set.
$isUnrestricted = $attrs['isUnrestricted'] ?? '1';

if ($isUnrestricted !== '1' && $isUnrestricted !== '0') {
throw new Exception('Invalid value given for the `isUnrestricted` attribute on the ' . $attrs['key'] . ' template.');
}

Repo::emailTemplate()->markTemplateAsUnrestricted($attrs['key'], (bool)$isUnrestricted, $contextId);
}

return true;
}
}
29 changes: 27 additions & 2 deletions classes/emailTemplate/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace PKP\emailTemplate;

use APP\core\Application;
use APP\emailTemplate\DAO;
use APP\facades\Repo;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -195,10 +196,11 @@ public function delete(EmailTemplate $emailTemplate)
/**
* Delete a collection of email templates
*/
public function deleteMany(Collector $collector)
public function deleteMany(Collector $collector): void
{
foreach ($collector->getMany() as $emailTemplate) {
$this->delete($emailTemplate);
$this->deleteTemplateGroupAccess(Application::get()->getRequest()->getContext()->getId(), $emailTemplate);
}
}

Expand All @@ -217,11 +219,14 @@ public function restoreDefaults($contextId): array
->getMany();

$deletedKeys = [];
$results->each(function ($emailTemplate) use ($deletedKeys) {
$results->each(function ($emailTemplate) use ($contextId, &$deletedKeys) {
$deletedKeys[] = $emailTemplate->getData('key');
$this->delete($emailTemplate);
});


$this->dao->installAlternateEmailTemplates($contextId);
Repo::emailTemplate()->restoreTemplateUserGroupAccess($contextId, $deletedKeys);
Hook::call('EmailTemplate::restoreDefaults', [&$deletedKeys, $contextId]);
return $deletedKeys;
}
Expand Down Expand Up @@ -366,4 +371,24 @@ public function markTemplateAsUnrestricted(string $emailKey, bool $isUnrestricte
->delete();
}
}


/**
* Deletes all user group access for an email
*/
public function deleteTemplateGroupAccess(int $contextId, array $emailKey): void
{
EmailTemplateAccessGroup::where('context_id', $contextId)->whereIn('email_key', $emailKey)->delete();
}

/**
* Deletes all user group access for an email template and set unrestricted to their default
*/
public function restoreTemplateUserGroupAccess(int $contextId, array $emailKeys)
{
if (!empty($emailKeys)) {
$this->deleteTemplateGroupAccess($contextId, $emailKeys);
return $this->dao->setTemplateDefaultUnrestirctedSetting($contextId, $emailKeys);
}
}
}
10 changes: 10 additions & 0 deletions classes/mail/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PKP\mail\mailables\SubmissionSavedForLater;
use PKP\mail\traits\Configurable;
use PKP\plugins\Hook;
use PKP\userGroup\UserGroup;

class Repository
{
Expand Down Expand Up @@ -101,6 +102,14 @@ public function summarizeMailable(string $class): array
$dataDescriptions = $class::getDataDescriptions();
ksort($dataDescriptions);

$userGroups = collect();
Repo::userGroup()->getCollector()
->filterByContextIds([Application::get()->getRequest()->getContext()->getId()])
->getMany()->each(fn (UserGroup $group) => $userGroups->add([
'id' => $group->getId(),
'name' => $group->getLocalizedName()
]));

return [
'_href' => Application::get()->getRequest()->getDispatcher()->url(
Application::get()->getRequest(),
Expand All @@ -117,6 +126,7 @@ public function summarizeMailable(string $class): array
'supportsTemplates' => $class::getSupportsTemplates(),
'toRoleIds' => $class::getToRoleIds(),
'canAssignUserGroupToTemplates' => $this->isGroupsAssignableToTemplates($class),
'assignableTemplateUserGroups' => $userGroups
];
}

Expand Down
2 changes: 1 addition & 1 deletion classes/services/PKPContextService.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public function add($context, $request)
$navigationMenuDao->installSettings($context->getId(), 'registry/navigationMenus.xml');

Repo::emailTemplate()->dao->installAlternateEmailTemplates($context->getId());
Repo::emailTemplate()->dao->registerDefaultTemplatesAsUnrestricted($context->getId());
Repo::emailTemplate()->dao->setTemplateDefaultUnrestirctedSetting($context->getId());

// Load all plugins so they can hook in and add their installation settings
PluginRegistry::loadAllPlugins();
Expand Down

0 comments on commit 80068ac

Please sign in to comment.