ContactPageAccess.php 3.02 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\contact\Access;

5
use Drupal\Core\Access\AccessResult;
6
use Drupal\Core\Config\ConfigFactoryInterface;
7
use Drupal\Core\Routing\Access\AccessInterface;
8
use Drupal\Core\Session\AccountInterface;
9
use Drupal\user\UserDataInterface;
10
use Drupal\user\UserInterface;
11 12 13 14

/**
 * Access check for contact_personal_page route.
 */
15
class ContactPageAccess implements AccessInterface {
16 17 18 19

  /**
   * The contact settings config object.
   *
20
   * @var \Drupal\Core\Config\ConfigFactoryInterface
21 22 23 24 25 26
   */
  protected $configFactory;

  /**
   * The user data service.
   *
27
   * @var \Drupal\user\UserDataInterface
28 29 30 31 32 33
   */
  protected $userData;

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

  /**
45 46 47 48 49 50 51
   * 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.
   *
52 53
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
54
   */
55 56
  public function access(UserInterface $user, AccountInterface $account) {
    $contact_account = $user;
57 58 59

    // Anonymous users cannot have contact forms.
    if ($contact_account->isAnonymous()) {
60
      return AccessResult::forbidden();
61 62
    }

63 64 65
    // Users may not contact themselves by default, hence this requires user
    // granularity for caching.
    $access = AccessResult::neutral()->cachePerUser();
66
    if ($account->id() == $contact_account->id()) {
67
      return $access;
68 69 70
    }

    // User administrators should always have access to personal contact forms.
71 72 73
    $permission_access = AccessResult::allowedIfHasPermission($account, 'administer users');
    if ($permission_access->isAllowed()) {
      return $access->orIf($permission_access);
74 75 76
    }

    // If requested user has been blocked, do not allow users to contact them.
77
    $access->addCacheableDependency($contact_account);
78
    if ($contact_account->isBlocked()) {
79
      return $access;
80 81
    }

82
    // Forbid access if the requested user has disabled their contact form.
83
    $account_data = $this->userData->get('contact', $contact_account->id(), 'enabled');
84 85
    if (isset($account_data) && !$account_data) {
      return $access;
86
    }
87

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

96
    return $access->orIf(AccessResult::allowedIfHasPermission($account, 'access user contact forms'));
97 98 99
  }

}