View source
<?php
namespace Drupal\social_event_invite;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\RequestStack;
class SocialEventInviteConfigOverride implements ConfigFactoryOverrideInterface {
protected $requestStack;
protected $emailValidator;
protected $database;
protected $configFactory;
public function __construct(RequestStack $request_stack, EmailValidatorInterface $email_validator, Connection $database, ConfigFactoryInterface $config_factory) {
$this->requestStack = $request_stack;
$this->emailValidator = $email_validator;
$this->database = $database;
$this->configFactory = $config_factory;
}
public function loadOverrides($names) {
$overrides = [];
$config_name = 'user.settings';
$verify_mail = $this->configFactory
->getEditable($config_name)
->get('verify_mail');
$event_invite = $this->configFactory
->getEditable('social_event_invite.settings');
$ignore_email_verification = $event_invite
->get('email_verification');
if (in_array($config_name, $names, TRUE) && $ignore_email_verification === TRUE && $verify_mail === TRUE) {
$request = $this->requestStack
->getCurrentRequest();
$invitee_mail = $request->query
->get('invitee_mail', '');
$destination = $request->query
->get('destination', '');
$is_valid = $this
->validateInviteData($invitee_mail, $destination);
if ($is_valid) {
$overrides[$config_name]['verify_mail'] = FALSE;
}
}
return $overrides;
}
public function validateInviteData($invitee_mail, $destination) {
if (empty($invitee_mail) || empty($destination)) {
return FALSE;
}
$invitee_mail = base64_decode(str_replace([
'-',
'_',
], [
'+',
'/',
], $invitee_mail));
if (!$this->emailValidator
->isValid($invitee_mail)) {
return FALSE;
}
$path = $this
->getPathByAlias($destination);
$path = explode('/', $path);
$id = array_pop($path);
if (empty($id) || !is_numeric($id)) {
return FALSE;
}
$query = $this->database
->select('event_enrollment__field_email', 'eefe');
$query
->fields('eefe', [
'entity_id',
]);
$query
->condition('eefe.field_email_value', $invitee_mail);
$query
->join('event_enrollment__field_request_or_invite_status', 'enfroi', 'eefe.entity_id = enfroi.entity_id');
$query
->condition('enfroi.field_request_or_invite_status_value', [
'4',
'5',
], 'IN');
$query
->join('event_enrollment__field_event', 'eefev', 'eefe.entity_id = eefev.entity_id');
$query
->condition('eefev.field_event_target_id', $id);
$invitations = $query
->execute()
->fetchField();
if (empty($invitations)) {
return FALSE;
}
else {
return TRUE;
}
}
private function getPathByAlias($alias) {
$query = $this->database
->select('path_alias', 'base_table');
$query
->condition('base_table.status', '1');
$query
->fields('base_table', [
'path',
]);
$query
->condition('base_table.alias', $this->database
->escapeLike($alias), 'LIKE');
if ($path = $query
->execute()
->fetchField()) {
return $path;
}
return $alias;
}
public function getCacheSuffix() {
return 'SocialGroupInviteConfigOverride';
}
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
}