Skip to content

Commit 23e6e05

Browse files
committed
Move config::set calls into single helper function
1 parent ca6a432 commit 23e6e05

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

app/Models/DatabaseHost.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace Pterodactyl\Models;
2626

27+
use Crypt;
28+
use Config;
2729
use Illuminate\Database\Eloquent\Model;
2830

2931
class DatabaseHost extends Model
@@ -62,6 +64,26 @@ class DatabaseHost extends Model
6264
'node_id' => 'integer',
6365
];
6466

67+
/**
68+
* Sets the database connection name with the details of the host.
69+
*
70+
* @param string $connection
71+
* @return void
72+
*/
73+
public function setDynamicConnection($connection = 'dynamic')
74+
{
75+
Config::set('database.connections.' . $connection, [
76+
'driver' => 'mysql',
77+
'host' => $this->host,
78+
'port' => $this->port,
79+
'database' => 'mysql',
80+
'username' => $this->username,
81+
'password' => Crypt::decrypt($this->password),
82+
'charset' => 'utf8',
83+
'collation' => 'utf8_unicode_ci',
84+
]);
85+
}
86+
6587
/**
6688
* Gets the node associated with a database host.
6789
*

app/Repositories/DatabaseRepository.php

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,9 @@ public function create($id, array $data)
8484
throw $ex;
8585
}
8686

87-
Config::set('database.connections.dynamic', [
88-
'driver' => 'mysql',
89-
'host' => $host->host,
90-
'port' => $host->port,
91-
'database' => 'mysql',
92-
'username' => $host->username,
93-
'password' => Crypt::decrypt($host->password),
94-
'charset' => 'utf8',
95-
'collation' => 'utf8_unicode_ci',
96-
]);
97-
9887
try {
88+
$host->setDynamicConnection();
89+
9990
DB::connection('dynamic')->statement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database->database));
10091
DB::connection('dynamic')->statement(sprintf(
10192
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
@@ -137,21 +128,11 @@ public function create($id, array $data)
137128
public function password($id, $password)
138129
{
139130
$database = Database::with('host')->findOrFail($id);
131+
$database->host->setDynamicConnection();
140132

141133
DB::transaction(function () use ($database, $password) {
142134
$database->password = Crypt::encrypt($password);
143135

144-
Config::set('database.connections.dynamic', [
145-
'driver' => 'mysql',
146-
'host' => $database->host->host,
147-
'port' => $database->host->port,
148-
'database' => 'mysql',
149-
'username' => $database->host->username,
150-
'password' => Crypt::decrypt($database->host->password),
151-
'charset' => 'utf8',
152-
'collation' => 'utf8_unicode_ci',
153-
]);
154-
155136
// We have to do the whole delete user, create user thing rather than
156137
// SET PASSWORD ... because MariaDB and PHP statements ends up inserting
157138
// a corrupted password. A way around this is strtoupper(sha1(sha1($password, true)))
@@ -180,19 +161,9 @@ public function password($id, $password)
180161
public function drop($id)
181162
{
182163
$database = Database::with('host')->findOrFail($id);
164+
$database->host->setDynamicConnection();
183165

184166
DB::transaction(function () use ($database) {
185-
Config::set('database.connections.dynamic', [
186-
'driver' => 'mysql',
187-
'host' => $database->host->host,
188-
'port' => $database->host->port,
189-
'database' => 'mysql',
190-
'username' => $database->host->username,
191-
'password' => Crypt::decrypt($database->host->password),
192-
'charset' => 'utf8',
193-
'collation' => 'utf8_unicode_ci',
194-
]);
195-
196167
DB::connection('dynamic')->statement(sprintf('DROP DATABASE IF EXISTS `%s`', $database->database));
197168
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
198169
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
@@ -248,20 +219,6 @@ public function add(array $data)
248219
}
249220

250221
return DB::transaction(function () use ($data) {
251-
Config::set('database.connections.dynamic', [
252-
'driver' => 'mysql',
253-
'host' => $data['host'],
254-
'port' => $data['port'],
255-
'database' => 'mysql',
256-
'username' => $data['username'],
257-
'password' => $data['password'],
258-
'charset' => 'utf8',
259-
'collation' => 'utf8_unicode_ci',
260-
]);
261-
262-
// Allows us to check that we can connect to things.
263-
DB::connection('dynamic')->select('SELECT 1 FROM dual');
264-
265222
$host = new DatabaseHost;
266223
$host->password = Crypt::encrypt($data['password']);
267224

@@ -274,6 +231,10 @@ public function add(array $data)
274231
'node_id' => (isset($data['node_id'])) ? $data['node_id'] : null,
275232
])->save();
276233

234+
// Allows us to check that we can connect to things.
235+
$host->setDynamicConnection();
236+
DB::connection('dynamic')->select('SELECT 1 FROM dual');
237+
277238
return $host;
278239
});
279240
}
@@ -315,18 +276,7 @@ public function update($id, array $data)
315276
$host->fill($data)->save();
316277

317278
// Check that we can still connect with these details.
318-
Config::set('database.connections.dynamic', [
319-
'driver' => 'mysql',
320-
'host' => $host->host,
321-
'port' => $host->port,
322-
'database' => 'mysql',
323-
'username' => $host->username,
324-
'password' => Crypt::decrypt($host->password),
325-
'charset' => 'utf8',
326-
'collation' => 'utf8_unicode_ci',
327-
]);
328-
329-
// Allows us to check that we can connect to things.
279+
$host->setDynamicConnection();
330280
DB::connection('dynamic')->select('SELECT 1 FROM dual');
331281

332282
return $host;

0 commit comments

Comments
 (0)