Replace subusers repository

This commit is contained in:
Lance Pioch 2022-10-23 03:52:49 -04:00
parent 860b2d890b
commit 1fbeec7719
5 changed files with 7 additions and 109 deletions

View file

@ -1,25 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Subuser;
interface SubuserRepositoryInterface extends RepositoryInterface
{
/**
* Return a subuser with the associated server relationship.
*/
public function loadServerAndUserRelations(Subuser $subuser, bool $refresh = false): Subuser;
/**
* Return a subuser with the associated permissions relationship.
*/
public function getWithPermissions(Subuser $subuser, bool $refresh = false): Subuser;
/**
* Return a subuser and associated permissions given a user_id and server_id.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithPermissionsUsingUserAndServer(int $user, int $server): Subuser;
}

View file

@ -8,7 +8,6 @@ use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity; use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Permission; use Pterodactyl\Models\Permission;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Services\Subusers\SubuserCreationService; use Pterodactyl\Services\Subusers\SubuserCreationService;
use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Transformers\Api\Client\SubuserTransformer; use Pterodactyl\Transformers\Api\Client\SubuserTransformer;
@ -25,7 +24,6 @@ class SubuserController extends ClientApiController
* SubuserController constructor. * SubuserController constructor.
*/ */
public function __construct( public function __construct(
private SubuserRepository $repository,
private SubuserCreationService $creationService, private SubuserCreationService $creationService,
private DaemonServerRepository $serverRepository private DaemonServerRepository $serverRepository
) { ) {
@ -110,9 +108,7 @@ class SubuserController extends ClientApiController
// have actually changed for the user. // have actually changed for the user.
if ($permissions !== $current) { if ($permissions !== $current) {
$log->transaction(function ($instance) use ($request, $subuser, $server) { $log->transaction(function ($instance) use ($request, $subuser, $server) {
$this->repository->update($subuser->id, [ $subuser->update(['permissions' => $this->getDefaultPermissions($request)]);
'permissions' => $this->getDefaultPermissions($request),
]);
try { try {
$this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id); $this->serverRepository->setServer($server)->revokeUserJTI($subuser->user_id);

View file

@ -11,7 +11,6 @@ use Pterodactyl\Repositories\Eloquent\UserRepository;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository;
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
@ -28,7 +27,6 @@ use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
@ -60,7 +58,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class); $this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class);
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::class); $this->app->bind(SessionRepositoryInterface::class, SessionRepository::class);
$this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class);
$this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class);
$this->app->bind(TaskRepositoryInterface::class, TaskRepository::class); $this->app->bind(TaskRepositoryInterface::class, TaskRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class); $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
} }

View file

@ -1,69 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
class SubuserRepository extends EloquentRepository implements SubuserRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Subuser::class;
}
/**
* Return a subuser with the associated server relationship.
*/
public function loadServerAndUserRelations(Subuser $subuser, bool $refresh = false): Subuser
{
if (!$subuser->relationLoaded('server') || $refresh) {
$subuser->load('server');
}
if (!$subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
}
/**
* Return a subuser with the associated permissions relationship.
*/
public function getWithPermissions(Subuser $subuser, bool $refresh = false): Subuser
{
if (!$subuser->relationLoaded('permissions') || $refresh) {
$subuser->load('permissions');
}
if (!$subuser->relationLoaded('user') || $refresh) {
$subuser->load('user');
}
return $subuser;
}
/**
* Return a subuser and associated permissions given a user_id and server_id.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithPermissionsUsingUserAndServer(int $user, int $server): Subuser
{
$instance = $this->getBuilder()->with('permissions')->where([
['user_id', '=', $user],
['server_id', '=', $server],
])->first();
if (is_null($instance)) {
throw new RecordNotFoundException();
}
return $instance;
}
}

View file

@ -7,7 +7,7 @@ use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser; use Pterodactyl\Models\Subuser;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException; use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException;
@ -20,7 +20,6 @@ class SubuserCreationService
*/ */
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private SubuserRepository $subuserRepository,
private UserCreationService $userCreationService, private UserCreationService $userCreationService,
private UserRepositoryInterface $userRepository private UserRepositoryInterface $userRepository
) { ) {
@ -31,9 +30,9 @@ class SubuserCreationService
* If the email address already belongs to a user on the system a new user will not * If the email address already belongs to a user on the system a new user will not
* be created. * be created.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException * @throws ServerSubuserExistsException
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException * @throws UserIsServerOwnerException
* @throws \Throwable * @throws \Throwable
*/ */
public function handle(Server $server, string $email, array $permissions): Subuser public function handle(Server $server, string $email, array $permissions): Subuser
@ -46,7 +45,7 @@ class SubuserCreationService
throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner')); throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner'));
} }
$subuserCount = $this->subuserRepository->findCountWhere([['user_id', '=', $user->id], ['server_id', '=', $server->id]]); $subuserCount = $server->subusers()->where('user_id', $user->id)->count();
if ($subuserCount !== 0) { if ($subuserCount !== 0) {
throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists')); throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists'));
} }
@ -64,7 +63,7 @@ class SubuserCreationService
]); ]);
} }
return $this->subuserRepository->create([ return Subuser::query()->create([
'user_id' => $user->id, 'user_id' => $user->id,
'server_id' => $server->id, 'server_id' => $server->id,
'permissions' => array_unique($permissions), 'permissions' => array_unique($permissions),