Skip to content

Commit 1294b70

Browse files
authored
Merge pull request #8 from OS2Forms/feature/446-update-access-checks
#446: Updated access checks to allow view_all permission access to API
2 parents 798b204 + cd53560 commit 1294b70

File tree

5 files changed

+12
-69
lines changed

5 files changed

+12
-69
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ about writing changes to this log.
88

99
## [Unreleased]
1010

11+
## [2.0.3]
12+
13+
- Gave users with `view_any` submission permission access to API.
14+
1115
## [2.0.2]
1216

1317
- Added `OS2Forms Attachment` to attachments data.
@@ -32,7 +36,8 @@ about writing changes to this log.
3236

3337
- Release 1.0.0
3438

35-
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.2...HEAD
39+
[Unreleased]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.3...HEAD
40+
[2.0.3]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.2...2.0.3
3641
[2.0.2]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.1...2.0.2
3742
[2.0.1]: https://github.com/OS2Forms/os2forms_rest_api/compare/2.0.0...2.0.1
3843
[2.0.0]: https://github.com/OS2Forms/os2forms_rest_api/compare/1.1.0...2.0.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ Response:
166166
To give access to webforms, you need to specify a list of API users that are
167167
allowed to access a webform's data via the API.
168168

169-
Go to Settings > General > Third party settings > OS2Forms > REST API to specify
169+
Go to Settings > Access > View any submissions > Users to specify
170170
which users can access a webform's data.
171171

172172
### Technical details

os2forms_rest_api.module

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,3 @@ use Drupal\os2forms_rest_api\WebformHelper;
1818
function os2forms_rest_api_webform_third_party_settings_form_alter(array &$form, FormStateInterface $form_state): void {
1919
\Drupal::service(WebformHelper::class)->webformThirdPartySettingsFormAlter($form, $form_state);
2020
}
21-
22-
/**
23-
* Implements hook_file_download().
24-
*
25-
* @phpstan-return int|array<string, string>|null
26-
*/
27-
function os2forms_rest_api_file_download(string $uri) {
28-
return \Drupal::service(WebformHelper::class)->fileDownload($uri);
29-
}

os2forms_rest_api.services.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ services:
77
arguments:
88
- '@entity_type.manager'
99
- '@current_user'
10-
- '@key_auth.authentication.key_auth'
11-
- '@request_stack'
1210

1311
Drupal\os2forms_rest_api\EventSubscriber\WebformAccessEventSubscriber:
1412
arguments:

src/WebformHelper.php

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
use Drupal\Core\Session\AccountProxyInterface;
99
use Drupal\Core\StringTranslation\StringTranslationTrait;
1010
use Drupal\Core\Url;
11-
use Drupal\key_auth\Authentication\Provider\KeyAuth;
1211
use Drupal\webform\WebformInterface;
1312
use Drupal\webform\WebformSubmissionInterface;
14-
use Symfony\Component\HttpFoundation\RequestStack;
1513

1614
/**
1715
* Webform helper for helping with webforms.
@@ -33,28 +31,12 @@ class WebformHelper {
3331
*/
3432
private AccountProxyInterface $currentUser;
3533

36-
/**
37-
* The key authentication service.
38-
*
39-
* @var \Drupal\key_auth\Authentication\Provider\KeyAuth
40-
*/
41-
private KeyAuth $keyAuth;
42-
43-
/**
44-
* The request stack.
45-
*
46-
* @var \Symfony\Component\HttpFoundation\RequestStack
47-
*/
48-
private RequestStack $requestStack;
49-
5034
/**
5135
* Constructor.
5236
*/
53-
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, KeyAuth $keyAuth, RequestStack $requestStack) {
37+
public function __construct(EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser) {
5438
$this->entityTypeManager = $entityTypeManager;
5539
$this->currentUser = $currentUser;
56-
$this->keyAuth = $keyAuth;
57-
$this->requestStack = $requestStack;
5840
}
5941

6042
/**
@@ -241,8 +223,9 @@ private function getAllowedUsers(WebformInterface $webform): array {
241223
/**
242224
* Check if a user has access to a webform.
243225
*
244-
* A user has access to a webform if the list of allowed users is empty or the
245-
* user is included in the list.
226+
* A user has access to a webform if the user is
227+
* contained in the list of allowed users or the
228+
* user has been granted the 'view_any' webform permission.
246229
*
247230
* @param \Drupal\webform\WebformInterface $webform
248231
* The webform.
@@ -260,7 +243,7 @@ public function hasWebformAccess(WebformInterface $webform, $user): bool {
260243

261244
$allowedUsers = $this->getAllowedUsers($webform);
262245

263-
return isset($allowedUsers[$userId]);
246+
return isset($allowedUsers[$userId]) || $webform->access('view_any');
264247
}
265248

266249
/**
@@ -275,40 +258,6 @@ private function loadUsers(array $spec): array {
275258
->loadMultiple(array_column($spec, 'target_id'));
276259
}
277260

278-
/**
279-
* Implements hook_file_download().
280-
*
281-
* Note: This is only used to deny access to a file that is attached to a
282-
* webform (submission) that the user does not have permission to access.
283-
* Permission to access private files are handled elsewhere.
284-
*
285-
* @phpstan-return int|array<string, string>|null
286-
*/
287-
public function fileDownload(string $uri) {
288-
$request = $this->requestStack->getCurrentRequest();
289-
290-
// We are only concerned with users authenticated via Key Auth (cf.
291-
// os2forms_rest_api.services.yml).
292-
if ($user = $this->keyAuth->authenticate($request)) {
293-
// Find webform id from uri, see example uri.
294-
// @Example: private://webform/some_webform_id/119/some_file_name.png
295-
$pattern = '/private:\/\/webform\/(?<webform>[^\/]*)/';
296-
if (preg_match($pattern, $uri, $matches)) {
297-
$webform = $this->getWebform($matches['webform']);
298-
if (NULL !== $webform) {
299-
// Deny access to file if user does not have access to the webform.
300-
if (!$this->hasWebformAccess($webform, $user)) {
301-
return -1;
302-
}
303-
}
304-
}
305-
}
306-
307-
// We cannot deny access to the file. Let others handle the access control
308-
// for the (private) file.
309-
return NULL;
310-
}
311-
312261
/**
313262
* Return current user.
314263
*

0 commit comments

Comments
 (0)