forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseRepository.php
More file actions
206 lines (184 loc) · 5.56 KB
/
DatabaseRepository.php
File metadata and controls
206 lines (184 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Database;
use Illuminate\Support\Collection;
use Illuminate\Foundation\Application;
use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
{
/**
* @var string
*/
protected $connection = self::DEFAULT_CONNECTION_NAME;
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/**
* DatabaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $application
* @param \Illuminate\Database\DatabaseManager $database
*/
public function __construct(Application $application, DatabaseManager $database)
{
parent::__construct($application);
$this->database = $database;
}
/**
* Return the model backing this repository.
*
* @return string
*/
public function model()
{
return Database::class;
}
/**
* Set the connection name to execute statements against.
*
* @param string $connection
* @return $this
*/
public function setConnection(string $connection)
{
$this->connection = $connection;
return $this;
}
/**
* Return the connection to execute statements against.
*
* @return string
*/
public function getConnection(): string
{
return $this->connection;
}
/**
* Return all of the databases belonging to a server.
*
* @param int $server
* @return \Illuminate\Support\Collection
*/
public function getDatabasesForServer(int $server): Collection
{
return $this->getBuilder()->where('server_id', $server)->get($this->getColumns());
}
/**
* Return all of the databases for a given host with the server relationship loaded.
*
* @param int $host
* @param int $count
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator
{
return $this->getBuilder()->with('server')
->where('database_host_id', $host)
->paginate($count, $this->getColumns());
}
/**
* Create a new database if it does not already exist on the host with
* the provided details.
*
* @param array $data
* @return \Pterodactyl\Models\Database
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException
*/
public function createIfNotExists(array $data): Database
{
$count = $this->getBuilder()->where([
['server_id', '=', array_get($data, 'server_id')],
['database_host_id', '=', array_get($data, 'database_host_id')],
['database', '=', array_get($data, 'database')],
])->count();
if ($count > 0) {
throw new DuplicateDatabaseNameException('A database with those details already exists for the specified server.');
}
return $this->create($data);
}
/**
* Create a new database on a given connection.
*
* @param string $database
* @return bool
*/
public function createDatabase(string $database): bool
{
return $this->run(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));
}
/**
* Create a new database user on a given connection.
*
* @param string $username
* @param string $remote
* @param string $password
* @return bool
*/
public function createUser(string $username, string $remote, string $password): bool
{
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
}
/**
* Give a specific user access to a given database.
*
* @param string $database
* @param string $username
* @param string $remote
* @return bool
*/
public function assignUserToDatabase(string $database, string $username, string $remote): bool
{
return $this->run(sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`',
$database,
$username,
$remote
));
}
/**
* Flush the privileges for a given connection.
*
* @return bool
*/
public function flush(): bool
{
return $this->run('FLUSH PRIVILEGES');
}
/**
* Drop a given database on a specific connection.
*
* @param string $database
* @return bool
*/
public function dropDatabase(string $database): bool
{
return $this->run(sprintf('DROP DATABASE IF EXISTS `%s`', $database));
}
/**
* Drop a given user on a specific connection.
*
* @param string $username
* @param string $remote
* @return mixed
*/
public function dropUser(string $username, string $remote): bool
{
return $this->run(sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote));
}
/**
* Run the provided statement against the database on a given connection.
*
* @param string $statement
* @return bool
*/
private function run(string $statement): bool
{
return $this->database->connection($this->getConnection())->statement($statement);
}
}