diff --git a/classes/components/forms/emailTemplate/EmailTemplateForm.php b/classes/components/forms/emailTemplate/EmailTemplateForm.php index 9011049e67d..02b8b96ba7f 100644 --- a/classes/components/forms/emailTemplate/EmailTemplateForm.php +++ b/classes/components/forms/emailTemplate/EmailTemplateForm.php @@ -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'), ])); } diff --git a/classes/emailTemplate/Repository.php b/classes/emailTemplate/Repository.php index 910a618aebc..af416779309 100644 --- a/classes/emailTemplate/Repository.php +++ b/classes/emailTemplate/Repository.php @@ -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; diff --git a/classes/emailTemplate/maps/Schema.php b/classes/emailTemplate/maps/Schema.php index 0df97f706ae..87e3024286f 100644 --- a/classes/emailTemplate/maps/Schema.php +++ b/classes/emailTemplate/maps/Schema.php @@ -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) { @@ -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; diff --git a/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php b/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php new file mode 100644 index 00000000000..fb4b75a2e55 --- /dev/null +++ b/classes/migration/upgrade/v3_5_0/I10403_EmailTemplateUserGroupAccess.php @@ -0,0 +1,75 @@ +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'); + } +} diff --git a/locale/en/admin.po b/locale/en/admin.po index 0d3e48a3e8b..9f487320792 100644 --- a/locale/en/admin.po +++ b/locale/en/admin.po @@ -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"