Files
device-management/app/Network/Clients/Hsgq/HsgqWebClient.php
T
Wian Drs 1e80be180e
tests / ci (push) Has been cancelled
Initial commit device-management
2026-08-25 09:42:00 +07:00

123 lines
3.7 KiB
PHP

<?php
namespace App\Network\Clients\Hsgq;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class HsgqWebClient
{
private ?string $token = null;
public function __construct(private readonly string $host, private readonly int $timeout = 10) {}
public function login(string $username, string $password): void
{
$response = $this->request()->post($this->url('/userlogin?form=login'), [
'method' => 'set',
'param' => [
'name' => $username,
'key' => md5($username.':'.$password),
'value' => '',
'captcha_v' => '',
'captcha_f' => '',
],
]);
$this->assertSuccess($response->json());
$this->token = $response->header('x-token');
if (! $this->token) {
throw new RuntimeException('HSGQ_WEB_AUTH_FAILED: WebGUI tidak memberikan token sesi.');
}
}
/** @return array<int, array<string, mixed>> */
public function users(): array
{
$response = $this->authenticated()->get($this->url('/usermgmt?form=userlist'));
$this->assertSuccess($response->json());
return $response->json('data', []);
}
public function addUser(string $username, string $password, int $level): void
{
$this->post('/usermgmt?form=userlist', [
'method' => 'add',
'param' => [
'name' => $username,
'key' => md5($username.':'.$password),
'level' => $level,
'reenter' => 4,
'info' => 'Managed by RADIQ NDM',
],
]);
}
public function changePassword(string $username, string $newPassword, string $currentPassword = ''): void
{
$this->post('/usermgmt?form=modifyps', [
'method' => 'set',
'param' => [
'name' => $username,
'key' => $currentPassword === '' ? '' : md5($username.':'.$currentPassword),
'key1' => md5($username.':'.$newPassword),
],
]);
}
public function deleteUser(string $username): void
{
$this->post('/usermgmt?form=userlist', [
'method' => 'delete',
'param' => ['name' => $username],
]);
}
public function hasUser(string $username): bool
{
return collect($this->users())->contains(fn (array $user) => ($user['name'] ?? null) === $username);
}
/** @return array<string, mixed> */
public function boardInfo(): array
{
$response = $this->authenticated()->get($this->url('/board_info'));
$this->assertSuccess($response->json());
return $response->json('data', []);
}
private function post(string $path, array $payload): void
{
$response = $this->authenticated()->post($this->url($path), $payload);
$this->assertSuccess($response->json());
}
private function request(): PendingRequest
{
return Http::acceptJson()->asJson()->timeout($this->timeout)->connectTimeout($this->timeout);
}
private function authenticated(): PendingRequest
{
if (! $this->token) {
throw new RuntimeException('HSGQ_WEB_AUTH_REQUIRED: sesi WebGUI belum dibuat.');
}
return $this->request()->withHeader('x-token', $this->token);
}
private function assertSuccess(?array $payload): void
{
if (($payload['code'] ?? null) !== 1) {
throw new RuntimeException('HSGQ_WEB_REJECTED: '.str($payload['message'] ?? 'WebGUI menolak operasi user.')->limit(160));
}
}
private function url(string $path): string
{
return 'http://'.$this->host.$path;
}
}