Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Databases/DatabasePasswordService.php
10262 views
1
<?php
2
3
namespace Pterodactyl\Services\Databases;
4
5
use Pterodactyl\Models\Database;
6
use Pterodactyl\Helpers\Utilities;
7
use Illuminate\Database\ConnectionInterface;
8
use Illuminate\Contracts\Encryption\Encrypter;
9
use Pterodactyl\Extensions\DynamicDatabaseConnection;
10
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
11
12
class DatabasePasswordService
13
{
14
/**
15
* DatabasePasswordService constructor.
16
*/
17
public function __construct(
18
private ConnectionInterface $connection,
19
private DynamicDatabaseConnection $dynamic,
20
private Encrypter $encrypter,
21
private DatabaseRepositoryInterface $repository,
22
) {
23
}
24
25
/**
26
* Updates a password for a given database.
27
*
28
* @throws \Throwable
29
*/
30
public function handle(Database|int $database): string
31
{
32
$password = Utilities::randomStringWithSpecialCharacters(24);
33
34
$this->connection->transaction(function () use ($database, $password) {
35
$this->dynamic->set('dynamic', $database->database_host_id);
36
37
$this->repository->withoutFreshModel()->update($database->id, [
38
'password' => $this->encrypter->encrypt($password),
39
]);
40
41
$this->repository->dropUser($database->username, $database->remote);
42
$this->repository->createUser($database->username, $database->remote, $password, $database->max_connections);
43
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
44
$this->repository->flush();
45
});
46
47
return $password;
48
}
49
}
50
51