Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary key permission #288

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions bin/tasks/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,13 @@ function addArticle($database, Generator $faker): void
'$permissions' => [
Permission::read(Role::any()),
Permission::read(Role::user($faker->randomNumber(9))),
Permission::read(Role::user($faker->randomNumber(9))),
Permission::read(Role::user($faker->randomNumber(9))),
Permission::read(Role::user($faker->randomNumber(9))),
Permission::create(Role::user($faker->randomNumber(9))),
Permission::create(Role::user($faker->randomNumber(9))),
Permission::create(Role::user($faker->randomNumber(9))),
Permission::update(Role::user($faker->randomNumber(9))),
Permission::update(Role::user($faker->randomNumber(9))),
Permission::update(Role::user($faker->randomNumber(9))),
Permission::delete(Role::user($faker->randomNumber(9))),
Permission::delete(Role::user($faker->randomNumber(9))),
Permission::delete(Role::user($faker->randomNumber(9))),
],
'author' => $faker->name(),
'created' => \Utopia\Database\DateTime::format($faker->dateTime()),
'text' => $faker->realTextBetween(1000, 4000),
'text' => $faker->realTextBetween(100, 1000),
'genre' => $faker->randomElement(['fashion', 'food', 'travel', 'music', 'lifestyle', 'fitness', 'diy', 'sports', 'finance']),
'views' => $faker->randomNumber(6)
]));
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 20 additions & 16 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ public function createCollection(string $name, array $attributes = [], array $in
`_type` VARCHAR(12) NOT NULL,
`_permission` VARCHAR(255) NOT NULL,
`_document` VARCHAR(255) NOT NULL,
`_documentId` int(11) unsigned NOT NULL,
PRIMARY KEY (`_id`),
UNIQUE INDEX `_index1` (`_document`,`_type`,`_permission`),
INDEX `_permission` (`_permission`,`_type`,`_document`)
INDEX `_permission` (`_permission`,`_type`,`_document`),
INDEX `_permission2` (`_permission`,`_type`,`_documentId`),
INDEX `_index2` (`_documentId`,`_type`,`_permission`)
)")
->execute();
} catch (\Exception $th) {
Expand Down Expand Up @@ -591,24 +594,24 @@ public function createDocument(string $collection, Document $document): Document
$attributeIndex++;
}

$permissions = [];
foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$permission = \str_replace('"', '', $permission);
$permissions[] = "('{$type}', '{$permission}', '{$document->getId()}')";
}
}

if (!empty($permissions)) {
$queryPermissions = "INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document) VALUES " . implode(', ', $permissions);
$stmtPermissions = $this->getPDO()->prepare($queryPermissions);
}

try {
$stmt->execute();

$document['$internalId'] = $this->getDocument($collection, $document->getId())->getInternalId();

$permissions = [];
foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$permission = \str_replace('"', '', $permission);
$permissions[] = "('{$type}', '{$permission}', '{$document->getId()}', {$document->getInternalId()})";
}
}

if (!empty($permissions)) {
$queryPermissions = "INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document, _documentId) VALUES " . implode(', ', $permissions);
$stmtPermissions = $this->getPDO()->prepare($queryPermissions);
}

if (isset($stmtPermissions)) {
$stmtPermissions->execute();
}
Expand Down Expand Up @@ -740,17 +743,18 @@ public function updateDocument(string $collection, Document $document): Document
$values = [];
foreach ($additions as $type => $permissions) {
foreach ($permissions as $i => $_) {
$values[] = "( :_uid, '{$type}', :_add_{$type}_{$i} )";
$values[] = "(:_id, :_uid, '{$type}', :_add_{$type}_{$i} )";
}
}

$stmtAddPermissions = $this->getPDO()
->prepare(
"
INSERT INTO {$this->getSQLTable($name . '_perms')}
(_document, _type, _permission) VALUES " . \implode(', ', $values)
(_documentId, _document, _type, _permission) VALUES " . \implode(', ', $values)
);

$stmtAddPermissions->bindValue(":_id", $document->getInternalId());
$stmtAddPermissions->bindValue(":_uid", $document->getId());
foreach ($additions as $type => $permissions) {
foreach ($permissions as $i => $permission) {
Expand Down