You are here

class ResponseSubscriber in Moderation Dashboard 8

Same name and namespace in other branches
  1. 2.0.x src/Routing/ResponseSubscriber.php \Drupal\moderation_dashboard\Routing\ResponseSubscriber

Response subscriber to redirect user login to the Moderation Dashboard.

Hierarchy

  • class \Drupal\moderation_dashboard\Routing\ResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ResponseSubscriber

1 string reference to 'ResponseSubscriber'
moderation_dashboard.services.yml in ./moderation_dashboard.services.yml
moderation_dashboard.services.yml
1 service uses ResponseSubscriber
moderation_dashboard.response_subscriber in ./moderation_dashboard.services.yml
Drupal\moderation_dashboard\Routing\ResponseSubscriber

File

src/Routing/ResponseSubscriber.php, line 18

Namespace

Drupal\moderation_dashboard\Routing
View source
class ResponseSubscriber implements EventSubscriberInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $conditionManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * ResponseSubscriber constructor.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Condition\ConditionManager $condition_manager
   *   The condition plugin manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(AccountProxyInterface $current_user, ConditionManager $condition_manager, ConfigFactoryInterface $config_factory) {
    $this->currentUser = $current_user;
    $this->conditionManager = $condition_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * Redirects user login to the Moderation Dashboard, when appropriate.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The response event.
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    $request = $event
      ->getRequest();
    $should_redirect = $this->configFactory
      ->get('moderation_dashboard.settings')
      ->get('redirect_on_login');
    if ($should_redirect && $response instanceof RedirectResponse) {
      $response_url_components = UrlHelper::parse($response
        ->getTargetUrl());
      $has_destination = isset($response_url_components['query']['destination']);
      $is_login = $request->request
        ->get('form_id') === 'user_login_form';
      $has_permission = $this->currentUser
        ->hasPermission('use moderation dashboard');
      $has_moderated_content_type = $this->conditionManager
        ->createInstance('has_moderated_content_type')
        ->execute();
      if ($has_permission && $is_login && $has_moderated_content_type && !$has_destination) {
        $url = Url::fromRoute('page_manager.page_view_moderation_dashboard_moderation_dashboard-panels_variant-0', [
          'user' => $this->currentUser
            ->id(),
        ]);
        $response
          ->setTargetUrl($url
          ->toString());
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      100,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResponseSubscriber::$conditionManager protected property The condition plugin manager.
ResponseSubscriber::$configFactory protected property The config factory.
ResponseSubscriber::$currentUser protected property The current user.
ResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ResponseSubscriber::onResponse public function Redirects user login to the Moderation Dashboard, when appropriate.
ResponseSubscriber::__construct public function ResponseSubscriber constructor.