<?php
namespace App\Controller;
use App\Entity\Identity;
use App\Enum\IdentityRole;
use App\Factory\EntityManagerFactory;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
#use Swp\Entity\SafetyDocument;
use Obzervr\Entity\ObzervrSafetyDocument;
use Obzervr\Repository\ObzervrSafetyDocumentRepository;
class DefaultController extends Controller
{
public function __construct(EntityManagerInterface $em)
{
EntityManagerFactory::$em = $em;
// This REFUSES to work on class initiation here - NO idea - some more symfony bs. Calling a custom function from here also fails
//EntityManagerFactory::$obzem = $this->getDoctrine()->getManager('obzervr');
}
/**
* @Route("/", name="index", methods={"GET"})
*
* @param SessionInterface $session
*
* @return Response
*/
public function indexAction(EntityManagerInterface $em, SessionInterface $session): Response
{
$session->remove('query');
// List V1 AND V2 database and show in list
// 27-04-2020 Obzervr closed their SQL Server connection and it broke the Liveboards database selection screen.
// No more Obzervr:
// $obzEm = $this->getDoctrine()->getManager('obzervr');
// THIS WORKS TO PROVE CONNECTION TO SQL SERVER
// $em->getConnection()->connect();
// $connected = $em->getConnection()->isConnected();
//$aaaa = get_declared_classes(); //new ObzervrSafetyDocument();
// $em = $this->getDoctrine()->getManager('obzervr');
//$em = EntityManagerFactory::createObzervr('lol');
// $aaaa = $em->getConnection();//$em->getConfiguration();order
// EntityManagerFactory::$em = $em;
//$repo = $obzEm->getRepository(ObzervrSafetyDocument::class);
//$aaaa = $repo->findAll(array(), array('name' => 'ASC'));
// 27-04-2020 Obzervr closed their SQL Server connection and it broke the Liveboards database selection screen.
// No more Obzervr:
$databases = array(); //$databases = $obzEm->getConnection()->getSchemaManager()->listDatabases(); // works
/* Default SQL Server databases to blacklist
$blacklist = [
'master',
'tempdb',
'model',
'msdb',
];*/
$blacklist = [];
// SWP v1 databases and blacklists - merging above Obzervr versions
$databases = array_merge($databases, $this->getDoctrine()->getConnection()->getSchemaManager()->listDatabases());
// There is some silly drama with trying to run say "SHOW DATABASES LIKE '%_swp_com' (swp v2) via Symfony that I do not have
// time to deal with, so instead, we will continue to list all databases above, and instead, we will simply filter out
// all databases that dont end with _swp_com before blacklisting anything special
// Now only keep v2 databases
foreach ($databases as $key => $database)
if (!$this->get('App\Service\UtilsService')::isSwpV2Database($database))
unset($databases[$key]);
$blacklist = [
'swp_swp_com',
];
$databases = array_diff($databases, $blacklist);
if (!$this->isGranted(IdentityRole::ROLE_ADMIN)) {
if ($this->getUser() instanceof Identity) {
/** @var Identity $identity */
$identity = $this->getUser();
$databases = array_intersect($databases, $identity->getAccessTo());
} else {
// This can't really happen in production anyway
throw $this->createAccessDeniedException(); // @codeCoverageIgnore
}
}
return $this->render('index.html.twig', ['databases' => $databases]);
}
}