Add support for returning transforming activity logs on the front-end
This commit is contained in:
parent
e15985ea39
commit
a5521ecb79
9 changed files with 162 additions and 6 deletions
30
app/Http/Controllers/Api/Client/ActivityLogController.php
Normal file
30
app/Http/Controllers/Api/Client/ActivityLogController.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Client;
|
||||
|
||||
use Spatie\QueryBuilder\QueryBuilder;
|
||||
use Spatie\QueryBuilder\AllowedFilter;
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
use Pterodactyl\Transformers\Api\Client\ActivityLogTransformer;
|
||||
|
||||
class ActivityLogController extends ClientApiController
|
||||
{
|
||||
/**
|
||||
* Returns a paginated set of the user's activity logs.
|
||||
*/
|
||||
public function __invoke(ClientApiRequest $request): array
|
||||
{
|
||||
$activity = QueryBuilder::for($request->user()->activity())
|
||||
->with('actor')
|
||||
->allowedFilters([
|
||||
AllowedFilter::exact('ip'),
|
||||
AllowedFilter::partial('event'),
|
||||
])
|
||||
->paginate(min($request->query('per_page', 50), 100))
|
||||
->appends($request->query());
|
||||
|
||||
return $this->fractal->collection($activity)
|
||||
->transformWith($this->getTransformer(ActivityLogTransformer::class))
|
||||
->toArray();
|
||||
}
|
||||
}
|
|
@ -22,7 +22,7 @@ use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
|||
* @property string|null $actor_type
|
||||
* @property int|null $actor_id
|
||||
* @property \Illuminate\Support\Collection|null $properties
|
||||
* @property string $timestamp
|
||||
* @property \Carbon\Carbon $timestamp
|
||||
* @property IlluminateModel|\Eloquent $actor
|
||||
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\ActivityLogSubject[] $subjects
|
||||
* @property int|null $subjects_count
|
||||
|
@ -47,6 +47,8 @@ class ActivityLog extends Model
|
|||
{
|
||||
use MassPrunable;
|
||||
|
||||
public const RESOURCE_NAME = 'activity_log';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $guarded = [
|
||||
|
@ -56,6 +58,7 @@ class ActivityLog extends Model
|
|||
|
||||
protected $casts = [
|
||||
'properties' => 'collection',
|
||||
'timestamp' => 'datetime',
|
||||
];
|
||||
|
||||
protected $with = ['subjects'];
|
||||
|
|
37
app/Transformers/Api/Client/ActivityLogTransformer.php
Normal file
37
app/Transformers/Api/Client/ActivityLogTransformer.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Transformers\Api\Client;
|
||||
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\ActivityLog;
|
||||
|
||||
class ActivityLogTransformer extends BaseClientTransformer
|
||||
{
|
||||
protected array $availableIncludes = ['actor'];
|
||||
|
||||
public function getResourceName(): string
|
||||
{
|
||||
return ActivityLog::RESOURCE_NAME;
|
||||
}
|
||||
|
||||
public function transform(ActivityLog $model): array
|
||||
{
|
||||
return [
|
||||
'batch' => $model->batch,
|
||||
'event' => $model->event,
|
||||
'ip' => $model->ip,
|
||||
'description' => $model->description,
|
||||
'properties' => $model->properties,
|
||||
'timestamp' => $model->timestamp->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
public function includeActor(ActivityLog $model)
|
||||
{
|
||||
if (!$model->actor instanceof User) {
|
||||
return $this->null();
|
||||
}
|
||||
|
||||
return $this->item($model->actor, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue