Skip to content
Snippets Groups Projects
Unverified Commit 00ed1ed4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2888853 by vaplas, Wim Leers: FileAccessControlHandler cacheability metadata inaccurate

parent 0d6d22af
No related branches found
No related tags found
No related merge requests found
......@@ -52,11 +52,11 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
// services can be more properly injected.
$allowed_fids = \Drupal::service('session')->get('anonymous_allowed_file_ids', []);
if (!empty($allowed_fids[$entity->id()])) {
return AccessResult::allowed();
return AccessResult::allowed()->addCacheContexts(['session', 'user']);
}
}
else {
return AccessResult::allowed();
return AccessResult::allowed()->addCacheContexts(['user']);
}
}
}
......
......@@ -123,4 +123,28 @@ public function testCreateAccess() {
$this->assertFalse($this->file->access('create'));
}
/**
* Tests cacheability metadata.
*/
public function testFileCacheability() {
$file = File::create([
'filename' => 'green-scarf',
'uri' => 'private://green-scarf',
'filemime' => 'text/plain',
'status' => FILE_STATUS_PERMANENT,
]);
$file->save();
\Drupal::service('session')->set('anonymous_allowed_file_ids', [$file->id() => $file->id()]);
$account = User::getAnonymousUser();
$file->setOwnerId($account->id())->save();
$this->assertSame(['session', 'user'], $file->access('view', $account, TRUE)->getCacheContexts());
$this->assertSame(['session', 'user'], $file->access('download', $account, TRUE)->getCacheContexts());
$account = $this->user1;
$file->setOwnerId($account->id())->save();
$this->assertSame(['user'], $file->access('view', $account, TRUE)->getCacheContexts());
$this->assertSame(['user'], $file->access('download', $account, TRUE)->getCacheContexts());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment