Skip to content
Draft
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
49 changes: 30 additions & 19 deletions ProcessMaker/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use ProcessMaker\Models\TaskDraft;
use ProcessMaker\Models\UserResourceView;
use ProcessMaker\Nayra\Contracts\Bpmn\ScriptTaskInterface;
use ProcessMaker\Services\SmartExtractConfiguration;
use ProcessMaker\Traits\HasControllerAddons;
use ProcessMaker\Traits\SearchAutocompleteTrait;
use ProcessMaker\Traits\TaskControllerIndexMethods;
Expand All @@ -43,6 +44,10 @@ class TaskController extends Controller
'overdue' => 'Due',
];

public function __construct(private readonly SmartExtractConfiguration $smartExtractConfiguration)
{
}

public function index()
{
$routerPath = Request::route('router');
Expand Down Expand Up @@ -190,25 +195,7 @@ public function edit(ProcessRequestToken $task, string $preview = '')
'datetime_format',
]);
$userConfiguration = (new UserConfigurationController())->index();
$hitlEnabled = config('smart-extract.hitl_enabled', false) && $isSmartExtractTask;

// Build the iframe source
$iframeSrc = null;
if ($hitlEnabled) {
$dashboardUrl = config('smart-extract.dashboard_url');
$requestData = $task->processRequest->data ?? [];

$documentToken = $requestData['documentToken'] ?? null;
$fileId = $requestData['fileId'] ?? null;

if ($documentToken && $fileId && !empty($dashboardUrl)) {
$queryParams = http_build_query([
'documentToken' => $documentToken,
'fileId' => $fileId,
]);
$iframeSrc = $dashboardUrl . '?' . $queryParams;
}
}
[$hitlEnabled, $iframeSrc] = $this->smartExtractHitlConfiguration($task, $isSmartExtractTask);

return view('tasks.edit', [
'task' => $task,
Expand All @@ -231,6 +218,30 @@ public function edit(ProcessRequestToken $task, string $preview = '')
}
}

private function smartExtractHitlConfiguration(
ProcessRequestToken $task,
bool $isSmartExtractTask
): array {
$hitlEnabled = $this->smartExtractConfiguration->hitlEnabled() && $isSmartExtractTask;
if (!$hitlEnabled) {
return [false, null];
}

$dashboardUrl = $this->smartExtractConfiguration->dashboardUrl();
$requestData = $task->processRequest->data ?? [];
$documentToken = $requestData['documentToken'] ?? null;
$fileId = $requestData['fileId'] ?? null;

if (!$documentToken || !$fileId || empty($dashboardUrl)) {
return [true, null];
}

return [true, $dashboardUrl . '?' . http_build_query([
'documentToken' => $documentToken,
'fileId' => $fileId,
])];
}

public function quickFillEdit(ProcessRequestToken $task)
{
$screenVersion = $task->getScreenVersion();
Expand Down
3 changes: 2 additions & 1 deletion ProcessMaker/Http/Resources/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use ProcessMaker\Services\SmartExtractConfiguration;
use ProcessMaker\Traits\TaskResourceIncludes;

class Task extends ApiResource
Expand Down Expand Up @@ -118,7 +119,7 @@ private function addAssignableUsers(&$array, $include)

private function mergeHitlCaseNumber(array &$array): void
{
if (!config('smart-extract.hitl_enabled')) {
if (!app(SmartExtractConfiguration::class)->hitlEnabled()) {
return;
}

Expand Down
3 changes: 3 additions & 0 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use ProcessMaker\Providers\PermissionServiceProvider;
use ProcessMaker\Repositories\SettingsConfigRepository;
use ProcessMaker\Services\ConditionalRedirectService;
use ProcessMaker\Services\SmartExtractConfiguration;
use RuntimeException;
use Spatie\Multitenancy\Events\MadeTenantCurrentEvent;
use Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent;
Expand Down Expand Up @@ -122,6 +123,8 @@ public function register(): void
// Register our permission services
$this->app->register(PermissionServiceProvider::class);

$this->app->scoped(SmartExtractConfiguration::class);

$this->app->singleton(Managers\PackageManager::class, function () {
return new Managers\PackageManager();
});
Expand Down
2 changes: 0 additions & 2 deletions ProcessMaker/ScriptRunners/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,9 @@ private function getEnvironmentVariables($useEscape = true)
// Add the url to the host
if ($useEscape) {
$variablesParameter[] = 'HOST_URL=' . escapeshellarg(config('app.docker_host_url'));
$variablesParameter[] = 'SMART_EXTRACT_API_HOST=' . escapeshellarg(config('smart-extract.api_host'));
$variablesParameter[] = 'SMART_EXTRACT_REQUEST_TIMEOUT=' . escapeshellarg((string) config('smart-extract.request_timeout'));
} else {
$variablesParameter[] = 'HOST_URL=' . config('app.docker_host_url');
$variablesParameter[] = 'SMART_EXTRACT_API_HOST=' . config('smart-extract.api_host');
$variablesParameter[] = 'SMART_EXTRACT_REQUEST_TIMEOUT=' . config('smart-extract.request_timeout');
}

Expand Down
1 change: 0 additions & 1 deletion ProcessMaker/ScriptRunners/ScriptMicroserviceRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ private function getEnvironmentVariables(User $user)
$variablesParameter['API_HOST'] = config('app.docker_host_url') . '/api/1.0';
$variablesParameter['APP_URL'] = config('app.docker_host_url');
$variablesParameter['API_SSL_VERIFY'] = (config('app.api_ssl_verify') ? '1' : '0');
$variablesParameter['SMART_EXTRACT_API_HOST'] = config('smart-extract.api_host');
$variablesParameter['SMART_EXTRACT_REQUEST_TIMEOUT'] = config('smart-extract.request_timeout');
}

Expand Down
81 changes: 81 additions & 0 deletions ProcessMaker/Services/SmartExtractConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace ProcessMaker\Services;

use ProcessMaker\Models\EnvironmentVariable;

class SmartExtractConfiguration
{
public const API_HOST = 'SMART_EXTRACT_API_HOST';

public const CLIENT_ID = 'SMART_EXTRACT_CLIENT_ID';

public const CLIENT_SECRET = 'SMART_EXTRACT_CLIENT_SECRET';

public const DASHBOARD_URL = 'SMART_EXTRACT_DASHBOARD_URL';

public const HITL_ENABLED = 'SMART_EXTRACT_HITL_ENABLED';

private ?array $values = null;

public function apiHost(): ?string
{
return $this->stringValue(self::API_HOST);
}

public function clientId(): ?string
{
return $this->stringValue(self::CLIENT_ID);
}

public function clientSecret(): ?string
{
return $this->stringValue(self::CLIENT_SECRET);
}

public function dashboardUrl(): ?string
{
return $this->stringValue(self::DASHBOARD_URL);
}

public function hitlEnabled(): bool
{
$value = $this->stringValue(self::HITL_ENABLED);

if ($value === null) {
return false;
}

return filter_var(trim($value), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
}

private function stringValue(string $name): ?string
{
$value = $this->values()[$name] ?? null;

return is_string($value) && trim($value) !== '' ? $value : null;
}

private function values(): array
{
if ($this->values !== null) {
return $this->values;
}

$this->values = EnvironmentVariable::query()
->whereIn('name', [
self::API_HOST,
self::CLIENT_ID,
self::CLIENT_SECRET,
self::DASHBOARD_URL,
self::HITL_ENABLED,
])
->get()
->mapWithKeys(fn (EnvironmentVariable $variable) => [
$variable->name => $variable->value,
])
->all();

return $this->values;
}
}
3 changes: 2 additions & 1 deletion ProcessMaker/Traits/TaskControllerIndexMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ProcessMaker\Models\User;
use ProcessMaker\Package\SavedSearch\Models\SavedSearch;
use ProcessMaker\Query\SyntaxError;
use ProcessMaker\Services\SmartExtractConfiguration;

trait TaskControllerIndexMethods
{
Expand Down Expand Up @@ -161,7 +162,7 @@ private function excludeNonVisibleTasks($query, $request)
{
$nonSystem = filter_var($request->input('non_system'), FILTER_VALIDATE_BOOLEAN);
$allTasks = filter_var($request->input('all_tasks'), FILTER_VALIDATE_BOOLEAN);
$hitlEnabled = filter_var(config('smart-extract.hitl_enabled'), FILTER_VALIDATE_BOOLEAN);
$hitlEnabled = app(SmartExtractConfiguration::class)->hitlEnabled();
$includeScreen = filter_var($request->input('includeScreen'), FILTER_VALIDATE_BOOLEAN);
$query->when(!$allTasks, function ($query) use ($includeScreen) {
$query->where(function ($query) use ($includeScreen) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Tests\Unit\ProcessMaker\Http\Controllers;

use ProcessMaker\Http\Controllers\TaskController;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Services\SmartExtractConfiguration;
use ReflectionMethod;
use Tests\TestCase;

class TaskControllerSmartExtractTest extends TestCase
{
public function test_hitl_configuration_uses_runtime_database_values(): void
{
EnvironmentVariable::factory()->create([
'name' => SmartExtractConfiguration::HITL_ENABLED,
'value' => 'true',
]);
EnvironmentVariable::factory()->create([
'name' => SmartExtractConfiguration::DASHBOARD_URL,
'value' => 'https://dashboard.example.com/edit.html',
]);

$processRequest = new ProcessRequest([
'data' => [
'documentToken' => 'document-token',
'fileId' => 'file-123',
],
]);
$task = new ProcessRequestToken();
$task->setRelation('processRequest', $processRequest);

$controller = new TaskController(app(SmartExtractConfiguration::class));
$method = new ReflectionMethod(TaskController::class, 'smartExtractHitlConfiguration');
$method->setAccessible(true);

[$enabled, $iframeUrl] = $method->invoke($controller, $task, true);

$this->assertTrue($enabled);
$this->assertSame(
'https://dashboard.example.com/edit.html?documentToken=document-token&fileId=file-123',
$iframeUrl
);
}

public function test_hitl_configuration_fails_closed_when_disabled(): void
{
EnvironmentVariable::factory()->create([
'name' => SmartExtractConfiguration::HITL_ENABLED,
'value' => 'false',
]);

$task = new ProcessRequestToken();
$task->setRelation('processRequest', new ProcessRequest(['data' => []]));

$controller = new TaskController(app(SmartExtractConfiguration::class));
$method = new ReflectionMethod(TaskController::class, 'smartExtractHitlConfiguration');
$method->setAccessible(true);

$this->assertSame([false, null], $method->invoke($controller, $task, true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Tests\Unit\ProcessMaker\ScriptRunners;

use Illuminate\Support\Facades\Cache;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\ScriptExecutor;
use ProcessMaker\Models\User;
use ProcessMaker\ScriptRunners\Base;
use ProcessMaker\ScriptRunners\ScriptMicroserviceRunner;
use ProcessMaker\Services\SmartExtractConfiguration;
use ReflectionMethod;
use Tests\TestCase;

class SmartExtractEnvironmentVariablesTest extends TestCase
{
public function test_local_runner_preserves_database_api_host_without_duplicates(): void
{
$this->createApiHost();
$executor = ScriptExecutor::factory()->create(['language' => 'php']);
$runner = new class ($executor) extends Base {
public function config($code, array $dockerConfig)
{
return $dockerConfig;
}
};

$method = new ReflectionMethod(Base::class, 'getEnvironmentVariables');
$method->setAccessible(true);
$variables = $method->invoke($runner, false);

$apiHosts = array_values(array_filter(
$variables,
fn (string $variable) => str_starts_with($variable, SmartExtractConfiguration::API_HOST . '=')
));

$this->assertSame([
SmartExtractConfiguration::API_HOST . '=https://database.example.com',
], $apiHosts);
}

public function test_microservice_runner_preserves_database_api_host(): void
{
$this->createApiHost();
$script = Script::factory()->create(['language' => 'php']);
$user = User::factory()->create();
Cache::put('script-runner-' . $user->id, 'access-token');

$runner = new ScriptMicroserviceRunner($script);
$method = new ReflectionMethod(ScriptMicroserviceRunner::class, 'getEnvironmentVariables');
$method->setAccessible(true);
$variables = $method->invoke($runner, $user);

$this->assertSame(
'https://database.example.com',
$variables[SmartExtractConfiguration::API_HOST]
);
}

private function createApiHost(): void
{
EnvironmentVariable::factory()->create([
'name' => SmartExtractConfiguration::API_HOST,
'value' => 'https://database.example.com',
]);
}
}
Loading
Loading