Skip to content

Commit 319297b

Browse files
authored
Merge pull request #1642 from AmazeeLabs/preview_token_access_no_entity_check
Skip entity check for preview links in the preview controller
2 parents e1338c0 + 5fc8327 commit 319297b

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

packages/composer/amazeelabs/silverback_preview_link/src/Controller/PreviewController.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Drupal\Core\Cache\CacheableResponse;
66
use Drupal\Core\Controller\ControllerBase;
7-
use Drupal\Core\Entity\ContentEntityInterface;
87
use Drupal\silverback_preview_link\QRCodeWithLogo;
98
use Drupal\user\Entity\User;
109
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -33,26 +32,34 @@ public function hasAccess() {
3332

3433
/**
3534
* 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.
3641
*/
3742
public function hasLinkAccess() {
3843
$requestContent = \Drupal::request()->getContent();
3944
$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'])) {
4546
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);
5553
}
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);
5663
}
5764
catch (\Exception $e) {
5865
$this->getLogger('silverback_preview_link')->error($e->getMessage());

0 commit comments

Comments
 (0)