Skip to content

Commit

Permalink
EZP-27439: Improve Multi File Upload loading time - backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Kobus committed Jun 9, 2017
1 parent f2d9826 commit fe55a41
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 84 deletions.
6 changes: 2 additions & 4 deletions bundle/Controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ public function __construct(
}

/**
* @param int $contentTypeId
* @param int $parentLocationId
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \EzSystems\MultiFileUpload\API\Repository\Values\PermissionReport
*/
public function checkPermission($contentTypeId, $parentLocationId, Request $request)
public function checkPermission($parentLocationId, Request $request)
{
if (!$request->isXmlHttpRequest()) {
throw new BadRequestHttpException('The request is not an AJAX request');
}

$contentType = $this->contentTypeService->loadContentType($contentTypeId);
$parentLocation = $this->locationService->loadLocation($parentLocationId);

return $this->permissionReportService->canUserCreateContent($contentType, $parentLocation);
return $this->permissionReportService->canUserCreateContent($parentLocation);
}
}
3 changes: 1 addition & 2 deletions bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ez_systems.multifile_upload.controller.rest.check_permission:
methods: [GET]
path: /multifileupload/v1/check-permission/{parentLocationId}/{contentTypeId}
path: /multifileupload/v1/check-permission/{parentLocationId}
requirements:
parentLocationId: '\d+'
contentTypeId: '\d+'
defaults:
_controller: ez_systems.multifile_upload.controller.rest:checkPermission
6 changes: 5 additions & 1 deletion bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ services:
arguments:
- '@ez_systems.multifile_upload.service.permission_resolver'
- '@ezpublish.api.service.content'
- '@ezpublish.api.service.content_type'
- '@ezpublish.api.service.location'
- '%ez_systems.multifile_upload.location_mappings%'
- '%ez_systems.multifile_upload.default_mappings%'
- '%ez_systems.multifile_upload.fallback_content_type%'

ez_systems.multifile_upload.controller.rest:
class: '%ez_systems.multifile_upload.controller.rest.class%'
Expand All @@ -30,7 +34,7 @@ services:
- '@ezpublish.api.service.location'
- '@ez_systems.multifile_upload.service.permission_report'

# todo: remove once kernel bundle exposes PermissionResolver as a DI service
# todo: remove once kernel exposes PermissionResolver as a DI service
ez_systems.multifile_upload.service.permission_resolver:
class: '%ez_systems.multifile_upload.service.permission_resolver.class%'
factory: ['@ezpublish.api.inner_repository', 'getPermissionResolver']
Expand Down
3 changes: 1 addition & 2 deletions lib/API/Repository/PermissionReportServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ interface PermissionReportServiceInterface
/**
* Returns PermissionReport regarding content:create permission for the contentType in location.
*
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation
* @param \eZ\Publish\API\Repository\Values\User\UserReference $userReference
*
* @return \EzSystems\MultiFileUpload\API\Repository\Values\PermissionReport
*/
public function canUserCreateContent(ContentType $contentType, Location $parentLocation, UserReference $userReference = null);
public function canUserCreateContent(Location $parentLocation, UserReference $userReference = null);
}
7 changes: 2 additions & 5 deletions lib/API/Repository/Values/PermissionReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class PermissionReport extends ValueObject
{
/** @var int */
public $contentTypeId;

/** @var int */
public $parentLocationId;

Expand All @@ -21,6 +18,6 @@ class PermissionReport extends ValueObject
/** @var string */
public $function;

/** @var bool */
public $allowed;
/** @var string[] */
public $allowedContentTypes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ public function visit(Visitor $visitor, Generator $generator, $data)
$generator->startValueElement('parentLocationId', $data->parentLocationId);
$generator->endValueElement('parentLocationId');

$generator->startValueElement('contentTypeId', $data->contentTypeId);
$generator->endValueElement('contentTypeId');

$generator->startValueElement('module', $data->module);
$generator->endValueElement('module');

$generator->startValueElement('function', $data->function);
$generator->endValueElement('function');

$generator->startValueElement('allowed', $data->allowed);
$generator->endValueElement('allowed');
$generator->startList('allowedContentTypes');
foreach ($data->allowedContentTypes as $contentTypeIdentifier) {
$generator->startValueElement('allowedContentTypes', $contentTypeIdentifier);
$generator->endValueElement('allowedContentTypes');
}
$generator->endList('allowedContentTypes');

$generator->endObjectElement('PermissionReport');
}
Expand Down
102 changes: 84 additions & 18 deletions lib/Core/Repository/PermissionReportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace EzSystems\MultiFileUpload\Core\Repository;

use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\PermissionResolver;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\API\Repository\Values\User\UserReference;
use EzSystems\MultiFileUpload\API\Repository\PermissionReportServiceInterface;
use EzSystems\MultiFileUpload\API\Repository\Values\PermissionReport;
Expand All @@ -25,59 +25,125 @@ class PermissionReportService implements PermissionReportServiceInterface
/** @var \eZ\Publish\API\Repository\ContentService */
protected $contentService;

/** @var \eZ\Publish\API\Repository\ContentTypeService */
protected $contentTypeService;

/** @var \eZ\Publish\API\Repository\LocationService */
protected $locationService;

/** @var array */
protected $locationMappings = [];

/** @var array */
protected $defaultMappings = [];

/** @var array */
protected $fallbackContentType = [];

/**
* @param \eZ\Publish\API\Repository\PermissionResolver $permissionResolver
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param array $locationMappings
* @param array $defaultMappings
* @param array $fallbackContentType
*/
public function __construct(
PermissionResolver $permissionResolver,
ContentService $contentService,
LocationService $locationService
ContentTypeService $contentTypeService,
LocationService $locationService,
array $locationMappings,
array $defaultMappings,
array $fallbackContentType
) {
$this->permissionResolver = $permissionResolver;
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
$this->locationService = $locationService;
$this->locationMappings = $locationMappings;
$this->defaultMappings = $defaultMappings;
$this->fallbackContentType = $fallbackContentType;
}

/**
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
* @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation
* @param \eZ\Publish\API\Repository\Values\User\UserReference|null $userReference
*
* @return \EzSystems\MultiFileUpload\API\Repository\Values\PermissionReport
*/
public function canUserCreateContent(
ContentType $contentType,
Location $parentLocation,
UserReference $userReference = null
) {
$module = 'content';
$function = 'create';

if (null !== $userReference) {
$this->permissionResolver->setCurrentUserReference($userReference);
}

$contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $contentType->mainLanguageCode);
$locationCreateStruct = $this->locationService->newLocationCreateStruct($parentLocation->id);
$mappings = $this->getContentTypeIdentifierMappingsForLocation($parentLocation);
$allowedContentTypes = [];

$permissionReport = new PermissionReport([
'contentTypeId' => $contentType->id,
'parentLocationId' => $parentLocation->id,
'module' => $module,
'function' => $function,
'allowed' => $this->permissionResolver->canUser(
$module,
$function,
foreach ($mappings as $contentTypeIdentifier) {
$contentType = $this->contentTypeService->loadContentTypeByIdentifier(
$contentTypeIdentifier
);
$contentCreateStruct = $this->contentService->newContentCreateStruct(
$contentType,
$contentType->mainLanguageCode
);
$isAllowed = $this->permissionResolver->canUser(
'content',
'create',
$contentCreateStruct,
[$locationCreateStruct]
),
);

if ($isAllowed) {
$allowedContentTypes[] = $contentTypeIdentifier;
}
}

return new PermissionReport([
'parentLocationId' => $parentLocation->id,
'module' => 'content',
'function' => 'create',
'allowedContentTypes' => array_unique($allowedContentTypes),
]);
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Location $parentLocation
*
* @return array
*/
private function getContentTypeIdentifierMappingsForLocation(Location $parentLocation)
{
$locationContentType = $this->contentTypeService->loadContentType($parentLocation->getContentInfo()->contentTypeId);
$mappings = [];

foreach ($this->locationMappings as $locationMapping) {
if ($locationMapping['content_type_identifier'] === $locationContentType->identifier) {
$mappings = $locationMapping['mappings'];
break;
}
}

return $permissionReport;
$mappings = !empty($mappings)
? $mappings
: array_merge($this->defaultMappings, [$this->fallbackContentType]);

return $this->getUniqueContentTypeIdentifiersFromMappings($mappings);
}

/**
* @param array $mappings
*
* @return array
*/
private function getUniqueContentTypeIdentifiersFromMappings(array $mappings)
{
return array_unique(array_column($mappings, 'content_type_identifier'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testGetConfig()
'mappings' => [
[
'mime_types' => [
'application/msword'
'application/msword',
],
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
Expand All @@ -51,7 +51,7 @@ public function testGetConfig()
[
'mime_types' => [
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
],
'content_type_identifier' => 'file',
'content_field_identifier' => 'file',
Expand Down
Loading

0 comments on commit fe55a41

Please sign in to comment.