Update backup logic to use activity logs, not audit logs
This commit is contained in:
parent
cbecfff6da
commit
2fc5a734f9
12 changed files with 222 additions and 159 deletions
|
@ -16,16 +16,13 @@ use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
|||
* @property string|null $description
|
||||
* @property string|null $actor_type
|
||||
* @property int|null $actor_id
|
||||
* @property string|null $subject_type
|
||||
* @property int|null $subject_id
|
||||
* @property \Illuminate\Support\Collection $properties
|
||||
* @property string $timestamp
|
||||
* @property IlluminateModel|\Eloquent $actor
|
||||
* @property IlluminateModel|\Eloquent $subject
|
||||
*
|
||||
* @method static Builder|ActivityLog forAction(string $action)
|
||||
* @method static Builder|ActivityLog forEvent(string $event)
|
||||
* @method static Builder|ActivityLog forActor(\Illuminate\Database\Eloquent\Model $actor)
|
||||
* @method static Builder|ActivityLog forSubject(\Illuminate\Database\Eloquent\Model $subject)
|
||||
* @method static Builder|ActivityLog newModelQuery()
|
||||
* @method static Builder|ActivityLog newQuery()
|
||||
* @method static Builder|ActivityLog query()
|
||||
|
@ -37,8 +34,6 @@ use Illuminate\Database\Eloquent\Model as IlluminateModel;
|
|||
* @method static Builder|ActivityLog whereId($value)
|
||||
* @method static Builder|ActivityLog whereIp($value)
|
||||
* @method static Builder|ActivityLog whereProperties($value)
|
||||
* @method static Builder|ActivityLog whereSubjectId($value)
|
||||
* @method static Builder|ActivityLog whereSubjectType($value)
|
||||
* @method static Builder|ActivityLog whereTimestamp($value)
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
|
@ -68,14 +63,9 @@ class ActivityLog extends Model
|
|||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function subject(): MorphTo
|
||||
public function scopeForEvent(Builder $builder, string $action): Builder
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function scopeForAction(Builder $builder, string $action): Builder
|
||||
{
|
||||
return $builder->where('action', $action);
|
||||
return $builder->where('event', $action);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,12 +75,4 @@ class ActivityLog extends Model
|
|||
{
|
||||
return $builder->whereMorphedTo('actor', $actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scopes a query to only return results where the subject is the given model.
|
||||
*/
|
||||
public function scopeForSubject(Builder $builder, IlluminateModel $subject): Builder
|
||||
{
|
||||
return $builder->whereMorphedTo('subject', $subject);
|
||||
}
|
||||
}
|
||||
|
|
40
app/Models/ActivityLogSubject.php
Normal file
40
app/Models/ActivityLogSubject.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
/**
|
||||
* \Pterodactyl\Models\ActivityLogSubject.
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $activity_log_id
|
||||
* @property int $subject_id
|
||||
* @property string $subject_type
|
||||
* @property \Pterodactyl\Models\ActivityLog|null $activityLog
|
||||
* @property \Illuminate\Database\Eloquent\Model|\Eloquent $subject
|
||||
*
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|ActivityLogSubject newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|ActivityLogSubject newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|ActivityLogSubject query()
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class ActivityLogSubject extends Pivot
|
||||
{
|
||||
public $incrementing = true;
|
||||
public $timestamps = false;
|
||||
|
||||
protected $table = 'activity_log_subjects';
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
public function activityLog()
|
||||
{
|
||||
return $this->belongsTo(ActivityLog::class);
|
||||
}
|
||||
|
||||
public function subject()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Znck\Eloquent\Traits\BelongsToThrough;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
|
||||
|
||||
/**
|
||||
|
@ -41,8 +41,6 @@ use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
|
|||
* @property \Pterodactyl\Models\Allocation|null $allocation
|
||||
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Allocation[] $allocations
|
||||
* @property int|null $allocations_count
|
||||
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\AuditLog[] $audits
|
||||
* @property int|null $audits_count
|
||||
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Backup[] $backups
|
||||
* @property int|null $backups_count
|
||||
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Database[] $databases
|
||||
|
@ -373,48 +371,11 @@ class Server extends Model
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a fresh AuditLog model for the server. This model is not saved to the
|
||||
* database when created, so it is up to the caller to correctly store it as needed.
|
||||
*
|
||||
* @return \Pterodactyl\Models\AuditLog
|
||||
* Returns all of the activity log entries where the server is the subject.
|
||||
*/
|
||||
public function newAuditEvent(string $action, array $metadata = []): AuditLog
|
||||
public function activity(): MorphToMany
|
||||
{
|
||||
return AuditLog::instance($action, $metadata)->fill([
|
||||
'server_id' => $this->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a new audit event for a server by using a transaction. If the transaction
|
||||
* fails for any reason everything executed within will be rolled back. The callback
|
||||
* passed in will receive the AuditLog model before it is saved and the second argument
|
||||
* will be the current server instance. The callback should modify the audit entry as
|
||||
* needed before finishing, any changes will be persisted.
|
||||
*
|
||||
* The response from the callback is returned to the caller.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function audit(string $action, Closure $callback)
|
||||
{
|
||||
return $this->getConnection()->transaction(function () use ($action, $callback) {
|
||||
$model = $this->newAuditEvent($action);
|
||||
$response = $callback($model, $this);
|
||||
$model->save();
|
||||
|
||||
return $response;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function audits()
|
||||
{
|
||||
return $this->hasMany(AuditLog::class);
|
||||
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,6 +14,7 @@ use Illuminate\Auth\Passwords\CanResetPassword;
|
|||
use Pterodactyl\Traits\Helpers\AvailableLanguages;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\Access\Authorizable;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
||||
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
|
||||
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
||||
|
@ -273,6 +274,15 @@ class User extends Model implements
|
|||
return $this->hasMany(UserSSHKey::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all of the activity logs where this user is the subject — not to
|
||||
* be confused by activity logs where this user is the _actor_.
|
||||
*/
|
||||
public function activity(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany(ActivityLog::class, 'subject', 'activity_log_subjects');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all of the servers that a user can access by way of being the owner of the
|
||||
* server, or because they are assigned as a subuser for that server.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue