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 int $id
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function newDatabase(Request $request, $id)
{
@ -595,20 +599,6 @@ class ServersController extends Controller
'remote' => $request->input('remote'),
'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();
}
@ -619,21 +609,20 @@ class ServersController extends Controller
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function resetDatabasePassword(Request $request, $id)
{
$database = Models\Database::where('server_id', $id)->findOrFail($request->input('database'));
$repo = new DatabaseRepository;
$database = $this->databaseRepository->findFirstWhere([
['server_id', '=', $id],
['id', '=', $request->input('database')],
]);
try {
$repo->password($database->id, str_random(20));
$this->databaseCreationService->changePassword($database->id, str_random(20));
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 $database
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function deleteDatabase(Request $request, $id, $database)
{
$database = Models\Database::where('server_id', $id)->findOrFail($database);
$repo = new DatabaseRepository;
$database = $this->databaseRepository->findFirstWhere([
['server_id', '=', $id],
['id', '=', $database],
]);
try {
$repo->drop($database->id);
$this->databaseCreationService->delete($database->id);
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(
$database->database, $database->username, $database->remote, 'dynamic'
);
$this->repository->flush();
$this->repository->flush('dynamic');
$this->database->commit();
} 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'
)->once()->andReturnNull();
$this->repository->shouldReceive('flush')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('flush')->with('dynamic')->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->changePassword(1, 'new_password');