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

perf: optimize the permission check to use an in-memory object #889

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
69 changes: 50 additions & 19 deletions src/WorkspaceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\PermissionEvents;
use Pimcore\Bundle\DataHubBundle\GraphQL\Exception\ClientSafeException;
use Pimcore\Bundle\DataHubBundle\GraphQL\Exception\NotAllowedException;
use Pimcore\Cache;
use Pimcore\Cache\RuntimeCache;
use Pimcore\Db;
use Pimcore\Logger;
use Pimcore\Model\DataObject\OwnerAwareFieldInterface;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Element\Service;
Expand Down Expand Up @@ -170,6 +170,7 @@ public static function saveWorkspaces(Configuration $config, $workspaces)
}
}
}
Cache::clearTags(self::getCacheTags($config));
}

/**
Expand Down Expand Up @@ -210,6 +211,8 @@ public static function deleteConfiguration(Configuration $config)
$db->delete(Dao::TABLE_NAME_DOCUMENT, ['configuration' => $config->getName()]);
$db->delete(Dao::TABLE_NAME_ASSET, ['configuration' => $config->getName()]);
$db->delete(Dao::TABLE_NAME_DATAOBJECT, ['configuration' => $config->getName()]);

Cache::clearTags(self::getCacheTags($config));
}

/**
Expand Down Expand Up @@ -276,33 +279,61 @@ public static function isAllowed($element, Configuration $configuration, string
$parentIds[] = $element->getId();
}

try {
$db = Db::get();
$sql = 'SELECT `' . $type . '` FROM plugin_datahub_workspaces_' . $elementType . ' WHERE cid IN (' . implode(',', $parentIds) . ') AND configuration = ' . $db->quote($configuration->getName()) . ' AND `' . $type . '`=1 ORDER BY LENGTH(cpath) DESC LIMIT 1';
$permissionsParent = $db->fetchOne($sql);

if ($permissionsParent) {
$lookupTable = self::fetchLookupTable($elementType, $configuration);
foreach ($parentIds as $parentId) {
if (isset($lookupTable[$parentId]) && $lookupTable[$parentId][$type] === 1) {
return true;
}
}

// exception for read permission
if (empty($permissionsParent) && $type === 'read') {
// check for children with permissions
$path = $element->getRealFullPath() . '/';
$path = str_replace('_', '\\_', $path);
if ($element->getId() === 1) {
$path = '/';
}
if ($type === 'read') {
$path = $element->getRealFullPath() . '/';
$path = str_replace('_', '\\_', $path);
if ($element->getId() === 1) {
$path = '/';
}

$permissionsChildren = $db->fetchOne('SELECT ' . $type . ' FROM plugin_datahub_workspaces_' . $elementType . ' WHERE cpath LIKE ? AND configuration = ' . $db->quote($configuration->getName()) . ' AND ' . $type . ' = 1 LIMIT 1', [$path . '%']);
if ($permissionsChildren) {
foreach ($lookupTable as $row) {
if (strpos($row['cpath'], $path) === 0 && $row[$type] == 1) {
return true;
}
}
} catch (\Exception $e) {
Logger::warn('Unable to get permission ' . $type . ' for ' . $elementType . ' ' . $element->getId());
}

return false;
}

private static function fetchLookupTable(string $elementType, Configuration $configuration): array
{
$cacheKey = 'datahub_workspace_permissions_' . $configuration->getName() . '_' . $elementType;
if (RuntimeCache::isRegistered($cacheKey)) {
return RuntimeCache::load($cacheKey);
}

$cache = Cache::load($cacheKey);
if ($cache !== false) {
RuntimeCache::save($cache, $cacheKey);

return $cache;
}

$db = Db::get();
$sql = 'SELECT cid, cpath, `create`, `read`, `update`, `delete` FROM plugin_datahub_workspaces_' . $elementType . ' WHERE configuration = ?';
$rows = $db->fetchAllAssociative($sql, [$configuration->getName()]);

$lookupTable = [];
foreach ($rows as $row) {
$lookupTable[$row['cid']] = $row;
}

RuntimeCache::save($lookupTable, $cacheKey);
Cache::save($lookupTable, $cacheKey, self::getCacheTags($configuration));

return $lookupTable;
}

private static function getCacheTags(Configuration $configuration): array
{
return ['datahub_workspace_permissions_'. $configuration->getName()];
}
}
Loading