Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit cfb830a

Browse files
authored
OS2FORMS-371 Added easy access to assigned maestro task by token (#131)
* OS2FORMS-371 Added token for easy getting os2forms execute task link in mail handlers * Added lookup for queue id based on webform token * OS2FORMS-371 Added access bypassing based on webforms submission token. * OS2FORMS-371 Review fixes
1 parent ff334c6 commit cfb830a

File tree

6 files changed

+120
-30
lines changed

6 files changed

+120
-30
lines changed

os2forms_forloeb.module

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Drupal\webform\WebformInterface;
44
use Drupal\Core\Form\FormStateInterface;
5+
use Drupal\Core\Render\BubbleableMetadata;
6+
use Drupal\Core\Url;
57
use Drupal\maestro\Engine\MaestroEngine;
68
use Drupal\webform\Entity\WebformSubmission;
79
use Drupal\user\Entity\User;
@@ -201,7 +203,7 @@ function os2forms_forloeb_spv_fetch_entity_username($uniqueWebformIdentifier, $w
201203

202204
/**
203205
* Returns array of custom task-types for OS2forms
204-
*
206+
*
205207
*/
206208
function os2forms_forloeb_get_custom_task_types() {
207209
return ['MaestroWebformMultiple', 'MaestroWebformInherit'];
@@ -268,3 +270,59 @@ function os2forms_forloeb_form_alter(&$form, FormStateInterface $form_state, $fo
268270
function os2forms_forloeb_preprocess_page(&$variables) {
269271
$variables['#attached']['library'][] = 'os2forms_forloeb/os2forms_forloeb';
270272
}
273+
274+
/**
275+
* Implements hook_token_info_alter().
276+
*/
277+
function os2forms_forloeb_token_info_alter(&$data) {
278+
$data['tokens']['webform_submission']['os2forms_forloeb_execute_task'] = [
279+
'name' => t('Execute task path for webform submission'),
280+
'description' => t("The token that can be user to get path for webform submission redirect URL."),
281+
'type' => 'webform_submission',
282+
];
283+
}
284+
285+
/**
286+
* Implements hook_tokens().
287+
*
288+
* Provides token value for webform_submission:os2forms_forloeb_execute_task.
289+
*/
290+
function os2forms_forloeb_tokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
291+
$replacements = [];
292+
293+
if ($type === 'webform_submission' && !empty($data['webform_submission']) && isset($tokens['os2forms_forloeb_execute_task'])) {
294+
$replacements[$tokens['os2forms_forloeb_execute_task']] = Url::fromRoute(
295+
'os2forms_forloeb.forloeb_task_console_controller_execute',
296+
['os2forms-forloeb-ws-token' => $data['webform_submission']->getToken()],
297+
['absolute' => TRUE]
298+
)->toString(TRUE)->getGeneratedUrl();
299+
}
300+
301+
return $replacements;
302+
}
303+
304+
/**
305+
* Implements hook_entity_access().
306+
* Allows requests with tokens to view the entity.
307+
*/
308+
function os2forms_forloeb_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
309+
if ($operation == 'update' && $entity instanceof WebformSubmission) {
310+
$token = \Drupal::request()->query->get('os2forms-forloeb-ws-token');
311+
if ($token && $token === $entity->getToken()) {
312+
return \Drupal\Core\Access\AccessResult::allowed();
313+
}
314+
}
315+
}
316+
317+
/**
318+
* Implements hook_maestro_post_fetch_assigned_queue_tasks().
319+
*/
320+
function os2forms_forloeb_maestro_post_fetch_assigned_queue_tasks($userID, &$queueIDs) {
321+
$token = \Drupal::request()->query->get('os2forms-forloeb-ws-token', '');
322+
if ($token) {
323+
$forloebTaskConsole = Drupal::service('os2forms_forloeb.task_console');
324+
$queueRecord = $forloebTaskConsole->getQueueIdByWebformSubmissionToken($token);
325+
$queueIDs[] = $queueRecord->id();
326+
$queueIDs = array_unique($queueIDs);
327+
}
328+
}

os2forms_forloeb.routing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ os2forms_forloeb.forloeb_task_console_controller_execute:
44
_controller: '\Drupal\os2forms_forloeb\Controller\ForloebTaskConsoleController::execute'
55
_title: 'Execute task'
66
requirements:
7-
_permission: 'view maestro task console'
7+
_permission: 'access content'
88
options:
99
no_cache: TRUE

os2forms_forloeb.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ services:
44
arguments: ['os2forms_forloeb']
55
os2forms_forloeb.task_console:
66
class: Drupal\os2forms_forloeb\ForloebTaskConsole
7-
arguments: ['@entity_type.manager']
7+
arguments: ['@entity_type.manager', '@logger.channel.os2forms_forloeb']

src/Controller/ForloebTaskConsoleController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function execute() {
6767
$redirect_to = Url::fromRoute('maestro_taskconsole.taskconsole');
6868

6969
// Check webform submission token.
70-
$token = \Drupal::request()->query->get('token', '');
70+
$token = \Drupal::request()->query->get('os2forms-forloeb-ws-token', '');
7171
if ($token) {
7272
$queueRecord = $this->forloebTaskConsole->getQueueIdByWebformSubmissionToken($token);
7373
}
@@ -126,6 +126,9 @@ public function execute() {
126126
break;
127127

128128
case 'function':
129+
if ($token) {
130+
$query_options['os2forms-forloeb-ws-token'] = $token;
131+
}
129132
$redirect_to = Url::fromRoute('maestro.execute', $query_options);
130133
break;
131134
}

src/ForloebTaskConsole.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,43 @@
22

33
namespace Drupal\os2forms_forloeb;
44
use Drupal\Core\Entity\EntityTypeManagerInterface;
5+
use Drupal\Core\StringTranslation\StringTranslationTrait;
56
use Drupal\maestro\Engine\MaestroEngine;
7+
use Drupal\maestro\Entity\MaestroEntityIdentifiers;
8+
use Drupal\maestro\Entity\MaestroProcess;
69
use Drupal\webform\Entity\WebformSubmission;
10+
use Psr\Log\LoggerInterface;
711

812
/**
913
* Class ForloebTaskConsole.
1014
*/
1115
class ForloebTaskConsole {
1216

17+
use StringTranslationTrait;
18+
1319
/**
1420
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
1521
*
1622
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
1723
*/
1824
protected $entityTypeManager;
1925

26+
/**
27+
* Logger.
28+
*
29+
* @var \Psr\Log\LoggerInterface
30+
*/
31+
protected $logger;
32+
2033
/**
2134
* Constructs a new ForloebTaskConsole object.
2235
*/
23-
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
36+
public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger) {
2437
$this->entityTypeManager = $entity_type_manager;
38+
$this->logger = $logger;
2539
}
2640

41+
2742
/**
2843
* Gets MaestroQueue record by webforms submission token.
2944
*
@@ -36,31 +51,32 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager) {
3651
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
3752
*/
3853
public function getQueueIdByWebformSubmissionToken($token = '') {
39-
$engine = new MaestroEngine();
40-
// Fetch the user's queue items.
41-
$queueIDs = $engine->getAssignedTaskQueueIds(\Drupal::currentUser()->id());
54+
/** @var WebformSubmission $webform_submission */
55+
$webform_submissions = $this->entityTypeManager->getStorage('webform_submission')->loadByProperties(['token' => $token]);
4256

43-
foreach ($queueIDs as $queueID) {
44-
$this->entityTypeManager->getStorage('maestro_queue')->resetCache([$queueID]);
45-
/** @var \Drupal\maestro\Entity\MaestroQueue $queueRecord */
46-
$queueRecord = $this->entityTypeManager->getStorage('maestro_queue')->load($queueID);
47-
$processID = $engine->getProcessIdFromQueueId($queueID);
48-
$templateMachineName = $engine->getTemplateIdFromProcessId($processID);
49-
50-
// Get user input from 'inherit_webform_unique_id'
51-
$taskMachineName = $engine->getTaskIdFromQueueId($queueID);
52-
$task = $engine->getTemplateTaskByID($templateMachineName, $taskMachineName);
53-
54-
// Load its corresponding webform submission.
55-
$sid = $engine->getEntityIdentiferByUniqueID($processID, $task['data']['inherit_webform_unique_id'] ?? '');
56-
$webform_submission = $sid ? WebformSubmission::load($sid) : NULL;
57+
if (empty($webform_submissions)) {
58+
$this->logger->warning($this->t('Submission with token @token not found', ['@token' => $token]));
59+
return NULL;
60+
}
5761

58-
// Compare webform submission with token from request.
59-
if ($webform_submission && $webform_submission->getToken() == $token) {
60-
return $queueRecord;
61-
}
62+
$webform_submission = reset($webform_submissions);
63+
/** @var MaestroEntityIdentifiers $maestro_entity_identifier */
64+
$maestro_entity_identifiers = $this->entityTypeManager->getStorage('maestro_entity_identifiers')->loadByProperties(['entity_type' => 'webform_submission', 'entity_id' => $webform_submission->id(),]);
65+
$maestro_entity_identifier = reset($maestro_entity_identifiers);
66+
$processIDs = $maestro_entity_identifier->process_id->referencedEntities();
67+
if (empty($processIDs)) {
68+
$this->logger->warning($this->t('Process with entity type: webform_submission and entity_id: @entity_id not found', ['@entity_id' => $webform_submission->id()]));
69+
return NULL;
6270
}
6371

64-
return NULL;
72+
$processID = reset($processIDs);
73+
$maestro_queues = $this->entityTypeManager->getStorage('maestro_queue')->loadByProperties(['process_id' => $processID->id(), 'task_class_name' => 'MaestroWebformInherit',]);
74+
if (empty($maestro_queues)) {
75+
$this->logger->warning($this->t('Maestro queue with task_class_name: MaestroWebformInherit and process_id: @process_id not found', ['@process_id' => $processID->id()]));
76+
return NULL;
77+
}
78+
$maestro_queue = reset($maestro_queues);
79+
return $maestro_queue;
6580
}
81+
6682
}

src/Plugin/EngineTasks/MaestroWebformInheritTask.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\os2forms_forloeb\Plugin\EngineTasks;
44

5+
use Drupal\Core\Url;
56
use Drupal\webform\Entity\WebformSubmission;
67
use Drupal\webform\WebformSubmissionForm;
78
use Drupal\maestro_webform\Plugin\EngineTasks\MaestroWebformTask;
@@ -12,6 +13,7 @@
1213
use Drupal\Core\Ajax\AjaxResponse;
1314
use Drupal\Core\Ajax\ReplaceCommand;
1415
use Drupal\Core\Messenger;
16+
use Symfony\Component\HttpFoundation\RedirectResponse;
1517

1618
/**
1719
* Maestro Webform Task Plugin for Multiple Submissions.
@@ -83,7 +85,7 @@ public function getTaskEditForm(array $task, $templateMachineName) {
8385
* {@inheritDoc}
8486
*/
8587
public function prepareTaskForSave(array &$form, FormStateInterface $form_state, array &$task) {
86-
88+
8789
// Inherit from parent
8890
parent::prepareTaskForSave($form, $form_state, $task);
8991
// Add custom field(s) to the inherited prepareTaskForSave method.
@@ -102,7 +104,7 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
102104

103105
// Get user input from 'inherit_webform_unique_id'
104106
$webformInheritID = $task['data']['inherit_webform_unique_id'];
105-
107+
106108
// Load its corresponding webform submission.
107109
$sid = MaestroEngine::getEntityIdentiferByUniqueID($this->processID, $webformInheritID);
108110
if ($sid) {
@@ -151,6 +153,17 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
151153
$new_submission->bundle(), $taskUniqueSubmissionId, $sid
152154
);
153155

154-
return parent::getExecutableForm($modal, $parent);
156+
$form = parent::getExecutableForm($modal, $parent);
157+
// Catch os2forms-forloeb access token and pass it further.
158+
if ($form instanceof RedirectResponse && $token = \Drupal::request()->query->get('os2forms-forloeb-ws-token')) {
159+
// Check token to previous submission and update it to new one.
160+
if ($token === $webform_submission->getToken()) {
161+
$token = $new_submission->getToken();
162+
$url = Url::fromUserInput($form->getTargetUrl(), ['query' => ['os2forms-forloeb-ws-token' => $token]]);
163+
$form = new RedirectResponse($url->toString());
164+
}
165+
}
166+
167+
return $form;
155168
}
156169
}

0 commit comments

Comments
 (0)