Skip to content

Commit 2edeeae

Browse files
committed
renamed reAuth to reconnect
1 parent e6907e0 commit 2edeeae

File tree

8 files changed

+250
-58
lines changed

8 files changed

+250
-58
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ You can enable the following middleware using the "middlewares" config parameter
148148
- "dbAuth": Support for "Database Authentication"
149149
- "jwtAuth": Support for "JWT Authentication"
150150
- "basicAuth": Support for "Basic Authentication"
151+
- "reconnect": Reconnect to the database with different parameters
151152
- "authorization": Restrict access to certain tables or columns
152153
- "validation": Return input validation errors for custom rules
153154
- "ipAddress": Fill a protected field with the IP address on create
@@ -190,6 +191,12 @@ You can tune the middleware behavior using middleware specific configuration par
190191
- "basicAuth.mode": Set to "optional" if you want to allow anonymous access ("required")
191192
- "basicAuth.realm": Text to prompt when showing login ("Username and password required")
192193
- "basicAuth.passwordFile": The file to read for username/password combinations (".htpasswd")
194+
- "reconnect.driverHandler": Handler to implement retrieval of the database driver ("")
195+
- "reconnect.addressHandler": Handler to implement retrieval of the database address ("")
196+
- "reconnect.portHandler": Handler to implement retrieval of the database port ("")
197+
- "reconnect.databaseHandler": Handler to implement retrieval of the database name ("")
198+
- "reconnect.usernameHandler": Handler to implement retrieval of the database username ("")
199+
- "reconnect.passwordHandler": Handler to implement retrieval of the database password ("")
193200
- "authorization.tableHandler": Handler to implement table authorization rules ("")
194201
- "authorization.columnHandler": Handler to implement column authorization rules ("")
195202
- "authorization.recordHandler": Handler to implement record authorization filter rules ("")
@@ -850,6 +857,10 @@ You can parse this output to make form fields show up with a red border and thei
850857

851858
### Multi-tenancy support
852859

860+
Two forms of multi-tenancy are supported:
861+
862+
#### Multi-tenancy middleware
863+
853864
You may use the "multiTenancy" middleware when you have a multi-tenant database.
854865
If your tenants are identified by the "customer_id" column you can use the following handler:
855866

@@ -860,6 +871,10 @@ If your tenants are identified by the "customer_id" column you can use the follo
860871
This construct adds a filter requiring "customer_id" to be "12" to every operation (except for "create").
861872
It also sets the column "customer_id" on "create" to "12" and removes the column from any other write operation.
862873

874+
#### Reconnect middleware
875+
876+
877+
863878
### Prevent database scraping
864879

865880
You may use the "joinLimits" and "pageLimits" middleware to prevent database scraping.

api.php

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5171,23 +5171,27 @@ public function convertColumnValues(ReflectedTable $table, array &$columnValues)
51715171
class GenericDB
51725172
{
51735173
private $driver;
5174+
private $address;
5175+
private $port;
51745176
private $database;
5177+
private $username;
5178+
private $password;
51755179
private $pdo;
51765180
private $reflection;
51775181
private $definition;
51785182
private $conditions;
51795183
private $columns;
51805184
private $converter;
51815185

5182-
private function getDsn(string $address, int $port, string $database): string
5186+
private function getDsn(): string
51835187
{
51845188
switch ($this->driver) {
51855189
case 'mysql':
5186-
return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
5190+
return "$this->driver:host=$this->address;port=$this->port;dbname=$this->database;charset=utf8mb4";
51875191
case 'pgsql':
5188-
return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
5192+
return "$this->driver:host=$this->address port=$this->port dbname=$this->database options='--client_encoding=UTF8'";
51895193
case 'sqlsrv':
5190-
return "$this->driver:Server=$address,$port;Database=$database";
5194+
return "$this->driver:Server=$this->address,$this->port;Database=$this->database";
51915195
}
51925196
}
51935197

@@ -5235,22 +5239,58 @@ private function getOptions(): array
52355239
}
52365240
}
52375241

5238-
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
5242+
private function initPdo(): bool
52395243
{
5240-
$this->driver = $driver;
5241-
$this->database = $database;
5242-
$dsn = $this->getDsn($address, $port, $database);
5243-
$options = $this->getOptions();
5244-
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
5244+
if ($this->pdo) {
5245+
$result = $this->pdo->reconstruct($this->getDsn(), $this->username, $this->password, $this->getOptions());
5246+
} else {
5247+
$this->pdo = new LazyPdo($this->getDsn(), $this->username, $this->password, $this->getOptions());
5248+
$result = true;
5249+
}
52455250
$commands = $this->getCommands();
52465251
foreach ($commands as $command) {
52475252
$this->pdo->addInitCommand($command);
52485253
}
5249-
$this->reflection = new GenericReflection($this->pdo, $driver, $database);
5250-
$this->definition = new GenericDefinition($this->pdo, $driver, $database);
5251-
$this->conditions = new ConditionsBuilder($driver);
5252-
$this->columns = new ColumnsBuilder($driver);
5253-
$this->converter = new DataConverter($driver);
5254+
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database);
5255+
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database);
5256+
$this->conditions = new ConditionsBuilder($this->driver);
5257+
$this->columns = new ColumnsBuilder($this->driver);
5258+
$this->converter = new DataConverter($this->driver);
5259+
return $result;
5260+
}
5261+
5262+
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
5263+
{
5264+
$this->driver = $driver;
5265+
$this->address = $address;
5266+
$this->port = $port;
5267+
$this->database = $database;
5268+
$this->username = $username;
5269+
$this->password = $password;
5270+
$this->initPdo();
5271+
}
5272+
5273+
public function reconstruct(string $driver, string $address, int $port, string $database, string $username, string $password): bool
5274+
{
5275+
if ($driver) {
5276+
$this->driver = $driver;
5277+
}
5278+
if ($address) {
5279+
$this->address = $address;
5280+
}
5281+
if ($port) {
5282+
$this->port = $port;
5283+
}
5284+
if ($database) {
5285+
$this->database = $database;
5286+
}
5287+
if ($username) {
5288+
$this->username = $username;
5289+
}
5290+
if ($password) {
5291+
$this->password = $password;
5292+
}
5293+
return $this->initPdo();
52545294
}
52555295

52565296
public function pdo(): LazyPdo
@@ -6036,15 +6076,18 @@ private function pdo()
60366076
return $this->pdo;
60376077
}
60386078

6039-
public function reauthenticate(/*?string*/$user, /*?string*/ $password): bool
6079+
public function reconstruct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array()): bool
60406080
{
6081+
$this->dsn = $dsn;
60416082
$this->user = $user;
60426083
$this->password = $password;
6084+
$this->options = $options;
6085+
$this->commands = array();
60436086
if ($this->pdo) {
60446087
$this->pdo = null;
6045-
return false;
6088+
return true;
60466089
}
6047-
return true;
6090+
return false;
60486091
}
60496092

60506093
public function inTransaction(): bool
@@ -7739,7 +7782,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
77397782
}
77407783
}
77417784

7742-
// file: src/Tqdev/PhpCrudApi/Middleware/ReAuthMiddleware.php
7785+
// file: src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php
77437786
namespace Tqdev\PhpCrudApi\Middleware {
77447787

77457788
use Psr\Http\Message\ResponseInterface;
@@ -7751,7 +7794,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
77517794
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
77527795
use Tqdev\PhpCrudApi\Middleware\Router\Router;
77537796

7754-
class ReAuthMiddleware extends Middleware
7797+
class ReconnectMiddleware extends Middleware
77557798
{
77567799
private $reflection;
77577800
private $db;
@@ -7763,6 +7806,42 @@ public function __construct(Router $router, Responder $responder, array $propert
77637806
$this->db = $db;
77647807
}
77657808

7809+
private function getDriver(): string
7810+
{
7811+
$driverHandler = $this->getProperty('driverHandler', '');
7812+
if ($driverHandler) {
7813+
return call_user_func($driverHandler);
7814+
}
7815+
return '';
7816+
}
7817+
7818+
private function getAddress(): string
7819+
{
7820+
$addressHandler = $this->getProperty('addressHandler', '');
7821+
if ($addressHandler) {
7822+
return call_user_func($addressHandler);
7823+
}
7824+
return '';
7825+
}
7826+
7827+
private function getPort(): int
7828+
{
7829+
$portHandler = $this->getProperty('portHandler', '');
7830+
if ($portHandler) {
7831+
return call_user_func($portHandler);
7832+
}
7833+
return 0;
7834+
}
7835+
7836+
private function getDatabase(): string
7837+
{
7838+
$databaseHandler = $this->getProperty('databaseHandler', '');
7839+
if ($databaseHandler) {
7840+
return call_user_func($databaseHandler);
7841+
}
7842+
return '';
7843+
}
7844+
77667845
private function getUsername(): string
77677846
{
77687847
$usernameHandler = $this->getProperty('usernameHandler', '');
@@ -7783,10 +7862,14 @@ private function getPassword(): string
77837862

77847863
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
77857864
{
7865+
$driver = $this->getDriver();
7866+
$address = $this->getAddress();
7867+
$port = $this->getPort();
7868+
$database = $this->getDatabase();
77867869
$username = $this->getUsername();
77877870
$password = $this->getPassword();
7788-
if ($username && $password) {
7789-
$this->db->pdo()->reauthenticate($username, $password);
7871+
if ($driver || $address || $port || $database || $username || $password) {
7872+
$this->db->reconstruct($driver, $address, $port, $database, $username, $password);
77907873
}
77917874
return $next->handle($request);
77927875
}
@@ -9577,7 +9660,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
95779660
use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
95789661
use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
95799662
use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
9580-
use Tqdev\PhpCrudApi\Middleware\ReAuthMiddleware;
9663+
use Tqdev\PhpCrudApi\Middleware\ReconnectMiddleware;
95819664
use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
95829665
use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
95839666
use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
@@ -9627,8 +9710,8 @@ public function __construct(Config $config)
96279710
case 'dbAuth':
96289711
new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
96299712
break;
9630-
case 'reAuth':
9631-
new ReAuthMiddleware($router, $responder, $properties, $reflection, $db);
9713+
case 'reconnect':
9714+
new ReconnectMiddleware($router, $responder, $properties, $reflection, $db);
96329715
break;
96339716
case 'validation':
96349717
new ValidationMiddleware($router, $responder, $properties, $reflection);

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
2626
use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
2727
use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
28-
use Tqdev\PhpCrudApi\Middleware\ReAuthMiddleware;
28+
use Tqdev\PhpCrudApi\Middleware\ReconnectMiddleware;
2929
use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
3030
use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
3131
use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
@@ -75,8 +75,8 @@ public function __construct(Config $config)
7575
case 'dbAuth':
7676
new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
7777
break;
78-
case 'reAuth':
79-
new ReAuthMiddleware($router, $responder, $properties, $reflection, $db);
78+
case 'reconnect':
79+
new ReconnectMiddleware($router, $responder, $properties, $reflection, $db);
8080
break;
8181
case 'validation':
8282
new ValidationMiddleware($router, $responder, $properties, $reflection);

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
class GenericDB
1111
{
1212
private $driver;
13+
private $address;
14+
private $port;
1315
private $database;
16+
private $username;
17+
private $password;
1418
private $pdo;
1519
private $reflection;
1620
private $definition;
1721
private $conditions;
1822
private $columns;
1923
private $converter;
2024

21-
private function getDsn(string $address, int $port, string $database): string
25+
private function getDsn(): string
2226
{
2327
switch ($this->driver) {
2428
case 'mysql':
25-
return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
29+
return "$this->driver:host=$this->address;port=$this->port;dbname=$this->database;charset=utf8mb4";
2630
case 'pgsql':
27-
return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
31+
return "$this->driver:host=$this->address port=$this->port dbname=$this->database options='--client_encoding=UTF8'";
2832
case 'sqlsrv':
29-
return "$this->driver:Server=$address,$port;Database=$database";
33+
return "$this->driver:Server=$this->address,$this->port;Database=$this->database";
3034
}
3135
}
3236

@@ -74,22 +78,58 @@ private function getOptions(): array
7478
}
7579
}
7680

77-
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
81+
private function initPdo(): bool
7882
{
79-
$this->driver = $driver;
80-
$this->database = $database;
81-
$dsn = $this->getDsn($address, $port, $database);
82-
$options = $this->getOptions();
83-
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
83+
if ($this->pdo) {
84+
$result = $this->pdo->reconstruct($this->getDsn(), $this->username, $this->password, $this->getOptions());
85+
} else {
86+
$this->pdo = new LazyPdo($this->getDsn(), $this->username, $this->password, $this->getOptions());
87+
$result = true;
88+
}
8489
$commands = $this->getCommands();
8590
foreach ($commands as $command) {
8691
$this->pdo->addInitCommand($command);
8792
}
88-
$this->reflection = new GenericReflection($this->pdo, $driver, $database);
89-
$this->definition = new GenericDefinition($this->pdo, $driver, $database);
90-
$this->conditions = new ConditionsBuilder($driver);
91-
$this->columns = new ColumnsBuilder($driver);
92-
$this->converter = new DataConverter($driver);
93+
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database);
94+
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database);
95+
$this->conditions = new ConditionsBuilder($this->driver);
96+
$this->columns = new ColumnsBuilder($this->driver);
97+
$this->converter = new DataConverter($this->driver);
98+
return $result;
99+
}
100+
101+
public function __construct(string $driver, string $address, int $port, string $database, string $username, string $password)
102+
{
103+
$this->driver = $driver;
104+
$this->address = $address;
105+
$this->port = $port;
106+
$this->database = $database;
107+
$this->username = $username;
108+
$this->password = $password;
109+
$this->initPdo();
110+
}
111+
112+
public function reconstruct(string $driver, string $address, int $port, string $database, string $username, string $password): bool
113+
{
114+
if ($driver) {
115+
$this->driver = $driver;
116+
}
117+
if ($address) {
118+
$this->address = $address;
119+
}
120+
if ($port) {
121+
$this->port = $port;
122+
}
123+
if ($database) {
124+
$this->database = $database;
125+
}
126+
if ($username) {
127+
$this->username = $username;
128+
}
129+
if ($password) {
130+
$this->password = $password;
131+
}
132+
return $this->initPdo();
93133
}
94134

95135
public function pdo(): LazyPdo

0 commit comments

Comments
 (0)