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

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\Http\Requests\Admin\UserFormRequest;
use App\Http\Requests\Admin\NewUserFormRequest;
use App\Contracts\Repository\UserRepositoryInterface;
class UserController extends Controller
{
@ -35,7 +34,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 App\Exceptions\DisplayException;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use App\Http\Requests\Auth\ResetPasswordRequest;
use App\Contracts\Repository\UserRepositoryInterface;
class ResetPasswordController extends Controller
{
@ -31,7 +30,6 @@ class ResetPasswordController extends Controller
public function __construct(
private Dispatcher $dispatcher,
private Hasher $hasher,
private UserRepositoryInterface $userRepository
) {
}
@ -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

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

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;
use App\Models\User;
use Illuminate\Support\Str;
use App\Models\Server;
use App\Models\Subuser;
use Illuminate\Database\ConnectionInterface;
use App\Services\Users\UserCreationService;
use App\Repositories\Eloquent\SubuserRepository;
use App\Contracts\Repository\UserRepositoryInterface;
use App\Exceptions\Repository\RecordNotFoundException;
use App\Exceptions\Service\Subuser\UserIsServerOwnerException;
use App\Exceptions\Service\Subuser\ServerSubuserExistsException;
@ -39,18 +39,8 @@ class SubuserCreationService
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 +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,

View File

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

View File

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

View File

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

View File

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