Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change token and account coupling system #25

Open
wants to merge 1 commit into
base: 5.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Config/seat-connector.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
'label' => 'seat-connector-teamspeak::seat.api_key',
'type' => 'text',
],
[
'name' => 'registration_group_name',
'label' => 'seat-connector-teamspeak::seat.registration_group',
'type' => 'text',
],
],
];
175 changes: 161 additions & 14 deletions src/Driver/TeamspeakClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
use GuzzleHttp\Exception\RequestException;
use Illuminate\Support\Arr;
use Seat\Services\Exceptions\SettingException;
use Seat\Web\Models\User;
use Warlof\Seat\Connector\Drivers\IClient;
use Warlof\Seat\Connector\Drivers\ISet;
use Warlof\Seat\Connector\Drivers\IUser;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\ConnexionException;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\EmptyResultException;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\ServerException;
use Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\TeamspeakException;
Expand Down Expand Up @@ -145,6 +147,18 @@ public static function getInstance(): IClient
return self::$instance;
}

/**
* @param int $instance_id
*
* @return $this
*/
public function setInstance(int $instance_id): self
{
$this->instance_id = $instance_id;

return $this;
}

/**
* @return \Warlof\Seat\Connector\Drivers\IUser[]
* @throws \Warlof\Seat\Connector\Exceptions\DriverException
Expand Down Expand Up @@ -282,6 +296,78 @@ public function findUserByName(string $nickname)
return $speaker;
}

/**
* @param \Seat\Web\Models\User $user
*
* @return \Warlof\Seat\Connector\Drivers\IUser[]
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
* @throws \Warlof\Seat\Connector\Exceptions\DriverException
* @throws \Warlof\Seat\Connector\Exceptions\InvalidDriverIdentityException
*/
public function customSearch(User $user): array
{
try {
$response = $this->sendCall('POST', '/{instance_id}/customsearch', [
'ident' => 'seat_user_id',
'pattern' => $user->getAuthIdentifier(),
'instance_id' => $this->instance_id,
]);
} catch (EmptyResultException $e) {
logger()->debug('Teamspeak return empty results', [
'command' => 'customsearch',
'ident' => 'seat_user_id',
'pattern' => $user->getAuthIdentifier(),
'instance_id' => $this->instance_id,
]);

return [];
}

$speakers = [];

foreach ($response as $speaker) {
$speakers[] = $this->getUser($speaker->cldbid);
}

return $speakers;
}

/**
* @param string $uid
*
* @return int[]
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
* @throws \Warlof\Seat\Connector\Exceptions\DriverException
* @throws \Warlof\Seat\Connector\Exceptions\InvalidDriverIdentityException
*/
public function clientGetIds(string $uid): array
{
try {
$response = $this->sendCall('GET', '/{instance}/clientgetids', [
'cluid' => $uid,
'instance' => $this->instance_id,
]);
} catch (EmptyResultException $e) {
logger()->debug('Teamspeak return empty results', [
'command' => 'clientgetids',
'cluid' => $uid,
'instance_id' => $this->instance_id,
]);

return [];
}

$speakers = [];

foreach ($response as $speaker) {
$speakers[] = $speaker->clid;
}

return $speakers;
}

/**
* @param string $id
* @return \Warlof\Seat\Connector\Drivers\ISet|null
Expand Down Expand Up @@ -385,12 +471,7 @@ public function getServerGroupMembers(ISet $server_group): array
*/
public function getSpeakerServerGroups(IUser $speaker): array
{
// scope: manage_scope
$response = $this->sendCall('GET', '/{instance}/serverinfo', [
'instance' => $this->instance_id,
]);

$server_info = Arr::first($response);
$server_info = $server_info = $this->serverInfo();

// scope: manage_scope
$response = $this->sendCall('GET', '/{instance}/servergroupsbyclientid', [
Expand All @@ -415,6 +496,74 @@ public function getSpeakerServerGroups(IUser $speaker): array
return $server_group;
}

/**
* @param int[] $clients
*
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
*/
public function kickClientFromServerInstance(array $clients)
{
$this->sendCall('POST', '/{instance}/clientkick', [
'clid' => $clients,
'reasonid' => 5,
'reasonmsg' => 'Duplicate account is not allowed !',
'instance' => $this->instance_id,
]);
}

/**
* @param \Warlof\Seat\Connector\Drivers\IUser $speaker
*
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
*/
public function deleteClientFromServerInstance(IUser $speaker)
{
$this->sendCall('POST', '/{instance}/clientdbdelete', [
'cldbid' => $speaker->getClientId(),
'instance' => $this->instance_id,
]);
}

/**
* @param \Seat\Web\Models\User $user
* @param int $server_group
*
* @return string
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
*/
public function tokenAdd(User $user, int $server_group): string
{
// scope: manage_scope
$response = $this->sendCall('POST', '/{instance}/tokenadd', [
'tokentype' => 0,
'tokenid1' => $server_group,
'tokenid2' => 0,
'tokendescription' => sprintf('Automatically created token from SeAT for %s', $user->name),
'tokencustomset' => sprintf('ident=seat_user_id value=%d', $user->id),
'instance' => $this->instance_id,
]);

return Arr::first($response)->token;
}

/**
* @return mixed
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\CommandException
* @throws \Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions\LoginException
*/
private function serverInfo()
{
// scope: manage_scope
$response = $this->sendCall('GET', '/{instance}/serverinfo', [
'instance' => $this->instance_id,
]);

return Arr::first($response);
}

/**
* @param string $method
* @param string $endpoint
Expand Down Expand Up @@ -462,7 +611,7 @@ private function sendCall(string $method, string $endpoint, array $arguments = [
'response' => [
'body' => $response->getBody()->getContents(),
],
],
]
);
} catch (ConnectException $e) {
throw new ConnexionException($e->getMessage(), $e->getCode(), $e);
Expand All @@ -476,6 +625,9 @@ private function sendCall(string $method, string $endpoint, array $arguments = [
if (in_array($result->status->code, [5122, 5124]))
throw new LoginException($result->status->message, $result->status->code);

if ($result->status->code == 1281)
throw new EmptyResultException($result->status->message, $result->status->code);

throw new CommandException($result->status->message, $result->status->code);
}

Expand All @@ -490,7 +642,7 @@ private function sendCall(string $method, string $endpoint, array $arguments = [
*/
private function seedSpeakers()
{
$from = 0;
$from = 0;

while (true) {
try {
Expand Down Expand Up @@ -527,12 +679,7 @@ private function seedSpeakers()
*/
private function seedServerGroups()
{
// scope: manage_scope
$response = $this->sendCall('GET', '/{instance}/serverinfo', [
'instance' => $this->instance_id,
]);

$server_info = Arr::first($response);
$server_info = $this->serverInfo();

// scope: manage_scope
$response = $this->sendCall('GET', '/{instance}/servergrouplist', [
Expand Down
31 changes: 31 additions & 0 deletions src/Exceptions/EmptyResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of SeAT Teamspeak Connector.
*
* Copyright (C) 2020 Warlof Tutsimo <[email protected]>
*
* SeAT Teamspeak Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* SeAT Teamspeak Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions;

/**
* Class EmptyResultException.
*
* @package Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions
*/
class EmptyResultException extends TeamspeakException
{

}
41 changes: 41 additions & 0 deletions src/Exceptions/InvalidServerGroupException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of SeAT Teamspeak Connector.
*
* Copyright (C) 2020 Warlof Tutsimo <[email protected]>
*
* SeAT Teamspeak Connector is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* SeAT Teamspeak Connector is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions;

use Throwable;

/**
* Class InvalidServerGroupException.
*
* @package Warlof\Seat\Connector\Drivers\Teamspeak\Exceptions
*/
class InvalidServerGroupException extends TeamspeakException
{
/**
* InvalidServerGroupException constructor.
*
* @param string $server_group
*/
public function __construct(string $server_group)
{
parent::__construct(sprintf('Server Group %s is not found.', $server_group));
}
}
Loading