Database abstraction

This commit is contained in:
2020-04-02 00:02:51 +02:00
parent 26d28377be
commit 81995b06b8
51 changed files with 1660 additions and 641 deletions

View File

@@ -2,6 +2,8 @@
namespace Objects;
use \Driver\SQL\Condition\Compare;
class Session extends ApiObject {
const DURATION = 120;
@@ -84,20 +86,22 @@ class Session extends ApiObject {
public function insert($stayLoggedIn) {
$this->updateMetaData();
$query = "INSERT INTO Session (expires, user_id, ipAddress, os, browser, data, stay_logged_in)
VALUES (DATE_ADD(NOW(), INTERVAL ? MINUTE),?,?,?,?,?,?)";
$request = new \Api\ExecuteStatement($this->user);
$sql = $this->user->getSQL();
$success = $request->execute(array(
'query' => $query,
Session::DURATION,
$this->user->getId(),
$this->ipAddress,
$this->os,
$this->browser,
json_encode($_SESSION),
$stayLoggedIn
));
$hours = Session::DURATION;
$columns = array("expires", "user_id", "ipAddress", "os", "browser", "data", "stay_logged_in");
$success = $sql
->insert("Session", $columns)
->addRow(
(new \DateTime)->modify("+$hours hour"),
$this->user->getId(),
$this->ipAddress,
$this->os,
$this->browser,
json_encode($_SESSION),
$stayLoggedIn)
->execute();
if($success) {
$this->sessionId = $this->user->getSQL()->getLastInsertId();
@@ -108,30 +112,30 @@ class Session extends ApiObject {
}
public function destroy() {
$query = 'DELETE FROM Session WHERE Session.uid=? OR (Session.stay_logged_in = 0 AND Session.expires<=NOW())';
$request = new \Api\ExecuteStatement($this->user);
$success = $request->execute(array('query' => $query, $this->sessionId));
$success = $this->user->getSQL()->update("Session")
->set("active", false)
->where(new Compare("Session.uid", $this->sessionId))
->where(new Compare("Session.user_id", $this->user->getId()))
->execute();
return $success;
}
public function update() {
$this->updateMetaData();
$hours = Session::DURATION;
$query = 'UPDATE Session
SET Session.expires=DATE_ADD(NOW(), INTERVAL ? MINUTE),
Session.ipAddress=?, Session.os=?, Session.browser=?, Session.data=?
WHERE Session.uid=?';
$sql = $this->user->getSQL();
$success = $sql->update("Session")
->set("Session.expires", (new \DateTime)->modify("+$hours hour"))
->set("Session.ipAddress", $this->ipAddress)
->set("Session.os", $this->os)
->set("Session.browser", $this->browser)
->set("Session.data", json_encode($_SESSION))
->where(new Compare("Session.uid", $this->sessionId))
->where(new Compare("Session.user_id", $this->user->getId()))
->execute();
$request = new \Api\ExecuteStatement($this->user);
$success = $request->execute(array(
'query' => $query,
Session::DURATION,
$this->ipAddress,
$this->os,
$this->browser,
json_encode($_SESSION),
$this->sessionId,
));
return $success;
}
}

View File

@@ -2,6 +2,11 @@
namespace Objects;
use Driver\SQL\Keyword;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool;
class User extends ApiObject {
private $sql;
@@ -30,7 +35,7 @@ class User extends ApiObject {
private function connectDb() {
$databaseConf = $this->configuration->getDatabase();
if($databaseConf) {
$this->sql = \Driver\SQL::createConnection($databaseConf);
$this->sql = \Driver\SQL\SQL::createConnection($databaseConf);
}
}
@@ -74,10 +79,13 @@ class User extends ApiObject {
}
public function logout() {
$success = true;
if($this->loggedIn) {
$this->session->destroy();
$success = $this->session->destroy();
$this->reset();
}
return $success;
}
public function updateLanguage($lang) {
@@ -96,30 +104,29 @@ class User extends ApiObject {
}
public function readData($userId, $sessionId, $sessionUpdate = true) {
$query = 'SELECT User.name as userName, Language.uid as langId, Language.code as langCode,
Language.name as langName, Session.data as sessionData, Session.stay_logged_in as stayLoggedIn
FROM User
INNER JOIN Session ON User.uid=Session.user_id
LEFT JOIN Language ON User.language_id=Language.uid
WHERE User.uid=? AND Session.uid=?
AND (Session.stay_logged_in OR Session.expires>now())';
$request = new \Api\ExecuteSelect($this);
$success = $request->execute(array('query' => $query, $userId, $sessionId));
// var_dump($userId);
// var_dump($sessionId);
// var_dump($request->getResult());
$res = $this->sql->select("User.name", "Language.uid as langId", "Language.code as langCode", "Language.name as langName",
"Session.data", "Session.stay_logged_in")
->from("User")
->innerJoin("Session", "Session.user_id", "User.uid")
->leftJoin("Language", "User.language_id", "Language.uid")
->where(new Compare("User.uid", $userId))
->where(new Compare("Session.uid", $sessionId))
->where(new Compare("Session.active", true))
->where(new CondBool("Session.stay_logged_in"), new Compare("Session.expires", new Keyword($this->sql->currentTimestamp()), '>'))
->execute();
$success = ($res !== FALSE);
if($success) {
if(count($request->getResult()['rows']) === 0) {
if(empty($res)) {
$success = false;
} else {
$row = $request->getResult()['rows'][0];
$this->username = $row['userName'];
$row = $res[0];
$this->username = $row['name'];
$this->uid = $userId;
$this->session = new Session($this, $sessionId);
$this->session->setData(json_decode($row["sessionData"]));
$this->session->stayLoggedIn($row["stayLoggedIn"]);
$this->session->setData(json_decode($row["data"]));
$this->session->stayLoggedIn($row["stay_logged_in"]);
if($sessionUpdate) $this->session->update();
$this->loggedIn = true;
@@ -127,6 +134,8 @@ class User extends ApiObject {
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
}
}
} else {
var_dump($this->sql->getLastError());
}
return $success;
@@ -171,29 +180,34 @@ class User extends ApiObject {
}
public function authorize($apiKey) {
if($this->loggedIn)
return true;
$query = 'SELECT ApiKey.user_id as uid, User.name as username, Language.uid as langId, Language.code as langCode
FROM ApiKey, User
LEFT JOIN Language ON User.language_id=Language.uid
WHERE api_key=? AND valid_until > now() AND User.uid = ApiKey.user_id';
$request = new \Api\ExecuteSelect($this);
$success = $request->execute(array('query' => $query, $apiKey));
$res = $this->sql->select("ApiKey.user_id as uid", "User.name as username", "Language.uid as langId", "Language.code as langCode", "Language.name as langName")
->from("ApiKey")
->innerJoin("User", "ApiKey.user_id", "User.uid")
->leftJoin("Language", "User.language_id", "Language.uid")
->where(new Compare("ApiKey.api_key", $apiKey))
->where(new Compare("valid_until", new Keyword($this->sql->currentTimestamp()), ">"))
->where(new COmpare("ApiKey.active", 1))
->execute();
$success = ($res !== FALSE);
if($success) {
if(count($request->getResult()['rows']) === 0) {
if(empty($res)) {
$success = false;
} else {
$row = $request->getResult()['rows'][0];
$row = $res[0];
$this->uid = $row['uid'];
$this->username = $row['username'];
if(!is_null($row['langId'])) {
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode']));
$this->setLangauge(Language::newInstance($row['langId'], $row['langCode'], $row['langName']));
}
}
} else {
var_dump($this->sql->getLastError());
}
return $success;