16 Commits
Author SHA1 Message Date
bence fd286c9cff Merge pull request 'check session validity by DatabaseSessionHandler' (#16) from feature/sessions-should-expire into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #16
2023-05-02 12:55:47 +02:00
bence 72fc78220f check session validity by DatabaseSessionHandler
soko-web/pipeline/pr-master This commit looks good
2023-05-02 12:37:31 +02:00
bence 0da1d00c3d Merge pull request 'feature/implement-separate-remember-me' (#15) from feature/implement-separate-remember-me into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #15
2023-05-02 12:08:16 +02:00
bence c57d1d40d4 fixup! remove unnecessary "pass by reference" variables from Request
soko-web/pipeline/pr-master This commit looks good
2023-05-02 12:02:30 +02:00
bence 6a35344210 pass Session object to Request
soko-web/pipeline/pr-master There was a failure building this commit
2023-05-02 11:47:08 +02:00
bence 9ade08d8bd session handler should receive table name in the constructor 2023-05-02 10:52:22 +02:00
bence a982be6645 remove unnecessary "pass by reference" variables from Request 2023-05-02 10:36:04 +02:00
bence 219b42f995 Merge pull request 'feature/use-the-same-table-in-multiple-joins' (#14) from feature/use-the-same-table-in-multiple-joins into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #14
2023-05-01 19:08:22 +02:00
bence 70a9e492e3 make left joins really work
soko-web/pipeline/pr-master This commit looks good
2023-05-01 18:56:36 +02:00
bence e9bfe4e4ad use the table name is column names if table is not specified 2023-05-01 18:56:36 +02:00
bence cf329a20e5 make it possible to use the same table in multiple joins 2023-05-01 18:56:36 +02:00
bence 88a2a99527 Merge pull request 'feature/withrelations-should-contain-names-instead-of-types' (#13) from feature/withrelations-should-contain-names-instead-of-types into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #13
2023-04-30 20:20:27 +02:00
bence e37ea7c09c make PersistentDataManager::$fillWithData private
soko-web/pipeline/pr-master This commit looks good
2023-04-30 19:21:04 +02:00
bence ad7ea0de9d withRelations in methods of PersistentDataManager should contain relation names instead of types 2023-04-30 19:19:00 +02:00
bence 445774e59a Merge pull request 'make auditlogger optional in persistent data manager' (#12) from bugfix/make-auditlogger-optional into master
soko-web/pipeline/head This commit looks good
Reviewed-on: #12
2023-04-20 00:24:31 +02:00
bence 7650b33cd2 make auditlogger optional in persistent data manager
soko-web/pipeline/pr-master This commit looks good
2023-04-20 00:23:14 +02:00
7 changed files with 108 additions and 95 deletions

No files matched your search

+6 -14
View File
@@ -276,13 +276,12 @@ class Select
return [(string) $table, $params]; return [(string) $table, $params];
} }
if ($table instanceof Select) if ($table instanceof Select) {
{
return $table->generateQuery(); return $table->generateQuery();
} }
if (isset($this->tableAliases[$table])) { if (isset($this->tableAliases[$table])) {
$queryString = ($defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table)); $queryString = $defineAlias ? Utils::backtick($this->tableAliases[$table]) . ' ' . Utils::backtick($table) : Utils::backtick($table);
return [$queryString, $params]; return [$queryString, $params];
} }
@@ -295,24 +294,17 @@ class Select
return (string) $column; return (string) $column;
} }
if (is_array($column)) { if (!is_array($column)) {
$out = ''; $column = [$this->table, $column];
if ($column[0]) {
list($tableName, $params) = $this->generateTable($column[0]);
$out .= $tableName . '.';
} }
$out .= Utils::backtick($column[1]); list($tableName, $params) = $this->generateTable($column[0]);
$out = $tableName . '.' . Utils::backtick($column[1]);
if (!empty($column[2])) { if (!empty($column[2])) {
$out .= ' ' . Utils::backtick($column[2]); $out .= ' ' . Utils::backtick($column[2]);
} }
return $out; return $out;
} else {
return Utils::backtick($column);
}
} }
private function generateColumns(): string private function generateColumns(): string
@@ -10,9 +10,9 @@ interface IPersistentDataManager
public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator; public function selectMultipleFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []): Generator;
public function selectFromDbById($id, string $type, bool $useRelations = false); public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = []);
public function loadRelationsFromDb(Model $model, bool $recursive): void; public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void;
public function saveToDb(Model $model): void; public function saveToDb(Model $model): void;
+1 -1
View File
@@ -4,7 +4,7 @@ use SokoWeb\Interfaces\Authentication\IUser;
interface IRequest interface IRequest
{ {
public function setParsedRouteParams(array &$routeParams): void; public function setParsedRouteParams(array $routeParams): void;
public function getBase(): string; public function getBase(): string;
+73 -61
View File
@@ -13,9 +13,9 @@ class PersistentDataManager implements IPersistentDataManager
{ {
private IConnection $dbConnection; private IConnection $dbConnection;
private IAuditLogger $auditLogger; private ?IAuditLogger $auditLogger;
public function __construct(IConnection $dbConnection, IAuditLogger $auditLogger) public function __construct(IConnection $dbConnection, ?IAuditLogger $auditLogger = null)
{ {
$this->dbConnection = $dbConnection; $this->dbConnection = $dbConnection;
$this->auditLogger = $auditLogger; $this->auditLogger = $auditLogger;
@@ -50,64 +50,22 @@ class PersistentDataManager implements IPersistentDataManager
} }
} }
public function selectFromDbById($id, string $type, bool $useRelations = false) public function selectFromDbById($id, string $type, bool $useRelations = false, array $withRelations = [])
{ {
$select = new Select($this->dbConnection); $select = new Select($this->dbConnection);
$select->whereId($id); $select->whereId($id);
return $this->selectFromDb($select, $type, $useRelations); return $this->selectFromDb($select, $type, $useRelations, $withRelations);
} }
public function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void public function loadRelationsFromDb(Model $model, bool $recursive = false, array $withRelations = []): void
{ {
$relations = $model::getRelations(); $relations = $model::getRelations();
if (count($withRelations)) { if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations); $relations = array_intersect_key($relations, array_flip($withRelations));
} }
while (key($data)) { foreach ($relations as $relation => $relationType) {
$key = key($data);
$value = current($data);
$relation = key($relations);
if (strpos($key, '__') === false) {
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
$key = substr($key, strlen($modelKey) + 2);
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
next($relations);
} else {
return;
}
}
$model->saveSnapshot();
}
public function loadRelationsFromDb(Model $model, bool $recursive): void
{
foreach ($model::getRelations() as $relation => $relationType) {
$camel = str_replace('_', '', ucwords($relation, '_')); $camel = str_replace('_', '', ucwords($relation, '_'));
$methodGet = 'get' . $camel . 'Id'; $methodGet = 'get' . $camel . 'Id';
@@ -116,7 +74,7 @@ class PersistentDataManager implements IPersistentDataManager
$relationId = $model->$methodGet(); $relationId = $model->$methodGet();
if ($relationId !== null) { if ($relationId !== null) {
$relationModel = $this->selectFromDbById($relationId, $relationType, $recursive); $relationModel = $this->selectFromDbById($relationId, $relationType, $recursive, $withRelations);
$model->$methodSet($relationModel); $model->$methodSet($relationModel);
} }
@@ -187,10 +145,10 @@ class PersistentDataManager implements IPersistentDataManager
if ($useRelations) { if ($useRelations) {
$relations = call_user_func([$type, 'getRelations']); $relations = call_user_func([$type, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$relations = array_intersect($relations, $withRelations); $relations = array_intersect_key($relations, array_flip($withRelations));
} }
$columns = array_merge($columns, $this->getRelationColumns($relations, $withRelations)); $columns = array_merge($columns, $this->getRelationColumns($table, $relations, $withRelations));
$this->leftJoinRelations($select, $table, $relations, $withRelations); $this->leftJoinRelations($select, $table, $relations, $withRelations);
$select->columns($columns); $select->columns($columns);
@@ -201,21 +159,22 @@ class PersistentDataManager implements IPersistentDataManager
return $select; return $select;
} }
private function getRelationColumns(array $relations, array $withRelations): array private function getRelationColumns(string $table, array $relations, array $withRelations): array
{ {
$columns = []; $columns = [];
foreach ($relations as $relation => $relationType) { foreach ($relations as $relation => $relationType) {
$relationTableAlias = $table . '__' . $relation;
$relationTable = call_user_func([$relationType, 'getTable']); $relationTable = call_user_func([$relationType, 'getTable']);
foreach (call_user_func([$relationType, 'getFields']) as $relationField) { foreach (call_user_func([$relationType, 'getFields']) as $relationField) {
$columns[] = [$relationTable, $relationField, $relation . '__' . $relationField]; $columns[] = [$relationTableAlias, $relationField, $relation . '__' . $relationField];
} }
$nextOrderRelations = call_user_func([$relationType, 'getRelations']); $relationsOfRelation = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations); $relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
} }
$columns = array_merge($columns, $this->getRelationColumns($nextOrderRelations, $withRelations)); $columns = array_merge($columns, $this->getRelationColumns($relationTable, $relationsOfRelation, $withRelations));
} }
return $columns; return $columns;
@@ -224,17 +183,70 @@ class PersistentDataManager implements IPersistentDataManager
private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void private function leftJoinRelations(Select $select, string $table, array $relations, array $withRelations): void
{ {
foreach ($relations as $relation => $relationType) { foreach ($relations as $relation => $relationType) {
$relationTableAlias = $table . '__' . $relation;
$relationTable = call_user_func([$relationType, 'getTable']); $relationTable = call_user_func([$relationType, 'getTable']);
$select->leftJoin($relationTable, [$relationTable, 'id'], '=', [$table, $relation . '_id']); $select->setTableAliases([$relationTableAlias => $relationTable]);
$select->leftJoin($relationTableAlias, [$relationTableAlias, 'id'], '=', [$table, $relation . '_id']);
$nextOrderRelations = call_user_func([$relationType, 'getRelations']); $relationsOfRelation = call_user_func([$relationType, 'getRelations']);
if (count($withRelations)) { if (count($withRelations)) {
$nextOrderRelations = array_intersect($nextOrderRelations, $withRelations); $relationsOfRelation = array_intersect_key($relationsOfRelation, array_flip($withRelations));
} }
$this->leftJoinRelations($select, $relationTable, $nextOrderRelations, $withRelations); $this->leftJoinRelations($select, $relationTable, $relationsOfRelation, $withRelations);
} }
} }
private function fillWithData(array &$data, Model $model, array $withRelations = [], ?string $modelKey = null): void
{
$relations = $model::getRelations();
if (count($withRelations)) {
$relations = array_intersect_key($relations, array_flip($withRelations));
}
while (key($data)) {
$key = key($data);
$value = current($data);
$relation = key($relations);
if (strpos($key, '__') === false) {
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (isset($modelKey) && substr($key, 0, strlen($modelKey . '__')) === $modelKey . '__') {
$key = substr($key, strlen($modelKey) + 2);
$method = 'set' . str_replace('_', '', ucwords($key, '_'));
if (method_exists($model, $method) && isset($value)) {
$model->$method($value);
}
next($data);
} else if (substr($key, 0, strlen($relation . '__')) === $relation . '__') {
if ($data[$relation . '__id'] !== null) {
$relationType = current($relations);
$relationModel = new $relationType();
$this->fillWithData($data, $relationModel, $withRelations, $relation);
$method = 'set' . str_replace('_', '', ucwords($relation, '_'));
$model->$method($relationModel);
} else {
next($data);
}
next($relations);
} else {
return;
}
}
$model->saveSnapshot();
}
private function syncRelations(Model $model): void private function syncRelations(Model $model): void
{ {
foreach ($model::getRelations() as $relation => $relationType) { foreach ($model::getRelations() as $relation => $relationType) {
+9 -9
View File
@@ -17,23 +17,23 @@ class Request implements IRequest
private array $headers; private array $headers;
private Session $session; private ISession $session;
private ?IUser $user = null; private ?IUser $user = null;
public function __construct( public function __construct(
string $base, string $base,
array &$get, array $get,
array &$post, array $post,
array $headers, array $headers,
array &$session, ISession $session,
IUserRepository $userRepository) IUserRepository $userRepository)
{ {
$this->base = $base; $this->base = $base;
$this->get = &$get; $this->get = $get;
$this->post = &$post; $this->post = $post;
$this->headers = $headers; $this->headers = $headers;
$this->session = new Session($session); $this->session = $session;
$userId = $this->session->get('userId'); $userId = $this->session->get('userId');
if ($userId !== null) { if ($userId !== null) {
@@ -41,9 +41,9 @@ class Request implements IRequest
} }
} }
public function setParsedRouteParams(array &$routeParams): void public function setParsedRouteParams(array $routeParams): void
{ {
$this->routeParams = &$routeParams; $this->routeParams = $routeParams;
} }
public function getBase(): string public function getBase(): string
+16 -7
View File
@@ -11,13 +11,19 @@ class DatabaseSessionHandler implements ISessionHandler
{ {
private IConnection $dbConnection; private IConnection $dbConnection;
private string $table;
private DateTime $shouldBeNewerThan;
private bool $exists = false; private bool $exists = false;
private bool $written = false; private bool $written = false;
public function __construct(IConnection $dbConnection) public function __construct(IConnection $dbConnection, string $table, DateTime $shouldBeNewerThan)
{ {
$this->dbConnection = $dbConnection; $this->dbConnection = $dbConnection;
$this->table = $table;
$this->shouldBeNewerThan = $shouldBeNewerThan;
} }
public function open($savePath, $sessionName): bool public function open($savePath, $sessionName): bool
@@ -32,24 +38,27 @@ class DatabaseSessionHandler implements ISessionHandler
public function read($id): string public function read($id): string
{ {
$select = new Select($this->dbConnection, 'sessions'); $select = new Select($this->dbConnection, $this->table);
$select->columns(['data']); $select->columns(['data', 'updated']);
$select->whereId(substr($id, 0, 32)); $select->whereId(substr($id, 0, 32));
$result = $select->execute()->fetch(IResultSet::FETCH_ASSOC); $result = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
if ($result === null) { if ($result === null) {
return ''; return '';
} }
$this->exists = true; $this->exists = true;
if (new DateTime($result['updated']) < $this->shouldBeNewerThan) {
return '';
}
return $result['data']; return $result['data'];
} }
public function write($id, $data): bool public function write($id, $data): bool
{ {
$modify = new Modify($this->dbConnection, 'sessions'); $modify = new Modify($this->dbConnection, $this->table);
if ($this->exists) { if ($this->exists) {
$modify->setId(substr($id, 0, 32)); $modify->setId(substr($id, 0, 32));
@@ -68,7 +77,7 @@ class DatabaseSessionHandler implements ISessionHandler
public function destroy($id): bool public function destroy($id): bool
{ {
$modify = new Modify($this->dbConnection, 'sessions'); $modify = new Modify($this->dbConnection, $this->table);
$modify->setId(substr($id, 0, 32)); $modify->setId(substr($id, 0, 32));
$modify->delete(); $modify->delete();
@@ -101,7 +110,7 @@ class DatabaseSessionHandler implements ISessionHandler
return true; return true;
} }
$modify = new Modify($this->dbConnection, 'sessions'); $modify = new Modify($this->dbConnection, $this->table);
$modify->setId(substr($id, 0, 32)); $modify->setId(substr($id, 0, 32));
$modify->set('updated', (new DateTime())->format('Y-m-d H:i:s')); $modify->set('updated', (new DateTime())->format('Y-m-d H:i:s'));
+1 -1
View File
@@ -14,7 +14,7 @@ Container::$routeCollection = new SokoWeb\Routing\RouteCollection();
Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']); Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']);
if (isset($_COOKIE['COOKIES_CONSENT'])) { if (isset($_COOKIE['COOKIES_CONSENT'])) {
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection); Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection, 'sessions');
session_set_save_handler(Container::$sessionHandler, true); session_set_save_handler(Container::$sessionHandler, true);
session_start([ session_start([