Skip to content

Commit

Permalink
Added get by resource method
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Apr 17, 2020
1 parent b35a68b commit b48b1f7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Audit/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ abstract public function log(string $userId, string $event, string $resource, st
*/
abstract public function getLogsByUser(string $userId):array;

/**
* Get All Logs By Resource.
*
* @param string $resource
*
* @return array
*/
abstract public function getLogsByResource(string $resource):array;

/**
* Get All Logs By User and Actions.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Audit/Adapters/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public function getLogsByUser(string $userId):array
return $st->fetchAll();
}

public function getLogsByResource(string $resource): array
{
$st = $this->getPDO()->prepare('SELECT *
FROM `'.$this->getNamespace().'.audit.audit`
WHERE resource = :resource
ORDER BY `time` DESC LIMIT 10
');

$st->bindValue(':resourceId', $resource, PDO::PARAM_STR);

$st->execute();

return $st->fetchAll();
}

public function getLogsByUserAndActions(string $userId, array $actions):array
{
$query = [];
Expand Down
16 changes: 13 additions & 3 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public function log(string $userId, string $event, string $resource, string $use
}

/**
* Get All Logs By User and Actions.
*
* Get all user logs logs by given action names
* Get All Logs By User ID.
*
* @param int $userId
*
Expand All @@ -51,6 +49,18 @@ public function getLogsByUser(string $userId):array
return $this->adapter->getLogsByUser($userId);
}

/**
* Get All Logs By Resource.
*
* @param string $resource
*
* @return array
*/
public function getLogsByResource(string $resource):array
{
return $this->adapter->getLogsByResource($resource);
}

/**
* Get All Logs By User and Actions.
*
Expand Down
15 changes: 12 additions & 3 deletions tests/Audit/AuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function testLog()
$location = 'US';
$data = ['key1' => 'value1','key2' => 'value2'];

$this->assertEquals($this->audit->log($userId, 'update', 'database/document-1', $userAgent, $ip, $location, $data), true);
$this->assertEquals($this->audit->log($userId, 'update', 'database/document-2', $userAgent, $ip, $location, $data), true);
$this->assertEquals($this->audit->log($userId, 'delete', 'database/document-2', $userAgent, $ip, $location, $data), true);
$this->assertEquals($this->audit->log($userId, 'update', 'database/document/1', $userAgent, $ip, $location, $data), true);
$this->assertEquals($this->audit->log($userId, 'update', 'database/document/2', $userAgent, $ip, $location, $data), true);
$this->assertEquals($this->audit->log($userId, 'delete', 'database/document/2', $userAgent, $ip, $location, $data), true);
}

public function testGetLogsByUser()
Expand All @@ -83,4 +83,13 @@ public function testGetLogsByUserAndAction()
$this->assertEquals(2, \count($logs1));
$this->assertEquals(3, \count($logs2));
}

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));
}
}

0 comments on commit b48b1f7

Please sign in to comment.