Remove repositories and replace usages

This commit is contained in:
Lance Pioch 2022-10-22 03:59:36 -04:00
parent 860b2d890b
commit 369c61f1a8
11 changed files with 49 additions and 93 deletions

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