<?php
/**
* User: Quentin
* Date: 20/10/2018
* Time: 19:09
*/
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use function Symfony\Component\String\s;
class AdminAuthSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return array(
KernelEvents::REQUEST => 'onKernelRequest',
);
}
public function onKernelRequest(RequestEvent $e) {
if (!s($e->getRequest()->getPathInfo())->startsWith('/admin')) {
return;
}
$authUser = 'adminsh';
$authPassword = 'Redact!onSH2021';
/** @see http://php.net/manual/fr/features.http-auth.php#73386 */
$valid_passwords = array($authUser => $authPassword);
$valid_users = array_keys($valid_passwords);
$user = $_SERVER['PHP_AUTH_USER'] ?? null;
$pass = $_SERVER['PHP_AUTH_PW'] ?? null;
$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);
if (!$validated) {
header('WWW-Authenticate: Basic realm="Admin"');
header('HTTP/1.0 401 Unauthorized');
die ("Not authorized");
}
}
}