AutologoutController.php in Automated Logout 8
File
src/Controller/AutologoutController.php
View source
<?php
namespace Drupal\autologout\Controller;
use Drupal\autologout\AutologoutManagerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\SettingsCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AutologoutController extends ControllerBase {
protected $autoLogoutManager;
protected $time;
public function __construct(AutologoutManagerInterface $autologout, TimeInterface $time) {
$this->autoLogoutManager = $autologout;
$this->time = $time;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('autologout.manager'), $container
->get('datetime.time'));
}
public function altLogout() {
$this->autoLogoutManager
->logout();
$redirect_url = $this
->config('autologout.settings')
->get('redirect_url');
$url = Url::fromUserInput($redirect_url, [
'absolute' => TRUE,
'query' => [
'autologout_timeout' => 1,
],
]);
return new RedirectResponse($url
->toString());
}
public function ajaxLogout() {
$this->autoLogoutManager
->logout();
$response = new AjaxResponse();
$response
->setStatusCode(200);
return $response;
}
public function ajaxSetLast() {
$_SESSION['autologout_last'] = $this->time
->getRequestTime();
$response = new AjaxResponse();
$markup = $this->autoLogoutManager
->createTimer();
$response
->addCommand(new ReplaceCommand('#timer', $markup));
return $response;
}
public function ajaxGetRemainingTime() {
$req = \Drupal::requestStack()
->getCurrentRequest();
$active = $req
->get('uactive');
$response = new AjaxResponse();
if (isset($active) && $active === "false") {
$response
->addCommand(new ReplaceCommand('#timer', 0));
$response
->addCommand(new SettingsCommand([
'time' => 0,
]));
return $response;
}
$time_remaining_ms = $this->autoLogoutManager
->getRemainingTime() * 1000;
$markup = $this->autoLogoutManager
->createTimer();
$response
->addCommand(new ReplaceCommand('#timer', $markup));
$response
->addCommand(new SettingsCommand([
'time' => $time_remaining_ms,
]));
return $response;
}
}