Skip to content

Commit

Permalink
add viewFiltersPerUser (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Sep 11, 2023
1 parent 3b31ac9 commit 360620d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Activity Log

## 1.6.0 - 2023-09-11
### Added
- Add `viewFiltersPerUser` setting

## 1.5.4 - 2023-09-09
### Fixed
- Incorrect reference to widgets table breaking prefixed table query
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ return [
The `$this` object in this context will be an instance of the request class ([`craft\web\Request`](https://docs.craftcms.com/api/v4/craft-web-request.html)).
Only requests satisfying the condition (returning `true`) will be recorded.

### View Filtering per User
While `reqestsFilter` allows you to control which requests are being **recorded** to the database, at times, you may wish to filter some recorded requests from the [**viewable** audit trail](#audit-trail-ui) for specific users, either due to permissions or in order to reduce the cognitive load of parsing unnecessary data.
This can be accomplished using the `viewFiltersPerUser` setting, following the same structure as the example below:
```php
'viewFiltersPerUser'=> [
[
'users'=>['admin'], // Username(s) of relevant users
'filters'=>[ // Filters to be applied for said users
'isCp'=>true, // Only Control Panel requests
'isAction'=>true, // Only Action requests
'isAjax'=>false, // Only page Requests
'siteId'=>1, // or (e.g) [1,2] for multiple sites
'actions'=>[ // Only display those actions
['user-settings','save-group'] // full action array, can be found under `actionSegments` in `activitylogs` table
]
]
]
```
Users not included in any of the array items will be shown the full audit trail.

### Action Requests
[Controller Actions](https://craftcms.com/docs/4.x/dev/controller-actions.html) are automatically labelled using a naming convention. E.g ["fields","save-group"] will become "Fields Save Group".
This is relevant for the "Action" search dropdown on the Logs page and for the Actions widget on the Statistics page.
Expand Down
34 changes: 29 additions & 5 deletions src/services/ApplyFiltersPerViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ public function __construct(Query $query)
*/
public function apply(): Query
{
$user = Craft::$app->getUser();
$username = $user->getIdentity()->username;

$filters = $this->getFilters();
if (getenv('CRAFT_ENVIRONMENT') === 'dev') {
$filters = $this->getFilters($username);
} else {
$filters = \Craft::$app->cache->getOrSet("activitylog_view_filters_user_{$username}", function () use ($username) {
return $this->getFilters($username);
}, 60 * 60 * 24);
}

if (count($filters) > 0) {
foreach ($filters as $key => $value) {
Expand Down Expand Up @@ -57,10 +65,8 @@ protected function getUserGroups(\craft\web\User $user): array
return $userGroups;
}

private function getFilters(): array
private function getFilters($username): array
{
$user = Craft::$app->getUser();
$username = $user->getIdentity()->username;
// $groups = $this->getUserGroups($user);

/** @var Settings $settings */
Expand Down Expand Up @@ -91,7 +97,25 @@ private function applyFilter($key, $value): void
$value = $value ? '1' : '0';
$this->query->andWhere("[[$key]]='{$value}'");
break;

case 'siteId':
if (is_array($value)) {
$sites = implode(',', $value);
$this->query->andWhere("[[siteId]] IN ({$sites})");
} else {
$this->query->andWhere("[[siteId]]='{$value}'");
}
break;
case 'actions':
if (is_array($value) && count($value) > 0) {
$actions = array_map(/**
* @throws \JsonException
*/ function ($action) {
return "'" . json_encode($action, JSON_THROW_ON_ERROR) . "'";
}, $value);
$actions = implode(',', $actions);
$this->query->andWhere("isAction=0 OR [[actionSegments]] IN ($actions)");
}
break;
default:
throw new Exception("Invalid key " . $key);
}
Expand Down
7 changes: 6 additions & 1 deletion src/services/VueTablesActivityLogRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use craft\db\Query;
use matfish\ActivityLog\Plugin;

class VueTablesActivityLogRetriever
{
Expand Down Expand Up @@ -42,7 +43,11 @@ public function retrieve(): array
$q->where("[[createdAt]]>='{$start}'");
$q->andWhere("[[createdAt]]<='{$end}'");

$q = (new ApplyFiltersPerViewer($q))->apply();
$initialFilters = Plugin::getInstance()->getSettings()->viewFiltersPerUser;

if ($initialFilters) {
$q = (new ApplyFiltersPerViewer($q))->apply();
}

foreach ($filters as $key => $value) {
if ($key === 'url') {
Expand Down

0 comments on commit 360620d

Please sign in to comment.