Fix studio simulation draft access
This commit is contained in:
@@ -517,21 +517,11 @@ class selfserve_studio_graph
|
||||
throw new \RuntimeException('lane_id is required for studio simulation.');
|
||||
}
|
||||
|
||||
$configSource = strtolower(trim((string)($payload['config_source'] ?? 'draft')));
|
||||
if (!in_array($configSource, ['draft', 'published'], true)) {
|
||||
$configSource = 'draft';
|
||||
}
|
||||
|
||||
$versioning = new selfserve_config_versioning();
|
||||
if ($configSource === 'published') {
|
||||
$version = $versioning->getPublishedV2Config($departmentId);
|
||||
$config = is_array($version['config'] ?? null) ? (array)$version['config'] : null;
|
||||
$versionId = isset($version['version_id']) ? (int)$version['version_id'] : null;
|
||||
} else {
|
||||
$version = $versioning->ensureDraftFromLegacy($departmentId, $userId, false);
|
||||
$config = is_array($version['config'] ?? null) ? (array)$version['config'] : $versioning->snapshotLegacyConfig($departmentId);
|
||||
$versionId = isset($version['id']) ? (int)$version['id'] : null;
|
||||
}
|
||||
$configContext = $this->loadSimulationConfig($departmentId, $payload, $permissions, $versioning);
|
||||
$configSource = $configContext['config_source'];
|
||||
$config = $configContext['config'];
|
||||
$versionId = $configContext['version_id'];
|
||||
|
||||
$includeHardware = filter_var($payload['include_hardware'] ?? true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
$includeHardware = $includeHardware !== false;
|
||||
@@ -592,6 +582,70 @@ class selfserve_studio_graph
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $payload
|
||||
* @param array<string,bool> $permissions
|
||||
* @return array{config_source:string,config:array<string,mixed>|null,version_id:int|null}
|
||||
*/
|
||||
private function loadSimulationConfig(
|
||||
int $departmentId,
|
||||
array $payload,
|
||||
array $permissions,
|
||||
selfserve_config_versioning $versioning
|
||||
): array {
|
||||
$configSource = $this->resolveSimulationConfigSource($payload, $permissions);
|
||||
|
||||
if ($configSource === 'published') {
|
||||
$version = $versioning->getPublishedV2Config($departmentId);
|
||||
return [
|
||||
'config_source' => $configSource,
|
||||
'config' => is_array($version['config'] ?? null) ? (array)$version['config'] : null,
|
||||
'version_id' => isset($version['version_id']) ? (int)$version['version_id'] : null,
|
||||
];
|
||||
}
|
||||
|
||||
$draft = (new selfserve_config_versions_o())->selectLatestByDepartmentAndStatus(
|
||||
$departmentId,
|
||||
selfserve_config_versioning::STATUS_DRAFT
|
||||
);
|
||||
if (!$draft->exists()) {
|
||||
return [
|
||||
'config_source' => $configSource,
|
||||
'config' => $versioning->snapshotLegacyConfig($departmentId),
|
||||
'version_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$config = (array)($draft->config_json->value() ?? []);
|
||||
if (!$versioning->isV2Config($config)) {
|
||||
$config = $versioning->migrateLegacyConfigToV2($config + ['department_id' => $departmentId]);
|
||||
}
|
||||
|
||||
return [
|
||||
'config_source' => $configSource,
|
||||
'config' => $config,
|
||||
'version_id' => (int)$draft->id,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $payload
|
||||
* @param array<string,bool> $permissions
|
||||
*/
|
||||
private function resolveSimulationConfigSource(array $payload, array $permissions): string
|
||||
{
|
||||
$configSource = strtolower(trim((string)($payload['config_source'] ?? 'draft')));
|
||||
if (!in_array($configSource, ['draft', 'published'], true)) {
|
||||
$configSource = 'draft';
|
||||
}
|
||||
|
||||
if ($configSource === 'draft' && !($permissions['can_view'] ?? false)) {
|
||||
throw new \RuntimeException('Draft studio simulation requires list_department_selfserve_config_versions permission.');
|
||||
}
|
||||
|
||||
return $configSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $payload
|
||||
* @param array<string,bool> $permissions
|
||||
@@ -605,21 +659,11 @@ class selfserve_studio_graph
|
||||
?callable $progressCallback = null
|
||||
): array
|
||||
{
|
||||
$configSource = strtolower(trim((string)($payload['config_source'] ?? 'draft')));
|
||||
if (!in_array($configSource, ['draft', 'published'], true)) {
|
||||
$configSource = 'draft';
|
||||
}
|
||||
|
||||
$versioning = new selfserve_config_versioning();
|
||||
if ($configSource === 'published') {
|
||||
$version = $versioning->getPublishedV2Config($departmentId);
|
||||
$config = is_array($version['config'] ?? null) ? (array)$version['config'] : null;
|
||||
$versionId = isset($version['version_id']) ? (int)$version['version_id'] : null;
|
||||
} else {
|
||||
$version = $versioning->ensureDraftFromLegacy($departmentId, $userId, false);
|
||||
$config = is_array($version['config'] ?? null) ? (array)$version['config'] : $versioning->snapshotLegacyConfig($departmentId);
|
||||
$versionId = isset($version['id']) ? (int)$version['id'] : null;
|
||||
}
|
||||
$configContext = $this->loadSimulationConfig($departmentId, $payload, $permissions, $versioning);
|
||||
$configSource = $configContext['config_source'];
|
||||
$config = $configContext['config'];
|
||||
$versionId = $configContext['version_id'];
|
||||
|
||||
$includeHardware = filter_var($payload['include_hardware'] ?? true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
$includeHardware = $includeHardware !== false;
|
||||
|
||||
Reference in New Issue
Block a user