Skip to content

Commit 5d5ffa9

Browse files
committed
OS2UOL-788 permissions changes and refactor
1 parent 8eed314 commit 5d5ffa9

File tree

7 files changed

+123
-95
lines changed

7 files changed

+123
-95
lines changed

webroot/modules/custom/os2uol_pretix/os2uol_pretix.permissions.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ view own pretix events:
77
title: 'View own Pretix events'
88
description: 'Only allow viewing Pretix events owned by the user.'
99

10+
add own pretix events:
11+
title: 'Add own Pretix events'
12+
description: 'Allows users to add dates to their own Pretix events.'
13+
restrict access: TRUE
14+
1015
add all pretix events:
1116
title: 'Add to all Pretix events'
12-
description: 'Allow adding to all Pretix events.'
17+
description: 'Allows users to add dates to any Pretix events.'
1318
restrict access: TRUE
1419

15-
add own pretix events:
16-
title: 'Add to own Pretix events'
17-
description: 'Only allow adding to Pretix events owned by the user.'
18-
1920
edit all pretix events:
2021
title: 'Edit all Pretix events'
2122
description: 'Allow editing all Pretix events.'
@@ -24,3 +25,8 @@ edit all pretix events:
2425
edit own pretix events:
2526
title: 'Edit own Pretix events'
2627
description: 'Only allow editing Pretix events owned by the user.'
28+
29+
remove pretix event connection:
30+
title: 'Remove Pretix event connection'
31+
description: 'Allows users to remove the connection between a Pretix event and a Drupal node.'
32+
restrict access: TRUE

webroot/modules/custom/os2uol_pretix/os2uol_pretix.routing.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ os2uol_pretix.webhooks.order:
2323
_access: 'TRUE'
2424
methods: [POST]
2525

26+
os2uol_pretix.add_date:
27+
path: '/node/{node}/pretix/add'
28+
defaults:
29+
_title: 'Add Date'
30+
requirements:
31+
_permission: 'add own pretix events'
32+
node: '\d+'
33+
2634
os2uol_pretix.remove_event_connection:
27-
path: '/admin/pretix/remove/{node}'
35+
path: '/node/{node}/pretix/remove'
2836
defaults:
29-
_controller: '\Drupal\os2uol_pretix\Controller\WebhooksController::unlinkEvent'
30-
_title: 'Remove Pretix Event Connection'
37+
_title: 'Remove Event Connection'
3138
requirements:
32-
_permission: 'administer nodes'
33-
node: \d+
39+
_permission: 'add all pretix events'
40+
node: '\d+'

webroot/modules/custom/os2uol_pretix/src/AbstractHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Drupal\ulf_pretix\Pretix;
3+
namespace Drupal\os2uol_pretix;
44

55
/**
66
* Abstract helper.

webroot/modules/custom/os2uol_pretix/src/Controller/WebhooksController.php

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,59 +45,25 @@ public function order($organizer, Request $request) {
4545
$payload = json_decode($request->getContent(), TRUE);
4646

4747
if (empty($payload)) {
48-
throw new BadRequestHttpException('Invalid or empty payload');
48+
throw new BadRequestHttpException('Invalid or empty payload');
4949
}
5050

5151
$action = $payload['action'] ?? NULL;
5252
switch ($action) {
53-
case PretixOrderManager::PRETIX_EVENT_ORDER_PLACED:
54-
case PretixOrderManager::PRETIX_EVENT_ORDER_CANCELED:
55-
return $this->orderManager->handleOrderUpdated($payload, $action);
53+
case PretixOrderManager::PRETIX_EVENT_ORDER_PLACED:
54+
case PretixOrderManager::PRETIX_EVENT_ORDER_CANCELED:
55+
return new JsonResponse($this->orderManager->handleOrderUpdated($payload, $action));
5656

57-
case 'event.soldout':
58-
return $this->handleSoldOutEvent($payload);
57+
case 'event.soldout':
58+
return new JsonResponse($this->orderManager->handleSoldOutEvent($payload));
5959

60-
case 'event.available':
61-
return $this->handleAvailableEvent($payload);
60+
case 'event.available':
61+
return new JsonResponse($this->orderManager->handleAvailableEvent($payload));
6262
}
6363

6464
return new JsonResponse(['status' => 'unhandled action']);
6565
}
6666

67-
/**
68-
* Handle the sold-out event.
69-
*/
70-
public function handleSoldOutEvent(array $payload) {
71-
$eventId = $payload['event']['id'];
72-
$nodeId = $this->getNodeIdFromPretixEventId($eventId);
73-
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
74-
75-
if ($node) {
76-
// Set the Boolean field for "Sold Out" to TRUE.
77-
$node->set('field_sold_out', TRUE);
78-
$node->save();
79-
}
80-
81-
return new JsonResponse(['status' => 'event marked as sold out']);
82-
}
83-
84-
/**
85-
* Handle the available event (when tickets become available again).
86-
*/
87-
public function handleAvailableEvent(array $payload) {
88-
$eventId = $payload['event']['id'];
89-
$nodeId = $this->getNodeIdFromPretixEventId($eventId);
90-
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
91-
92-
if ($node) {
93-
// Set the Boolean field for "Sold Out" to FALSE.
94-
$node->set('field_sold_out', FALSE);
95-
$node->save();
96-
}
97-
98-
return new JsonResponse(['status' => 'event marked as available']);
99-
}
100-
10167
/**
10268
* Unlinks the Pretix event from a Drupal node.
10369
*

webroot/modules/custom/os2uol_pretix/src/OrderHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

3-
namespace Drupal\ulf_pretix\Pretix;
3+
namespace Drupal\os2uol_pretix;
4+
5+
use Drupal\os2uol_pretix\AbstractHelper;
46

57
/**
68
* Pretix order helper.

webroot/modules/custom/os2uol_pretix/src/Plugin/Derivative/PretixLocalActions.php

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
<?php
22

3-
declare(strict_types = 1);
4-
53
namespace Drupal\os2uol_pretix\Plugin\Derivative;
64

75
use Drupal\Component\Plugin\Derivative\DeriverBase;
86
use Drupal\Component\Serialization\Json;
97
use Drupal\Core\Entity\EntityTypeManagerInterface;
108
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
119
use Drupal\Core\StringTranslation\StringTranslationTrait;
12-
use Drupal\os2uol_pretix\Plugin\Menu\LocalAction\PretixLocalAction;
13-
use Drupal\os2uol_pretix\Routing\PretixRouteProvider;
1410
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
use Drupal\Core\Session\AccountInterface;
1512

1613
/**
1714
* Pretix actions for entities.
@@ -22,18 +19,22 @@ class PretixLocalActions extends DeriverBase implements ContainerDeriverInterfac
2219

2320
protected string $basePluginId;
2421
protected EntityTypeManagerInterface $entityTypeManager;
22+
protected AccountInterface $currentUser;
2523

2624
/**
27-
* Creates a new ScheduledTransitionsLocalActions.
25+
* Constructs a new PretixLocalActions object.
2826
*
2927
* @param string $base_plugin_id
3028
* The base plugin ID.
3129
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
3230
* Entity type manager.
31+
* @param \Drupal\Core\Session\AccountInterface $current_user
32+
* The current user.
3333
*/
34-
public function __construct(string $base_plugin_id, EntityTypeManagerInterface $entity_type_manager) {
34+
public function __construct(string $base_plugin_id, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
3535
$this->basePluginId = $base_plugin_id;
3636
$this->entityTypeManager = $entity_type_manager;
37+
$this->currentUser = $current_user;
3738
}
3839

3940
/**
@@ -43,6 +44,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
4344
return new static(
4445
$base_plugin_id,
4546
$container->get('entity_type.manager'),
47+
$container->get('current_user')
4648
);
4749
}
4850

@@ -53,44 +55,46 @@ public function getDerivativeDefinitions($base_plugin_definition) {
5355
$this->derivatives = [];
5456

5557
foreach ($this->entityTypeManager->getDefinitions() as $entityType) {
56-
if ($entityType->hasLinkTemplate(PretixRouteProvider::LINK_TEMPLATE)) {
58+
if ($entityType->hasLinkTemplate('edit-form')) {
5759
$entityTypeId = $entityType->id();
5860

59-
// Add the "Add date" action
60-
$this->derivatives["$entityTypeId.add_pretix"] = [
61-
'route_name' => PretixRouteProvider::getPretixAddRouteName($entityType),
62-
'appears_on' => [PretixRouteProvider::getPretixRouteName($entityType)],
63-
'base_route' => "entity.$entityTypeId.canonical",
64-
'class' => PretixLocalAction::class,
65-
'title' => $this->t('Add date'),
66-
'options' => [
67-
'attributes' => [
68-
'class' => ['use-ajax'],
69-
'data-dialog-type' => 'modal',
70-
'data-dialog-options' => Json::encode([
71-
'width' => '80%',
72-
]),
61+
// Add the "Add date" action only if the user has the required permission.
62+
if ($this->currentUser->hasPermission('add own pretix events') || $this->currentUser->hasPermission('add all pretix events')) {
63+
$this->derivatives["$entityTypeId.add_pretix"] = [
64+
'route_name' => 'os2uol_pretix.add_date',
65+
'appears_on' => ['entity.' . $entityTypeId . '.pretix'],
66+
'base_route' => "entity.$entityTypeId.pretix",
67+
'title' => $this->t('Add date'),
68+
'options' => [
69+
'attributes' => [
70+
'class' => ['use-ajax'],
71+
'data-dialog-type' => 'modal',
72+
'data-dialog-options' => Json::encode([
73+
'width' => '80%',
74+
]),
75+
],
7376
],
74-
],
75-
] + $base_plugin_definition;
77+
] + $base_plugin_definition;
78+
}
7679

77-
// Add the "Remove event connection" action
78-
$this->derivatives["$entityTypeId.remove_pretix"] = [
79-
'route_name' => 'os2uol_pretix.remove_event_connection',
80-
'appears_on' => [PretixRouteProvider::getPretixRouteName($entityType)],
81-
'base_route' => "entity.$entityTypeId.canonical",
82-
'class' => PretixLocalAction::class,
83-
'title' => $this->t('Remove event connection'),
84-
'options' => [
85-
'attributes' => [
86-
'class' => ['use-ajax'],
87-
'data-dialog-type' => 'modal',
88-
'data-dialog-options' => Json::encode([
89-
'width' => '50%',
90-
]),
80+
// Add the "Remove event connection" action only if the user has the required permission.
81+
if ($this->currentUser->hasPermission('remove pretix event connection')) {
82+
$this->derivatives["$entityTypeId.remove_pretix"] = [
83+
'route_name' => 'os2uol_pretix.remove_event_connection',
84+
'appears_on' => ['entity.' . $entityTypeId . '.pretix'],
85+
'base_route' => "entity.$entityTypeId.pretix",
86+
'title' => $this->t('Remove event connection'),
87+
'options' => [
88+
'attributes' => [
89+
'class' => ['use-ajax'],
90+
'data-dialog-type' => 'modal',
91+
'data-dialog-options' => Json::encode([
92+
'width' => '50%',
93+
]),
94+
],
9195
],
92-
],
93-
] + $base_plugin_definition;
96+
] + $base_plugin_definition;
97+
}
9498
}
9599
}
96100

webroot/modules/custom/os2uol_pretix/src/PretixOrderManager.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Drupal\Core\Mail\MailManagerInterface;
1313
use Drupal\Core\Messenger\MessengerInterface;
1414
use Drupal\Core\Url;
15-
use Drupal\ulf_pretix\Pretix\OrderHelper;
1615
use Drupal\user\EntityOwnerTrait;
1716
use Drupal\user\UserInterface;
17+
use Drupal\os2uol_pretix\OrderHelper;
1818

1919
class PretixOrderManager extends PretixAbstractManager {
2020

@@ -176,7 +176,7 @@ public function handleOrderUpdated(array $payload, string $action): array {
176176

177177
$content = $this->renderOrder($order, $orderLines);
178178

179-
$to = $this->getMailRecipients($entity);
179+
$to = implode(',', $this->getMailRecipients($entity));
180180
$langcode = $entity->language()->getId();
181181
/** @var EntityOwnerTrait $entity_owner */
182182
$entity_owner = $entity;
@@ -430,14 +430,57 @@ protected function getEntity($organizerSlug, $eventSlug): ?EditorialContentEntit
430430
catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
431431
return NULL;
432432
}
433+
433434
$ids = $nodeStorage->getQuery()
434435
->condition('field_pretix_event_short_form', $eventSlug)
436+
->accessCheck(TRUE)
435437
->execute();
438+
436439
if (!empty($ids)) {
437440
/** @var EditorialContentEntityBase $entity */
438441
$entity = $nodeStorage->load($ids[array_key_first($ids)]);
439442
}
443+
440444
return $entity;
441445
}
442446

447+
public function handleSoldOutEvent(array $payload) {
448+
$eventId = $payload['event']['id'];
449+
$nodeId = $this->getNodeIdFromPretixEventId($eventId);
450+
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
451+
452+
if ($node) {
453+
// Set the Boolean field for "Sold Out" to TRUE.
454+
$node->set('field_sold_out', TRUE);
455+
$node->save();
456+
}
457+
458+
return new JsonResponse(['status' => 'event marked as sold out']);
459+
}
460+
461+
public function handleAvailableEvent(array $payload) {
462+
$eventId = $payload['event']['id'];
463+
$nodeId = $this->getNodeIdFromPretixEventId($eventId);
464+
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
465+
466+
if ($node) {
467+
// Set the Boolean field for "Sold Out" to FALSE.
468+
$node->set('field_sold_out', FALSE);
469+
$node->save();
470+
}
471+
472+
return new JsonResponse(['status' => 'event marked as available']);
473+
}
474+
475+
private function getNodeIdFromPretixEventId($eventId) {
476+
// Your entityQuery to map eventId to Drupal node.
477+
$query = \Drupal::entityQuery('node')
478+
->condition('type', ['internship', 'course_educators', 'course'], 'IN')
479+
->condition('field_pretix_event_id', $eventId)
480+
->range(0, 1);
481+
$nids = $query->execute();
482+
return !empty($nids) ? reset($nids) : NULL;
483+
}
484+
485+
443486
}

0 commit comments

Comments
 (0)