diff --git a/app/Console/Commands/User/DisableTwoFactorCommand.php b/app/Console/Commands/User/DisableTwoFactorCommand.php index 3cfe5e4d0..c45a5968e 100644 --- a/app/Console/Commands/User/DisableTwoFactorCommand.php +++ b/app/Console/Commands/User/DisableTwoFactorCommand.php @@ -2,8 +2,8 @@ namespace App\Console\Commands\User; +use App\Models\User; use Illuminate\Console\Command; -use App\Contracts\Repository\UserRepositoryInterface; class DisableTwoFactorCommand extends Command { @@ -11,14 +11,6 @@ class DisableTwoFactorCommand extends Command protected $signature = 'p:user:disable2fa {--email= : The email of the user to disable 2-Factor for.}'; - /** - * DisableTwoFactorCommand constructor. - */ - public function __construct(private UserRepositoryInterface $repository) - { - parent::__construct(); - } - /** * Handle command execution process. * @@ -32,12 +24,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 8c379f11a..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 24826acc1..bdce7eee5 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -6,7 +6,6 @@ use Illuminate\Support\ServiceProvider; use App\Repositories\Eloquent\EggRepository; use App\Repositories\Eloquent\NodeRepository; use App\Repositories\Eloquent\TaskRepository; -use App\Repositories\Eloquent\UserRepository; use App\Repositories\Eloquent\ApiKeyRepository; use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SubuserRepository; @@ -18,7 +17,6 @@ use App\Contracts\Repository\EggRepositoryInterface; use App\Repositories\Eloquent\EggVariableRepository; use App\Contracts\Repository\NodeRepositoryInterface; use App\Contracts\Repository\TaskRepositoryInterface; -use App\Contracts\Repository\UserRepositoryInterface; use App\Repositories\Eloquent\DatabaseHostRepository; use App\Contracts\Repository\ApiKeyRepositoryInterface; use App\Repositories\Eloquent\ServerVariableRepository; @@ -53,6 +51,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 bedb761ac..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 +54,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 4c835ddb1..f510f8d4e 100644 --- a/app/Services/Users/ToggleTwoFactorService.php +++ b/app/Services/Users/ToggleTwoFactorService.php @@ -8,7 +8,6 @@ use App\Models\User; use PragmaRX\Google2FA\Google2FA; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Encryption\Encrypter; -use App\Contracts\Repository\UserRepositoryInterface; use App\Repositories\Eloquent\RecoveryTokenRepository; use App\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; @@ -22,7 +21,6 @@ class ToggleTwoFactorService private Encrypter $encrypter, private Google2FA $google2FA, private RecoveryTokenRepository $recoveryTokenRepository, - private UserRepositoryInterface $repository ) { } @@ -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 f9c143c23..8488e35d0 100644 --- a/app/Services/Users/TwoFactorSetupService.php +++ b/app/Services/Users/TwoFactorSetupService.php @@ -4,7 +4,6 @@ namespace App\Services\Users; use App\Models\User; use Illuminate\Contracts\Encryption\Encrypter; -use App\Contracts\Repository\UserRepositoryInterface; use Illuminate\Contracts\Config\Repository as ConfigRepository; class TwoFactorSetupService @@ -17,7 +16,6 @@ class TwoFactorSetupService public function __construct( private ConfigRepository $config, private Encrypter $encrypter, - private UserRepositoryInterface $repository ) { } @@ -40,9 +38,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 ea2bee488..2f5369603 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Database\ConnectionInterface; use Illuminate\Contracts\Auth\PasswordBroker; use App\Notifications\AccountCreated; -use App\Contracts\Repository\UserRepositoryInterface; class UserCreationService { @@ -19,7 +18,6 @@ class UserCreationService private ConnectionInterface $connection, private Hasher $hasher, private PasswordBroker $passwordBroker, - private UserRepositoryInterface $repository ) { } @@ -36,15 +34,14 @@ 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 \App\Models\User $user */ - $user = $this->repository->create(array_merge($data, [ + $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 18e7e4e33..b86bf6b0c 100644 --- a/app/Services/Users/UserDeletionService.php +++ b/app/Services/Users/UserDeletionService.php @@ -5,7 +5,6 @@ namespace App\Services\Users; use App\Models\User; use App\Exceptions\DisplayException; use Illuminate\Contracts\Translation\Translator; -use App\Contracts\Repository\UserRepositoryInterface; class UserDeletionService { @@ -13,7 +12,6 @@ class UserDeletionService * UserDeletionService constructor. */ public function __construct( - protected UserRepositoryInterface $repository, protected Translator $translator ) { } @@ -33,6 +31,6 @@ class UserDeletionService throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers')); } - return $this->repository->delete($user->id); + return $user->delete(); } }