ContactPageAccess.php 2.97 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
    }

    // Users may not contact themselves.
69
    if ($account->id() == $contact_account->id()) {
70
      return AccessResult::forbidden()->cachePerUser();
71 72 73
    }

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

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

    // If the requested user has disabled their contact form, do not allow users
    // to contact them.
    $account_data = $this->userData->get('contact', $contact_account->id(), 'enabled');
    if (isset($account_data) && empty($account_data)) {
90
      return $access;
91 92 93 94
    }
    // If the requested user did not save a preference yet, deny access if the
    // configured default is disabled.
    else if (!$this->configFactory->get('contact.settings')->get('user_default_enabled')) {
95
      return $access;
96 97
    }

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

}