misc_pterodactyl-panel/app/Services/Subusers/SubuserCreationService.php

111 lines
4 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
2017-09-26 02:43:01 +00:00
/**
2017-08-24 02:34:11 +00:00
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-08-24 02:34:11 +00:00
*/
namespace Pterodactyl\Services\Subusers;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
2017-08-24 02:34:11 +00:00
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Users\UserCreationService;
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
2017-08-24 02:34:11 +00:00
use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException;
2017-08-24 02:34:11 +00:00
class SubuserCreationService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
2017-08-24 02:34:11 +00:00
/**
* @var \Pterodactyl\Repositories\Eloquent\SubuserRepository
2017-08-24 02:34:11 +00:00
*/
private $subuserRepository;
2017-08-24 02:34:11 +00:00
/**
* @var \Pterodactyl\Services\Users\UserCreationService
2017-08-24 02:34:11 +00:00
*/
private $userCreationService;
2017-08-24 02:34:11 +00:00
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
private $userRepository;
2017-08-24 02:34:11 +00:00
2017-09-04 23:12:13 +00:00
/**
* SubuserCreationService constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Repositories\Eloquent\SubuserRepository $subuserRepository
2019-09-06 04:32:57 +00:00
* @param \Pterodactyl\Services\Users\UserCreationService $userCreationService
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
2017-09-04 23:12:13 +00:00
*/
2017-08-24 02:34:11 +00:00
public function __construct(
ConnectionInterface $connection,
SubuserRepository $subuserRepository,
2017-09-25 03:28:16 +00:00
UserCreationService $userCreationService,
UserRepositoryInterface $userRepository
2017-08-24 02:34:11 +00:00
) {
$this->connection = $connection;
2017-09-25 03:28:16 +00:00
$this->subuserRepository = $subuserRepository;
2017-08-24 02:34:11 +00:00
$this->userRepository = $userRepository;
$this->userCreationService = $userCreationService;
}
/**
* Creates a new user on the system and assigns them access to the provided server.
* If the email address already belongs to a user on the system a new user will not
* be created.
*
* @param \Pterodactyl\Models\Server $server
2019-09-06 04:32:57 +00:00
* @param string $email
* @param array $permissions
2017-08-24 02:34:11 +00:00
* @return \Pterodactyl\Models\Subuser
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
* @throws \Throwable
2017-08-24 02:34:11 +00:00
*/
public function handle(Server $server, string $email, array $permissions): Subuser
2017-08-24 02:34:11 +00:00
{
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 $exception) {
$user = $this->userCreationService->handle([
'email' => $email,
'username' => preg_replace('/([^\w\.-]+)/', '', strtok($email, '@')) . str_random(3),
'name_first' => 'Server',
'name_last' => 'Subuser',
'root_admin' => false,
]);
2017-08-24 02:34:11 +00:00
}
return $this->subuserRepository->create([
'user_id' => $user->id,
'server_id' => $server->id,
'permissions' => $permissions,
2017-09-04 23:12:13 +00:00
]);
});
2017-08-24 02:34:11 +00:00
}
}