annotate core/modules/contact/src/Access/ContactPageAccess.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\contact\Access;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 7 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 8 use Drupal\Core\Session\AccountInterface;
Chris@0 9 use Drupal\user\UserDataInterface;
Chris@0 10 use Drupal\user\UserInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Access check for contact_personal_page route.
Chris@0 14 */
Chris@0 15 class ContactPageAccess implements AccessInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The contact settings config object.
Chris@0 19 *
Chris@0 20 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 21 */
Chris@0 22 protected $configFactory;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The user data service.
Chris@0 26 *
Chris@17 27 * @var \Drupal\user\UserDataInterface
Chris@0 28 */
Chris@0 29 protected $userData;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Constructs a ContactPageAccess instance.
Chris@0 33 *
Chris@0 34 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 35 * The config factory.
Chris@0 36 * @param \Drupal\user\UserDataInterface $user_data
Chris@0 37 * The user data service.
Chris@0 38 */
Chris@0 39 public function __construct(ConfigFactoryInterface $config_factory, UserDataInterface $user_data) {
Chris@0 40 $this->configFactory = $config_factory;
Chris@0 41 $this->userData = $user_data;
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Checks access to the given user's contact page.
Chris@0 46 *
Chris@0 47 * @param \Drupal\user\UserInterface $user
Chris@0 48 * The user being contacted.
Chris@0 49 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 50 * The currently logged in account.
Chris@0 51 *
Chris@0 52 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 53 * The access result.
Chris@0 54 */
Chris@0 55 public function access(UserInterface $user, AccountInterface $account) {
Chris@0 56 $contact_account = $user;
Chris@0 57
Chris@0 58 // Anonymous users cannot have contact forms.
Chris@0 59 if ($contact_account->isAnonymous()) {
Chris@0 60 return AccessResult::forbidden();
Chris@0 61 }
Chris@0 62
Chris@0 63 // Users may not contact themselves by default, hence this requires user
Chris@0 64 // granularity for caching.
Chris@0 65 $access = AccessResult::neutral()->cachePerUser();
Chris@0 66 if ($account->id() == $contact_account->id()) {
Chris@0 67 return $access;
Chris@0 68 }
Chris@0 69
Chris@0 70 // User administrators should always have access to personal contact forms.
Chris@0 71 $permission_access = AccessResult::allowedIfHasPermission($account, 'administer users');
Chris@0 72 if ($permission_access->isAllowed()) {
Chris@0 73 return $access->orIf($permission_access);
Chris@0 74 }
Chris@0 75
Chris@0 76 // If requested user has been blocked, do not allow users to contact them.
Chris@0 77 $access->addCacheableDependency($contact_account);
Chris@0 78 if ($contact_account->isBlocked()) {
Chris@0 79 return $access;
Chris@0 80 }
Chris@0 81
Chris@0 82 // Forbid access if the requested user has disabled their contact form.
Chris@0 83 $account_data = $this->userData->get('contact', $contact_account->id(), 'enabled');
Chris@0 84 if (isset($account_data) && !$account_data) {
Chris@0 85 return $access;
Chris@0 86 }
Chris@0 87
Chris@0 88 // If the requested user did not save a preference yet, deny access if the
Chris@0 89 // configured default is disabled.
Chris@0 90 $contact_settings = $this->configFactory->get('contact.settings');
Chris@0 91 $access->cacheUntilConfigurationChanges($contact_settings);
Chris@0 92 if (!isset($account_data) && !$contact_settings->get('user_default_enabled')) {
Chris@0 93 return $access;
Chris@0 94 }
Chris@0 95
Chris@0 96 return $access->orIf(AccessResult::allowedIfHasPermission($account, 'access user contact forms'));
Chris@0 97 }
Chris@0 98
Chris@0 99 }