login_destination.module 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @file
  4. * Control where users are directed to, once they login, register or logout.
  5. */
  6. use \Drupal\Core\Routing\RouteMatchInterface;
  7. use \Drupal\Core\Url;
  8. use \Drupal\Core\Session\AccountInterface;
  9. use \Drupal\Core\Entity\EntityInterface;
  10. use \Drupal\user\UserInterface;
  11. /**
  12. * Implements hook_help().
  13. */
  14. function login_destination_help($route_name, RouteMatchInterface $route_match) {
  15. switch ($route_name) {
  16. case 'help.page.login_destination':
  17. $output = '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Login Destination module allows you to customize the destination that the user is redirected to after logging in, registering to the site, using a one-time login link or logging out. The destination can be an internal page or an external URL. You may specify certain conditions like pages or user roles and make the destination depend upon them.') . '</p>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Creating login destinations') . '</dt>';
  22. $output .= '<dd>' . t('Users with sufficient permissions can create login destination through the !link. The page listing the login destinations provides an interface to add, edit and delete them', [
  23. '!link' => (string) \Drupal::l('Login destination page', Url::fromRoute('login_destination.list')),
  24. ]) . '</dd>';
  25. $output .= '<dt>' . t('Assigning destinations') . '</dt>';
  26. $output .= '<dd>' . t('You can add login destinations and specify the page where the user will be redirected when it logs in or logs out. You can also configure specific pages where the destination can work or not and select for what user roles the login destination applies') . '</dd>';
  27. $output .= '</dl>';
  28. return $output;
  29. case 'login_destination.list':
  30. $output = '<p>' . t('Login destination rules are evaluated each time a user logs in, registers to the site, uses a one-time login link or logs out. Each rule consists of the destination, path conditions and user roles conditions. First matching rule gets executed.') . '</p>';
  31. return $output;
  32. }
  33. }
  34. /**
  35. * Implements hook_user_login().
  36. */
  37. function login_destination_user_login(AccountInterface $account) {
  38. if (isset($account->user_is_new) && $account->user_is_new) {
  39. // User is just registered.
  40. login_destination_perform_redirect('registration', $account);
  41. }
  42. elseif (\Drupal::service('current_route_match')->getRouteName() === 'user.reset.login') {
  43. // User is used a one-time login link.
  44. login_destination_perform_redirect('one-time-login', $account);
  45. }
  46. else {
  47. login_destination_perform_redirect('login', $account);
  48. }
  49. }
  50. /**
  51. * Implements hook_user_logout().
  52. */
  53. function login_destination_user_logout(AccountInterface $account) {
  54. login_destination_perform_redirect('logout', $account);
  55. }
  56. /**
  57. * Implements hook_toolbar_alter().
  58. */
  59. function login_destination_toolbar_alter(&$items) {
  60. if (empty($items['user']['tray']['user_links'])) {
  61. return;
  62. }
  63. // Disable cache for user links in toolbar, to be able set current param.
  64. $items['user']['tray']['user_links']['#cache']['context'] = [];
  65. $items['user']['tray']['user_links']['#cache']['max-age'] = 0;
  66. if (\Drupal::currentUser()->isAnonymous()) {
  67. $url = &$items['user']['tray']['user_links']['#links']['login']['url'];
  68. // Change route name, since route "user.page" always redirects to "user.login".
  69. $url = Url::fromRoute('user.login');
  70. }
  71. else {
  72. $url = &$items['user']['tray']['user_links']['#links']['logout']['url'];
  73. }
  74. // Get current path.
  75. $current = \Drupal::service('path.current')->getPath();
  76. // Add current param to be able to evaluate previous page.
  77. $url->setOptions(['query' => ['current' => $current]]);
  78. }
  79. /**
  80. * Implements hook_entity_presave().
  81. */
  82. function login_destination_entity_presave(EntityInterface $entity) {
  83. // Verify that entity is a user entity, and this is new entity.
  84. if (!$entity instanceof UserInterface || !$entity->isNew()) {
  85. return;
  86. }
  87. // Save parameter to user entity, which will allow us understand that user
  88. // is just created. Do not need to save parameter, when needs verification
  89. // by administrator.
  90. $needs_verification = \Drupal::config('user.settings')->get('verify_mail');
  91. if ($needs_verification || !$entity->isActive()) {
  92. return;
  93. }
  94. $entity->user_is_new = TRUE;
  95. }
  96. /**
  97. * Evaluate rules and perform redirect.
  98. *
  99. * This function is intended to be used by external modules.
  100. *
  101. * @param string $trigger
  102. * Action of login destination rule.
  103. * @param \Drupal\Core\Session\AccountInterface $account
  104. * User Account.
  105. */
  106. function login_destination_perform_redirect($trigger, AccountInterface $account) {
  107. /** @var Drupal\login_destination\LoginDestinationManager $service */
  108. $service = \Drupal::service('login_destination.manager');
  109. $destination = $service->findDestination($trigger, $account);
  110. if ($destination) {
  111. $service->prepareDestination($destination);
  112. }
  113. }