Compare commits

...

1 commit

Author SHA1 Message Date
Lance Pioch
369c61f1a8 Remove repositories and replace usages 2022-10-22 03:59:36 -04:00
11 changed files with 49 additions and 93 deletions

View file

@ -3,7 +3,7 @@
namespace Pterodactyl\Console\Commands\User; namespace Pterodactyl\Console\Commands\User;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Models\User;
class DisableTwoFactorCommand extends Command class DisableTwoFactorCommand extends Command
{ {
@ -14,7 +14,7 @@ class DisableTwoFactorCommand extends Command
/** /**
* DisableTwoFactorCommand constructor. * DisableTwoFactorCommand constructor.
*/ */
public function __construct(private UserRepositoryInterface $repository) public function __construct()
{ {
parent::__construct(); parent::__construct();
} }
@ -32,12 +32,12 @@ class DisableTwoFactorCommand extends Command
} }
$email = $this->option('email') ?? $this->ask(trans('command/messages.user.ask_email')); $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, [ $user = User::query()->where('email', $email)->firstOrFail();
'use_totp' => false, $user->use_totp = false;
'totp_secret' => null, $user->totp_secret = null;
]); $user->save();
$this->info(trans('command/messages.user.2fa_disabled', ['email' => $user->email])); $this->info(trans('command/messages.user.2fa_disabled', ['email' => $user->email]));
} }
} }

View file

@ -1,7 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
interface UserRepositoryInterface extends RepositoryInterface
{
}

View file

@ -19,7 +19,6 @@ use Pterodactyl\Traits\Helpers\AvailableLanguages;
use Pterodactyl\Services\Users\UserCreationService; use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Services\Users\UserDeletionService; use Pterodactyl\Services\Users\UserDeletionService;
use Pterodactyl\Http\Requests\Admin\UserFormRequest; use Pterodactyl\Http\Requests\Admin\UserFormRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller class UserController extends Controller
{ {
@ -34,7 +33,6 @@ class UserController extends Controller
protected UserDeletionService $deletionService, protected UserDeletionService $deletionService,
protected Translator $translator, protected Translator $translator,
protected UserUpdateService $updateService, protected UserUpdateService $updateService,
protected UserRepositoryInterface $repository,
protected ViewFactory $view protected ViewFactory $view
) { ) {
} }

View file

@ -12,7 +12,6 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Foundation\Auth\ResetsPasswords;
use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest; use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class ResetPasswordController extends Controller class ResetPasswordController extends Controller
{ {
@ -30,8 +29,7 @@ class ResetPasswordController extends Controller
*/ */
public function __construct( public function __construct(
private Dispatcher $dispatcher, private Dispatcher $dispatcher,
private Hasher $hasher, private Hasher $hasher
private UserRepositoryInterface $userRepository
) { ) {
} }
@ -75,10 +73,9 @@ class ResetPasswordController extends Controller
*/ */
protected function resetPassword($user, $password) protected function resetPassword($user, $password)
{ {
$user = $this->userRepository->update($user->id, [ $user->password = $this->hasher->make($password);
'password' => $this->hasher->make($password), $user->setRememberToken(Str::random(60));
$user->getRememberTokenName() => Str::random(60), $user->save();
]);
$this->dispatcher->dispatch(new PasswordReset($user)); $this->dispatcher->dispatch(new PasswordReset($user));

View file

@ -7,7 +7,6 @@ use Pterodactyl\Repositories\Eloquent\EggRepository;
use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository;
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;
@ -22,7 +21,6 @@ use Pterodactyl\Repositories\Eloquent\EggVariableRepository;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -62,6 +60,5 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class);
$this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::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);
} }
} }

View file

@ -1,17 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserRepository extends EloquentRepository implements UserRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return User::class;
}
}

View file

@ -3,13 +3,13 @@
namespace Pterodactyl\Services\Subusers; namespace Pterodactyl\Services\Subusers;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server; 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\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException; use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException;
use Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException; use Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException;
@ -21,8 +21,7 @@ class SubuserCreationService
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private SubuserRepository $subuserRepository, private SubuserRepository $subuserRepository,
private UserCreationService $userCreationService, private UserCreationService $userCreationService
private UserRepositoryInterface $userRepository
) { ) {
} }
@ -31,26 +30,16 @@ 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
{ {
return $this->connection->transaction(function () use ($server, $email, $permissions) { return $this->connection->transaction(function () use ($server, $email, $permissions) {
try { $user = User::query()->where('email', $email)->first();
$user = $this->userRepository->findFirstWhere([['email', '=', $email]]); if (!$user) {
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) {
// Just cap the username generated at 64 characters at most and then append a random string // Just cap the username generated at 64 characters at most and then append a random string
// to the end to make it "unique"... // to the end to make it "unique"...
$username = substr(preg_replace('/([^\w\.-]+)/', '', strtok($email, '@')), 0, 64) . Str::random(3); $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([ return $this->subuserRepository->create([
'user_id' => $user->id, 'user_id' => $user->id,
'server_id' => $server->id, 'server_id' => $server->id,

View file

@ -8,7 +8,6 @@ use Pterodactyl\Models\User;
use PragmaRX\Google2FA\Google2FA; use PragmaRX\Google2FA\Google2FA;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\RecoveryTokenRepository; use Pterodactyl\Repositories\Eloquent\RecoveryTokenRepository;
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
@ -21,8 +20,7 @@ class ToggleTwoFactorService
private ConnectionInterface $connection, private ConnectionInterface $connection,
private Encrypter $encrypter, private Encrypter $encrypter,
private Google2FA $google2FA, private Google2FA $google2FA,
private RecoveryTokenRepository $recoveryTokenRepository, private RecoveryTokenRepository $recoveryTokenRepository
private UserRepositoryInterface $repository
) { ) {
} }
@ -78,10 +76,9 @@ class ToggleTwoFactorService
$this->recoveryTokenRepository->insert($inserts); $this->recoveryTokenRepository->insert($inserts);
} }
$this->repository->withoutFreshModel()->update($user->id, [ $user->totp_authenticated_at = now();
'totp_authenticated_at' => Carbon::now(), $user->use_totp = (is_null($toggleState) ? !$user->use_totp : $toggleState);
'use_totp' => (is_null($toggleState) ? !$user->use_totp : $toggleState), $user->save();
]);
return $tokens; return $tokens;
}); });

View file

@ -6,7 +6,6 @@ use Exception;
use RuntimeException; use RuntimeException;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
class TwoFactorSetupService class TwoFactorSetupService
@ -18,8 +17,7 @@ class TwoFactorSetupService
*/ */
public function __construct( public function __construct(
private ConfigRepository $config, private ConfigRepository $config,
private Encrypter $encrypter, private Encrypter $encrypter
private UserRepositoryInterface $repository
) { ) {
} }
@ -27,9 +25,6 @@ class TwoFactorSetupService
* Generate a 2FA token and store it in the database before returning the * 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 * QR code URL. This URL will need to be attached to a QR generating service in
* order to function. * order to function.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function handle(User $user): array public function handle(User $user): array
{ {
@ -42,9 +37,8 @@ class TwoFactorSetupService
throw new RuntimeException($exception->getMessage(), 0, $exception); throw new RuntimeException($exception->getMessage(), 0, $exception);
} }
$this->repository->withoutFreshModel()->update($user->id, [ $user->totp_secret = $this->encrypter->encrypt($secret);
'totp_secret' => $this->encrypter->encrypt($secret), $user->save();
]);
$company = urlencode(preg_replace('/\s/', '', $this->config->get('app.name'))); $company = urlencode(preg_replace('/\s/', '', $this->config->get('app.name')));

View file

@ -8,7 +8,7 @@ use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Auth\PasswordBroker; use Illuminate\Contracts\Auth\PasswordBroker;
use Pterodactyl\Notifications\AccountCreated; use Pterodactyl\Notifications\AccountCreated;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Exceptions\Model\DataValidationException;
class UserCreationService class UserCreationService
{ {
@ -18,8 +18,7 @@ class UserCreationService
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private Hasher $hasher, private Hasher $hasher,
private PasswordBroker $passwordBroker, private PasswordBroker $passwordBroker
private UserRepositoryInterface $repository
) { ) {
} }
@ -27,7 +26,7 @@ class UserCreationService
* Create a new user on the system. * Create a new user on the system.
* *
* @throws \Exception * @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws DataValidationException
*/ */
public function handle(array $data): User public function handle(array $data): User
{ {
@ -36,15 +35,17 @@ class UserCreationService
} }
$this->connection->beginTransaction(); $this->connection->beginTransaction();
if (!isset($data['password']) || empty($data['password'])) { if (empty($data['password'])) {
$generateResetToken = true; $generateResetToken = true;
$data['password'] = $this->hasher->make(str_random(30)); $data['password'] = $this->hasher->make(str_random(30));
} }
/** @var \Pterodactyl\Models\User $user */ // /** @var User $user */
$user = $this->repository->create(array_merge($data, [ // $user = $this->repository->create($data, true, true);
$user = User::query()->forceCreate(array_merge($data, [
'uuid' => Uuid::uuid4()->toString(), 'uuid' => Uuid::uuid4()->toString(),
]), true, true); ]));
if (isset($generateResetToken)) { if (isset($generateResetToken)) {
$token = $this->passwordBroker->createToken($user); $token = $this->passwordBroker->createToken($user);

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Services\Users;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class UserDeletionService class UserDeletionService
@ -14,7 +13,6 @@ class UserDeletionService
* UserDeletionService constructor. * UserDeletionService constructor.
*/ */
public function __construct( public function __construct(
protected UserRepositoryInterface $repository,
protected ServerRepositoryInterface $serverRepository, protected ServerRepositoryInterface $serverRepository,
protected Translator $translator 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. * 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 public function handle(int|User $user): ?bool
{ {
if ($user instanceof User) { if (is_int($user)) {
$user = $user->id; $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) { if ($servers > 0) {
throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers')); throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers'));
} }
return $this->repository->delete($user); return $user->delete();
} }
} }