diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bf0cc6..06f0c5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 09549b8..5d4bdb7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/services/ApplyFiltersPerViewer.php b/src/services/ApplyFiltersPerViewer.php index ab214d4..d643a23 100644 --- a/src/services/ApplyFiltersPerViewer.php +++ b/src/services/ApplyFiltersPerViewer.php @@ -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) { @@ -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 */ @@ -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); } diff --git a/src/services/VueTablesActivityLogRetriever.php b/src/services/VueTablesActivityLogRetriever.php index d4cdef9..ab2930c 100644 --- a/src/services/VueTablesActivityLogRetriever.php +++ b/src/services/VueTablesActivityLogRetriever.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use craft\db\Query; +use matfish\ActivityLog\Plugin; class VueTablesActivityLogRetriever { @@ -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') {