diff --git a/app/Model/Database/KufrikConnectionFactory.php b/app/Model/Database/KufrikConnectionFactory.php index 34e2213..4c7df93 100644 --- a/app/Model/Database/KufrikConnectionFactory.php +++ b/app/Model/Database/KufrikConnectionFactory.php @@ -11,11 +11,13 @@ use Nette\Security\User; class KufrikConnectionFactory { private User $user; - private array $baseConfig; + private string $dbUser; + private string $dbPassword; - public function __construct(array $baseConfig, User $user) + public function __construct(string $dbUser, string $dbPassword, User $user) { - $this->baseConfig = $baseConfig; + $this->dbUser = $dbUser; + $this->dbPassword = $dbPassword; $this->user = $user; } @@ -27,23 +29,21 @@ class KufrikConnectionFactory $identity = $this->user->getIdentity(); - // 1. Dynamické načtení z identity (každý uživatel může mít jiný server i DB!) - $host = $identity->db_host ?? $this->baseConfig['host']; - $databaseName = $identity->db_name ?? $this->baseConfig['default_db']; - $firmaId = $identity->firma_id ?? null; + // Server i Databáze se načtou z Identity přihlášeného uživatele (z tabulky LOGIN) + $host = $identity->server ?? null; + $databaseName = $identity->database ?? null; - if (!$firmaId) { - throw new \LogicException('Uživatel nemá zvolenou žádnou aktivní firmu.'); + if (empty($host) || empty($databaseName)) { + throw new \LogicException('Přihlášený uživatel nemá v tabulce LOGIN definovaný SERVER nebo DATABASE.'); } - // 2. Sestavení DSN pro konkrétní SQL Server a DB - $dsn = "sqlsrv:Server={$host};Database={$databaseName}"; + // Dynamický DSN řetězec + $dsn = "sqlsrv:Server={$host};Database={$databaseName};LoginTimeout=3;"; - // Přihlašovací údaje k SQL serveru (buď společné ze souboru, nebo dynamické) - $connection = new Connection( + return new Connection( $dsn, - $this->baseConfig['username'], - $this->baseConfig['password'], + $this->dbUser, + $this->dbPassword, [ 'lazy' => false, 'driverOptions' => [ @@ -51,11 +51,6 @@ class KufrikConnectionFactory ] ] ); - - // 3. Nastavení RLS kontextu pro firmu - $connection->query('EXEC sp_set_session_context @key = N\'FirmaId\', @value = ?', $firmaId); - - return $connection; } public function createExplorer(): Explorer diff --git a/app/Model/Security/Authentificator.php b/app/Model/Security/Authentificator.php index 7298894..b5523e5 100644 --- a/app/Model/Security/Authentificator.php +++ b/app/Model/Security/Authentificator.php @@ -28,8 +28,8 @@ class Authenticator implements IAuthenticator // 1. Načtení uživatele podle tvého přesného schématu UZIVATEL $row = $this->masterDb->fetch( - 'SELECT ID, EMAIL, HESLO_HASH, JMENO, PRIJMENI, DATABASE_NAME, CONNECTION_STRING_KEY - FROM dbo.UZIVATEL + 'SELECT ID, EMAIL, HESLO_HASH, [DATABASE], [SERVER], FIRMA_ID + FROM dbo.LOGIN WHERE EMAIL = ? AND AKTIVNI = 1', $email ); @@ -43,18 +43,18 @@ class Authenticator implements IAuthenticator // 2. Aktualizace času posledního přihlášení $this->masterDb->query( - 'UPDATE dbo.UZIVATEL SET POSLEDNI_PRIHLASENI = SYSDATETIME() WHERE ID = ?', + 'UPDATE dbo.LOGIN SET POSLEDNI_PRIHLASENI = SYSDATETIME() WHERE ID = ?', $row->ID ); // 3. Načtení výchozí FIRMA_ID z vazební tabulky UZIVATEL_FIRMA (pokud existuje) - $firmaRow = $this->masterDb->fetch( - 'SELECT TOP 1 FIRMA_ID FROM dbo.UZIVATEL_FIRMA WHERE UZIVATEL_ID = ?', - $row->ID - ); + // $firmaRow = $this->masterDb->fetch( + // 'SELECT TOP 1 FIRMA_ID FROM dbo.UZIVATEL_FIRMA WHERE UZIVATEL_ID = ?', + // $row->ID + // ); // Fallback: Pokud vazební tabulka ještě není/nepoužívá se, použije se ID uživatele - $firmaId = $firmaRow ? $firmaRow->FIRMA_ID : $row->ID; + //$firmaId = $firmaRow ? $firmaRow->FIRMA_ID : $row->ID; // 4. Předání dat do Nette Identity return new SimpleIdentity( @@ -62,10 +62,9 @@ class Authenticator implements IAuthenticator null, // role [ 'email' => $row->EMAIL, - 'jmeno' => trim(($row->JMENO ?? '') . ' ' . ($row->PRIJMENI ?? '')), - 'db_name' => $row->DATABASE_NAME, // Nulllable -> KufrikConnectionFactory použije default_db - 'connection_key' => $row->CONNECTION_STRING_KEY, - 'firma_id' => $firmaId, + 'database' => $row->DATABASE, // Nulllable -> KufrikConnectionFactory použije default_db + 'server' => $row->SERVER, + 'firma_id' => (int) $row->FIRMA_ID, // Zpřístupní $user->identity->firma_id ] ); } diff --git a/app/UI/@layout.latte b/app/UI/@layout.latte index 1cd3c2d..01d363c 100644 --- a/app/UI/@layout.latte +++ b/app/UI/@layout.latte @@ -21,7 +21,7 @@
- +