Files

47 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Admin\Servers;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Databases\RotatesDatabasePasswords;
2026-10-11 21:35:11 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
use Pterodactyl\Http\Requests\Api\Admin\Servers\Databases\ServerDatabaseWriteRequest;
2026-10-11 21:35:11 -04:00
use Pterodactyl\Http\Resources\Api\Admin\ServerDatabaseResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Database;
use Pterodactyl\Models\Server;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
#[Subgroup('Server Databases', 'Create, inspect, and delete databases assigned to a server.')]
class RotateDatabasePasswordController extends AdminApiController
{
/**
* Rotate server database password.
*/
#[Endpoint('Rotate server database password', 'Rotates the password for a database assigned to a server.')]
2026-10-11 21:35:11 -04:00
#[ResponseFromResource(ServerDatabaseResource::class, Database::class, description: 'Database password rotated.', resourceKey: 'server_database', include: ['password'])]
public function __invoke(ServerDatabaseWriteRequest $request, RotatesDatabasePasswords $passwordService, Server $server, Database $database): ServerDatabaseResource
2026-10-05 18:10:18 -04:00
{
if ($database->server_id !== $server->id) {
throw (new ModelNotFoundException)->setModel(Database::class);
}
2026-10-08 17:40:00 -04:00
// The action commits its own transaction around the remote user change, so the
// activity is logged afterwards instead of wrapping a change it cannot undo.
$passwordService->rotate($database);
2026-10-05 18:10:18 -04:00
Activity::event('admin:server-database.rotate-password')
->subject($database)
->property('name', $database->database)
2026-10-08 17:40:00 -04:00
->log();
2026-10-05 18:10:18 -04:00
2026-10-11 21:35:11 -04:00
return ServerDatabaseResource::make($database->refresh())->withIncludes('password');
2026-10-05 18:10:18 -04:00
}
}