|
4 | 4 |
|
5 | 5 | use Drupal\Core\Cache\CacheableResponse;
|
6 | 6 | use Drupal\Core\Controller\ControllerBase;
|
7 |
| -use Drupal\Core\Entity\ContentEntityInterface; |
8 | 7 | use Drupal\silverback_preview_link\QRCodeWithLogo;
|
9 | 8 | use Drupal\user\Entity\User;
|
10 | 9 | use Symfony\Component\HttpFoundation\JsonResponse;
|
@@ -33,26 +32,34 @@ public function hasAccess() {
|
33 | 32 |
|
34 | 33 | /**
|
35 | 34 | * Skip Drupal authentication if there is a valid preview token.
|
| 35 | + * |
| 36 | + * @todo: previously, this method used to also check if the preview access |
| 37 | + * token has been attached to an entity (the entity type and entity ids were |
| 38 | + * sent as parameters). This approach will change in the future (as part of a |
| 39 | + * bigger refactoring) where preview links won't be attached to content |
| 40 | + * entities anymore, so this method might change again. |
36 | 41 | */
|
37 | 42 | public function hasLinkAccess() {
|
38 | 43 | $requestContent = \Drupal::request()->getContent();
|
39 | 44 | $body = json_decode($requestContent, TRUE);
|
40 |
| - if ( |
41 |
| - !empty($body['preview_access_token']) && |
42 |
| - !empty($body['entity_id']) && |
43 |
| - !empty($body['entity_type_id']) |
44 |
| - ) { |
| 45 | + if (!empty($body['preview_access_token'])) { |
45 | 46 | try {
|
46 |
| - $entity = \Drupal::entityTypeManager()->getStorage($body['entity_type_id'])->load($body['entity_id']); |
47 |
| - if ($entity instanceof ContentEntityInterface) { |
48 |
| - $previewAccessChecker = \Drupal::service('access_check.silverback_preview_link'); |
49 |
| - $accessResult = $previewAccessChecker->access($entity, $body['preview_access_token']); |
50 |
| - if ($accessResult->isAllowed()) { |
51 |
| - return new JsonResponse([ |
52 |
| - 'access' => TRUE, |
53 |
| - ], 200); |
54 |
| - } |
| 47 | + $storage = \Drupal::entityTypeManager()->getStorage('silverback_preview_link'); |
| 48 | + $previewLink = $storage->loadByProperties(['token' => $body['preview_access_token']]); |
| 49 | + if (empty($previewLink)) { |
| 50 | + return new JsonResponse([ |
| 51 | + 'access' => FALSE, |
| 52 | + ], 403); |
55 | 53 | }
|
| 54 | + |
| 55 | + // @todo: optionally, we could also check if the link has expired. |
| 56 | + // Expired links should be, however, deleted by the cron job. As this |
| 57 | + // part of the code will probably suffer modifications during the next |
| 58 | + // bigger refactoring (see the todo in the method's description), we |
| 59 | + // will just check for now if the link simply exists. |
| 60 | + return new JsonResponse([ |
| 61 | + 'access' => TRUE, |
| 62 | + ], 200); |
56 | 63 | }
|
57 | 64 | catch (\Exception $e) {
|
58 | 65 | $this->getLogger('silverback_preview_link')->error($e->getMessage());
|
|
0 commit comments