2017-08-24 02:34:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Subusers;
|
|
|
|
|
2020-09-25 02:37:39 +00:00
|
|
|
use Illuminate\Support\Str;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Models\Subuser;
|
2017-08-24 02:34:11 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Users\UserCreationService;
|
2020-03-27 21:23:13 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-09-05 00:07:00 +00:00
|
|
|
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
|
|
|
|
{
|
2017-09-04 23:12:13 +00:00
|
|
|
/**
|
|
|
|
* SubuserCreationService constructor.
|
|
|
|
*/
|
2017-08-24 02:34:11 +00:00
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private SubuserRepository $subuserRepository,
|
|
|
|
private UserCreationService $userCreationService,
|
|
|
|
private UserRepositoryInterface $userRepository
|
2017-08-24 02:34:11 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-27 21:23:13 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2017-08-24 02:34:11 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
|
2020-03-27 21:23:13 +00:00
|
|
|
* @throws \Throwable
|
2017-08-24 02:34:11 +00:00
|
|
|
*/
|
2020-03-27 21:23:13 +00:00
|
|
|
public function handle(Server $server, string $email, array $permissions): Subuser
|
2017-08-24 02:34:11 +00:00
|
|
|
{
|
2020-03-27 21:23:13 +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'));
|
|
|
|
}
|
2022-10-14 16:59:20 +00:00
|
|
|
} catch (RecordNotFoundException) {
|
2020-09-25 02:37:39 +00:00
|
|
|
// 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);
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
$user = $this->userCreationService->handle([
|
|
|
|
'email' => $email,
|
2020-09-25 02:37:39 +00:00
|
|
|
'username' => $username,
|
2020-03-27 21:23:13 +00:00
|
|
|
'root_admin' => false,
|
|
|
|
]);
|
2017-08-24 02:34:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
return $this->subuserRepository->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'server_id' => $server->id,
|
2020-03-27 23:42:27 +00:00
|
|
|
'permissions' => array_unique($permissions),
|
2017-09-04 23:12:13 +00:00
|
|
|
]);
|
2020-03-27 21:23:13 +00:00
|
|
|
});
|
2017-08-24 02:34:11 +00:00
|
|
|
}
|
|
|
|
}
|