Repository navigation
FOUR-26628 | Fix Error When Opening a Task with an Imported Assignment Rule #9110
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
Open
mcraeteisha
wants to merge
13
commits into
develop
Choose a base branch
from
bugfix/FOUR-26628
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
02adb1a
Reconcile imported assignment rules and UI warnings
mcraeteisha 6c4f656
Add tests for reconciling assignment rules
mcraeteisha 6386cf3
Normalize group assignment IDs
mcraeteisha 4ed176b
Guard unresolved assignment rules
mcraeteisha bcf3f58
Reconcile assignment rules in model-only imports
mcraeteisha 653c95b
Handle translated inactive user suffixes
mcraeteisha 9152e0a
Skip unresolved assignment rules
mcraeteisha f6b9587
Merge branch 'develop' into bugfix/FOUR-26628
mcraeteisha 7eff62f
Merge branch 'develop' into bugfix/FOUR-26628
mcraeteisha a7d2574
Merge branch 'develop' into bugfix/FOUR-26628
mcraeteisha eb3880a
Refactor functions for cognitive complexity
mcraeteisha 951734e
Sonarqube refactors
mcraeteisha af28016
Merge branch 'develop' into bugfix/FOUR-26628
mcraeteisha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
299 changes: 299 additions & 0 deletions
299
ProcessMaker/ImportExport/AssignmentRulesReconciliation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| $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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.