Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / quickedit / src / Access / QuickEditEntityFieldAccessCheck.php
1 <?php
2
3 namespace Drupal\quickedit\Access;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\Access\AccessInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Entity\EntityInterface;
9
10 /**
11  * Access check for in-place editing entity fields.
12  */
13 class QuickEditEntityFieldAccessCheck implements AccessInterface, QuickEditEntityFieldAccessCheckInterface {
14
15   /**
16    * Checks Quick Edit access to the field.
17    *
18    * @param \Drupal\Core\Entity\EntityInterface $entity
19    *   The entity containing the field.
20    * @param string $field_name
21    *   The field name.
22    * @param string $langcode
23    *   The langcode.
24    * @param \Drupal\Core\Session\AccountInterface $account
25    *   The currently logged in account.
26    *
27    * @return \Drupal\Core\Access\AccessResultInterface
28    *   The access result.
29    *
30    * @todo Use the $account argument: https://www.drupal.org/node/2266809.
31    */
32   public function access(EntityInterface $entity, $field_name, $langcode, AccountInterface $account) {
33     if (!$this->validateRequestAttributes($entity, $field_name, $langcode)) {
34       return AccessResult::forbidden();
35     }
36
37     return $this->accessEditEntityField($entity, $field_name);
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function accessEditEntityField(EntityInterface $entity, $field_name) {
44     return $entity->access('update', NULL, TRUE)->andIf($entity->get($field_name)->access('edit', NULL, TRUE));
45   }
46
47   /**
48    * Validates request attributes.
49    */
50   protected function validateRequestAttributes(EntityInterface $entity, $field_name, $langcode) {
51     // Validate the field name and language.
52     if (!$field_name || !$entity->hasField($field_name)) {
53       return FALSE;
54     }
55     if (!$langcode || !$entity->hasTranslation($langcode)) {
56       return FALSE;
57     }
58
59     return TRUE;
60   }
61
62 }