Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import custom objects on background #318

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@
'custom_item.permission.provider',
'custom_field.repository',
'translator',
'mautic.user.model.user',
],
],
'custom_item.contact.subscriber' => [
Expand Down
12 changes: 9 additions & 3 deletions EventListener/ImportSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mautic\LeadBundle\Event\ImportProcessEvent;
use Mautic\LeadBundle\Event\ImportValidateEvent;
use Mautic\LeadBundle\LeadEvents;
use Mautic\UserBundle\Model\UserModel;
use MauticPlugin\CustomObjectsBundle\Entity\CustomField;
use MauticPlugin\CustomObjectsBundle\Exception\ForbiddenException;
use MauticPlugin\CustomObjectsBundle\Exception\NotFoundException;
Expand Down Expand Up @@ -56,20 +57,24 @@ class ImportSubscriber implements EventSubscriberInterface
*/
private $customFieldRepository;

private UserModel $userModel;

public function __construct(
CustomObjectModel $customObjectModel,
CustomItemImportModel $customItemImportModel,
ConfigProvider $configProvider,
CustomItemPermissionProvider $permissionProvider,
CustomFieldRepository $customFieldRepository,
TranslatorInterface $translator
TranslatorInterface $translator,
UserModel $userModel
) {
$this->customObjectModel = $customObjectModel;
$this->customItemImportModel = $customItemImportModel;
$this->configProvider = $configProvider;
$this->permissionProvider = $permissionProvider;
$this->customFieldRepository = $customFieldRepository;
$this->translator = $translator;
$this->userModel = $userModel;
}

/**
Expand Down Expand Up @@ -185,8 +190,9 @@ public function onImportProcess(ImportProcessEvent $event): void

try {
$customObjectId = $this->getCustomObjectId($event->import->getObject());
$this->permissionProvider->canCreate($customObjectId);
$customObject = $this->customObjectModel->fetchEntity($customObjectId);
$user = $event->import->isBackgroundProcess() ? $this->userModel->getEntity($event->import->getCreatedBy()) : null;
$this->permissionProvider->canCreate($customObjectId, $user);
$customObject = $this->customObjectModel->fetchEntity($customObjectId);
$merged = $this->customItemImportModel->import($event->import, $event->rowData, $customObject);
$event->setWasMerged($merged);
} catch (NotFoundException $e) {
Expand Down
11 changes: 6 additions & 5 deletions Provider/CustomItemPermissionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MauticPlugin\CustomObjectsBundle\Provider;

use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\UserBundle\Entity\User;
use MauticPlugin\CustomObjectsBundle\Entity\CustomItem;
use MauticPlugin\CustomObjectsBundle\Exception\ForbiddenException;
use MauticPlugin\CustomObjectsBundle\Security\Permissions\CustomObjectPermissions;
Expand All @@ -24,9 +25,9 @@ public function __construct(CorePermissions $corePermissions)
/**
* @throws ForbiddenException
*/
public function isGranted(string $permission, int $customObjectId): void
public function isGranted(string $permission, int $customObjectId, ?User $user = null): void
{
if (!$this->corePermissions->isGranted($this->getPermissionName($customObjectId, $permission))) {
if (!$this->corePermissions->isGranted($this->getPermissionName($customObjectId, $permission), 'MATCH_ALL', $user)) {
throw new ForbiddenException($permission, 'Items for Custom Object', $customObjectId);
}
}
Expand All @@ -45,9 +46,9 @@ public function hasEntityAccess(string $permission, CustomItem $entity): void
/**
* @throws ForbiddenException
*/
public function canCreate(int $customObjectId): void
public function canCreate(int $customObjectId, ?User $user = null): void
{
$this->isGranted('create', $customObjectId);
$this->isGranted('create', $customObjectId, $user);
}

/**
Expand Down Expand Up @@ -94,7 +95,7 @@ public function canDelete(CustomItem $entity): void
$this->hasEntityAccess('delete', $entity);
}

private function getPermissionName(int $customObjectId, string $permission): string
public function getPermissionName(int $customObjectId, string $permission): string
{
return sprintf('%s:%d:%s', CustomObjectPermissions::NAME, $customObjectId, $permission);
}
Expand Down