Remove user repository

This commit is contained in:
Lance Pioch 2024-03-16 19:10:31 -04:00
parent f27f673615
commit 1813e6f549
11 changed files with 30 additions and 82 deletions

View File

@ -2,8 +2,8 @@
namespace App\Console\Commands\User; namespace App\Console\Commands\User;
use App\Models\User;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use App\Contracts\Repository\UserRepositoryInterface;
class DisableTwoFactorCommand extends Command 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.}'; 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. * Handle command execution process.
* *
@ -32,12 +24,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 App\Contracts\Repository;
interface UserRepositoryInterface extends RepositoryInterface
{
}

View File

@ -20,7 +20,6 @@ use App\Services\Users\UserCreationService;
use App\Services\Users\UserDeletionService; use App\Services\Users\UserDeletionService;
use App\Http\Requests\Admin\UserFormRequest; use App\Http\Requests\Admin\UserFormRequest;
use App\Http\Requests\Admin\NewUserFormRequest; use App\Http\Requests\Admin\NewUserFormRequest;
use App\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller class UserController extends Controller
{ {
@ -35,7 +34,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 App\Exceptions\DisplayException;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords; use Illuminate\Foundation\Auth\ResetsPasswords;
use App\Http\Requests\Auth\ResetPasswordRequest; use App\Http\Requests\Auth\ResetPasswordRequest;
use App\Contracts\Repository\UserRepositoryInterface;
class ResetPasswordController extends Controller class ResetPasswordController extends Controller
{ {
@ -31,7 +30,6 @@ 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

@ -6,7 +6,6 @@ use Illuminate\Support\ServiceProvider;
use App\Repositories\Eloquent\EggRepository; use App\Repositories\Eloquent\EggRepository;
use App\Repositories\Eloquent\NodeRepository; use App\Repositories\Eloquent\NodeRepository;
use App\Repositories\Eloquent\TaskRepository; use App\Repositories\Eloquent\TaskRepository;
use App\Repositories\Eloquent\UserRepository;
use App\Repositories\Eloquent\ApiKeyRepository; use App\Repositories\Eloquent\ApiKeyRepository;
use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SessionRepository;
use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\SubuserRepository;
@ -18,7 +17,6 @@ use App\Contracts\Repository\EggRepositoryInterface;
use App\Repositories\Eloquent\EggVariableRepository; use App\Repositories\Eloquent\EggVariableRepository;
use App\Contracts\Repository\NodeRepositoryInterface; use App\Contracts\Repository\NodeRepositoryInterface;
use App\Contracts\Repository\TaskRepositoryInterface; use App\Contracts\Repository\TaskRepositoryInterface;
use App\Contracts\Repository\UserRepositoryInterface;
use App\Repositories\Eloquent\DatabaseHostRepository; use App\Repositories\Eloquent\DatabaseHostRepository;
use App\Contracts\Repository\ApiKeyRepositoryInterface; use App\Contracts\Repository\ApiKeyRepositoryInterface;
use App\Repositories\Eloquent\ServerVariableRepository; use App\Repositories\Eloquent\ServerVariableRepository;
@ -53,6 +51,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 App\Repositories\Eloquent;
use App\Models\User;
use App\Contracts\Repository\UserRepositoryInterface;
class UserRepository extends EloquentRepository implements UserRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return User::class;
}
}

View File

@ -2,13 +2,13 @@
namespace App\Services\Subusers; namespace App\Services\Subusers;
use App\Models\User;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Models\Server; use App\Models\Server;
use App\Models\Subuser; use App\Models\Subuser;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use App\Services\Users\UserCreationService; use App\Services\Users\UserCreationService;
use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\SubuserRepository;
use App\Contracts\Repository\UserRepositoryInterface;
use App\Exceptions\Repository\RecordNotFoundException; use App\Exceptions\Repository\RecordNotFoundException;
use App\Exceptions\Service\Subuser\UserIsServerOwnerException; use App\Exceptions\Service\Subuser\UserIsServerOwnerException;
use App\Exceptions\Service\Subuser\ServerSubuserExistsException; use App\Exceptions\Service\Subuser\ServerSubuserExistsException;
@ -39,18 +39,8 @@ class SubuserCreationService
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 +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([ 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 App\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 App\Contracts\Repository\UserRepositoryInterface;
use App\Repositories\Eloquent\RecoveryTokenRepository; use App\Repositories\Eloquent\RecoveryTokenRepository;
use App\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; use App\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
@ -22,7 +21,6 @@ class ToggleTwoFactorService
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

@ -4,7 +4,6 @@ namespace App\Services\Users;
use App\Models\User; use App\Models\User;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use App\Contracts\Repository\UserRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
class TwoFactorSetupService class TwoFactorSetupService
@ -17,7 +16,6 @@ class TwoFactorSetupService
public function __construct( public function __construct(
private ConfigRepository $config, private ConfigRepository $config,
private Encrypter $encrypter, private Encrypter $encrypter,
private UserRepositoryInterface $repository
) { ) {
} }
@ -40,9 +38,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,6 @@ use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Auth\PasswordBroker; use Illuminate\Contracts\Auth\PasswordBroker;
use App\Notifications\AccountCreated; use App\Notifications\AccountCreated;
use App\Contracts\Repository\UserRepositoryInterface;
class UserCreationService class UserCreationService
{ {
@ -19,7 +18,6 @@ class UserCreationService
private ConnectionInterface $connection, private ConnectionInterface $connection,
private Hasher $hasher, private Hasher $hasher,
private PasswordBroker $passwordBroker, private PasswordBroker $passwordBroker,
private UserRepositoryInterface $repository
) { ) {
} }
@ -36,15 +34,14 @@ 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 \App\Models\User $user */ $user = User::query()->forceCreate(array_merge($data, [
$user = $this->repository->create(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 App\Services\Users;
use App\Models\User; use App\Models\User;
use App\Exceptions\DisplayException; use App\Exceptions\DisplayException;
use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Translation\Translator;
use App\Contracts\Repository\UserRepositoryInterface;
class UserDeletionService class UserDeletionService
{ {
@ -13,7 +12,6 @@ class UserDeletionService
* UserDeletionService constructor. * UserDeletionService constructor.
*/ */
public function __construct( public function __construct(
protected UserRepositoryInterface $repository,
protected Translator $translator protected Translator $translator
) { ) {
} }
@ -33,6 +31,6 @@ class UserDeletionService
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->id); return $user->delete();
} }
} }