Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Eloquent/DatabaseRepository.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Repositories\Eloquent;
4
5
use Pterodactyl\Models\Database;
6
use Illuminate\Support\Collection;
7
use Illuminate\Foundation\Application;
8
use Illuminate\Database\DatabaseManager;
9
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
11
12
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
13
{
14
protected string $connection = self::DEFAULT_CONNECTION_NAME;
15
16
/**
17
* DatabaseRepository constructor.
18
*/
19
public function __construct(Application $application, private DatabaseManager $database)
20
{
21
parent::__construct($application);
22
}
23
24
/**
25
* Return the model backing this repository.
26
*/
27
public function model(): string
28
{
29
return Database::class;
30
}
31
32
/**
33
* Return the connection to execute statements against.
34
*/
35
public function getConnection(): string
36
{
37
return $this->connection;
38
}
39
40
/**
41
* Set the connection name to execute statements against.
42
*/
43
public function setConnection(string $connection): self
44
{
45
$this->connection = $connection;
46
47
return $this;
48
}
49
50
/**
51
* Return all the databases belonging to a server.
52
*/
53
public function getDatabasesForServer(int $server): Collection
54
{
55
return $this->getBuilder()->with('host')->where('server_id', $server)->get($this->getColumns());
56
}
57
58
/**
59
* Return all the databases for a given host with the server relationship loaded.
60
*/
61
public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator
62
{
63
return $this->getBuilder()->with('server')
64
->where('database_host_id', $host)
65
->paginate($count, $this->getColumns());
66
}
67
68
/**
69
* Create a new database on a given connection.
70
*/
71
public function createDatabase(string $database): bool
72
{
73
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));
74
}
75
76
/**
77
* Create a new database user on a given connection.
78
*/
79
public function createUser(string $username, string $remote, string $password, ?int $max_connections): bool
80
{
81
$args = [$username, $remote, $password];
82
$command = 'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'';
83
84
if (!empty($max_connections)) {
85
$args[] = $max_connections;
86
$command .= ' WITH MAX_USER_CONNECTIONS %s';
87
}
88
89
return $this->run(sprintf($command, ...$args));
90
}
91
92
/**
93
* Give a specific user access to a given database.
94
*/
95
public function assignUserToDatabase(string $database, string $username, string $remote): bool
96
{
97
return $this->run(sprintf(
98
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, REFERENCES, INDEX, LOCK TABLES, CREATE ROUTINE, ALTER ROUTINE, EXECUTE, CREATE TEMPORARY TABLES, CREATE VIEW, SHOW VIEW, EVENT, TRIGGER ON `%s`.* TO `%s`@`%s`',
99
$database,
100
$username,
101
$remote
102
));
103
}
104
105
/**
106
* Flush the privileges for a given connection.
107
*/
108
public function flush(): bool
109
{
110
return $this->run('FLUSH PRIVILEGES');
111
}
112
113
/**
114
* Drop a given database on a specific connection.
115
*/
116
public function dropDatabase(string $database): bool
117
{
118
return $this->run(sprintf('DROP DATABASE IF EXISTS `%s`', $database));
119
}
120
121
/**
122
* Drop a given user on a specific connection.
123
*/
124
public function dropUser(string $username, string $remote): bool
125
{
126
return $this->run(sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote));
127
}
128
129
/**
130
* Run the provided statement against the database on a given connection.
131
*/
132
private function run(string $statement): bool
133
{
134
return $this->database->connection($this->getConnection())->statement($statement);
135
}
136
}
137
138