Skip to content

Commit b48b1f7

Browse files
committed
Added get by resource method
1 parent b35a68b commit b48b1f7

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/Audit/Adapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ abstract public function log(string $userId, string $event, string $resource, st
7676
*/
7777
abstract public function getLogsByUser(string $userId):array;
7878

79+
/**
80+
* Get All Logs By Resource.
81+
*
82+
* @param string $resource
83+
*
84+
* @return array
85+
*/
86+
abstract public function getLogsByResource(string $resource):array;
87+
7988
/**
8089
* Get All Logs By User and Actions.
8190
*

src/Audit/Adapters/MySQL.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ public function getLogsByUser(string $userId):array
7373
return $st->fetchAll();
7474
}
7575

76+
public function getLogsByResource(string $resource): array
77+
{
78+
$st = $this->getPDO()->prepare('SELECT *
79+
FROM `'.$this->getNamespace().'.audit.audit`
80+
WHERE resource = :resource
81+
ORDER BY `time` DESC LIMIT 10
82+
');
83+
84+
$st->bindValue(':resourceId', $resource, PDO::PARAM_STR);
85+
86+
$st->execute();
87+
88+
return $st->fetchAll();
89+
}
90+
7691
public function getLogsByUserAndActions(string $userId, array $actions):array
7792
{
7893
$query = [];

src/Audit/Audit.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public function log(string $userId, string $event, string $resource, string $use
3838
}
3939

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

52+
/**
53+
* Get All Logs By Resource.
54+
*
55+
* @param string $resource
56+
*
57+
* @return array
58+
*/
59+
public function getLogsByResource(string $resource):array
60+
{
61+
return $this->adapter->getLogsByResource($resource);
62+
}
63+
5464
/**
5565
* Get All Logs By User and Actions.
5666
*

tests/Audit/AuditTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function testLog()
6363
$location = 'US';
6464
$data = ['key1' => 'value1','key2' => 'value2'];
6565

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

7171
public function testGetLogsByUser()
@@ -83,4 +83,13 @@ public function testGetLogsByUserAndAction()
8383
$this->assertEquals(2, \count($logs1));
8484
$this->assertEquals(3, \count($logs2));
8585
}
86+
87+
public function testGetLogsByResource()
88+
{
89+
$logs1 = $this->audit->getLogsByResource('database/document/1');
90+
$logs2 = $this->audit->getLogsByResource('database/document/2');
91+
92+
$this->assertEquals(1, \count($logs1));
93+
$this->assertEquals(2, \count($logs2));
94+
}
8695
}

0 commit comments

Comments
 (0)