ContactPageAccess.php 2.69 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\Config\ConfigFactoryInterface;
11
use Drupal\Core\Routing\Access\AccessInterface;
12
use Drupal\Core\Session\AccountInterface;
13
use Drupal\user\UserDataInterface;
14
use Drupal\user\UserInterface;
15 16 17 18

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

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

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

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

  /**
49 50 51 52 53 54 55 56 57
   * 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.
   *
   * @return string
   *   A \Drupal\Core\Access\AccessInterface constant value.
58
   */
59 60
  public function access(UserInterface $user, AccountInterface $account) {
    $contact_account = $user;
61 62 63 64 65 66 67

    // Anonymous users cannot have contact forms.
    if ($contact_account->isAnonymous()) {
      return static::DENY;
    }

    // Users may not contact themselves.
68
    if ($account->id() == $contact_account->id()) {
69 70 71 72
      return static::DENY;
    }

    // User administrators should always have access to personal contact forms.
73
    if ($account->hasPermission('administer users')) {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
      return static::ALLOW;
    }

    // If requested user has been blocked, do not allow users to contact them.
    if ($contact_account->isBlocked()) {
      return static::DENY;
    }

    // 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)) {
      return static::DENY;
    }
    // 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')) {
      return static::DENY;
    }

94
    return $account->hasPermission('access user contact forms') ? static::ALLOW : static::DENY;
95 96 97
  }

}