Skip to content

Commit b9dea62

Browse files
committed
Merge branch 'main' into charles/workflows
2 parents b9d11e2 + cdccca8 commit b9dea62

File tree

36 files changed

+1240
-474
lines changed

36 files changed

+1240
-474
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Exceptions\Http;
4+
5+
use Illuminate\Http\Response;
6+
use Symfony\Component\HttpKernel\Exception\HttpException;
7+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
8+
9+
class TwoFactorAuthRequiredException extends HttpException implements HttpExceptionInterface
10+
{
11+
/**
12+
* TwoFactorAuthRequiredException constructor.
13+
*/
14+
public function __construct(?\Throwable $previous = null)
15+
{
16+
parent::__construct(Response::HTTP_BAD_REQUEST, 'Two-factor authentication is required on this account in order to access this endpoint.', $previous);
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks\Schemas;
4+
5+
use App\Models\Schedule;
6+
use App\Models\Task;
7+
use App\Services\Backups\InitiateBackupService;
8+
9+
final class CreateBackupSchema extends TaskSchema
10+
{
11+
public function __construct(private InitiateBackupService $backupService) {}
12+
13+
public function getId(): string
14+
{
15+
return 'backup';
16+
}
17+
18+
public function runTask(Task $task): void
19+
{
20+
$this->backupService->setIgnoredFiles(explode(PHP_EOL, $task->payload))->handle($task->server, null, true);
21+
}
22+
23+
public function canCreate(Schedule $schedule): bool
24+
{
25+
return $schedule->server->backup_limit > 0;
26+
}
27+
28+
public function getPayloadLabel(): string
29+
{
30+
return trans('server/schedule.tasks.actions.backup.files_to_ignore');
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks\Schemas;
4+
5+
use App\Models\Task;
6+
use App\Services\Files\DeleteFilesService;
7+
8+
final class DeleteFilesSchema extends TaskSchema
9+
{
10+
public function __construct(private DeleteFilesService $deleteFilesService) {}
11+
12+
public function getId(): string
13+
{
14+
return 'delete_files';
15+
}
16+
17+
public function runTask(Task $task): void
18+
{
19+
$this->deleteFilesService->handle($task->server, explode(PHP_EOL, $task->payload));
20+
}
21+
22+
public function getPayloadLabel(): string
23+
{
24+
return trans('server/schedule.tasks.actions.delete_files.files_to_delete');
25+
}
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks\Schemas;
4+
5+
use App\Models\Task;
6+
use App\Repositories\Daemon\DaemonServerRepository;
7+
use Filament\Forms\Components\Select;
8+
use Filament\Schemas\Components\Component;
9+
use Illuminate\Support\Str;
10+
11+
final class PowerActionSchema extends TaskSchema
12+
{
13+
public function __construct(private DaemonServerRepository $serverRepository) {}
14+
15+
public function getId(): string
16+
{
17+
return 'power';
18+
}
19+
20+
public function runTask(Task $task): void
21+
{
22+
$this->serverRepository->setServer($task->server)->power($task->payload);
23+
}
24+
25+
public function getDefaultPayload(): string
26+
{
27+
return 'restart';
28+
}
29+
30+
public function getPayloadLabel(): string
31+
{
32+
return trans('server/schedule.tasks.actions.power.action');
33+
}
34+
35+
public function formatPayload(string $payload): string
36+
{
37+
return Str::ucfirst($payload);
38+
}
39+
40+
/** @return Component[] */
41+
public function getPayloadForm(): array
42+
{
43+
return [
44+
Select::make('payload')
45+
->label($this->getPayloadLabel())
46+
->required()
47+
->options([
48+
'start' => trans('server/schedule.tasks.actions.power.start'),
49+
'restart' => trans('server/schedule.tasks.actions.power.restart'),
50+
'stop' => trans('server/schedule.tasks.actions.power.stop'),
51+
'kill' => trans('server/schedule.tasks.actions.power.kill'),
52+
])
53+
->selectablePlaceholder(false)
54+
->default($this->getDefaultPayload()),
55+
];
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks\Schemas;
4+
5+
use App\Models\Task;
6+
use Filament\Forms\Components\TextInput;
7+
use Filament\Schemas\Components\Component;
8+
9+
final class SendCommandSchema extends TaskSchema
10+
{
11+
public function getId(): string
12+
{
13+
return 'command';
14+
}
15+
16+
public function runTask(Task $task): void
17+
{
18+
$task->server->send($task->payload);
19+
}
20+
21+
public function getPayloadLabel(): string
22+
{
23+
return trans('server/schedule.tasks.actions.command.command');
24+
}
25+
26+
/** @return Component[] */
27+
public function getPayloadForm(): array
28+
{
29+
return [
30+
TextInput::make('payload')
31+
->required()
32+
->label($this->getPayloadLabel())
33+
->default($this->getDefaultPayload()),
34+
];
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks\Schemas;
4+
5+
use App\Extensions\Tasks\TaskSchemaInterface;
6+
use App\Models\Schedule;
7+
use Filament\Forms\Components\Textarea;
8+
use Filament\Schemas\Components\Component;
9+
10+
abstract class TaskSchema implements TaskSchemaInterface
11+
{
12+
public function getName(): string
13+
{
14+
return trans('server/schedule.tasks.actions.' . $this->getId() . '.title');
15+
}
16+
17+
public function canCreate(Schedule $schedule): bool
18+
{
19+
return true;
20+
}
21+
22+
public function getDefaultPayload(): ?string
23+
{
24+
return null;
25+
}
26+
27+
public function getPayloadLabel(): ?string
28+
{
29+
return null;
30+
}
31+
32+
/** @return null|string|string[] */
33+
public function formatPayload(string $payload): null|string|array
34+
{
35+
if (empty($payload)) {
36+
return null;
37+
}
38+
39+
return explode(PHP_EOL, $payload);
40+
}
41+
42+
/** @return Component[] */
43+
public function getPayloadForm(): array
44+
{
45+
return [
46+
Textarea::make('payload')
47+
->label($this->getPayloadLabel() ?? trans('server/schedule.tasks.payload'))
48+
->default($this->getDefaultPayload())
49+
->autosize(),
50+
];
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks;
4+
5+
use App\Models\Schedule;
6+
use App\Models\Task;
7+
use Filament\Schemas\Components\Component;
8+
9+
interface TaskSchemaInterface
10+
{
11+
public function getId(): string;
12+
13+
public function getName(): string;
14+
15+
public function runTask(Task $task): void;
16+
17+
public function canCreate(Schedule $schedule): bool;
18+
19+
public function getDefaultPayload(): ?string;
20+
21+
public function getPayloadLabel(): ?string;
22+
23+
/** @return null|string|string[] */
24+
public function formatPayload(string $payload): null|string|array;
25+
26+
/** @return Component[] */
27+
public function getPayloadForm(): array;
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Extensions\Tasks;
4+
5+
class TaskService
6+
{
7+
/** @var array<string, TaskSchemaInterface> */
8+
private array $schemas = [];
9+
10+
/**
11+
* @return TaskSchemaInterface[]
12+
*/
13+
public function getAll(): array
14+
{
15+
return $this->schemas;
16+
}
17+
18+
public function get(string $id): ?TaskSchemaInterface
19+
{
20+
return array_get($this->schemas, $id);
21+
}
22+
23+
public function register(TaskSchemaInterface $schema): void
24+
{
25+
if (array_key_exists($schema->getId(), $this->schemas)) {
26+
return;
27+
}
28+
29+
$this->schemas[$schema->getId()] = $schema;
30+
}
31+
32+
/** @return array<string, string> */
33+
public function getMappings(): array
34+
{
35+
return collect($this->schemas)->mapWithKeys(fn ($schema) => [$schema->getId() => $schema->getName()])->all();
36+
}
37+
}

0 commit comments

Comments
 (0)