Fix database management for servers

This commit is contained in:
Dane Everitt 2017-07-22 14:07:51 -05:00
parent 63e39fbe58
commit 3add44d342
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 62 additions and 74 deletions

View file

@ -587,6 +587,10 @@ class ServersController extends Controller
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param int $id * @param int $id
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function newDatabase(Request $request, $id) public function newDatabase(Request $request, $id)
{ {
@ -595,20 +599,6 @@ class ServersController extends Controller
'remote' => $request->input('remote'), 'remote' => $request->input('remote'),
'database_host_id' => $request->input('database_host_id'), 'database_host_id' => $request->input('database_host_id'),
]); ]);
// $repo = new DatabaseRepository;
//
// try {
// $repo->create($id, $request->only(['host', 'database', 'connection']));
//
// Alert::success('A new database was assigned to this server successfully.')->flash();
// } catch (DisplayValidationException $ex) {
// return redirect()->route('admin.servers.view.database', $id)->withInput()->withErrors(json_decode($ex->getMessage()))->withInput();
// } catch (DisplayException $ex) {
// Alert::danger($ex->getMessage())->flash();
// } catch (\Exception $ex) {
// Log::error($ex);
// Alert::danger('An exception occured while attempting to add a new database for this server. This error has been logged.')->flash();
// }
return redirect()->route('admin.servers.view.database', $id)->withInput(); return redirect()->route('admin.servers.view.database', $id)->withInput();
} }
@ -619,21 +609,20 @@ class ServersController extends Controller
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param int $id * @param int $id
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function resetDatabasePassword(Request $request, $id) public function resetDatabasePassword(Request $request, $id)
{ {
$database = Models\Database::where('server_id', $id)->findOrFail($request->input('database')); $database = $this->databaseRepository->findFirstWhere([
$repo = new DatabaseRepository; ['server_id', '=', $id],
['id', '=', $request->input('database')],
]);
try { $this->databaseCreationService->changePassword($database->id, str_random(20));
$repo->password($database->id, str_random(20));
return response('', 204); return response('', 204);
} catch (\Exception $ex) {
Log::error($ex);
return response()->json(['error' => 'A unhandled exception occurred while attempting to reset this password. This error has been logged.'], 503);
}
} }
/** /**
@ -643,20 +632,19 @@ class ServersController extends Controller
* @param int $id * @param int $id
* @param int $database * @param int $database
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function deleteDatabase(Request $request, $id, $database) public function deleteDatabase(Request $request, $id, $database)
{ {
$database = Models\Database::where('server_id', $id)->findOrFail($database); $database = $this->databaseRepository->findFirstWhere([
$repo = new DatabaseRepository; ['server_id', '=', $id],
['id', '=', $database],
]);
try { $this->databaseCreationService->delete($database->id);
$repo->drop($database->id);
return response('', 204); return response('', 204);
} catch (\Exception $ex) {
Log::error($ex);
return response()->json(['error' => 'A unhandled exception occurred while attempting to drop this database. This error has been logged.'], 503);
}
} }
} }

View file

@ -150,7 +150,7 @@ class CreationService
$this->repository->assignUserToDatabase( $this->repository->assignUserToDatabase(
$database->database, $database->username, $database->remote, 'dynamic' $database->database, $database->username, $database->remote, 'dynamic'
); );
$this->repository->flush(); $this->repository->flush('dynamic');
$this->database->commit(); $this->database->commit();
} catch (\Exception $ex) { } catch (\Exception $ex) {

View file

@ -246,7 +246,7 @@ class CreationServiceTest extends TestCase
self::TEST_DATA['database'], self::TEST_DATA['username'], self::TEST_DATA['remote'], 'dynamic' self::TEST_DATA['database'], self::TEST_DATA['username'], self::TEST_DATA['remote'], 'dynamic'
)->once()->andReturnNull(); )->once()->andReturnNull();
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturnNull(); $this->repository->shouldReceive('flush')->with('dynamic')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->changePassword(1, 'new_password'); $response = $this->service->changePassword(1, 'new_password');