From cf01490883ec5785619b7f24e152241c1bfc7ce1 Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sat, 18 Jun 2022 15:48:22 -0400 Subject: [PATCH] Support hiding activity from admin accounts not associated with the server --- .../Client/Servers/ActivityLogController.php | 19 +++++++++++++++++++ config/activity.php | 11 +++++++---- .../elements/table/PaginationFooter.tsx | 4 ++++ .../server/ServerActivityLogContainer.tsx | 17 ++++++++++------- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/Client/Servers/ActivityLogController.php b/app/Http/Controllers/Api/Client/Servers/ActivityLogController.php index 979384db0..dabd9df78 100644 --- a/app/Http/Controllers/Api/Client/Servers/ActivityLogController.php +++ b/app/Http/Controllers/Api/Client/Servers/ActivityLogController.php @@ -2,10 +2,13 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; +use Pterodactyl\Models\User; use Pterodactyl\Models\Server; use Pterodactyl\Models\Permission; use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\AllowedFilter; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Query\JoinClause; use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest; use Pterodactyl\Transformers\Api\Client\ActivityLogTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; @@ -26,6 +29,22 @@ class ActivityLogController extends ClientApiController AllowedFilter::exact('ip'), AllowedFilter::partial('event'), ]) + ->when(config('activity.hide_admin_activity'), function (Builder $builder) use ($server) { + // We could do this with a query and a lot of joins, but that gets pretty + // painful so for now we'll execute a simpler query. + $subusers = $server->subusers()->pluck('user_id')->merge($server->owner_id); + + $builder->select('activity_logs.*') + ->leftJoin('users', function (JoinClause $join) { + $join->on('users.id', 'activity_logs.actor_id') + ->where('activity_logs.actor_type', (new User())->getMorphClass()); + }) + ->where(function (Builder $builder) use ($subusers) { + $builder->whereNull('users.id') + ->orWhere('users.root_admin', 0) + ->orWhereIn('users.id', $subusers); + }); + }) ->paginate(min($request->query('per_page', 25), 100)) ->appends($request->query()); diff --git a/config/activity.php b/config/activity.php index 642be0630..3491d2e2b 100644 --- a/config/activity.php +++ b/config/activity.php @@ -1,9 +1,12 @@ env('APP_ACTIVITY_PRUNE_DAYS', 90), + + // If set to true activity log entries generated by an admin user that is not also + // a part of the server in question will be hidden from the activity logs API response. + // + // Activity will still be properly tracked, just not displayed. + 'hide_admin_activity' => env('APP_ACTIVITY_HIDE_ADMIN', false), ]; diff --git a/resources/scripts/components/elements/table/PaginationFooter.tsx b/resources/scripts/components/elements/table/PaginationFooter.tsx index fb1c53a8d..5af89ac10 100644 --- a/resources/scripts/components/elements/table/PaginationFooter.tsx +++ b/resources/scripts/components/elements/table/PaginationFooter.tsx @@ -26,6 +26,10 @@ const PaginationFooter = ({ pagination, className, onPageSelect }: Props) => { } } + if (pagination.total === 0) { + return null; + } + return (

diff --git a/resources/scripts/components/server/ServerActivityLogContainer.tsx b/resources/scripts/components/server/ServerActivityLogContainer.tsx index d2a6a31f1..63efea949 100644 --- a/resources/scripts/components/server/ServerActivityLogContainer.tsx +++ b/resources/scripts/components/server/ServerActivityLogContainer.tsx @@ -48,13 +48,16 @@ export default () => { {!data && isValidating ? : -

- {data?.items.map((activity) => ( - - - - ))} -
+ !data?.items.length ? +

No activity logs available for this server.

+ : +
+ {data?.items.map((activity) => ( + + + + ))} +
} {data &&