Skip to content

Commit e6907e0

Browse files
committed
reAuth implemented
1 parent c6f372e commit e6907e0

File tree

1 file changed

+110
-34
lines changed

1 file changed

+110
-34
lines changed

api.php

Lines changed: 110 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4102,10 +4102,18 @@ public function __construct(GenericDB $db, Cache $cache, int $ttl)
41024102
$this->db = $db;
41034103
$this->cache = $cache;
41044104
$this->ttl = $ttl;
4105-
$this->database = $this->loadDatabase(true);
4105+
$this->database = null;
41064106
$this->tables = [];
41074107
}
41084108

4109+
private function database(): ReflectedDatabase
4110+
{
4111+
if (!$this->database) {
4112+
$this->database = $this->loadDatabase(true);
4113+
}
4114+
return $this->database;
4115+
}
4116+
41094117
private function loadDatabase(bool $useCache): ReflectedDatabase
41104118
{
41114119
$data = $useCache ? $this->cache->get('ReflectedDatabase') : '';
@@ -4125,7 +4133,7 @@ private function loadTable(string $tableName, bool $useCache): ReflectedTable
41254133
if ($data != '') {
41264134
$table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
41274135
} else {
4128-
$tableType = $this->database->getType($tableName);
4136+
$tableType = $this->database()->getType($tableName);
41294137
$table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
41304138
$data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
41314139
$this->cache->set("ReflectedTable($tableName)", $data, $this->ttl);
@@ -4145,12 +4153,12 @@ public function refreshTable(string $tableName)
41454153

41464154
public function hasTable(string $tableName): bool
41474155
{
4148-
return $this->database->hasTable($tableName);
4156+
return $this->database()->hasTable($tableName);
41494157
}
41504158

41514159
public function getType(string $tableName): string
41524160
{
4153-
return $this->database->getType($tableName);
4161+
return $this->database()->getType($tableName);
41544162
}
41554163

41564164
public function getTable(string $tableName): ReflectedTable
@@ -4163,20 +4171,19 @@ public function getTable(string $tableName): ReflectedTable
41634171

41644172
public function getTableNames(): array
41654173
{
4166-
return $this->database->getTableNames();
4174+
return $this->database()->getTableNames();
41674175
}
41684176

41694177
public function getDatabaseName(): string
41704178
{
4171-
return $this->database->getName();
4179+
return $this->database()->getName();
41724180
}
41734181

41744182
public function removeTable(string $tableName): bool
41754183
{
41764184
unset($this->tables[$tableName]);
4177-
return $this->database->removeTable($tableName);
4185+
return $this->database()->removeTable($tableName);
41784186
}
4179-
41804187
}
41814188
}
41824189

@@ -5175,25 +5182,30 @@ class GenericDB
51755182
private function getDsn(string $address, int $port, string $database): string
51765183
{
51775184
switch ($this->driver) {
5178-
case 'mysql':return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
5179-
case 'pgsql':return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
5180-
case 'sqlsrv':return "$this->driver:Server=$address,$port;Database=$database";
5185+
case 'mysql':
5186+
return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
5187+
case 'pgsql':
5188+
return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
5189+
case 'sqlsrv':
5190+
return "$this->driver:Server=$address,$port;Database=$database";
51815191
}
51825192
}
51835193

51845194
private function getCommands(): array
51855195
{
51865196
switch ($this->driver) {
5187-
case 'mysql':return [
5197+
case 'mysql':
5198+
return [
51885199
'SET SESSION sql_warnings=1;',
51895200
'SET NAMES utf8mb4;',
51905201
'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
51915202
];
5192-
case 'pgsql':return [
5203+
case 'pgsql':
5204+
return [
51935205
"SET NAMES 'UTF8';",
51945206
];
5195-
case 'sqlsrv':return [
5196-
];
5207+
case 'sqlsrv':
5208+
return [];
51975209
}
51985210
}
51995211

@@ -5204,16 +5216,19 @@ private function getOptions(): array
52045216
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
52055217
);
52065218
switch ($this->driver) {
5207-
case 'mysql':return $options + [
5219+
case 'mysql':
5220+
return $options + [
52085221
\PDO::ATTR_EMULATE_PREPARES => false,
52095222
\PDO::MYSQL_ATTR_FOUND_ROWS => true,
52105223
\PDO::ATTR_PERSISTENT => true,
52115224
];
5212-
case 'pgsql':return $options + [
5225+
case 'pgsql':
5226+
return $options + [
52135227
\PDO::ATTR_EMULATE_PREPARES => false,
52145228
\PDO::ATTR_PERSISTENT => true,
52155229
];
5216-
case 'sqlsrv':return $options + [
5230+
case 'sqlsrv':
5231+
return $options + [
52175232
\PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
52185233
\PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
52195234
];
@@ -5229,7 +5244,7 @@ public function __construct(string $driver, string $address, int $port, string $
52295244
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
52305245
$commands = $this->getCommands();
52315246
foreach ($commands as $command) {
5232-
$this->pdo->query($command);
5247+
$this->pdo->addInitCommand($command);
52335248
}
52345249
$this->reflection = new GenericReflection($this->pdo, $driver, $database);
52355250
$this->definition = new GenericDefinition($this->pdo, $driver, $database);
@@ -5238,7 +5253,7 @@ public function __construct(string $driver, string $address, int $port, string $
52385253
$this->converter = new DataConverter($driver);
52395254
}
52405255

5241-
public function pdo(): \PDO
5256+
public function pdo(): LazyPdo
52425257
{
52435258
return $this->pdo;
52445259
}
@@ -5990,7 +6005,8 @@ class LazyPdo extends \PDO
59906005
private $dsn;
59916006
private $user;
59926007
private $password;
5993-
private $options = array();
6008+
private $options;
6009+
private $commands;
59946010

59956011
private $pdo = null;
59966012

@@ -6000,35 +6016,37 @@ public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $
60006016
$this->user = $user;
60016017
$this->password = $password;
60026018
$this->options = $options;
6019+
$this->commands = array();
60036020
// explicitly NOT calling super::__construct
60046021
}
60056022

6023+
public function addInitCommand(string $command)/*: void*/
6024+
{
6025+
$this->commands[] = $command;
6026+
}
6027+
60066028
private function pdo()
60076029
{
60086030
if (!$this->pdo) {
60096031
$this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
6032+
foreach ($this->commands as $command) {
6033+
$this->pdo->query($command);
6034+
}
60106035
}
60116036
return $this->pdo;
60126037
}
60136038

6014-
public function setUser(/*?string*/ $user): bool
6039+
public function reauthenticate(/*?string*/$user, /*?string*/ $password): bool
60156040
{
6016-
if ($this->pdo) {
6017-
return false;
6018-
}
60196041
$this->user = $user;
6020-
return true;
6021-
}
6022-
6023-
public function setPassword(/*?string*/ $password): bool
6024-
{
6042+
$this->password = $password;
60256043
if ($this->pdo) {
6044+
$this->pdo = null;
60266045
return false;
60276046
}
6028-
$this->password = $password;
60296047
return true;
60306048
}
6031-
6049+
60326050
public function inTransaction(): bool
60336051
{
60346052
// Do not call parent method if there is no pdo object
@@ -6037,7 +6055,7 @@ public function inTransaction(): bool
60376055

60386056
public function setAttribute($attribute, $value): bool
60396057
{
6040-
if ($this->pdo) {
6058+
if ($this->pdo) {
60416059
return $this->pdo()->setAttribute($attribute, $value);
60426060
}
60436061
$this->options[$attribute] = $value;
@@ -6096,7 +6114,7 @@ public function lastInsertId(/* ?string */$name = null): string
60966114

60976115
public function query(string $statement): \PDOStatement
60986116
{
6099-
return call_user_func_array(array($this->pdo(), __FUNCTION__), func_get_args());
6117+
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
61006118
}
61016119
}
61026120
}
@@ -7721,6 +7739,60 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
77217739
}
77227740
}
77237741

7742+
// file: src/Tqdev/PhpCrudApi/Middleware/ReAuthMiddleware.php
7743+
namespace Tqdev\PhpCrudApi\Middleware {
7744+
7745+
use Psr\Http\Message\ResponseInterface;
7746+
use Psr\Http\Message\ServerRequestInterface;
7747+
use Psr\Http\Server\RequestHandlerInterface;
7748+
use Tqdev\PhpCrudApi\Column\ReflectionService;
7749+
use Tqdev\PhpCrudApi\Controller\Responder;
7750+
use Tqdev\PhpCrudApi\Database\GenericDB;
7751+
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
7752+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
7753+
7754+
class ReAuthMiddleware extends Middleware
7755+
{
7756+
private $reflection;
7757+
private $db;
7758+
7759+
public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db)
7760+
{
7761+
parent::__construct($router, $responder, $properties);
7762+
$this->reflection = $reflection;
7763+
$this->db = $db;
7764+
}
7765+
7766+
private function getUsername(): string
7767+
{
7768+
$usernameHandler = $this->getProperty('usernameHandler', '');
7769+
if ($usernameHandler) {
7770+
return call_user_func($usernameHandler);
7771+
}
7772+
return '';
7773+
}
7774+
7775+
private function getPassword(): string
7776+
{
7777+
$passwordHandler = $this->getProperty('passwordHandler', '');
7778+
if ($passwordHandler) {
7779+
return call_user_func($passwordHandler);
7780+
}
7781+
return '';
7782+
}
7783+
7784+
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
7785+
{
7786+
$username = $this->getUsername();
7787+
$password = $this->getPassword();
7788+
if ($username && $password) {
7789+
$this->db->pdo()->reauthenticate($username, $password);
7790+
}
7791+
return $next->handle($request);
7792+
}
7793+
}
7794+
}
7795+
77247796
// file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
77257797
namespace Tqdev\PhpCrudApi\Middleware {
77267798

@@ -9505,6 +9577,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
95059577
use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
95069578
use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
95079579
use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
9580+
use Tqdev\PhpCrudApi\Middleware\ReAuthMiddleware;
95089581
use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
95099582
use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
95109583
use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
@@ -9554,6 +9627,9 @@ public function __construct(Config $config)
95549627
case 'dbAuth':
95559628
new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
95569629
break;
9630+
case 'reAuth':
9631+
new ReAuthMiddleware($router, $responder, $properties, $reflection, $db);
9632+
break;
95579633
case 'validation':
95589634
new ValidationMiddleware($router, $responder, $properties, $reflection);
95599635
break;

0 commit comments

Comments
 (0)