LoginDestinationManager.php in Login Destination 8.2
File
src/LoginDestinationManager.php
View source
<?php
namespace Drupal\login_destination;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\path_alias\AliasManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\login_destination\Entity\LoginDestination;
use Symfony\Component\HttpFoundation\RequestStack;
class LoginDestinationManager implements LoginDestinationManagerInterface {
protected $entityTypeManager;
protected $aliasManager;
protected $pathMatcher;
protected $currentPath;
protected $configFactory;
protected $requestStack;
protected $languageManager;
protected $token;
public function __construct(EntityTypeManagerInterface $entity_type_manager, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher, CurrentPathStack $current_path, ConfigFactoryInterface $config_factory, RequestStack $request_stack, LanguageManagerInterface $language_manager, Token $token) {
$this->entityTypeManager = $entity_type_manager;
$this->aliasManager = $alias_manager;
$this->pathMatcher = $path_matcher;
$this->currentPath = $current_path;
$this->configFactory = $config_factory;
$this->requestStack = $request_stack;
$this->languageManager = $language_manager;
$this->token = $token;
}
public function findDestination($trigger, AccountInterface $account) {
$destinations = $this->entityTypeManager
->getStorage('login_destination')
->loadMultiple();
uasort($destinations, '\\Drupal\\login_destination\\Entity\\LoginDestination::sort');
$path = $this
->getCurrentPath();
$path_alias = mb_strtolower($this->aliasManager
->getAliasByPath($path));
$user_roles = $account
->getRoles();
foreach ($destinations as $destination) {
if (!$destination
->isEnabled()) {
continue;
}
$destination_triggers = $destination
->getTriggers();
if (!in_array($trigger, $destination_triggers)) {
continue;
}
$destination_roles = $destination
->getRoles();
$role_match = array_intersect($user_roles, $destination_roles);
if (empty($role_match) && !empty($destination_roles)) {
continue;
}
$destination_language = $destination
->getLanguage();
$lang_code = $this->languageManager
->getCurrentLanguage()
->getId();
if ($destination_language != '' && $destination_language != $lang_code) {
continue;
}
$pages = mb_strtolower($destination
->getPages());
if (!empty($pages)) {
$type = $destination
->getPagesType();
$page_match = $this->pathMatcher
->matchPath($path_alias, $pages) || $this->pathMatcher
->matchPath($path, $pages);
if ($page_match && $type == $destination::REDIRECT_LISTED || !$page_match && $type == $destination::REDIRECT_NOT_LISTED) {
return $destination;
}
continue;
}
return $destination;
}
return FALSE;
}
public function prepareDestination(LoginDestination $destination) {
$config = $this->configFactory
->get('login_destination.settings');
if ($config
->get('preserve_destination')) {
$drupal_destination = $this->requestStack
->getCurrentRequest()->query
->get('destination');
if (UrlHelper::isExternal($drupal_destination)) {
$drupal_destination = NULL;
}
if (!empty($drupal_destination)) {
return;
}
}
$path = $this->token
->replace($destination
->getDestination());
if ($destination
->isDestinationCurrent()) {
$request = $this->requestStack
->getCurrentRequest();
$query = $request
->get('current');
$path_destination = !empty($query) ? $query : '';
}
else {
$path_destination = Url::fromUri($path)
->toString();
}
$this->requestStack
->getCurrentRequest()->query
->set('destination', $path_destination);
}
protected function getCurrentPath() {
$current = $this->requestStack
->getCurrentRequest()
->get('current', '');
if (empty($current)) {
$current = $this->currentPath
->getPath();
}
return $current;
}
}