Files
2026-07-03 17:08:52 -07:00

84 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\NuqloudScrum\Service;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
use OCP\IURLGenerator;
class TalkConversationPermissionService {
public function __construct(
private IRequest $request,
private IURLGenerator $urlGenerator,
private IClientService $clientService,
) {
}
public function canCurrentUserModerateConversation(string $conversationToken): bool {
$room = $this->fetchConversation($conversationToken);
return is_array($room) && $this->extractModeratorFlag($room);
}
public function canCurrentUserAccessConversation(string $conversationToken): bool {
return is_array($this->fetchConversation($conversationToken));
}
/** @return array<string, mixed>|null */
private function fetchConversation(string $conversationToken): ?array {
$conversationToken = trim($conversationToken);
$cookieHeader = trim((string)$this->request->getHeader('cookie'));
if ($conversationToken === '' || $cookieHeader === '') {
return null;
}
$url = $this->urlGenerator->getAbsoluteURL(
'/ocs/v2.php/apps/spreed/api/v4/room/' . rawurlencode($conversationToken) . '?format=json'
);
try {
$response = $this->clientService->newClient()->get($url, [
'timeout' => 15,
'headers' => [
'Accept' => 'application/json',
'OCS-APIRequest' => 'true',
'Cookie' => $cookieHeader,
],
]);
$decoded = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable) {
return null;
}
$meta = is_array($decoded) ? ($decoded['ocs']['meta'] ?? null) : null;
$data = is_array($decoded) ? ($decoded['ocs']['data'] ?? null) : null;
if (!is_array($meta) || !is_array($data)) {
return null;
}
return strtolower(trim((string)($meta['status'] ?? ''))) === 'ok' ? $data : null;
}
/** @param array<string, mixed> $room */
private function extractModeratorFlag(array $room): bool {
foreach (['canModerate', 'isModerator', 'isOwner', 'moderator', 'owner'] as $key) {
if (array_key_exists($key, $room) && filter_var($room[$key], FILTER_VALIDATE_BOOLEAN)) {
return true;
}
}
foreach (['participantType', 'attendeeType', 'actorType'] as $key) {
if (array_key_exists($key, $room)) {
$type = (int)$room[$key];
if ($type > 0 && $type <= 2) {
return true;
}
}
}
foreach (['participant', 'self', 'attendee'] as $key) {
if (isset($room[$key]) && is_array($room[$key]) && $this->extractModeratorFlag($room[$key])) {
return true;
}
}
return false;
}
}