From 11ca83be6d5ea501d288a00b3ff94693efc50aaf Mon Sep 17 00:00:00 2001 From: kjmartens Date: Wed, 11 Dec 2024 13:21:36 -0600 Subject: [PATCH] fix(SFT-1647): accounting for Submission Read permissions too --- .../Duplication/FormDuplicator.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packages/plugin/src/Bundles/Persistence/Duplication/FormDuplicator.php b/packages/plugin/src/Bundles/Persistence/Duplication/FormDuplicator.php index b85b28940..73841d159 100644 --- a/packages/plugin/src/Bundles/Persistence/Duplication/FormDuplicator.php +++ b/packages/plugin/src/Bundles/Persistence/Duplication/FormDuplicator.php @@ -2,12 +2,18 @@ namespace Solspace\Freeform\Bundles\Persistence\Duplication; +use craft\db\Query; +use craft\db\Table; use craft\helpers\StringHelper; +use craft\records\UserPermission; +use craft\records\UserPermission_User; +use craft\records\UserPermission_UserGroup; use Solspace\Freeform\Attributes\Property\Input\Field; use Solspace\Freeform\Attributes\Property\Input\Special\Properties\FieldMapping; use Solspace\Freeform\Bundles\Attributes\Property\PropertyProvider; use Solspace\Freeform\Fields\Implementations\Pro\GroupField; use Solspace\Freeform\Form\Managers\ContentManager; +use Solspace\Freeform\Freeform; use Solspace\Freeform\Library\Helpers\JsonHelper; use Solspace\Freeform\Library\Helpers\StringHelper as FreeformStringHelper; use Solspace\Freeform\Notifications\Types\Conditional\Conditional; @@ -83,6 +89,7 @@ public function clone(int $id): bool $this->cloneNotifications($id, $form); $this->cloneRules($id, $form); $this->cloneIntegrations($id, $form); + $this->updatePermissions($id, $form); $form = $this->formsService->getFormById($form->id); @@ -402,6 +409,66 @@ private function cloneIntegrations(int $originalId, FormRecord $form): void } } + private function updatePermissions(int $id, FormRecord $form): void + { + $userId = \Craft::$app->user->getIdentity()->id; + $permissions = [ + Freeform::PERMISSION_SUBMISSIONS_READ, + Freeform::PERMISSION_SUBMISSIONS_MANAGE, + Freeform::PERMISSION_FORMS_MANAGE, + ]; + + foreach ($permissions as $permissionName) { + $name = strtolower($permissionName.':'.$id); + $newName = strtolower($permissionName.':'.$form->id); + + $permissionId = (int) (new Query()) + ->select('id') + ->from(Table::USERPERMISSIONS) + ->where(['name' => $name]) + ->scalar() + ; + + $groupIds = (new Query()) + ->select('groupId') + ->from(Table::USERPERMISSIONS_USERGROUPS) + ->where(['permissionId' => $permissionId]) + ->column() + ; + + $userPermissionId = (new Query()) + ->select('id') + ->from(Table::USERPERMISSIONS_USERS) + ->where([ + 'permissionId' => $permissionId, + 'userId' => $userId, + ]) + ->scalar() + ; + + $permission = UserPermission::find()->where(['name' => $newName])->one(); + if (!$permission) { + $permission = new UserPermission(); + $permission->name = $newName; + $permission->save(); + } + + if ($userPermissionId) { + $userPermission = new UserPermission_User(); + $userPermission->userId = $userId; + $userPermission->permissionId = $permission->id; + $userPermission->save(); + } else { + foreach ($groupIds as $groupId) { + $groupPermission = new UserPermission_UserGroup(); + $groupPermission->groupId = $groupId; + $groupPermission->permissionId = $permission->id; + $groupPermission->save(); + } + } + } + } + private function warmUpLayout(int $formId): void { $this->layouts = $this->fetchRecords(FormLayoutRecord::class, $formId);