Skip to content

Commit

Permalink
Added back accidentally deleted migration and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
taslangraham committed Dec 18, 2024
1 parent 77244d7 commit f232829
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(string $action, array $locales)
)
->addField(new FieldEmailTemplateUserGroupSettings('userGroupIds', [
'type' => 'checkbox',
'label' => __('workflow.userGroup.allowed'),
'label' => __('admin.workflow.email.userGroup.allowed'),

]));
}
Expand Down
8 changes: 1 addition & 7 deletions classes/emailTemplate/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,13 @@ public function isTemplateUnrestricted(string $templateKey, int $contextId): boo
*/
public function isTemplateAccessibleToUser(User $user, EmailTemplate $template, int $contextId): bool
{
if ($user->hasRole([Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_MANAGER,], $contextId)) {
if ($user->hasRole([Role::ROLE_ID_SITE_ADMIN, Role::ROLE_ID_MANAGER,], $contextId) || $this->isTemplateUnrestricted($template->getData('key'), $contextId)) {
return true;
}

$userUserGroups = Repo::userGroup()->userUserGroups($user->getId(), $contextId)->all();
$templateUserGroups = $this->getUserGroupsIdsAssignedToTemplate($template->getData('key'), $contextId);

// Null entry indicates that template is unrestricted
if(in_array(null, $templateUserGroups)) {
return true;
}


foreach ($userUserGroups as $userGroup) {
if (in_array($userGroup->getId(), $templateUserGroups)) {
return true;
Expand Down
27 changes: 17 additions & 10 deletions classes/emailTemplate/maps/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ protected function mapByProperties(array $props, EmailTemplate $item, string $ma
$mailableClass = $mailableClass ?? Repo::mailable()->getMailableByEmailTemplate($item);
$assignedUserGroupsIds = [];
// some mailable are not found during some operations such as performing a search for templates. So ensure mailable exist before using
if ($mailableClass) {
$isUserGroupsAssignable = Repo::mailable()->isGroupsAssignableToTemplates($mailableClass);
$assignedUserGroupsIds = Repo::emailTemplate()->getUserGroupsIdsAssignedToTemplate($item->getData('key'), Application::get()->getRequest()->getContext()->getId());
if (!$isUserGroupsAssignable) {
$output['assignedUserGroupIds'] = [];
} else {
// Get the current user groups assigned to the template
$output['assignedUserGroupIds'] = $assignedUserGroupsIds;
}
}
// if ($mailableClass) {
// $isUserGroupsAssignable = Repo::mailable()->isGroupsAssignableToTemplates($mailableClass);
// $assignedUserGroupsIds = Repo::emailTemplate()->getUserGroupsIdsAssignedToTemplate($item->getData('key'), Application::get()->getRequest()->getContext()->getId());
// if (!$isUserGroupsAssignable) {
// $output['assignedUserGroupIds'] = [];
// } else {
// // Get the current user groups assigned to the template
// $output['assignedUserGroupIds'] = $assignedUserGroupsIds;
// }
// }


foreach ($props as $prop) {
Expand All @@ -111,6 +111,13 @@ protected function mapByProperties(array $props, EmailTemplate $item, string $ma
case 'isUnrestricted':
$output['isUnrestricted'] = Repo::emailTemplate()->isTemplateUnrestricted($item->getData('key'), Application::get()->getRequest()->getContext()->getId());
break;
case 'assignedUserGroupIds':
if ($mailableClass && Repo::mailable()->isGroupsAssignableToTemplates($mailableClass)) {
$output['assignedUserGroupIds'] = Repo::emailTemplate()->getUserGroupsIdsAssignedToTemplate($item->getData('key'), Application::get()->getRequest()->getContext()->getId());
} else {
$output['assignedUserGroupIds'] = [];
}
break;
default:
$output[$prop] = $item->getData($prop);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PKP\context\Context;
use PKP\emailTemplate\EmailTemplateAccessGroup;
use PKP\migration\Migration;

class I10403_EmailTemplateUserGroupAccess extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
$contextDao = \APP\core\Application::getContextDAO();
Schema::create('email_template_user_group_access', function (Blueprint $table) use ($contextDao) {
$table->bigInteger('email_template_user_group_access_id')->autoIncrement();
$table->string('email_key', 255);
$table->bigInteger('context_id');
$table->bigInteger('user_group_id')->nullable();

$table->foreign('context_id')->references($contextDao->primaryKeyColumn)->on($contextDao->tableName)->onDelete('cascade');
$table->foreign('user_group_id')->references('user_group_id')->on('user_groups')->onDelete('cascade');
});

$contextIds = array_map(fn (Context $context) => $context->getId(), $contextDao->getAll()->toArray());

if (!empty($contextIds)) {
DB::table('email_template_user_group_access')->select('*')->get();

$defaultTemplateKeys = DB::table('email_templates_default_data')->select()->pluck('email_key')->all();
$alternateTemplates = DB::table('email_templates')->select(['context_id', 'email_key'])->get()->toArray();

$data = [];

// Record any existing default templates as unrestricted for existing Contexts
foreach ($defaultTemplateKeys as $defaultTemplateKey) {
foreach ($contextIds as $contextId) {
$data[] = [
'email_key' => $defaultTemplateKey,
'context_id' => $contextId,
'user_group_id' => null
];
}
}

// For any existing alternate template, register it as unrestricted within its assigned context
foreach ($alternateTemplates as $template) {
foreach ($contextIds as $contextId) {
if ($contextId === $template->context_id) {
$data[] = [
'email_key' => $template->email_key,
'context_id' => $contextId,
'user_group_id' => null
];
}
}
}

EmailTemplateAccessGroup::insert($data);
}
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::drop('email_template_user_group_access');
}
}
2 changes: 1 addition & 1 deletion locale/en/admin.po
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ msgstr "No matching scheduled task found."
msgid "admin.cli.tool.scheduler.run.prompt"
msgstr "Which command would you like to run?"

msgid "workflow.email.userGroup.allowed"
msgid "admin.workflow.email.userGroup.allowed"
msgstr "Allowed User Groups"

msgid "workflow.email.userGroup.assign.unrestricted"
Expand Down

0 comments on commit f232829

Please sign in to comment.