2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Database\Validator\Authorization;
|
|
|
|
use Database\Validator\Structure;
|
|
|
|
use Database\Exception\Authorization as AuthorizationException;
|
|
|
|
use Database\Exception\Structure as StructureException;
|
|
|
|
|
|
|
|
class Database
|
|
|
|
{
|
|
|
|
// System Core
|
|
|
|
const SYSTEM_COLLECTION_COLLECTIONS = 0;
|
|
|
|
const SYSTEM_COLLECTION_RULES = 'rules';
|
|
|
|
|
|
|
|
// Project
|
|
|
|
const SYSTEM_COLLECTION_PROJECTS = 'projects';
|
|
|
|
const SYSTEM_COLLECTION_WEBHOOKS = 'webhooks';
|
|
|
|
const SYSTEM_COLLECTION_KEYS = 'keys';
|
|
|
|
const SYSTEM_COLLECTION_TASKS = 'tasks';
|
|
|
|
const SYSTEM_COLLECTION_PLATFORMS = 'platforms';
|
|
|
|
const SYSTEM_COLLECTION_USAGES = 'usages'; //TODO add structure
|
2020-02-21 09:45:12 +13:00
|
|
|
const SYSTEM_COLLECTION_DOMAINS = 'domains';
|
|
|
|
const SYSTEM_COLLECTION_CERTIFICATES = 'certificates';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
// Auth, Account and Users (private to user)
|
|
|
|
const SYSTEM_COLLECTION_USERS = 'users';
|
|
|
|
const SYSTEM_COLLECTION_TOKENS = 'tokens';
|
|
|
|
|
|
|
|
// Teams (shared among team members)
|
|
|
|
const SYSTEM_COLLECTION_MEMBERSHIPS = 'memberships';
|
|
|
|
const SYSTEM_COLLECTION_TEAMS = 'teams';
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
const SYSTEM_COLLECTION_FILES = 'files';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-08-10 02:46:09 +12:00
|
|
|
protected $mocks = [];
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Adapter
|
|
|
|
*/
|
|
|
|
protected $adapter;
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Set Adapter.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @param Adapter $adapter
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setAdapter(Adapter $adapter)
|
|
|
|
{
|
|
|
|
$this->adapter = $adapter;
|
2019-09-07 05:04:26 +12:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Set Namespace.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Set namespace to divide different scope of data sets
|
|
|
|
*
|
|
|
|
* @param $namespace
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return $this
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function setNamespace($namespace)
|
|
|
|
{
|
|
|
|
$this->adapter->setNamespace($namespace);
|
2019-09-07 05:04:26 +12:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Namespace.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Get namespace of current set scope
|
|
|
|
*
|
|
|
|
* @return string
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function getNamespace()
|
|
|
|
{
|
|
|
|
return $this->adapter->getNamespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Create Namespace.
|
|
|
|
*
|
|
|
|
* @param int $namespace
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function createNamespace($namespace)
|
|
|
|
{
|
|
|
|
return $this->adapter->createNamespace($namespace);
|
|
|
|
}
|
|
|
|
|
2019-08-23 08:53:50 +12:00
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Delete Namespace.
|
|
|
|
*
|
|
|
|
* @param int $namespace
|
2019-08-23 08:53:50 +12:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function deleteNamespace($namespace)
|
|
|
|
{
|
|
|
|
return $this->adapter->deleteNamespace($namespace);
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
|
|
|
* @param array $options
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Document[]|Document
|
|
|
|
*/
|
|
|
|
public function getCollection(array $options)
|
|
|
|
{
|
|
|
|
$options = array_merge([
|
|
|
|
'offset' => 0,
|
|
|
|
'limit' => 15,
|
|
|
|
'search' => '',
|
|
|
|
'relations' => true,
|
2020-02-17 20:16:11 +13:00
|
|
|
'orderField' => '$id',
|
2019-05-09 18:54:39 +12:00
|
|
|
'orderType' => 'ASC',
|
|
|
|
'orderCast' => 'int',
|
|
|
|
'first' => false,
|
|
|
|
'last' => false,
|
|
|
|
'filters' => [],
|
|
|
|
], $options);
|
|
|
|
|
|
|
|
$results = $this->adapter->getCollection($options);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
foreach ($results as &$node) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$node = new Document($node);
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if ($options['first']) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$results = reset($results);
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if ($options['last']) {
|
2019-05-09 18:54:39 +12:00
|
|
|
$results = end($results);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param int $id
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param bool $mock is mocked data allowed?
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Document
|
|
|
|
*/
|
|
|
|
public function getDocument($id, $mock = true)
|
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
if (is_null($id)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
return new Document([]);
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$document = new Document((isset($this->mocks[$id]) && $mock) ? $this->mocks[$id] : $this->adapter->getDocument($id));
|
|
|
|
$validator = new Authorization($document, 'read');
|
2019-07-04 07:50:04 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($document->getPermissions())) { // Check if user has read access to this document
|
2019-05-09 18:54:39 +12:00
|
|
|
return new Document([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $document;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Document|bool
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws AuthorizationException
|
|
|
|
* @throws StructureException
|
|
|
|
*/
|
2020-03-22 10:10:06 +13:00
|
|
|
public function createDocument(array $data, array $unique = [])
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
$document = new Document($data);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$validator = new Authorization($document, 'write');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($document->getPermissions())) { // Check if user has write access to this document
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new AuthorizationException($validator->getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new Structure($this);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($document)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new StructureException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
2020-03-22 10:10:06 +13:00
|
|
|
return new Document($this->adapter->createDocument($data, $unique));
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Document|false
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function updateDocument(array $data)
|
|
|
|
{
|
2020-02-17 20:16:11 +13:00
|
|
|
if (!isset($data['$id'])) {
|
|
|
|
throw new Exception('Must define $id attribute');
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
$document = $this->getDocument($data['$id']); // TODO make sure user don\'t need read permission for write operations
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
// Make sure reserved keys stay constant
|
2020-02-17 20:16:11 +13:00
|
|
|
$data['$id'] = $document->getId();
|
2019-05-09 18:54:39 +12:00
|
|
|
$data['$collection'] = $document->getCollection();
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$validator = new Authorization($document, 'write');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($document->getPermissions())) { // Check if user has write access to this document
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new AuthorizationException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = new Document($data);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($new->getPermissions())) { // Check if user has write access to this document
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new AuthorizationException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new Structure($this);
|
|
|
|
|
2020-02-17 20:16:11 +13:00
|
|
|
if (!$validator->isValid($new)) { // Make sure updated structure still apply collection rules (if any)
|
|
|
|
throw new StructureException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Document($this->adapter->updateDocument($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Document|false
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function overwriteDocument(array $data)
|
|
|
|
{
|
|
|
|
if (!isset($data['$id'])) {
|
|
|
|
throw new Exception('Must define $id attribute');
|
|
|
|
}
|
|
|
|
|
|
|
|
$document = $this->getDocument($data['$id']); // TODO make sure user don\'t need read permission for write operations
|
|
|
|
|
|
|
|
$validator = new Authorization($document, 'write');
|
|
|
|
|
|
|
|
if (!$validator->isValid($document->getPermissions())) { // Check if user has write access to this document
|
|
|
|
throw new AuthorizationException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = new Document($data);
|
|
|
|
|
|
|
|
if (!$validator->isValid($new->getPermissions())) { // Check if user has write access to this document
|
|
|
|
throw new AuthorizationException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = new Structure($this);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($new)) { // Make sure updated structure still apply collection rules (if any)
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new StructureException($validator->getDescription()); // var_dump($validator->getDescription()); return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Document($this->adapter->updateDocument($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return Document|false
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @throws AuthorizationException
|
|
|
|
*/
|
|
|
|
public function deleteDocument($id)
|
|
|
|
{
|
|
|
|
$document = $this->getDocument($id);
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$validator = new Authorization($document, 'write');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!$validator->isValid($document->getPermissions())) { // Check if user has write access to this document
|
2019-05-09 18:54:39 +12:00
|
|
|
throw new AuthorizationException($validator->getDescription());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Document($this->adapter->deleteDocument($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDebug()
|
|
|
|
{
|
|
|
|
return $this->adapter->getDebug();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getSum()
|
|
|
|
{
|
|
|
|
$debug = $this->getDebug();
|
|
|
|
|
|
|
|
return (isset($debug['sum'])) ? $debug['sum'] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $options
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getCount(array $options)
|
|
|
|
{
|
|
|
|
$options = array_merge([
|
|
|
|
'filters' => [],
|
|
|
|
], $options);
|
|
|
|
|
|
|
|
$results = $this->adapter->getCount($options);
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:11:22 +12:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-08-10 02:11:22 +12:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public function setMock($key, $value)
|
|
|
|
{
|
2019-08-10 02:11:22 +12:00
|
|
|
$this->mocks[$key] = $value;
|
2019-09-07 05:04:26 +12:00
|
|
|
|
2019-08-10 02:11:22 +12:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-08-10 02:46:09 +12:00
|
|
|
/**
|
|
|
|
* @param string $mocks
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-08-10 02:46:09 +12:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public function setMocks(array $mocks)
|
|
|
|
{
|
2019-08-10 02:46:09 +12:00
|
|
|
$this->mocks = $mocks;
|
2019-09-07 05:04:26 +12:00
|
|
|
|
2019-08-10 02:46:09 +12:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public function getMocks()
|
|
|
|
{
|
2019-05-09 18:54:39 +12:00
|
|
|
return $this->mocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Get Last Modified.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Return unix timestamp of last time a node queried in current session has been changed
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function lastModified()
|
|
|
|
{
|
|
|
|
return $this->adapter->lastModified();
|
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|