ContactPageAccess.php 3.09 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\contact\Access\ContactPageAccess.
 */

namespace Drupal\contact\Access;

10
use Drupal\Core\Access\AccessResult;
11
use Drupal\Core\Config\ConfigFactoryInterface;
12
use Drupal\Core\Routing\Access\AccessInterface;
13
use Drupal\Core\Session\AccountInterface;
14
use Drupal\user\UserDataInterface;
15
use Drupal\user\UserInterface;
16 17 18 19

/**
 * Access check for contact_personal_page route.
 */
20
class ContactPageAccess implements AccessInterface {
21 22 23 24

  /**
   * The contact settings config object.
   *
25
   * @var \Drupal\Core\Config\ConfigFactoryInterface
26 27 28 29 30 31 32 33 34 35 36 37 38
   */
  protected $configFactory;

  /**
   * The user data service.
   *
   * @var \Drupal\user\UserDataInterface;
   */
  protected $userData;

  /**
   * Constructs a ContactPageAccess instance.
   *
39
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
40 41 42 43
   *   The config factory.
   * @param \Drupal\user\UserDataInterface $user_data
   *   The user data service.
   */
44
  public function __construct(ConfigFactoryInterface $config_factory, UserDataInterface $user_data) {
45 46 47 48 49
    $this->configFactory = $config_factory;
    $this->userData = $user_data;
  }

  /**
50 51 52 53 54 55 56
   * Checks access to the given user's contact page.
   *
   * @param \Drupal\user\UserInterface $user
   *   The user being contacted.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
57 58
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
59
   */
60 61
  public function access(UserInterface $user, AccountInterface $account) {
    $contact_account = $user;
62 63 64

    // Anonymous users cannot have contact forms.
    if ($contact_account->isAnonymous()) {
65
      return AccessResult::forbidden();
66 67
    }

68 69 70
    // Users may not contact themselves by default, hence this requires user
    // granularity for caching.
    $access = AccessResult::neutral()->cachePerUser();
71
    if ($account->id() == $contact_account->id()) {
72
      return $access;
73 74 75
    }

    // User administrators should always have access to personal contact forms.
76 77 78
    $permission_access = AccessResult::allowedIfHasPermission($account, 'administer users');
    if ($permission_access->isAllowed()) {
      return $access->orIf($permission_access);
79 80 81
    }

    // If requested user has been blocked, do not allow users to contact them.
82
    $access->addCacheableDependency($contact_account);
83
    if ($contact_account->isBlocked()) {
84
      return $access;
85 86
    }

87
    // Forbid access if the requested user has disabled their contact form.
88
    $account_data = $this->userData->get('contact', $contact_account->id(), 'enabled');
89 90
    if (isset($account_data) && !$account_data) {
      return $access;
91
    }
92

93 94
    // If the requested user did not save a preference yet, deny access if the
    // configured default is disabled.
95 96 97
    $contact_settings = $this->configFactory->get('contact.settings');
    $access->cacheUntilConfigurationChanges($contact_settings);
    if (!isset($account_data) && !$contact_settings->get('user_default_enabled')) {
98
      return $access;
99 100
    }

101
    return $access->orIf(AccessResult::allowedIfHasPermission($account, 'access user contact forms'));
102 103 104
  }

}