CommandContext::getOperationLogManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jabe\Impl\Interceptor;
4
5
use Jabe\Application\{
6
    InvocationContext,
7
    ProcessApplicationReferenceInterface
8
};
9
use Jabe\{
10
    AuthorizationException,
11
    BadUserRequestException,
12
    IdentityServiceInterface,
13
    OptimisticLockingException,
14
    ProcessEngineException,
15
    TaskAlreadyClaimedException
16
};
17
use Jabe\Impl\ProcessEngineLogger;
18
use Jabe\Impl\Cfg\{
19
    ProcessEngineConfigurationImpl,
20
    TransactionContextInterface,
21
    TransactionContextFactoryInterface
22
};
23
use Jabe\Impl\Context\{
24
    Context,
25
    ProcessApplicationContextUtil
26
};
27
use Jabe\Impl\Db\EntityManager\DbEntityManager;
28
use Jabe\Impl\Db\Sql\DbSqlSession;
29
use Jabe\Impl\Form\Entity\FormDefinitionManager;
30
//use Jabe\Impl\History\Event\HistoricDecisionInstanceManager;
31
use Jabe\Impl\Identity\{
32
    Authentication,
33
    ReadOnlyIdentityProviderInterface,
34
    WritableIdentityProviderInterface
35
};
36
use Jabe\Impl\JobExecutor\FailedJobCommandFactoryInterface;
37
use Jabe\Impl\Optimize\OptimizeManager;
38
use Jabe\Impl\Persistence\Entity\{
39
    AttachmentManager,
40
    AuthorizationManager,
41
    BatchManager,
42
    ByteArrayManager,
43
    CommentManager,
44
    DeploymentManager,
45
    EventSubscriptionManager,
46
    ExecutionManager,
47
    ExternalTaskManager,
48
    FilterManager,
49
    HistoricActivityInstanceManager,
50
    HistoricBatchManager,
51
    //HistoricCaseActivityInstanceManager,
52
    //HistoricCaseInstanceManager,
53
    HistoricDetailManager,
54
    HistoricExternalTaskLogManager,
55
    HistoricIdentityLinkLogManager,
56
    HistoricIncidentManager,
57
    HistoricJobLogManager,
58
    HistoricProcessInstanceManager,
59
    HistoricStatisticsManager,
60
    HistoricTaskInstanceManager,
61
    HistoricVariableInstanceManager,
62
    IdentityInfoManager,
63
    IdentityLinkManager,
64
    IncidentManager,
65
    JobDefinitionManager,
66
    JobEntity,
67
    JobManager,
68
    MeterLogManager,
69
    ProcessDefinitionManager,
70
    PropertyManager,
71
    ReportManager,
72
    ResourceManager,
73
    SchemaLogManager,
74
    StatisticsManager,
75
    TableDataManager,
76
    TaskManager,
77
    TaskReportManager,
78
    TenantManager,
79
    UserOperationLogManager,
80
    VariableInstanceManager
81
};
82
use Jabe\Impl\Util\EnsureUtil;
83
84
class CommandContext
85
{
86
    //private final static ContextLogger LOG = ProcessEngineLogger.CONTEXT_LOGGER;
87
88
    protected $authorizationCheckEnabled = true;
89
    protected $userOperationLogEnabled = true;
90
    protected $tenantCheckEnabled = true;
91
    protected $restrictUserOperationLogToAuthenticatedUsers;
92
93
    protected $transactionContext;
94
    protected $sessionFactories = [];
95
    protected $sessions = [];
96
    protected $sessionList = [];
97
    protected $processEngineConfiguration;
98
    protected $failedJobCommandFactory;
99
100
    protected $currentJob = null;
101
102
    protected $commandContextListeners = [];
103
104
    protected $operationId;
105
106
    public function __construct(ProcessEngineConfigurationImpl $processEngineConfiguration, ?TransactionContextFactory $transactionContextFactory = null)
107
    {
108
        if ($transactionContextFactory == null) {
109
            $transactionContextFactory = $processEngineConfiguration->getTransactionContextFactory();
0 ignored issues
show
Bug introduced by
The method getTransactionContextFactory() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            /** @scrutinizer ignore-call */ 
110
            $transactionContextFactory = $processEngineConfiguration->getTransactionContextFactory();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        }
111
        $this->processEngineConfiguration = $processEngineConfiguration;
112
        $this->failedJobCommandFactory = $processEngineConfiguration->getFailedJobCommandFactory();
0 ignored issues
show
Bug introduced by
The method getFailedJobCommandFactory() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        /** @scrutinizer ignore-call */ 
113
        $this->failedJobCommandFactory = $processEngineConfiguration->getFailedJobCommandFactory();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
        $this->sessionFactories = $processEngineConfiguration->getSessionFactories();
0 ignored issues
show
Bug introduced by
The method getSessionFactories() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
        /** @scrutinizer ignore-call */ 
114
        $this->sessionFactories = $processEngineConfiguration->getSessionFactories();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        $this->transactionContext = $transactionContextFactory->openTransactionContext($this);
115
        $this->restrictUserOperationLogToAuthenticatedUsers = $processEngineConfiguration->isRestrictUserOperationLogToAuthenticatedUsers();
0 ignored issues
show
Bug introduced by
The method isRestrictUserOperationLogToAuthenticatedUsers() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        /** @scrutinizer ignore-call */ 
116
        $this->restrictUserOperationLogToAuthenticatedUsers = $processEngineConfiguration->isRestrictUserOperationLogToAuthenticatedUsers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
    }
117
118
    public function performOperation(/*CmmnAtomicOperation*/$executionOperation, /*CaseExecutionEntity*/$execution): void
119
    {
120
        $targetProcessApplication = $this->getTargetProcessApplication($execution);
121
122
        if ($this->requiresContextSwitch($targetProcessApplication)) {
123
            $scope = $this;
124
            Context::executeWithinProcessApplication(function () use ($scope, $executionOperation, $execution) {
125
                $scope->performOperation($executionOperation, $execution);
126
            }, $targetProcessApplication, new InvocationContext($execution));
127
        } else {
128
            try {
129
                Context::setExecutionContext($execution);
130
                //LOG.debugExecutingAtomicOperation(executionOperation, execution);
131
                $executionOperation->execute($execution);
132
            } finally {
133
                Context::removeExecutionContext();
134
            }
135
        }
136
    }
137
138
    public function getProcessEngineConfiguration(): ProcessEngineConfigurationImpl
139
    {
140
        return $this->processEngineConfiguration;
141
    }
142
143
    protected function getTargetProcessApplication(/*CaseExecutionEntity*/$execution): ProcessApplicationReferenceInterface
144
    {
145
        return ProcessApplicationContextUtil::getTargetProcessApplication($execution);
146
    }
147
148
    protected function requiresContextSwitch(ProcessApplicationReferenceInterface $processApplicationReference): bool
149
    {
150
        return ProcessApplicationContextUtil::requiresContextSwitch($processApplicationReference);
151
    }
152
153
    public function close(CommandInvocationContext $commandInvocationContext): void
154
    {
155
        // the intention of this method is that all resources are closed properly,
156
        // even
157
        // if exceptions occur in close or flush methods of the sessions or the
158
        // transaction context.
159
160
        try {
161
            try {
162
                try {
163
                    if ($commandInvocationContext->getThrowable() == null) {
164
                        $this->fireCommandContextClose();
165
                        $this->flushSessions();
166
                    }
167
                } catch (\Throwable $exception) {
168
                    $commandInvocationContext->trySetThrowable($exception);
169
                } finally {
170
                    try {
171
                        if ($commandInvocationContext->getThrowable() == null) {
172
                            $this->transactionContext->commit();
173
                        }
174
                    } catch (\Throwable $exception) {
175
                        if (DbSqlSession::isCrdbConcurrencyConflict($exception)) {
0 ignored issues
show
Bug introduced by
The method isCrdbConcurrencyConflict() does not exist on Jabe\Impl\Db\Sql\DbSqlSession. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
                        if (DbSqlSession::/** @scrutinizer ignore-call */ isCrdbConcurrencyConflict($exception)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
                            //$exception = ProcessEngineLogger.PERSISTENCE_LOGGER.crdbTransactionRetryExceptionOnCommit(exception);
177
                        }
178
                        $commandInvocationContext->trySetThrowable($exception);
179
                    }
180
181
                    if ($commandInvocationContext->getThrowable() !== null) {
182
                        // fire command failed (must not fail itself)
183
                        $this->fireCommandFailed($commandInvocationContext->getThrowable());
184
185
                        if ($this->shouldLogCmdException()) {
186
                            if ($this->shouldLogInfo($commandInvocationContext->getThrowable())) {
187
                                //LOG.infoException(commandInvocationContext->getThrowable());
188
                            } elseif ($this->shouldLogFine($commandInvocationContext->getThrowable())) {
189
                                //LOG.debugException(commandInvocationContext->getThrowable());
190
                            } else {
191
                                //LOG.errorException(commandInvocationContext->getThrowable());
192
                            }
193
                        }
194
                        $this->transactionContext->rollback();
195
                    }
196
                }
197
            } catch (\Throwable $exception) {
198
                $commandInvocationContext->trySetThrowable($exception);
199
            } finally {
200
                $this->closeSessions($commandInvocationContext);
201
            }
202
        } catch (\Throwable $exception) {
203
            $commandInvocationContext->trySetThrowable(exception);
0 ignored issues
show
Bug introduced by
The constant Jabe\Impl\Interceptor\exception was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
204
        }
205
206
        // rethrow the original exception if there was one
207
        $commandInvocationContext->rethrow();
208
    }
209
210
    protected function shouldLogInfo(\Throwable $exception): bool
211
    {
212
        return $exception instanceof TaskAlreadyClaimedException;
213
    }
214
215
    protected function shouldLogFine(\Throwable $exception): bool
216
    {
217
        return $exception instanceof OptimisticLockingException ||
218
            $exception instanceof BadUserRequestException ||
219
            $exception instanceof AuthorizationException;
220
    }
221
222
    protected function shouldLogCmdException(): bool
223
    {
224
        //return ProcessEngineLogger::shouldLogCmdException($processEngineConfiguration);
225
        return false;
226
    }
227
228
    protected function fireCommandContextClose(): void
229
    {
230
        foreach ($this->commandContextListeners as $listener) {
231
            $listener->onCommandContextClose($this);
232
        }
233
    }
234
235
    protected function fireCommandFailed(\Throwable $t): void
236
    {
237
        foreach ($this->commandContextListeners as $listener) {
238
            try {
239
                $listener->onCommandFailed($this, $t);
240
            } catch (\Throwable $ex) {
241
                //LOG.exceptionWhileInvokingOnCommandFailed(t);
242
            }
243
        }
244
    }
245
246
    protected function flushSessions(): void
247
    {
248
        for ($i = 0; $i < count($this->sessionList); $i += 1) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
249
            $this->sessionList[$i]->flush();
250
        }
251
    }
252
253
    protected function closeSessions(CommandInvocationContext $commandInvocationContext): void
254
    {
255
        foreach ($this->sessionList as $session) {
256
            try {
257
                $session->close();
258
            } catch (\Throwable $exception) {
259
                $commandInvocationContext->trySetThrowable($exception);
260
            }
261
        }
262
    }
263
264
    public function getSession($sessionClass)
265
    {
266
        $session = null;
267
        if (array_key_exists($sessionClass, $this->sessions)) {
268
            $session = $this->sessions[$sessionClass];
269
        }
270
        if ($session == null) {
271
            $sessionFactory = null;
272
            if (array_key_exists($sessionClass, $this->sessionFactories)) {
273
                $sessionFactory = $this->sessionFactories[$sessionClass];
274
            }
275
            EnsureUtil::ensureNotNull("no session factory configured for " . $sessionClass, "sessionFactory", $sessionFactory);
276
            $session = $sessionFactory->openSession();
277
            $this->sessions[$sessionClass] = $session;
278
            array_unshift($this->sessionList, $session);
279
        }
280
281
        return $session;
282
    }
283
284
    public function addSession(string $sessionClass, DbSqlSession $session): void
285
    {
286
        $this->sessions[$sessionClass] = $session;
287
    }
288
289
    public function getDbEntityManager(): DbEntityManager
290
    {
291
        return $this->getSession(DbEntityManager::class);
292
    }
293
294
    public function getDbSqlSession(): DbSqlSession
295
    {
296
        return $this->getSession(DbSqlSession::class);
297
    }
298
299
    public function getDeploymentManager(): DeploymentManager
300
    {
301
        return $this->getSession(DeploymentManager::class);
302
    }
303
304
    public function getResourceManager(): ResourceManager
305
    {
306
        return $this->getSession(ResourceManager::class);
307
    }
308
309
    public function getByteArrayManager(): ByteArrayManager
310
    {
311
        return $this->getSession(ByteArrayManager::class);
312
    }
313
314
    public function getProcessDefinitionManager(): ProcessDefinitionManager
315
    {
316
        return $this->getSession(ProcessDefinitionManager::class);
317
    }
318
319
    public function getExecutionManager(): ExecutionManager
320
    {
321
        return $this->getSession(ExecutionManager::class);
322
    }
323
324
    public function getTaskManager(): TaskManager
325
    {
326
        return $this->getSession(TaskManager::class);
327
    }
328
329
    public function getTaskReportManager(): TaskReportManager
330
    {
331
        return $this->getSession(TaskReportManager::class);
332
    }
333
334
    public function getMeterLogManager(): MeterLogManager
335
    {
336
        return $this->getSession(MeterLogManager::class);
337
    }
338
339
    public function getIdentityLinkManager(): IdentityLinkManager
340
    {
341
        return $this->getSession(IdentityLinkManager::class);
342
    }
343
344
    public function getVariableInstanceManager(): VariableInstanceManager
345
    {
346
        return $this->getSession(VariableInstanceManager::class);
347
    }
348
349
    public function getHistoricProcessInstanceManager(): HistoricProcessInstanceManager
350
    {
351
        return $this->getSession(HistoricProcessInstanceManager::class);
352
    }
353
354
    /*public function getHistoricCaseInstanceManager(): HistoricCaseInstanceManager
355
    {
356
        return $this->getSession(HistoricCaseInstanceManager::class);
357
    }*/
358
359
    public function getHistoricDetailManager(): HistoricDetailManager
360
    {
361
        return $this->getSession(HistoricDetailManager::class);
362
    }
363
364
    public function getOperationLogManager(): UserOperationLogManager
365
    {
366
        return $this->getSession(UserOperationLogManager::class);
367
    }
368
369
    public function getHistoricVariableInstanceManager(): HistoricVariableInstanceManager
370
    {
371
        return $this->getSession(HistoricVariableInstanceManager::class);
372
    }
373
374
    public function getHistoricActivityInstanceManager(): HistoricActivityInstanceManager
375
    {
376
        return $this->getSession(HistoricActivityInstanceManager::class);
377
    }
378
379
    public function getHistoricCaseActivityInstanceManager(): HistoricCaseActivityInstanceManager
380
    {
381
        return $this->getSession(HistoricCaseActivityInstanceManager::class);
382
    }
383
384
    public function getHistoricTaskInstanceManager(): HistoricTaskInstanceManager
385
    {
386
        return $this->getSession(HistoricTaskInstanceManager::class);
387
    }
388
389
    public function getHistoricIncidentManager(): HistoricIncidentManager
390
    {
391
        return $this->getSession(HistoricIncidentManager::class);
392
    }
393
394
    public function getHistoricIdentityLinkManager(): HistoricIdentityLinkLogManager
395
    {
396
        return $this->getSession(HistoricIdentityLinkLogManager::class);
397
    }
398
399
    public function getJobManager(): JobManager
400
    {
401
        return $this->getSession(JobManager::class);
402
    }
403
404
    public function getBatchManager(): BatchManager
405
    {
406
        return $this->getSession(BatchManager::class);
407
    }
408
409
    public function getHistoricBatchManager(): HistoricBatchManager
410
    {
411
        return $this->getSession(HistoricBatchManager::class);
412
    }
413
414
    public function getJobDefinitionManager(): JobDefinitionManager
415
    {
416
        return $this->getSession(JobDefinitionManager::class);
417
    }
418
419
    public function getIncidentManager(): IncidentManager
420
    {
421
        return $this->getSession(IncidentManager::class);
422
    }
423
424
    public function getIdentityInfoManager(): IdentityInfoManager
425
    {
426
        return $this->getSession(IdentityInfoManager::class);
427
    }
428
429
    public function getAttachmentManager(): AttachmentManager
430
    {
431
        return $this->getSession(AttachmentManager::class);
432
    }
433
434
    public function getTableDataManager(): TableDataManager
435
    {
436
        return $this->getSession(TableDataManager::class);
437
    }
438
439
    public function getCommentManager(): CommentManager
440
    {
441
        return $this->getSession(CommentManager::class);
442
    }
443
444
    public function getEventSubscriptionManager(): EventSubscriptionManager
445
    {
446
        return $this->getSession(EventSubscriptionManager::class);
447
    }
448
449
    public function getSessionFactories(): array
450
    {
451
        return $this->sessionFactories;
452
    }
453
454
    public function getPropertyManager(): PropertyManager
455
    {
456
        return $this->getSession(PropertyManager::class);
457
    }
458
459
    public function getStatisticsManager(): StatisticsManager
460
    {
461
        return $this->getSession(StatisticsManager::class);
462
    }
463
464
    public function getHistoricStatisticsManager(): HistoricStatisticsManager
465
    {
466
        return $this->getSession(HistoricStatisticsManager::class);
467
    }
468
469
    public function getHistoricJobLogManager(): HistoricJobLogManager
470
    {
471
        return $this->getSession(HistoricJobLogManager::class);
472
    }
473
474
    public function getHistoricExternalTaskLogManager(): HistoricExternalTaskLogManager
475
    {
476
        return $this->getSession(HistoricExternalTaskLogManager::class);
477
    }
478
479
    public function getHistoricReportManager(): ReportManager
480
    {
481
        return $this->getSession(ReportManager::class);
482
    }
483
484
    public function getAuthorizationManager(): AuthorizationManager
485
    {
486
        return $this->getSession(AuthorizationManager::class);
487
    }
488
489
    public function getReadOnlyIdentityProvider(): ReadOnlyIdentityProvider
490
    {
491
        return $this->getSession(ReadOnlyIdentityProvider::class);
492
    }
493
494
    public function getWritableIdentityProvider(): WritableIdentityProvider
495
    {
496
        return $this->getSession(WritableIdentityProvider::class);
497
    }
498
499
    public function getTenantManager(): TenantManager
500
    {
501
        return $this->getSession(TenantManager::class);
502
    }
503
504
    public function getSchemaLogManager(): SchemaLogManager
505
    {
506
        return $this->getSession(SchemaLogManager::class);
507
    }
508
509
    public function getFormDefinitionManager(): FormDefinitionManager
510
    {
511
        return $this->getSession(FormDefinitionManager::class);
512
    }
513
514
    // CMMN /////////////////////////////////////////////////////////////////////
515
516
    /*public CaseDefinitionManager getCaseDefinitionManager() {
517
        return $this->getSession(CaseDefinitionManager::class);
518
    }
519
520
    public CaseExecutionManager getCaseExecutionManager() {
521
        return $this->getSession(CaseExecutionManager::class);
522
    }
523
524
    public CaseSentryPartManager getCaseSentryPartManager() {
525
        return $this->getSession(CaseSentryPartManager::class);
526
    }*/
527
528
    // DMN //////////////////////////////////////////////////////////////////////
529
530
    /*public DecisionDefinitionManager getDecisionDefinitionManager() {
531
        return $this->getSession(DecisionDefinitionManager::class);
532
    }
533
534
    public DecisionRequirementsDefinitionManager getDecisionRequirementsDefinitionManager() {
535
        return $this->getSession(DecisionRequirementsDefinitionManager::class);
536
    }
537
538
    public HistoricDecisionInstanceManager getHistoricDecisionInstanceManager() {
539
        return $this->getSession(HistoricDecisionInstanceManager::class);
540
    }*/
541
542
    // Filter ////////////////////////////////////////////////////////////////////
543
544
    public function getFilterManager(): FilterManager
545
    {
546
        return $this->getSession(FilterManager::class);
547
    }
548
549
    // External Tasks ////////////////////////////////////////////////////////////
550
551
    public function getExternalTaskManager(): ExternalTaskManager
552
    {
553
        return $this->getSession(ExternalTaskManager::class);
554
    }
555
556
    // getters and setters //////////////////////////////////////////////////////
557
558
    public function registerCommandContextListener(CommandContextListenerInterface $commandContextListener): void
559
    {
560
        $exists = false;
561
        foreach ($this->commandContextListeners as $listener) {
562
            if ($listener == $commandContextListener) {
563
                $exists = true;
564
            }
565
        }
566
        if (!$exists) {
567
            $this->commandContextListeners[] = $commandContextListener;
568
        }
569
    }
570
571
    public function getTransactionContext(): TransactionContextInterface
572
    {
573
        return $this->transactionContext;
574
    }
575
576
    public function getSessions(): array
577
    {
578
        return $this->sessions;
579
    }
580
581
    public function getFailedJobCommandFactory(): FailedJobCommandFactoryInterface
582
    {
583
        return $this->failedJobCommandFactory;
584
    }
585
586
    public function getAuthentication(): Authentication
587
    {
588
        $identityService = $this->processEngineConfiguration->getIdentityService();
0 ignored issues
show
Bug introduced by
The method getIdentityService() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

588
        /** @scrutinizer ignore-call */ 
589
        $identityService = $this->processEngineConfiguration->getIdentityService();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
589
        return $identityService->getCurrentAuthentication();
590
    }
591
592
    public function runWithoutAuthorization($command, ?CommandContext $commandContext = null)
593
    {
594
        if (is_callable($command)) {
595
            if ($commandContext == null) {
596
                $commandContext = Context::getCommandContext();
597
            }
598
            $authorizationEnabled = $commandContext->isAuthorizationCheckEnabled();
599
            try {
600
                $commandContext->disableAuthorizationCheck();
601
                return $command();
602
            } catch (\Exception $e) {
603
                throw new ProcessEngineException($e->getMessage(), $e);
604
            } finally {
605
                if ($authorizationEnabled) {
606
                    $commandContext->enableAuthorizationCheck();
607
                }
608
            }
609
        } elseif ($command instanceof CommandInterface) {
610
            $commandContext = Context::getCommandContext();
611
            return $this->runWithoutAuthorization(
612
                function () use ($command, $commandContext) {
613
                    return $command->execute($commandContext);
0 ignored issues
show
Bug introduced by
It seems like $commandContext can also be of type null; however, parameter $commandContext of Jabe\Impl\Interceptor\CommandInterface::execute() does only seem to accept Jabe\Impl\Interceptor\CommandContext, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

613
                    return $command->execute(/** @scrutinizer ignore-type */ $commandContext);
Loading history...
614
                },
615
                $commandContext
616
            );
617
        }
618
    }
619
620
    public function getAuthenticatedUserId(): ?string
621
    {
622
        $identityService = $this->processEngineConfiguration->getIdentityService();
623
        $currentAuthentication = $identityService->getCurrentAuthentication();
624
        if ($currentAuthentication == null) {
625
            return null;
626
        } else {
627
            return $currentAuthentication->getUserId();
628
        }
629
    }
630
631
    public function getAuthenticatedGroupIds(): array
632
    {
633
        $identityService = $this->processEngineConfiguration->getIdentityService();
634
        $currentAuthentication = $identityService->getCurrentAuthentication();
635
        if ($currentAuthentication == null) {
636
            return [];
637
        } else {
638
            return $currentAuthentication->getGroupIds();
639
        }
640
    }
641
642
    public function enableAuthorizationCheck(): void
643
    {
644
        $this->authorizationCheckEnabled = true;
645
    }
646
647
    public function disableAuthorizationCheck(): void
648
    {
649
        $this->authorizationCheckEnabled = false;
650
    }
651
652
    public function isAuthorizationCheckEnabled(): bool
653
    {
654
        return $this->authorizationCheckEnabled;
655
    }
656
657
    public function setAuthorizationCheckEnabled(bool $authorizationCheckEnabled): void
658
    {
659
        $this->authorizationCheckEnabled = $authorizationCheckEnabled;
660
    }
661
662
    public function enableUserOperationLog(): void
663
    {
664
        $this->userOperationLogEnabled = true;
665
    }
666
667
    public function disableUserOperationLog(): void
668
    {
669
        $this->userOperationLogEnabled = false;
670
    }
671
672
    public function isUserOperationLogEnabled(): bool
673
    {
674
        return $this->userOperationLogEnabled;
675
    }
676
677
    public function setLogUserOperationEnabled(bool $userOperationLogEnabled): void
678
    {
679
        $this->userOperationLogEnabled = $userOperationLogEnabled;
680
    }
681
682
    public function enableTenantCheck(): void
683
    {
684
        $this->tenantCheckEnabled = true;
685
    }
686
687
    public function disableTenantCheck(): void
688
    {
689
        $this->tenantCheckEnabled = false;
690
    }
691
692
    public function setTenantCheckEnabled(bool $tenantCheckEnabled): void
693
    {
694
        $this->tenantCheckEnabled = $tenantCheckEnabled;
695
    }
696
697
    public function isTenantCheckEnabled(): bool
698
    {
699
        return $this->tenantCheckEnabled;
700
    }
701
702
    public function getCurrentJob(): JobEntity
703
    {
704
        return $this->currentJob;
705
    }
706
707
    public function setCurrentJob(JobEntity $currentJob): void
708
    {
709
        $this->currentJob = $currentJob;
710
    }
711
712
    public function isRestrictUserOperationLogToAuthenticatedUsers(): bool
713
    {
714
        return $this->restrictUserOperationLogToAuthenticatedUsers;
715
    }
716
717
    public function setRestrictUserOperationLogToAuthenticatedUsers(bool $restrictUserOperationLogToAuthenticatedUsers): void
718
    {
719
        $this->restrictUserOperationLogToAuthenticatedUsers = $restrictUserOperationLogToAuthenticatedUsers;
720
    }
721
722
    public function getOperationId(): ?string
723
    {
724
        if (!$this->getOperationLogManager()->isUserOperationLogEnabled()) {
725
            return null;
726
        }
727
        if ($this->operationId == null) {
728
            $this->operationId = Context::getProcessEngineConfiguration()->getIdGenerator()->getNextId();
0 ignored issues
show
Bug introduced by
The method getIdGenerator() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

728
            $this->operationId = Context::getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getIdGenerator()->getNextId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
729
        }
730
        return $this->operationId;
731
    }
732
733
    public function setOperationId(string $operationId): void
734
    {
735
        $this->operationId = $operationId;
736
    }
737
738
    public function getOptimizeManager(): OptimizeManager
739
    {
740
        return $this->getSession(OptimizeManager::class);
741
    }
742
743
    public function executeWithOperationLogPrevented(CommandInterface $command): void
744
    {
745
        $initialLegacyRestrictions = $this->isRestrictUserOperationLogToAuthenticatedUsers();
746
747
        $this->disableUserOperationLog();
748
        $this->setRestrictUserOperationLogToAuthenticatedUsers(true);
749
750
        try {
751
            $command->execute($this);
752
        } finally {
753
            $this->enableUserOperationLog();
754
            $this->setRestrictUserOperationLogToAuthenticatedUsers($initialLegacyRestrictions);
755
        }
756
    }
757
}
758