Skip to content

Commit c01cfb9

Browse files
committed
#446: Updated access checks to allow view_all permission access to API
1 parent 798b204 commit c01cfb9

File tree

4 files changed

+11
-48
lines changed

4 files changed

+11
-48
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-
}

src/WebformHelper.php

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,9 @@ private function getAllowedUsers(WebformInterface $webform): array {
241241
/**
242242
* Check if a user has access to a webform.
243243
*
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.
244+
* A user has access to a webform if the user is
245+
* contained in the list of allowed users or the
246+
* user has been granted the 'view_any' webform permission.
246247
*
247248
* @param \Drupal\webform\WebformInterface $webform
248249
* The webform.
@@ -260,7 +261,7 @@ public function hasWebformAccess(WebformInterface $webform, $user): bool {
260261

261262
$allowedUsers = $this->getAllowedUsers($webform);
262263

263-
return isset($allowedUsers[$userId]);
264+
return isset($allowedUsers[$userId]) || $webform->access('view_any');
264265
}
265266

266267
/**
@@ -275,40 +276,6 @@ private function loadUsers(array $spec): array {
275276
->loadMultiple(array_column($spec, 'target_id'));
276277
}
277278

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-
312279
/**
313280
* Return current user.
314281
*

0 commit comments

Comments
 (0)