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,