From 369c61f1a8bc3207e1e4b80a6d8e1a318ae11144 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sat, 22 Oct 2022 03:59:36 -0400 Subject: [PATCH] Remove repositories and replace usages --- .../Commands/User/DisableTwoFactorCommand.php | 14 ++++---- .../Repository/UserRepositoryInterface.php | 7 ---- app/Http/Controllers/Admin/UserController.php | 2 -- .../Auth/ResetPasswordController.php | 11 +++--- app/Providers/RepositoryServiceProvider.php | 3 -- app/Repositories/Eloquent/UserRepository.php | 17 --------- .../Subusers/SubuserCreationService.php | 36 +++++++++---------- app/Services/Users/ToggleTwoFactorService.php | 11 +++--- app/Services/Users/TwoFactorSetupService.php | 12 ++----- app/Services/Users/UserCreationService.php | 17 ++++----- app/Services/Users/UserDeletionService.php | 12 +++---- 11 files changed, 49 insertions(+), 93 deletions(-) delete mode 100644 app/Contracts/Repository/UserRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/UserRepository.php diff --git a/app/Console/Commands/User/DisableTwoFactorCommand.php b/app/Console/Commands/User/DisableTwoFactorCommand.php index 052220114..10b0f6693 100644 --- a/app/Console/Commands/User/DisableTwoFactorCommand.php +++ b/app/Console/Commands/User/DisableTwoFactorCommand.php @@ -3,7 +3,7 @@ namespace Pterodactyl\Console\Commands\User; use Illuminate\Console\Command; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; +use Pterodactyl\Models\User; class DisableTwoFactorCommand extends Command { @@ -14,7 +14,7 @@ class DisableTwoFactorCommand extends Command /** * DisableTwoFactorCommand constructor. */ - public function __construct(private UserRepositoryInterface $repository) + public function __construct() { parent::__construct(); } @@ -32,12 +32,12 @@ class DisableTwoFactorCommand extends Command } $email = $this->option('email') ?? $this->ask(trans('command/messages.user.ask_email')); - $user = $this->repository->setColumns(['id', 'email'])->findFirstWhere([['email', '=', $email]]); - $this->repository->withoutFreshModel()->update($user->id, [ - 'use_totp' => false, - 'totp_secret' => null, - ]); + $user = User::query()->where('email', $email)->firstOrFail(); + $user->use_totp = false; + $user->totp_secret = null; + $user->save(); + $this->info(trans('command/messages.user.2fa_disabled', ['email' => $user->email])); } } diff --git a/app/Contracts/Repository/UserRepositoryInterface.php b/app/Contracts/Repository/UserRepositoryInterface.php deleted file mode 100644 index 9efd29fac..000000000 --- a/app/Contracts/Repository/UserRepositoryInterface.php +++ /dev/null @@ -1,7 +0,0 @@ -userRepository->update($user->id, [ - 'password' => $this->hasher->make($password), - $user->getRememberTokenName() => Str::random(60), - ]); + $user->password = $this->hasher->make($password); + $user->setRememberToken(Str::random(60)); + $user->save(); $this->dispatcher->dispatch(new PasswordReset($user)); diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..46d1c9650 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -7,7 +7,6 @@ use Pterodactyl\Repositories\Eloquent\EggRepository; use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; -use Pterodactyl\Repositories\Eloquent\UserRepository; use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository; @@ -22,7 +21,6 @@ use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; @@ -62,6 +60,5 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class); $this->app->bind(TaskRepositoryInterface::class, TaskRepository::class); - $this->app->bind(UserRepositoryInterface::class, UserRepository::class); } } diff --git a/app/Repositories/Eloquent/UserRepository.php b/app/Repositories/Eloquent/UserRepository.php deleted file mode 100644 index 444534625..000000000 --- a/app/Repositories/Eloquent/UserRepository.php +++ /dev/null @@ -1,17 +0,0 @@ -connection->transaction(function () use ($server, $email, $permissions) { - try { - $user = $this->userRepository->findFirstWhere([['email', '=', $email]]); - - if ($server->owner_id === $user->id) { - throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner')); - } - - $subuserCount = $this->subuserRepository->findCountWhere([['user_id', '=', $user->id], ['server_id', '=', $server->id]]); - if ($subuserCount !== 0) { - throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists')); - } - } catch (RecordNotFoundException) { + $user = User::query()->where('email', $email)->first(); + if (!$user) { // Just cap the username generated at 64 characters at most and then append a random string // to the end to make it "unique"... $username = substr(preg_replace('/([^\w\.-]+)/', '', strtok($email, '@')), 0, 64) . Str::random(3); @@ -64,6 +53,15 @@ class SubuserCreationService ]); } + if ($server->owner_id === $user->id) { + throw new UserIsServerOwnerException(trans('exceptions.subusers.user_is_owner')); + } + + $subuserCount = $this->subuserRepository->findCountWhere([['user_id', '=', $user->id], ['server_id', '=', $server->id]]); + if ($subuserCount !== 0) { + throw new ServerSubuserExistsException(trans('exceptions.subusers.subuser_exists')); + } + return $this->subuserRepository->create([ 'user_id' => $user->id, 'server_id' => $server->id, diff --git a/app/Services/Users/ToggleTwoFactorService.php b/app/Services/Users/ToggleTwoFactorService.php index a91615b1c..73ec687bb 100644 --- a/app/Services/Users/ToggleTwoFactorService.php +++ b/app/Services/Users/ToggleTwoFactorService.php @@ -8,7 +8,6 @@ use Pterodactyl\Models\User; use PragmaRX\Google2FA\Google2FA; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Encryption\Encrypter; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Repositories\Eloquent\RecoveryTokenRepository; use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; @@ -21,8 +20,7 @@ class ToggleTwoFactorService private ConnectionInterface $connection, private Encrypter $encrypter, private Google2FA $google2FA, - private RecoveryTokenRepository $recoveryTokenRepository, - private UserRepositoryInterface $repository + private RecoveryTokenRepository $recoveryTokenRepository ) { } @@ -78,10 +76,9 @@ class ToggleTwoFactorService $this->recoveryTokenRepository->insert($inserts); } - $this->repository->withoutFreshModel()->update($user->id, [ - 'totp_authenticated_at' => Carbon::now(), - 'use_totp' => (is_null($toggleState) ? !$user->use_totp : $toggleState), - ]); + $user->totp_authenticated_at = now(); + $user->use_totp = (is_null($toggleState) ? !$user->use_totp : $toggleState); + $user->save(); return $tokens; }); diff --git a/app/Services/Users/TwoFactorSetupService.php b/app/Services/Users/TwoFactorSetupService.php index 87f1e5443..8971f3f45 100644 --- a/app/Services/Users/TwoFactorSetupService.php +++ b/app/Services/Users/TwoFactorSetupService.php @@ -6,7 +6,6 @@ use Exception; use RuntimeException; use Pterodactyl\Models\User; use Illuminate\Contracts\Encryption\Encrypter; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Illuminate\Contracts\Config\Repository as ConfigRepository; class TwoFactorSetupService @@ -18,8 +17,7 @@ class TwoFactorSetupService */ public function __construct( private ConfigRepository $config, - private Encrypter $encrypter, - private UserRepositoryInterface $repository + private Encrypter $encrypter ) { } @@ -27,9 +25,6 @@ class TwoFactorSetupService * Generate a 2FA token and store it in the database before returning the * QR code URL. This URL will need to be attached to a QR generating service in * order to function. - * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle(User $user): array { @@ -42,9 +37,8 @@ class TwoFactorSetupService throw new RuntimeException($exception->getMessage(), 0, $exception); } - $this->repository->withoutFreshModel()->update($user->id, [ - 'totp_secret' => $this->encrypter->encrypt($secret), - ]); + $user->totp_secret = $this->encrypter->encrypt($secret); + $user->save(); $company = urlencode(preg_replace('/\s/', '', $this->config->get('app.name'))); diff --git a/app/Services/Users/UserCreationService.php b/app/Services/Users/UserCreationService.php index 130ae1a4e..8729d4952 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -8,7 +8,7 @@ use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Auth\PasswordBroker; use Pterodactyl\Notifications\AccountCreated; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; +use Pterodactyl\Exceptions\Model\DataValidationException; class UserCreationService { @@ -18,8 +18,7 @@ class UserCreationService public function __construct( private ConnectionInterface $connection, private Hasher $hasher, - private PasswordBroker $passwordBroker, - private UserRepositoryInterface $repository + private PasswordBroker $passwordBroker ) { } @@ -27,7 +26,7 @@ class UserCreationService * Create a new user on the system. * * @throws \Exception - * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws DataValidationException */ public function handle(array $data): User { @@ -36,15 +35,17 @@ class UserCreationService } $this->connection->beginTransaction(); - if (!isset($data['password']) || empty($data['password'])) { + if (empty($data['password'])) { $generateResetToken = true; $data['password'] = $this->hasher->make(str_random(30)); } - /** @var \Pterodactyl\Models\User $user */ - $user = $this->repository->create(array_merge($data, [ + // /** @var User $user */ + // $user = $this->repository->create($data, true, true); + + $user = User::query()->forceCreate(array_merge($data, [ 'uuid' => Uuid::uuid4()->toString(), - ]), true, true); + ])); if (isset($generateResetToken)) { $token = $this->passwordBroker->createToken($user); diff --git a/app/Services/Users/UserDeletionService.php b/app/Services/Users/UserDeletionService.php index f7f060cee..f937e45a9 100644 --- a/app/Services/Users/UserDeletionService.php +++ b/app/Services/Users/UserDeletionService.php @@ -5,7 +5,6 @@ namespace Pterodactyl\Services\Users; use Pterodactyl\Models\User; use Pterodactyl\Exceptions\DisplayException; use Illuminate\Contracts\Translation\Translator; -use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class UserDeletionService @@ -14,7 +13,6 @@ class UserDeletionService * UserDeletionService constructor. */ public function __construct( - protected UserRepositoryInterface $repository, protected ServerRepositoryInterface $serverRepository, protected Translator $translator ) { @@ -23,19 +21,19 @@ class UserDeletionService /** * Delete a user from the panel only if they have no servers attached to their account. * - * @throws \Pterodactyl\Exceptions\DisplayException + * @throws DisplayException */ public function handle(int|User $user): ?bool { - if ($user instanceof User) { - $user = $user->id; + if (is_int($user)) { + $user = User::query()->findOrFail($user); } - $servers = $this->serverRepository->setColumns('id')->findCountWhere([['owner_id', '=', $user]]); + $servers = $this->serverRepository->setColumns('id')->findCountWhere([['owner_id', '=', $user->id]]); if ($servers > 0) { throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers')); } - return $this->repository->delete($user); + return $user->delete(); } }