Skip to content
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
299 changes: 299 additions & 0 deletions ProcessMaker/ImportExport/AssignmentRulesReconciliation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
<?php

namespace ProcessMaker\ImportExport;

use DOMElement;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\User;
use ProcessMaker\Providers\WorkflowServiceProvider;

/**
* Import-only reconciliation for pm:assignmentRules (rule_expression tasks).
*
* The numeric assignee in an exported rule is a source-tenant ID and is never
* trusted on its own. Rules already carry a display identity in
* assignmentName, so import rebinds by that identity when it uniquely matches a
* target record; otherwise the rule is marked unresolved and its stale
* assignee is cleared.
*/
class AssignmentRulesReconciliation
{
public const ATTRIBUTE = 'assignmentRules';

/** Set on a rule when import could not bind a target user/group. */
public const KEY_UNRESOLVED = 'unresolved';

private const HUMAN_TASK_TAGS = ['task', 'userTask', 'manualTask'];

/**
* Cached labels for SelectUserGroup's inactive suffix ($t('Inactive')).
*
* @var array<int, string>|null
*/
private static ?array $inactiveStatusLabels = null;

/**
* @return array<int, object|array<string, mixed>>
*/
public static function decodeRules(?string $json): array
{
if ($json === null || $json === '') {
return [];
}

$list = json_decode($json);
if (!is_array($list)) {
return [];
}

return $list;
}

/**
* @param array<int, object|array<string, mixed>> $rules
*/
public static function encodeRules(array $rules): string
{
return json_encode(array_values($rules), JSON_UNESCAPED_UNICODE);
}

/**
* @param array<int, object|array<string, mixed>> $rules
* @return array<int, object|array<string, mixed>>
*/
public static function reconcileRulesAfterImport(array $rules): array
{
$reconciled = [];
foreach ($rules as $rule) {
$reconciled[] = self::reconcileRuleAfterImport($rule);
}

return $reconciled;
}

/**
* @param object|array<string, mixed> $rule
* @return object|array<string, mixed>
*/
public static function reconcileRuleAfterImport(object|array $rule): object|array
{
$type = self::ruleGet($rule, 'type');
$assignee = null;

if ($type === 'user') {
$username = self::usernameFromAssignmentName(self::ruleGet($rule, 'assignmentName'));
// username is unique in the users table and is the same key
// UserExporter's fallback matching already trusts on import.
$assignee = $username ? User::where('username', $username)->value('id') : null;
} elseif ($type === 'group') {
$name = self::ruleGet($rule, 'assignmentName');
$name = is_string($name) ? trim($name) : '';
// Group names are not unique in the schema, so only an
// unambiguous match may be rebound.
$groups = $name !== '' ? Group::where('name', $name)->get() : collect();
$assignee = $groups->count() === 1 ? $groups->first()->id : null;
} else {
return $rule;
}

return $assignee === null
? self::markRuleUnresolved($rule)
: self::markRuleResolved($rule, $assignee);
}

/**
* Extract the username from "Full Name (username)", tolerating the
* inactive suffix SelectUserGroup appends via $t('Inactive')
* The assignmentName was written in whatever locale the designer used,
* so import must recognize every available translation of that key.
*/
public static function usernameFromAssignmentName(mixed $assignmentName): ?string
{
if (!is_string($assignmentName) || !preg_match('/\(([^()]*)\)\s*$/', trim($assignmentName), $matches)) {
return null;
}

$username = self::stripInactiveStatusSuffix(trim($matches[1]));

return $username !== '' ? $username : null;
}

/**
* Labels used by SelectUserGroup for inactive users: " - " + $t('Inactive').
*
* @return array<int, string>
*/
public static function inactiveStatusLabels(): array
{
if (self::$inactiveStatusLabels !== null) {
return self::$inactiveStatusLabels;
}

$labels = ['Inactive'];
foreach (glob(resource_path('lang/*.json')) ?: [] as $file) {
$locale = basename($file, '.json');
$translated = trans('Inactive', [], $locale);
if (is_string($translated) && $translated !== '') {
$labels[] = $translated;
}
}

self::$inactiveStatusLabels = array_values(array_unique($labels));

return self::$inactiveStatusLabels;
}

/**
* Reset cached inactive labels (for tests).
*/
public static function clearInactiveStatusLabelCache(): void
{
self::$inactiveStatusLabels = null;
}

private static function stripInactiveStatusSuffix(string $username): string
{
foreach (self::inactiveStatusLabels() as $label) {
$quoted = preg_quote($label, '/');
$stripped = preg_replace('/\s+-\s+' . $quoted . '$/iu', '', $username);
if (is_string($stripped) && $stripped !== $username) {
return trim($stripped);
}
}

return $username;
}

/**
* @param object|array<string, mixed> $rule
* @return object|array<string, mixed>
*/
public static function markRuleUnresolved(object|array $rule): object|array
{
$rule = self::ruleSet($rule, 'assignee', null);
Comment thread
mcraeteisha marked this conversation as resolved.
Resolved
$rule = self::ruleSet($rule, self::KEY_UNRESOLVED, true);

return $rule;
}

/**
* Whether a rule was left unbound after import (or equivalent).
* Matching unresolved rules must not assign or fall through to the Process Manager.
*
* @param object|array<string, mixed> $rule
*/
public static function isUnresolvedRule(object|array $rule): bool
{
if (self::ruleGet($rule, self::KEY_UNRESOLVED) === true) {
return true;
}

$type = self::ruleGet($rule, 'type');
if ($type !== 'user' && $type !== 'group') {
return false;
}

$assignee = self::ruleGet($rule, 'assignee');

return $assignee === null || $assignee === '';
}

/**
* @return iterable<int, array{element: DOMElement, path: string, rules: array<int, object|array<string, mixed>>}>
*/
public static function collectAssignmentRulesFromDocument(\DOMDocument $document): iterable
{
$ns = WorkflowServiceProvider::PROCESS_MAKER_NS;
foreach (self::HUMAN_TASK_TAGS as $tag) {
foreach ($document->getElementsByTagName($tag) as $element) {
if (!$element instanceof DOMElement) {
continue;
}
if ($element->getAttributeNS($ns, 'assignment') !== 'rule_expression') {
continue;
}
$raw = $element->getAttributeNS($ns, self::ATTRIBUTE);
if ($raw === '') {
continue;
}
yield [
'element' => $element,
'path' => $element->getNodePath(),
'rules' => self::decodeRules($raw),
];
}
}
}

public static function writeRulesOnElement(DOMElement $element, array $rules): void
{
$ns = WorkflowServiceProvider::PROCESS_MAKER_NS;
$element->setAttributeNS($ns, self::ATTRIBUTE, self::encodeRules($rules));
}

/**
* Reconcile every rule_expression assignmentRules block on a BPMN document.
*/
public static function reconcileAssignmentRulesInDocument(\DOMDocument $document): bool
{
$updated = false;

foreach (self::collectAssignmentRulesFromDocument($document) as $item) {
$reconciled = self::reconcileRulesAfterImport($item['rules']);
self::writeRulesOnElement($item['element'], $reconciled);
$updated = true;
}

return $updated;
}

/**
* @param object|array<string, mixed> $rule
* @return object|array<string, mixed>
*/
private static function markRuleResolved(object|array $rule, mixed $assignee): object|array
{
$rule = self::ruleSet($rule, 'assignee', $assignee);

if (is_array($rule)) {
unset($rule[self::KEY_UNRESOLVED]);

return $rule;
}

if (property_exists($rule, self::KEY_UNRESOLVED)) {
unset($rule->{self::KEY_UNRESOLVED});
}

return $rule;
}

/**
* @param object|array<string, mixed> $rule
*/
private static function ruleGet(object|array $rule, string $key): mixed
{
if (is_array($rule)) {
return $rule[$key] ?? null;
}

return $rule->$key ?? null;
}

/**
* @param object|array<string, mixed> $rule
* @return object|array<string, mixed>
*/
private static function ruleSet(object|array $rule, string $key, mixed $value): object|array
{
if (is_array($rule)) {
$rule[$key] = $value;

return $rule;
}

$rule->$key = $value;

return $rule;
}
}
15 changes: 15 additions & 0 deletions ProcessMaker/ImportExport/Exporters/ProcessExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DOMXPath;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use ProcessMaker\ImportExport\AssignmentRulesReconciliation;
use ProcessMaker\ImportExport\DependentType;
use ProcessMaker\ImportExport\Psudomodels\Signal;
use ProcessMaker\ImportExport\Utils;
Expand Down Expand Up @@ -127,6 +128,11 @@ public function import($existingAssetInDatabase = null, $importingFromTemplate =

$this->importAssetsByMode();

// Assignment rules are embedded in BPMN (not separate assets), so they
// must be reconciled for saveModelOnly template editing as well as
// saveAllAssets imports.
$this->importAssignmentRules();

$process->save();

$process->notification_settings()->delete();
Expand Down Expand Up @@ -314,6 +320,15 @@ private function exportAssignments()
}
}

private function importAssignmentRules(): void
{
$document = $this->model->getDefinitions(true);

if (AssignmentRulesReconciliation::reconcileAssignmentRulesInDocument($document)) {
$this->model->bpmn = $document->saveXML();
}
}

private function exportScreens()
{
$tags = [
Expand Down
4 changes: 4 additions & 0 deletions ProcessMaker/Jobs/ImportProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use ProcessMaker\Events\ImportedScreenSaved;
use ProcessMaker\ImportExport\AssignmentRulesReconciliation;
use ProcessMaker\Managers\ExportManager;
use ProcessMaker\Models\AnonymousUser;
use ProcessMaker\Models\Process;
Expand Down Expand Up @@ -518,6 +519,7 @@ protected function saveScreen($screen)
return $new;
} catch (\Exception $e) {
Log::error('Import Screen: ' . $e->getMessage());

return false;
}
}
Expand Down Expand Up @@ -781,6 +783,8 @@ private function validatePackages($process)
*/
private function saveBpmn($process)
{
AssignmentRulesReconciliation::reconcileAssignmentRulesInDocument($this->definitions);

$this->new['process']->bpmn = $this->definitions->saveXML();
if (isset($process->cancel_screen_id)) {
$this->new['process']->cancel_screen_id = $process->cancel_screen_id;
Expand Down
Loading
Loading