forked from unimatrix/web
48 lines
2.9 KiB
PHP
48 lines
2.9 KiB
PHP
<?php
|
|
if (!defined('IN_MYBB')) die('Direct access denied');
|
|
require_once __DIR__.'/system-board-policy.php';
|
|
// Provisioning runs only in the background publisher, never on a page load.
|
|
|
|
function dune_system_board_registry(): array {
|
|
global $db;
|
|
if (!$db->table_exists('dune_system_boards')) return [];
|
|
$query = $db->simple_select('dune_system_boards', '*', "kind IN ('clan','company','politics')");
|
|
$rows = [];
|
|
while ($row = $db->fetch_array($query)) $rows[$row['kind'].':'.$row['organisation_id']] = $row;
|
|
return $rows;
|
|
}
|
|
|
|
function dune_apply_system_board_membership(array $identity): void {
|
|
global $mybb, $db;
|
|
if (empty($mybb->user['uid'])) return;
|
|
$projection = $identity['systemBoards'] ?? null;
|
|
if (!is_array($projection) || ($projection['version'] ?? null) !== 1) throw new RuntimeException('Forum membership projection unavailable');
|
|
$boards = dune_system_board_registry();
|
|
|
|
$gids = array_values(array_unique(array_filter(array_merge([(int)$mybb->user['usergroup']], array_map('intval', explode(',', (string)($mybb->user['additionalgroups'] ?? '')))), fn($gid) => $gid > 0)));
|
|
if ($boards) {
|
|
$fids = array_map(fn($board) => (int)$board['fid'], array_values($boards));
|
|
$checks = $db->simple_select('forumpermissions', 'fid,COUNT(DISTINCT gid) AS policy_count', 'fid IN ('.implode(',', $fids).') AND gid IN ('.implode(',', $gids).')', ['group_by'=>'fid']);
|
|
$covered = [];
|
|
while ($row = $db->fetch_array($checks)) if ((int)$row['policy_count'] === count($gids)) $covered[(int)$row['fid']] = true;
|
|
foreach ($fids as $fid) if (empty($covered[$fid])) throw new RuntimeException('Forum group policies require refresh');
|
|
}
|
|
$projection = $identity['systemBoards'] ?? null;
|
|
if (!is_array($projection)) throw new RuntimeException('Forum membership projection unavailable');
|
|
$GLOBALS['dune_system_board_identity'] = $identity;
|
|
$managed = [];
|
|
foreach ($boards as $board) { $managed[] = (int)$board['member_gid']; $managed[] = (int)$board['leader_gid']; }
|
|
$allowed = dune_board_managed_groups($projection, $boards);
|
|
$groups = dune_board_additional_groups((string)($mybb->user['additionalgroups'] ?? ''), $managed, $allowed);
|
|
$old = array_filter(array_map('intval', explode(',', (string)($mybb->user['additionalgroups'] ?? ''))));
|
|
sort($old, SORT_NUMERIC);
|
|
if (implode(',', $old) === $groups) return;
|
|
$db->update_query('users', ['additionalgroups'=>$groups], 'uid='.(int)$mybb->user['uid']);
|
|
// MyBB has already resolved this request's usergroup. Discard it and start
|
|
// a fresh GET before permission caches, posting or attachment handlers run.
|
|
$path = $_SERVER['REQUEST_URI'] ?? '/';
|
|
if (!str_starts_with($path, '/') || str_starts_with($path, '//') || preg_match('/[\r\n]/', $path)) $path = '/';
|
|
header('Cache-Control: private, no-store');
|
|
header('Location: '.$path, true, 303);
|
|
exit;
|
|
}
|

Staff