diff --git a/app/Contracts/Repository/RepositoryInterface.php b/app/Contracts/Repository/RepositoryInterface.php index 470dc3eb..747fb03f 100644 --- a/app/Contracts/Repository/RepositoryInterface.php +++ b/app/Contracts/Repository/RepositoryInterface.php @@ -108,6 +108,14 @@ interface RepositoryInterface */ public function findWhere(array $fields); + /** + * Find and return the first matching instance for the given fields. + * + * @param array $fields + * @return mixed + */ + public function findFirstWhere(array $fields); + /** * Update a given ID with the passed array of fields. * diff --git a/app/Http/Controllers/Admin/DatabaseController.php b/app/Http/Controllers/Admin/DatabaseController.php index 94d60a0c..0f48987f 100644 --- a/app/Http/Controllers/Admin/DatabaseController.php +++ b/app/Http/Controllers/Admin/DatabaseController.php @@ -28,7 +28,7 @@ use Pterodactyl\Models\Location; use Pterodactyl\Models\DatabaseHost; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Services\DatabaseHostService; +use Pterodactyl\Services\Administrative\DatabaseHostService; use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; class DatabaseController extends Controller @@ -49,17 +49,17 @@ class DatabaseController extends Controller protected $locationModel; /** - * @var \Pterodactyl\Services\DatabaseHostService + * @var \Pterodactyl\Services\Administrative\DatabaseHostService */ protected $service; /** * DatabaseController constructor. * - * @param \Prologue\Alerts\AlertsMessageBag $alert - * @param \Pterodactyl\Models\DatabaseHost $hostModel - * @param \Pterodactyl\Models\Location $locationModel - * @param \Pterodactyl\Services\DatabaseHostService $service + * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Models\DatabaseHost $hostModel + * @param \Pterodactyl\Models\Location $locationModel + * @param \Pterodactyl\Services\Administrative\DatabaseHostService $service */ public function __construct( AlertsMessageBag $alert, diff --git a/app/Http/Controllers/Admin/LocationController.php b/app/Http/Controllers/Admin/LocationController.php index 4a6ffb35..db358e5c 100644 --- a/app/Http/Controllers/Admin/LocationController.php +++ b/app/Http/Controllers/Admin/LocationController.php @@ -26,10 +26,10 @@ namespace Pterodactyl\Http\Controllers\Admin; use Pterodactyl\Models\Location; use Prologue\Alerts\AlertsMessageBag; -use Pterodactyl\Services\LocationService; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Requests\Admin\LocationRequest; +use Pterodactyl\Services\Administrative\LocationService; class LocationController extends Controller { @@ -44,16 +44,16 @@ class LocationController extends Controller protected $locationModel; /** - * @var \Pterodactyl\Services\LocationService + * @var \Pterodactyl\Services\Administrative\\LocationService */ protected $service; /** * LocationController constructor. * - * @param \Prologue\Alerts\AlertsMessageBag $alert - * @param \Pterodactyl\Models\Location $locationModel - * @param \Pterodactyl\Services\LocationService $service + * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Models\Location $locationModel + * @param \Pterodactyl\Services\Administrative\LocationService $service */ public function __construct( AlertsMessageBag $alert, diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 40379f0f..151d9774 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -28,7 +28,7 @@ use Illuminate\Http\Request; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Models\User; use Prologue\Alerts\AlertsMessageBag; -use Pterodactyl\Services\UserService; +use Pterodactyl\Services\Administrative\UserService; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Requests\Admin\UserFormRequest; @@ -41,7 +41,7 @@ class UserController extends Controller protected $alert; /** - * @var \Pterodactyl\Services\UserService + * @var \Pterodactyl\Services\Administrative\UserService */ protected $service; @@ -59,7 +59,7 @@ class UserController extends Controller * UserController constructor. * * @param \Prologue\Alerts\AlertsMessageBag $alert - * @param \Pterodactyl\Services\UserService $service + * @param \Pterodactyl\Services\Administrative\UserService $service * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository * @param \Pterodactyl\Models\User $model */ diff --git a/app/Http/Controllers/Base/APIController.php b/app/Http/Controllers/Base/APIController.php index 72995d00..94783036 100644 --- a/app/Http/Controllers/Base/APIController.php +++ b/app/Http/Controllers/Base/APIController.php @@ -26,12 +26,13 @@ namespace Pterodactyl\Http\Controllers\Base; use Illuminate\Http\Request; -use Pterodactyl\Models\APIKey; use Prologue\Alerts\AlertsMessageBag; use Pterodactyl\Models\APIPermission; use Pterodactyl\Services\ApiKeyService; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Requests\ApiKeyRequest; +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; +use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; class APIController extends Controller { @@ -41,9 +42,9 @@ class APIController extends Controller protected $alert; /** - * @var \Pterodactyl\Models\APIKey + * @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface */ - protected $model; + protected $repository; /** * @var \Pterodactyl\Services\ApiKeyService @@ -53,13 +54,17 @@ class APIController extends Controller /** * APIController constructor. * - * @param \Prologue\Alerts\AlertsMessageBag $alert - * @param \Pterodactyl\Services\ApiKeyService $service + * @param \Prologue\Alerts\AlertsMessageBag $alert + * @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository + * @param \Pterodactyl\Services\ApiKeyService $service */ - public function __construct(AlertsMessageBag $alert, ApiKeyService $service, APIKey $model) - { + public function __construct( + AlertsMessageBag $alert, + ApiKeyRepositoryInterface $repository, + ApiKeyService $service + ) { $this->alert = $alert; - $this->model = $model; + $this->repository = $repository; $this->service = $service; } @@ -72,7 +77,7 @@ class APIController extends Controller public function index(Request $request) { return view('base.api.index', [ - 'keys' => APIKey::where('user_id', $request->user()->id)->get(), + 'keys' => $this->repository->findWhere([['user_id', '=', $request->user()->id]]), ]); } @@ -85,8 +90,8 @@ class APIController extends Controller { return view('base.api.new', [ 'permissions' => [ - 'user' => collect(APIPermission::PERMISSIONS)->pull('_user'), - 'admin' => collect(APIPermission::PERMISSIONS)->except('_user')->toArray(), + 'user' => collect(APIPermission::CONST_PERMISSIONS)->pull('_user'), + 'admin' => collect(APIPermission::CONST_PERMISSIONS)->except('_user')->toArray(), ], ]); } @@ -113,7 +118,11 @@ class APIController extends Controller 'memo' => $request->input('memo'), ], $request->input('permissions') ?? [], $adminPermissions); - $this->alert->success('An API Key-Pair has successfully been generated. The API secret for this public key is shown below and will not be shown again.

' . $secret . '')->flash(); + $this->alert->success( + "An API Key-Pair has successfully been generated. The API secret + for this public key is shown below and will not be shown again. +

{$secret}" + )->flash(); return redirect()->route('account.api'); } @@ -127,12 +136,16 @@ class APIController extends Controller */ public function revoke(Request $request, $key) { - $key = $this->model->newQuery() - ->where('user_id', $request->user()->id) - ->where('public', $key) - ->firstOrFail(); + try { + $key = $this->repository->withColumns('id')->findFirstWhere([ + ['user_id', '=', $request->user()->id], + ['public', $key], + ]); - $this->service->revoke($key); + $this->service->revoke($key->id); + } catch (RecordNotFoundException $ex) { + return abort(404); + } return response('', 204); } diff --git a/app/Repositories/Eloquent/EloquentRepository.php b/app/Repositories/Eloquent/EloquentRepository.php index 86f07db8..b6af22f7 100644 --- a/app/Repositories/Eloquent/EloquentRepository.php +++ b/app/Repositories/Eloquent/EloquentRepository.php @@ -83,10 +83,26 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf /** * {@inheritdoc} + * @return \Illuminate\Database\Eloquent\Collection */ public function findWhere(array $fields) { - // TODO: Implement findWhere() method. + return $this->getBuilder()->where($fields)->get($this->getColumns()); + } + + /** + * {@inheritdoc} + * @return \Illuminate\Database\Eloquent\Model + */ + public function findFirstWhere(array $fields) + { + $instance = $this->getBuilder()->where($fields)->first($this->getColumns()); + + if (! $instance) { + throw new RecordNotFoundException(); + } + + return $instance; } /** diff --git a/app/Services/DatabaseHostService.php b/app/Services/Administrative/DatabaseHostService.php similarity index 98% rename from app/Services/DatabaseHostService.php rename to app/Services/Administrative/DatabaseHostService.php index b3f2be41..24acd63a 100644 --- a/app/Services/DatabaseHostService.php +++ b/app/Services/Administrative/DatabaseHostService.php @@ -22,7 +22,7 @@ * SOFTWARE. */ -namespace Pterodactyl\Services; +namespace Pterodactyl\Services\Administrative; use Illuminate\Database\DatabaseManager; use Illuminate\Contracts\Encryption\Encrypter; diff --git a/app/Services/LocationService.php b/app/Services/Administrative/LocationService.php similarity index 98% rename from app/Services/LocationService.php rename to app/Services/Administrative/LocationService.php index 2bf7a41e..ae050ce3 100644 --- a/app/Services/LocationService.php +++ b/app/Services/Administrative/LocationService.php @@ -22,7 +22,7 @@ * SOFTWARE. */ -namespace Pterodactyl\Services; +namespace Pterodactyl\Services\Administrative; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; diff --git a/app/Services/UserService.php b/app/Services/Administrative/UserService.php similarity index 99% rename from app/Services/UserService.php rename to app/Services/Administrative/UserService.php index a7c87c57..a7503b6b 100644 --- a/app/Services/UserService.php +++ b/app/Services/Administrative/UserService.php @@ -22,7 +22,7 @@ * SOFTWARE. */ -namespace Pterodactyl\Services; +namespace Pterodactyl\Services\Administrative; use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; diff --git a/tests/Unit/Services/DatabaseHostServiceTest.php b/tests/Unit/Services/Administrative/DatabaseHostServiceTest.php similarity index 97% rename from tests/Unit/Services/DatabaseHostServiceTest.php rename to tests/Unit/Services/Administrative/DatabaseHostServiceTest.php index 1239c1ec..0d4e31be 100644 --- a/tests/Unit/Services/DatabaseHostServiceTest.php +++ b/tests/Unit/Services/Administrative/DatabaseHostServiceTest.php @@ -22,15 +22,15 @@ * SOFTWARE. */ -namespace Tests\Unit\Services; +namespace Tests\Unit\Services\Administrative; use Mockery as m; use Tests\TestCase; use Illuminate\Database\DatabaseManager; -use Pterodactyl\Services\DatabaseHostService; use Illuminate\Contracts\Encryption\Encrypter; use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Contracts\Repository\DatabaseHostInterface; +use Pterodactyl\Services\Administrative\DatabaseHostService; class DatabaseHostServiceTest extends TestCase { @@ -55,7 +55,7 @@ class DatabaseHostServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\DatabaseHostService + * @var \Pterodactyl\Services\Administrative\DatabaseHostService */ protected $service; diff --git a/tests/Unit/Services/LocationServiceTest.php b/tests/Unit/Services/Administrative/LocationServiceTest.php similarity index 96% rename from tests/Unit/Services/LocationServiceTest.php rename to tests/Unit/Services/Administrative/LocationServiceTest.php index 442b02b5..1feda6f1 100644 --- a/tests/Unit/Services/LocationServiceTest.php +++ b/tests/Unit/Services/Administrative/LocationServiceTest.php @@ -26,7 +26,7 @@ namespace Tests\Unit\Services; use Mockery as m; use Tests\TestCase; -use Pterodactyl\Services\LocationService; +use Pterodactyl\Services\Administrative\LocationService; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; class LocationServiceTest extends TestCase @@ -37,7 +37,7 @@ class LocationServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\LocationService + * @var \Pterodactyl\Services\Administrative\LocationService */ protected $service; diff --git a/tests/Unit/Services/UserServiceTest.php b/tests/Unit/Services/Administrative/UserServiceTest.php similarity index 98% rename from tests/Unit/Services/UserServiceTest.php rename to tests/Unit/Services/Administrative/UserServiceTest.php index ede6adee..a80a277f 100644 --- a/tests/Unit/Services/UserServiceTest.php +++ b/tests/Unit/Services/Administrative/UserServiceTest.php @@ -26,12 +26,12 @@ namespace Tests\Unit\Services; use Mockery as m; use Tests\TestCase; -use Pterodactyl\Services\UserService; use Illuminate\Foundation\Application; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; use Illuminate\Notifications\ChannelManager; use Pterodactyl\Notifications\AccountCreated; +use Pterodactyl\Services\Administrative\UserService; use Pterodactyl\Services\Helpers\TemporaryPasswordService; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; @@ -68,7 +68,7 @@ class UserServiceTest extends TestCase protected $repository; /** - * @var \Pterodactyl\Services\UserService + * @var \Pterodactyl\Services\Administrative\UserService */ protected $service;