8
8
use Drupal \Core \Session \AccountProxyInterface ;
9
9
use Drupal \Core \StringTranslation \StringTranslationTrait ;
10
10
use Drupal \Core \Url ;
11
- use Drupal \key_auth \Authentication \Provider \KeyAuth ;
12
11
use Drupal \webform \WebformInterface ;
13
12
use Drupal \webform \WebformSubmissionInterface ;
14
- use Symfony \Component \HttpFoundation \RequestStack ;
15
13
16
14
/**
17
15
* Webform helper for helping with webforms.
@@ -33,28 +31,12 @@ class WebformHelper {
33
31
*/
34
32
private AccountProxyInterface $ currentUser ;
35
33
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
-
50
34
/**
51
35
* Constructor.
52
36
*/
53
- public function __construct (EntityTypeManagerInterface $ entityTypeManager , AccountProxyInterface $ currentUser, KeyAuth $ keyAuth , RequestStack $ requestStack ) {
37
+ public function __construct (EntityTypeManagerInterface $ entityTypeManager , AccountProxyInterface $ currentUser ) {
54
38
$ this ->entityTypeManager = $ entityTypeManager ;
55
39
$ this ->currentUser = $ currentUser ;
56
- $ this ->keyAuth = $ keyAuth ;
57
- $ this ->requestStack = $ requestStack ;
58
40
}
59
41
60
42
/**
@@ -241,8 +223,9 @@ private function getAllowedUsers(WebformInterface $webform): array {
241
223
/**
242
224
* Check if a user has access to a webform.
243
225
*
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.
246
229
*
247
230
* @param \Drupal\webform\WebformInterface $webform
248
231
* The webform.
@@ -260,7 +243,7 @@ public function hasWebformAccess(WebformInterface $webform, $user): bool {
260
243
261
244
$ allowedUsers = $ this ->getAllowedUsers ($ webform );
262
245
263
- return isset ($ allowedUsers [$ userId ]);
246
+ return isset ($ allowedUsers [$ userId ]) || $ webform -> access ( ' view_any ' ) ;
264
247
}
265
248
266
249
/**
@@ -275,40 +258,6 @@ private function loadUsers(array $spec): array {
275
258
->loadMultiple (array_column ($ spec , 'target_id ' ));
276
259
}
277
260
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
-
312
261
/**
313
262
* Return current user.
314
263
*
0 commit comments