web/website/integrations/mybb/system-board-policy.php
2026-11-21 20:22:37 +01:00

53 lines
2.5 KiB
PHP

<?php
// Pure policy: no database writes and no browser-supplied identity.
function dune_board_managed_groups(array $projection, array $boards): array {
if (($projection['version'] ?? null) !== 1) throw new RuntimeException('Forum membership projection unavailable');
$allowed = [];
foreach ($projection['organisations'] ?? [] as $grant) {
if (!in_array($grant['kind'] ?? '', ['clan', 'company', 'politics'], true) || empty($grant['id'])) continue;
$key = $grant['kind'].':'.$grant['id'];
if (!isset($boards[$key])) continue;
$board = $boards[$key];
if (($grant['read'] ?? false) !== true || ($grant['post'] ?? false) !== true) continue;
$allowed[] = (int)$board['member_gid'];
if (($grant['manage'] ?? false) === true) $allowed[] = (int)$board['leader_gid'];
}
if (($projection['inspectAll'] ?? false) === true || ($projection['moderateAll'] ?? false) === true) {
foreach ($boards as $board) $allowed[] = (int)$board['member_gid'];
}
$allowed = array_values(array_unique(array_filter($allowed, fn($id) => $id > 0)));
sort($allowed, SORT_NUMERIC);
return $allowed;
}
function dune_board_additional_groups(string $current, array $managed, array $allowed): string {
// Remove only groups owned by this integration. Political, staff and all
// other unrelated additional groups remain untouched.
$old = array_filter(array_map('intval', explode(',', $current)), fn($id) => $id > 0);
$groups = array_values(array_unique(array_merge(array_diff($old, $managed), $allowed)));
sort($groups, SORT_NUMERIC);
return implode(',', $groups);
}
function dune_board_private_permissions(array $columns, bool $member): array {
$allowed = ['canview', 'canviewthreads', 'candlattachments', 'canpostthreads', 'canpostreplys',
'canpostattachments', 'caneditposts', 'caneditattachments', 'canpostpolls', 'canvotepolls', 'cansearch'];
$result = [];
foreach ($columns as $column) {
if (in_array($column, ['pid', 'fid', 'gid'], true)) continue;
$result[$column] = $member && in_array($column, $allowed, true) ? 1 : 0;
}
return $result;
}
function dune_board_descendant(int $target, int $root, array $parents): bool {
if ($root < 1 || $target < 1) return false;
$seen = [];
while ($target > 0) {
if ($target === $root) return true;
if (isset($seen[$target]) || !array_key_exists($target, $parents)) return false;
$seen[$target] = true;
$target = (int)$parents[$target];
}
return false;
}