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;
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]));
}
}

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\UserDeletionService;
use Pterodactyl\Http\Requests\Admin\UserFormRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller
{
@ -34,7 +33,6 @@ class UserController extends Controller
protected UserDeletionService $deletionService,
protected Translator $translator,
protected UserUpdateService $updateService,
protected UserRepositoryInterface $repository,
protected ViewFactory $view
) {
}

View file

@ -12,7 +12,6 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Pterodactyl\Http\Requests\Auth\ResetPasswordRequest;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class ResetPasswordController extends Controller
{
@ -30,8 +29,7 @@ class ResetPasswordController extends Controller
*/
public function __construct(
private Dispatcher $dispatcher,
private Hasher $hasher,
private UserRepositoryInterface $userRepository
private Hasher $hasher
) {
}
@ -75,10 +73,9 @@ class ResetPasswordController extends Controller
*/
protected function resetPassword($user, $password)
{
$user = $this->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));

View file

@ -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);
}
}

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;
use Illuminate\Support\Str;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException;
use Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException;
@ -21,8 +21,7 @@ class SubuserCreationService
public function __construct(
private ConnectionInterface $connection,
private SubuserRepository $subuserRepository,
private UserCreationService $userCreationService,
private UserRepositoryInterface $userRepository
private UserCreationService $userCreationService
) {
}
@ -31,26 +30,16 @@ class SubuserCreationService
* If the email address already belongs to a user on the system a new user will not
* be created.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
* @throws DataValidationException
* @throws ServerSubuserExistsException
* @throws UserIsServerOwnerException
* @throws \Throwable
*/
public function handle(Server $server, string $email, array $permissions): Subuser
{
return $this->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,

View file

@ -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;
});

View file

@ -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')));

View file

@ -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);

View file

@ -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();
}
}