88use Drupal \Core \Session \AccountProxyInterface ;
99use Drupal \Core \StringTranslation \StringTranslationTrait ;
1010use Drupal \Core \Url ;
11- use Drupal \key_auth \Authentication \Provider \KeyAuth ;
1211use Drupal \webform \WebformInterface ;
1312use 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