Skip to content

Commit

Permalink
fix(SFT-1647): accounting for Submission Read permissions too
Browse files Browse the repository at this point in the history
  • Loading branch information
kjmartens committed Dec 11, 2024
1 parent ee89d7f commit 11ca83b
Showing 1 changed file with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 11ca83b

Please sign in to comment.