Skip to content

Commit

Permalink
Merge pull request #12 from lohanidamodar/feat-new-getters-pagination
Browse files Browse the repository at this point in the history
Pagination and new function
  • Loading branch information
TorstenDittmann authored Aug 13, 2021
2 parents 971dcd5 + dbb0888 commit 2ec39a5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 56 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": ">=7.4",
"ext-pdo": "*",
"utopia-php/database": "0.6.*"
"utopia-php/database": "0.7.*"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

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

130 changes: 85 additions & 45 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public function setup(): void
'lengths' => [],
'orders' => [],
]),
new Document([
'$id' => 'index5',
'type' => Database::INDEX_KEY,
'attributes' => ['resource', 'event'],
'lengths' => [],
'orders' => [],
]),
];

$this->db->createCollection(Audit::COLLECTION, $attributes, $indexes);
Expand All @@ -158,75 +165,108 @@ public function setup(): void
*/
public function log(string $userId, string $event, string $resource, string $userAgent, string $ip, string $location, array $data = []): bool
{
Authorization::disable();
$this->db->createDocument(Audit::COLLECTION, new Document([
'$read' => [],
'$write' => [],
'userId' => $userId,
'event' => $event,
'resource' => $resource,
'userAgent' => $userAgent,
'ip' => $ip,
'location' => $location,
'data' => $data,
'time' => \time(),
]));
Authorization::reset();
Authorization::skip(function () use ($userId, $event, $resource, $userAgent, $ip, $location, $data) {
$this->db->createDocument(Audit::COLLECTION, new Document([
'$read' => [],
'$write' => [],
'userId' => $userId,
'event' => $event,
'resource' => $resource,
'userAgent' => $userAgent,
'ip' => $ip,
'location' => $location,
'data' => $data,
'time' => \time(),
]));
});
return true;
}

/**
* Get All Logs By User ID.
*
* @param string $userId
* @param int $limit
* @param int $offset
* @param Document|null $orderAfter
*
* @return array
*/
public function getLogsByUser(string $userId): array
public function getLogsByUser(string $userId, int $limit = 25, int $offset = 0, Document $orderAfter = null): array
{
Authorization::disable();
$result = $this->db->find(Audit::COLLECTION, [
new Query('userId', Query::TYPE_EQUAL, [$userId]),
], 25, 0, ['_id'], ['DESC']);
Authorization::reset();
$result = Authorization::skip(function () use ($userId, $limit, $offset, $orderAfter) {
return $this->db->find(Audit::COLLECTION, [
new Query('userId', Query::TYPE_EQUAL, [$userId]),
], $limit, $offset, [], ['DESC'], $orderAfter);
});
return $result;
}

/**
* Get All Logs By Resource.
*
* @param string $resource
* @param int $limit
* @param int $offset
* @param Document|null $orderAfter
*
* @return array
*/
public function getLogsByResource(string $resource): array
public function getLogsByResource(string $resource, int $limit = 25, int $offset = 0, Document $orderAfter = null): array
{
Authorization::disable();
$results = $this->db->find(Audit::COLLECTION, [
new Query('resource', Query::TYPE_EQUAL, [$resource]),
], 25, 0, ['_id'], ['DESC']);
Authorization::reset();
$results = Authorization::skip(function () use ($resource, $limit, $offset, $orderAfter) {
return $this->db->find(Audit::COLLECTION, [
new Query('resource', Query::TYPE_EQUAL, [$resource]),
], $limit, $offset, [], ['DESC'], $orderAfter);
});
return $results;
}

/**
* Get All Logs By User and Actions.
* Get All Logs By User and Events.
*
* Get all user logs logs by given action names
*
* @param string $userId
* @param array $events
* @param int $limit
* @param int $offset
* @param Document|null $orderAfter
*
* @return array
*/
public function getLogsByUserAndEvents(string $userId, array $events): array
public function getLogsByUserAndEvents(string $userId, array $events, int $limit = 25, int $offset = 0, Document $orderAfter = null): array
{
Authorization::disable();
$results = $this->db->find(Audit::COLLECTION, [
new Query('userId', Query::TYPE_EQUAL, [$userId]),
new Query('event', Query::TYPE_EQUAL, $events),
], 25, 0, ['_id'], ['DESC']);
Authorization::reset();
$results = Authorization::skip(function () use ($userId, $events, $limit, $offset, $orderAfter) {
return $this->db->find(Audit::COLLECTION, [
new Query('userId', Query::TYPE_EQUAL, [$userId]),
new Query('event', Query::TYPE_EQUAL, $events),
], $limit, $offset, [], ['DESC'], $orderAfter);
});
return $results;
}

/**
* Get All Logs By Resource and Events.
*
* Get all user logs logs by given action names
*
* @param string $resource
* @param array $events
* @param int $limit
* @param int $offset
* @param Document|null $orderAfter
*
* @return array
*/
public function getLogsByResourceAndEvents(string $resource, array $events, int $limit = 25, int $offset = 0, Document $orderAfter = null): array
{
$results = Authorization::skip(function () use ($resource, $events, $limit, $offset, $orderAfter) {
return $this->db->find(Audit::COLLECTION, [
new Query('resource', Query::TYPE_EQUAL, [$resource]),
new Query('event', Query::TYPE_EQUAL, $events),
], $limit, $offset, [], ['DESC'], $orderAfter);
});
return $results;
}

Expand All @@ -239,17 +279,17 @@ public function getLogsByUserAndEvents(string $userId, array $events): array
*/
public function cleanup(int $timestamp): bool
{
Authorization::disable();
do {
$documents = $this->db->find(Audit::COLLECTION, [
new Query('time', Query::TYPE_LESSER, [$timestamp]),
]);
foreach ($documents as $document) {
$this->db->deleteDocument(Audit::COLLECTION, $document['$id']);
}
} while(!empty($documents));
Authorization::reset();
Authorization::skip(function () use ($timestamp) {
do {
$documents = $this->db->find(Audit::COLLECTION, [
new Query('time', Query::TYPE_LESSER, [$timestamp]),
]);

foreach ($documents as $document) {
$this->db->deleteDocument(Audit::COLLECTION, $document['$id']);
}
} while (!empty($documents));
});
return true;
}
}
51 changes: 48 additions & 3 deletions tests/Audit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,71 @@ public function testLog()
public function testGetLogsByUser()
{
$logs = $this->audit->getLogsByUser('userId');

$this->assertEquals(3, \count($logs));

$logs1 = $this->audit->getLogsByUser('userId', 1, 1);
$this->assertEquals(1, \count($logs1));
$this->assertEquals($logs1[0]->getId(), $logs[1]->getId());

$logs2 = $this->audit->getLogsByUser('userId', 1, 0, $logs[0]);
$this->assertEquals(1, \count($logs2));
$this->assertEquals($logs2[0]->getId(), $logs[1]->getId());
}

public function testGetLogsByUserAndAction()
public function testGetLogsByUserAndEvents()
{
$logs1 = $this->audit->getLogsByUserAndEvents('userId', ['update']);
$logs2 = $this->audit->getLogsByUserAndEvents('userId', ['update', 'delete']);

$this->assertEquals(2, \count($logs1));
$this->assertEquals(3, \count($logs2));

$logs3 = $this->audit->getLogsByUserAndEvents('userId', ['update', 'delete'], 1, 1);

$this->assertEquals(1, \count($logs3));
$this->assertEquals($logs3[0]->getId(), $logs2[1]->getId());

$logs4 = $this->audit->getLogsByUserAndEvents('userId', ['update', 'delete'], 1, 0, $logs2[0]);

$this->assertEquals(1, \count($logs4));
$this->assertEquals($logs4[0]->getId(), $logs2[1]->getId());
}

public function testGetLogsByResourceAndEvents()
{
$logs1 = $this->audit->getLogsByResourceAndEvents('database/document/1', ['update']);
$logs2 = $this->audit->getLogsByResourceAndEvents('database/document/2', ['update', 'delete']);

$this->assertEquals(1, \count($logs1));
$this->assertEquals(2, \count($logs2));

$logs3 = $this->audit->getLogsByResourceAndEvents('database/document/2', ['update', 'delete'], 1, 1);

$this->assertEquals(1, \count($logs3));
$this->assertEquals($logs3[0]->getId(), $logs2[1]->getId());

$logs4 = $this->audit->getLogsByResourceAndEvents('database/document/2', ['update', 'delete'], 1, 0, $logs2[0]);

$this->assertEquals(1, \count($logs4));
$this->assertEquals($logs4[0]->getId(), $logs2[1]->getId());
}

public function testGetLogsByResource()
{
$logs1 = $this->audit->getLogsByResource('database/document/1');
$logs2 = $this->audit->getLogsByResource('database/document/2');

$this->assertEquals(1, \count($logs1));
$this->assertEquals(2, \count($logs2));

$logs3 = $this->audit->getLogsByResource('database/document/2', 1, 1);
$this->assertEquals(1, \count($logs3));
$this->assertEquals($logs3[0]->getId(), $logs2[1]->getId());

$logs4 = $this->audit->getLogsByResource('database/document/2', 1, 0, $logs2[0]);
$this->assertEquals(1, \count($logs4));
$this->assertEquals($logs4[0]->getId(), $logs2[1]->getId());

}

public function testCleanup() {
Expand Down

0 comments on commit 2ec39a5

Please sign in to comment.