diff --git a/docs/windows-hosted-verifier.md b/docs/windows-hosted-verifier.md index 612a5040..face174d 100644 --- a/docs/windows-hosted-verifier.md +++ b/docs/windows-hosted-verifier.md @@ -44,7 +44,11 @@ older than 30 minutes cannot authorize even if the periodic callback is late. Read-only mode does not register the legacy verifier's member/thread mutation listeners, approve guild join requests or run verification-driven role/ban -timers. It does not synchronize Discord roles. Registration, token challenges, +timers. Portable instances cannot add, remove, or replace Discord member roles, +including indirect ban-check, game moderation, and faction command paths. +The official hosted CivilizationBot owns those writes. A local game moderation +action can still change local game state, but does not apply a Discord role; +use the hosted bot for Discord role changes. Registration, token challenges, provisioning and deletion are rejected before local or remote writes. Direct users to the primary hosted bot to complete or change verification. Other bot commands retain their existing authorization checks; this mode grants none. diff --git a/src/Civ13/Civ13.php b/src/Civ13/Civ13.php index 484495a3..3c9ef469 100644 --- a/src/Civ13/Civ13.php +++ b/src/Civ13/Civ13.php @@ -1098,6 +1098,32 @@ public function statusChanger(Activity $activity, string $state = 'online'): voi { $this->discord->updatePresence($activity, false, $state); } + public const DISCORD_ROLE_AUTHORITY_MESSAGE = 'Discord roles are managed by the primary CivilizationBot on civ13.com.'; + + /** Only the hosted bot can write roles. Replica freshness never grants write authority. */ + public function canWriteDiscordRoles(): bool + { + return $this->portable_state_dir === '' + && ! ($this->options['verification_read_only'] ?? false) + && ! (isset($this->verifier->readOnly) && $this->verifier->readOnly); + } + + /** Skip Discord side effects on replicas while preserving read-only checks and game moderation. */ + public function addDiscordRole(Member $member, mixed $role, ?string $reason = null): PromiseInterface + { + return $this->canWriteDiscordRoles() ? $member->addRole($role, $reason) : resolve($member); + } + + public function removeDiscordRole(Member $member, mixed $role, ?string $reason = null): PromiseInterface + { + return $this->canWriteDiscordRoles() ? $member->removeRole($role, $reason) : resolve($member); + } + + public function replaceDiscordRoles(Member $member, mixed $roles, ?string $reason = null): PromiseInterface + { + return $this->canWriteDiscordRoles() ? $member->setRoles($roles, $reason) : resolve($member); + } + /** * Removes specified roles from a member. * @@ -1111,17 +1137,21 @@ public function removeRoles( Collection|array|Role|string|int $roles, bool $patch = true ): PromiseInterface { + if (! $this->canWriteDiscordRoles()) { + return resolve($member); + } + if (! $role_ids = array_filter(self::__rolesToIdArray($roles), fn ($role_id) => $member->roles->has($role_id))) { return resolve($member); } return $patch ? ((($new_roles = $member->roles->filter(static fn (Role $role) => ! in_array($role->id, $role_ids))) !== $member->roles) - ? $member->setRoles($new_roles->toArray()) + ? $this->replaceDiscordRoles($member, $new_roles->toArray()) : resolve($member)) : array_reduce( $role_ids, - fn ($promise, $role_id) => $promise->then(static fn () => $member->removeRole($role_id)), + fn ($promise, $role_id) => $promise->then(fn () => $this->removeDiscordRole($member, $role_id)), resolve($member) // Each role is removed exactly once, in order ) ->then(static fn () => $member->guild->members->get('id', $member->id)); @@ -1139,12 +1169,17 @@ public function addRoles( Collection|array|Role|string|int $roles, bool $patch = true ): PromiseInterface { + if (! $this->canWriteDiscordRoles()) { + return resolve($member); + } + if (! $role_ids = array_filter(self::__rolesToIdArray($roles), fn ($role_id) => ! $member->roles->has($role_id))) { return resolve($member); } return $patch - ? $member->setRoles( + ? $this->replaceDiscordRoles( + $member, array_merge( $member->roles ->map(fn ($role) => $role->id) @@ -1154,7 +1189,7 @@ public function addRoles( ) : array_reduce( $role_ids, - fn ($promise, $role_id) => $promise->then(static fn () => $member->addRole($role_id)), + fn ($promise, $role_id) => $promise->then(fn () => $this->addDiscordRole($member, $role_id)), resolve($member) // Each role is added exactly once, in order ) ->then(static fn () => $member->guild->members->get('id', $member->id)); @@ -1173,6 +1208,10 @@ public function setRoles( Collection|array|Role|string|int $add_roles = [], Collection|array|Role|string|int $remove_roles = [] ): PromiseInterface { + if (! $this->canWriteDiscordRoles()) { + return resolve($member); + } + $add_roles = array_filter(self::__rolesToIdArray($add_roles), static fn ($role_id) => ! $member->roles->has($role_id)); $remove_roles = array_filter(self::__rolesToIdArray($remove_roles), static fn ($role_id) => $member->roles->has($role_id)); if (! $add_roles && ! $remove_roles) { @@ -1183,7 +1222,7 @@ public function setRoles( $current[] = (string) $role->id; } - return $member->setRoles(array_values(array_unique(array_diff(array_merge($current, $add_roles), $remove_roles)))); + return $this->replaceDiscordRoles($member, array_values(array_unique(array_diff(array_merge($current, $add_roles), $remove_roles)))); } /** * Convert roles to an array of role IDs. @@ -1812,13 +1851,13 @@ private function __bancheckTimer(): bool } //$this->logger->debug("Checking bans for {$item['ss13']}..."); if (($banned = $this->bancheck($item['ss13'], true, false)) && ! ($member->roles->has($this->role_ids['Banished']) || $member->roles->has($this->role_ids['Permabanished']))) { - $member->addRole($this->role_ids['Banished'], 'bancheck timer'); + $this->addDiscordRole($member, $this->role_ids['Banished'], 'bancheck timer'); if (isset($this->channel_ids['staff_bot']) && $channel = $this->discord->getChannel($this->channel_ids['staff_bot'])) { $this->sendMessage($channel, "Added the banished role to $member."); } } elseif (! $banned && ($member->roles->has($this->role_ids['Banished']) || $member->roles->has($this->role_ids['Permabanished']))) { - $member->removeRole($this->role_ids['Banished'], 'bancheck timer'); - $member->removeRole($this->role_ids['Permabanished'], 'bancheck timer'); + $this->removeDiscordRole($member, $this->role_ids['Banished'], 'bancheck timer'); + $this->removeDiscordRole($member, $this->role_ids['Permabanished'], 'bancheck timer'); if (isset($this->channel_ids['staff_bot']) && $channel = $this->discord->getChannel($this->channel_ids['staff_bot'])) { $this->sendMessage($channel, "Removed the banished role from $member."); } @@ -1845,13 +1884,13 @@ public function bancheck(string $ckey, bool $bypass = false, bool $use_cache = f return false; } $banned = array_reduce($this->enabled_gameservers, fn ($carry, $gameserver) => $carry || $gameserver->bancheck($ckey, false, $use_cache), false); - if ($bypass || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) { + if ($bypass || ! $this->canWriteDiscordRoles() || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) { return $banned; } if ($banned && ! $member->roles->has($this->role_ids['Banished']) && ! $member->roles->has($this->role_ids['Admin'])) { - $member->addRole($this->role_ids['Banished'], "bancheck ($ckey)"); + $this->addDiscordRole($member, $this->role_ids['Banished'], "bancheck ($ckey)"); } elseif (! $banned && $member->roles->has($this->role_ids['Banished'])) { - $member->removeRole($this->role_ids['Banished'], "bancheck ($ckey)"); + $this->removeDiscordRole($member, $this->role_ids['Banished'], "bancheck ($ckey)"); } return $banned; @@ -1876,13 +1915,13 @@ public function permabancheck(string $ckey, bool $bypass = false): bool return false; } $permabanned = array_reduce($this->enabled_gameservers, fn ($carry, $gameserver) => $carry || $gameserver->permabancheck($ckey), false); - if ($bypass || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) { + if ($bypass || ! $this->canWriteDiscordRoles() || ! isset($this->verifier) || ! $member = $this->verifier->getVerifiedMember($ckey)) { return $permabanned; } if ($permabanned && ! $member->roles->has($this->role_ids['Permabanished']) && ! $member->roles->has($this->role_ids['Admin'])) { - $member->setRoles([$this->role_ids['Banished'], $this->role_ids['Permabanished']], "permabancheck ($ckey)"); + $this->replaceDiscordRoles($member, [$this->role_ids['Banished'], $this->role_ids['Permabanished']], "permabancheck ($ckey)"); } elseif (! $permabanned && $member->roles->has($this->role_ids['Permabanished'])) { - $member->removeRole($this->role_ids['Permabanished'], "permabancheck ($ckey)"); + $this->removeDiscordRole($member, $this->role_ids['Permabanished'], "permabancheck ($ckey)"); } return $permabanned; @@ -1963,12 +2002,12 @@ public function ban(array $array /* = ['ckey' => '', 'duration' => '', 'reason' } $array['ckey'] = $item['ss13']; } - if (isset($this->verifier) && $member = $this->verifier->getVerifiedMember($array['ckey'])) { + if ($this->canWriteDiscordRoles() && isset($this->verifier) && $member = $this->verifier->getVerifiedMember($array['ckey'])) { if (! $member->roles->has($this->role_ids['Banished'])) { if (! $permanent) { - $member->addRole($this->role_ids['Banished'], "Banned for {$array['duration']} with the reason {$array['reason']}"); + $this->addDiscordRole($member, $this->role_ids['Banished'], "Banned for {$array['duration']} with the reason {$array['reason']}"); } else { - $member->setRoles([$this->role_ids['Banished'], $this->role_ids['Permabanished']], "Banned for {$array['duration']} with the reason {$array['reason']}"); + $this->replaceDiscordRoles($member, [$this->role_ids['Banished'], $this->role_ids['Permabanished']], "Banned for {$array['duration']} with the reason {$array['reason']}"); } } } diff --git a/src/Civ13/GameServer.php b/src/Civ13/GameServer.php index a812a455..d849aa0f 100644 --- a/src/Civ13/GameServer.php +++ b/src/Civ13/GameServer.php @@ -1653,10 +1653,10 @@ public function ban(array $array /* = ['ckey' => '', 'duration' => '', 'reason' if (!$this->legacy) { return $this->sqlBan($array, $admin); } $result = $this->legacyBan($array, $admin); if (str_starts_with($result, 'Unable to open')) { return $result; } - if (isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($array['ckey'])) { + if ($this->civ13->canWriteDiscordRoles() && isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($array['ckey'])) { if (! $member->roles->has($this->civ13->role_ids['Banished'])) { $string = "Banned for {$array['duration']} with the reason {$array['reason']}"; - $permanent ? $member->setRoles([$this->civ13->role_ids['Banished'], $this->civ13->role_ids['Permabanished']], $string) : $member->addRole($this->civ13->role_ids['Banished'], $string); + $permanent ? $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Banished'], $this->civ13->role_ids['Permabanished']], $string) : $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished'], $string); } } @@ -1720,13 +1720,13 @@ public function unban(string $ckey, ?string $admin = null): PromiseInterface if (!$this->legacy) { return reject(new \LogicException('SQL unban is not implemented.')); } return $this->legacyUnban($ckey, $admin)->then(function () use ($ckey, $admin): PromiseInterface { $roles = []; - if (isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($ckey)) { + if ($this->civ13->canWriteDiscordRoles() && isset($this->civ13->verifier) && $member = $this->civ13->verifier->getVerifiedMember($ckey)) { if ($member->roles->has($this->civ13->role_ids['Banished'])) { - $roles[] = $member->removeRole($this->civ13->role_ids['Banished'], "Unbanned by $admin"); + $roles[] = $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Banished'], "Unbanned by $admin"); } if ($member->roles->has($this->civ13->role_ids['Permabanished'])) { - $roles[] = $member->removeRole($this->civ13->role_ids['Permabanished'], "Unbanned by $admin")->then(function () use (&$member, $admin) { - return $member->addRole($this->civ13->role_ids['Verified'], "Unbanned by $admin"); + $roles[] = $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Permabanished'], "Unbanned by $admin")->then(function () use (&$member, $admin) { + return $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], "Unbanned by $admin"); }); } } diff --git a/src/Civ13/MessageCommand/Commands/ApproveMe.php b/src/Civ13/MessageCommand/Commands/ApproveMe.php index b49fb4fa..0fbc274e 100644 --- a/src/Civ13/MessageCommand/Commands/ApproveMe.php +++ b/src/Civ13/MessageCommand/Commands/ApproveMe.php @@ -27,11 +27,15 @@ class ApproveMe extends Civ13MessageCommand */ public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE); + } + if (isset($this->civ13->role_ids['Verified']) && $message->member->roles->has($this->civ13->role_ids['Verified'])) { return $this->civ13->reply($message, 'You already have the verification role!'); } if ($item = $this->civ13->verifier->getVerifiedItem($message->author)) { - return $message->member->setRoles([$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}") + return $this->civ13->replaceDiscordRoles($message->member, [$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}") ->then(static fn () => $message->react('👍')); } if (! $ckey = self::messageWithoutCommand($command, $message_filtered, true, true)) { diff --git a/src/Civ13/MessageCommand/Commands/BanCheck.php b/src/Civ13/MessageCommand/Commands/BanCheck.php index ab9d956b..56cf08eb 100644 --- a/src/Civ13/MessageCommand/Commands/BanCheck.php +++ b/src/Civ13/MessageCommand/Commands/BanCheck.php @@ -115,17 +115,17 @@ protected function fillContent(GameServer &$gameserver, string &$content, bool & */ protected function updateBanished(bool $found, string $ckey): ?PromiseInterface { - if (! isset($this->civ13->role_ids['Banished'])) { + if (! $this->civ13->canWriteDiscordRoles() || ! isset($this->civ13->role_ids['Banished'])) { return null; } if (! $member = $this->civ13->verifier->getVerifiedMember($ckey)) { return null; } if ($found && ! $member->roles->has($this->civ13->role_ids['Banished'])) { - return $member->addRole($this->civ13->role_ids['Banished']); + return $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished']); } - //if (! $found && $member->roles->has($this->civ13->role_ids['Banished'])) return $member->removeRole($this->civ13->role_ids['Banished']); // They might be banned on a disabled server + //if (! $found && $member->roles->has($this->civ13->role_ids['Banished'])) return $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Banished']); // They might be banned on a disabled server return null; } } diff --git a/src/Civ13/MessageCommand/Commands/Civ13GameServerBan.php b/src/Civ13/MessageCommand/Commands/Civ13GameServerBan.php index c6dee1f1..eef2bf1c 100644 --- a/src/Civ13/MessageCommand/Commands/Civ13GameServerBan.php +++ b/src/Civ13/MessageCommand/Commands/Civ13GameServerBan.php @@ -55,9 +55,9 @@ public function __invoke(Message $message, string $command, array $message_filte } $arr = ['ckey' => $split_message[0], 'duration' => $split_message[1], 'reason' => $split_message[2]." Appeal at {$this->civ13->discord_formatted}"]; $result = $this->civ13->ban($arr, $this->authorCkey($message), $this->gameserver->key); - if ($member = $this->civ13->verifier->getVerifiedMember($split_message[0])) { + if ($this->civ13->canWriteDiscordRoles() && $member = $this->civ13->verifier->getVerifiedMember($split_message[0])) { if (! $member->roles->has($this->civ13->role_ids['Banished'])) { - $member->addRole($this->civ13->role_ids['Banished'], $result); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished'], $result); } } diff --git a/src/Civ13/MessageCommand/Commands/FixRoles.php b/src/Civ13/MessageCommand/Commands/FixRoles.php index 77b075a9..955b5828 100644 --- a/src/Civ13/MessageCommand/Commands/FixRoles.php +++ b/src/Civ13/MessageCommand/Commands/FixRoles.php @@ -28,6 +28,10 @@ class FixRoles extends Civ13MessageCommand */ public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE); + } + if (! $guild = $this->civ13->discord->guilds->get('id', $this->civ13->civ13_guild_id)) { return $message->react('🔥'); } @@ -38,7 +42,7 @@ public function __invoke(Message $message, string $command, array $message_filte })) { foreach ($unverified_members as $member) { if ($this->civ13->verifier->getVerifiedItem($member)) { - $member->addRole($this->civ13->role_ids['Verified'], 'fixroles'); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], 'fixroles'); } } } @@ -47,7 +51,7 @@ public function __invoke(Message $message, string $command, array $message_filte ) { foreach ($verified_members as $member) { if (! $this->civ13->verifier->getVerifiedItem($member)) { - $member->removeRole($this->civ13->role_ids['Verified'], 'fixroles'); + $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Verified'], 'fixroles'); } } } diff --git a/src/Civ13/MessageCommand/Commands/JoinRoles.php b/src/Civ13/MessageCommand/Commands/JoinRoles.php index 5cd36321..717bcb89 100644 --- a/src/Civ13/MessageCommand/Commands/JoinRoles.php +++ b/src/Civ13/MessageCommand/Commands/JoinRoles.php @@ -28,6 +28,10 @@ class JoinRoles extends Civ13MessageCommand */ public function __invoke(Message $message, string $command, array $message_filtered): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->civ13->reply($message, \Civ13\Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE); + } + $this->civ13->verifier->getVerified(); foreach ($this->civ13->verifier->provisional as $item) { $this->civ13->verifier->provisionalRegistration($item['ss13'], $item['discord']); diff --git a/src/Civ13/MessageServiceManager.php b/src/Civ13/MessageServiceManager.php index 44744f6e..85301405 100644 --- a/src/Civ13/MessageServiceManager.php +++ b/src/Civ13/MessageServiceManager.php @@ -84,7 +84,10 @@ public function handle(Message $message): ?PromiseInterface if ($message_array['message_content_lower'] === 'dev') { // This is a special case for the developer to test things if (isset($this->civ13->technician_id) && isset($this->civ13->role_ids['Chief Technical Officer'])) { if ($message->user_id === $this->civ13->technician_id) { - return $message->member->addRole($this->civ13->role_ids['Chief Technical Officer']); + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->civ13->reply($message, Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE); + } + return $this->civ13->addDiscordRole($message->member, $this->civ13->role_ids['Chief Technical Officer']); } } } @@ -226,8 +229,8 @@ private function paroleCommand(bool $parole): \Closure $role = $this->civ13->role_ids['Paroled']; if (($member = $this->civ13->verifier->getVerifiedMember($item)) && $member->roles->has($role) !== $parole) { $parole - ? $member->addRole($role, "`$admin` ({$message->member->displayname}) paroled `$ckey`") - : $member->removeRole($role, "`$admin` ({$message->member->displayname}) released `$ckey`"); + ? $this->civ13->addDiscordRole($member, $role, "`$admin` ({$message->member->displayname}) paroled `$ckey`") + : $this->civ13->removeDiscordRole($member, $role, "`$admin` ({$message->member->displayname}) released `$ckey`"); } if ($channel = $this->discord->getChannel($this->civ13->channel_ids['parole_logs'])) { $this->civ13->sendMessage($channel, "`$ckey` (<@{$item['discord']}>) has been ".($parole ? 'placed on parole' : 'released from parole')." by `$admin` (<@{$message->user_id}>)."); diff --git a/src/Civ13/Panels.php b/src/Civ13/Panels.php index 7779c61a..fc80c766 100644 --- a/src/Civ13/Panels.php +++ b/src/Civ13/Panels.php @@ -345,6 +345,10 @@ private function verifyModal(Interaction $i): PromiseInterface private function refreshRoles(Interaction $i): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $i->respondWithMessage($this->plain(Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE), true); + } + $item = $this->civ13->verifier->get('discord', $i->member->id); if (! $item) { return $i->respondWithMessage($this->plain('You are not verified, so there are no roles to sync.'), true); @@ -393,6 +397,10 @@ private function myBans(Interaction $i): PromiseInterface private function joinCampaign(Interaction $i): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $i->respondWithMessage($this->plain(Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE), true); + } + if (! $this->civ13->verifier->getVerifiedItem($i->member->id)) { return $i->respondWithMessage($this->plain('You must verify first.'), true); } @@ -410,7 +418,7 @@ private function joinCampaign(Interaction $i): PromiseInterface } $lowest = array_keys($counts, min($counts), true); $chosen = $lowest[array_rand($lowest)]; - $i->member->addRole($chosen); + $this->civ13->addDiscordRole($i->member, $chosen); return $i->respondWithMessage($this->plain("You have joined <@&{$chosen}>."), true); } diff --git a/src/Civ13/Slash.php b/src/Civ13/Slash.php index e9d18165..dee35ac1 100644 --- a/src/Civ13/Slash.php +++ b/src/Civ13/Slash.php @@ -727,12 +727,12 @@ private function __declareListeners(): void $content .= "No bans were found for `{$item['ss13']}`.".PHP_EOL; if ($member = $this->civ13->verifier->getVerifiedMember($item['ss13'])) { if ($member->roles->has($this->civ13->role_ids['Banished'])) { - $member->removeRole($this->civ13->role_ids['Banished']); + $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Banished']); } } } elseif ($member = $this->civ13->verifier->getVerifiedMember($item['ss13'])) { if (! $member->roles->has($this->civ13->role_ids['Banished'])) { - $member->addRole($this->civ13->role_ids['Banished']); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Banished']); } } if (strlen($content) <= 2000) { @@ -821,7 +821,7 @@ private function __declareListeners(): void $admin = $this->civ13->verifier->getVerifiedItem($interaction->user->id)['ss13']; if ($member = $this->civ13->verifier->getVerifiedMember($item)) { if (! $member->roles->has($this->civ13->role_ids['Paroled'])) { - $member->addRole($this->civ13->role_ids['Paroled'], "`$admin` ({$interaction->user->username}) paroled `$ckey`"); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Paroled'], "`$admin` ({$interaction->user->username}) paroled `$ckey`"); } } if ($channel = $this->discord->getChannel($this->civ13->channel_ids['parole_logs'])) { @@ -839,7 +839,7 @@ private function __declareListeners(): void $admin = $this->civ13->verifier->getVerifiedItem($interaction->user->id)['ss13']; if ($member = $this->civ13->verifier->getVerifiedMember($item)) { if ($member->roles->has($this->civ13->role_ids['Paroled'])) { - $member->removeRole($this->civ13->role_ids['Paroled'], "`$admin` ({$interaction->user->username}) released `$ckey`"); + $this->civ13->removeDiscordRole($member, $this->civ13->role_ids['Paroled'], "`$admin` ({$interaction->user->username}) released `$ckey`"); } } if ($channel = $this->discord->getChannel($this->civ13->channel_ids['parole_logs'])) { @@ -973,6 +973,10 @@ private function __declareListeners(): void ); $this->discord->listenCommand('join_campaign', function (Interaction $interaction): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent(Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE), true); + } + //return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent('Factions are not ready to be assigned yet'), true); if (! $this->civ13->verifier->getVerifiedItem($interaction->member->id)) { return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent('You are either not currently verified with a byond username or do not exist in the cache yet'), true); @@ -990,12 +994,16 @@ private function __declareListeners(): void return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent('No factions are currently available'), true); } $selectedRoles = array_keys($roleCounts, min($roleCounts)); // Get the role(s) with the lowest member count - $interaction->member->addRole($selectedRole = $selectedRoles[array_rand($selectedRoles)]); + $this->civ13->addDiscordRole($interaction->member, $selectedRole = $selectedRoles[array_rand($selectedRoles)]); return $this->respondWithMessage($interaction, Civ13::createBuilder(true)->setContent("You've been assigned to <@&$selectedRole>"), true); }); $this->discord->listenCommand('assign_faction', function (Interaction $interaction): PromiseInterface { + if (! $this->civ13->canWriteDiscordRoles()) { + return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent(Civ13::DISCORD_ROLE_AUTHORITY_MESSAGE), true); + } + if (! $interaction->member->roles->has($this->civ13->role_ids['Faction Organizer'])) { return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent('You do not have permission to assign factions!'), true); } @@ -1088,7 +1096,7 @@ private function __declareListeners(): void } if ($this->civ13->verifier->get('discord', $interaction->member->id)) { return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent('You are already verified! Your roles are being restored.'), true)->then( - fn (): PromiseInterface => $interaction->member->setRoles([$this->civ13->role_ids['Verified']], 'approveme in-game (already verified)') + fn (): PromiseInterface => $this->civ13->replaceDiscordRoles($interaction->member, [$this->civ13->role_ids['Verified']], 'approveme in-game (already verified)') ); } @@ -1112,7 +1120,7 @@ private function __declareListeners(): void } return $this->respondWithMessage($interaction, Civ13::createBuilder()->setContent("Welcome to {$interaction->member->guild->name}}! Your roles have been set and you should now have access to the rest of the server."), true)->then( - fn (): PromiseInterface => $interaction->member->setRoles([$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}") + fn (): PromiseInterface => $this->civ13->replaceDiscordRoles($interaction->member, [$this->civ13->role_ids['Verified']], "approveme {$item['ss13']}") ); }); diff --git a/src/Civ13/Verifier.php b/src/Civ13/Verifier.php index fe1f10f6..e7476950 100644 --- a/src/Civ13/Verifier.php +++ b/src/Civ13/Verifier.php @@ -324,16 +324,16 @@ public function joinRoles(Member $member, bool $send_welcome_message = true): ?P $banned = $this->civ13->bancheck($item['ss13'], true); $paroled = isset($this->civ13->paroled[$item['ss13']]); if ($banned && $paroled) { - return $member->setroles([$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished'], $this->civ13->role_ids['Paroled']], "bancheck join {$item['ss13']}"); + return $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished'], $this->civ13->role_ids['Paroled']], "bancheck join {$item['ss13']}"); } if ($banned) { - return $member->setroles([$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished']], "bancheck join {$item['ss13']}"); + return $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished']], "bancheck join {$item['ss13']}"); } if ($paroled) { - return $member->setroles([$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Paroled']], "parole join {$item['ss13']}"); + return $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Paroled']], "parole join {$item['ss13']}"); } - return $member->setroles([$this->civ13->role_ids['Verified']], "verified join {$item['ss13']}"); + return $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified']], "verified join {$item['ss13']}"); } } if ($send_welcome_message) { @@ -521,7 +521,7 @@ private function eligibility(string $ckey, string $discord_id, ?Member $m = null } if (! isset($this->civ13->permitted[$ckey]) && $this->civ13->permabancheck($ckey)) { if ($m && ! $m->roles->has($this->civ13->role_ids['Permabanished'])) { - $m->addRole($this->civ13->role_ids['Permabanished'], "permabancheck $ckey"); + $this->civ13->addDiscordRole($m, $this->civ13->role_ids['Permabanished'], "permabancheck $ckey"); } if (isset($ban_appeals)) { $this->civ13->then( @@ -534,7 +534,7 @@ private function eligibility(string $ckey, string $discord_id, ?Member $m = null } if (isset($this->civ13->softbanned[$ckey]) || isset($this->civ13->softbanned[$discord_id])) { if ($m && ! $m->roles->has($this->civ13->role_ids['Permabanished'])) { - $m->addRole($this->civ13->role_ids['Permabanished'], "permabancheck $ckey"); + $this->civ13->addDiscordRole($m, $this->civ13->role_ids['Permabanished'], "permabancheck $ckey"); } if (isset($ban_appeals)) { $this->civ13->then( @@ -547,7 +547,7 @@ private function eligibility(string $ckey, string $discord_id, ?Member $m = null } if ($this->get('discord', $discord_id)) { if (($member = $this->civ13->discord->guilds->get('id', $this->civ13->civ13_guild_id)->members->get('id', $discord_id)) && ! $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->setRoles([$this->civ13->role_ids['Verified']], "approveme join $ckey"); + $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified']], "approveme join $ckey"); } return 'You are already verified!'; @@ -693,21 +693,21 @@ public function verify(string $ckey, string $discord_id, bool $provisional = fal $this->civ13->__panicUnban($ckey); $error .= ' and the panic bunker ban removed.'; if (! $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->addRole($this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); } if ($channel) { $this->civ13->sendMessage($channel, "Verified and removed the panic bunker ban from $member (`$ckey` - {$this->civ13->ages[$ckey]})."); } } elseif ($this->civ13->bancheck($ckey, true)) { if (! $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->setroles([$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished']], "approveme verified ($ckey)"); + $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified'], $this->civ13->role_ids['Banished']], "approveme verified ($ckey)"); } if ($channel) { $this->civ13->sendMessage($channel, "Added the banished role to $member (`$ckey` - {$this->civ13->ages[$ckey]})."); } } else { if (! $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->addRole($this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); + $this->civ13->addDiscordRole($member, $this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); } if ($channel) { $this->civ13->sendMessage($channel, "Verified $member. (`$ckey`".((isset($this->civ13->ages[$ckey])) ? " - {$this->civ13->ages[$ckey]})" : ')')); @@ -741,7 +741,7 @@ public function verify(string $ckey, string $discord_id, bool $provisional = fal if (! $m = $this->getVerifiedMember($member)) { break; } - $m->addRole($this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); + $this->civ13->addDiscordRole($m, $this->civ13->role_ids['Verified'], "approveme verified ($ckey)"); break; case 404: $error = 'The website could not be found or is misconfigured. Please try again later.'.PHP_EOL."If this error persists, contact <@{$this->civ13->technician_id}>."; @@ -826,7 +826,7 @@ public function unverify(string $id): array // ['success' => bool, 'message' => } foreach ($members as $member) { if (isset($this->civ13->role_ids['Verified']) && $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->setRoles([], "unverified ($id)"); + $this->civ13->replaceDiscordRoles($member, [], "unverified ($id)"); } } if ($channel = isset($this->civ13->channel_ids['staff_bot']) ? $this->civ13->discord->getChannel($this->civ13->channel_ids['staff_bot']) : null) { @@ -1002,14 +1002,14 @@ public function provisionalRegistration(string $ckey, string $discord_id): bool } } if ($member && ! $member->roles->has($this->civ13->role_ids['Verified'])) { - $member->setRoles([$this->civ13->role_ids['Verified']], "Provisional verification `$ckey`"); + $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['Verified']], "Provisional verification `$ckey`"); } return true; } if ($member = $this->civ13->discord->guilds->get('id', $this->civ13->civ13_guild_id)->members->get('id', $discord_id)) { if ($member->roles->has($this->civ13->role_ids['Verified'])) { - $member->setRoles([], 'Provisional verification failed'); + $this->civ13->replaceDiscordRoles($member, [], 'Provisional verification failed'); } } $this->provisional->pull($ckey); diff --git a/src/Civ14/Verifier.php b/src/Civ14/Verifier.php index 85d56d7b..ff78d98c 100644 --- a/src/Civ14/Verifier.php +++ b/src/Civ14/Verifier.php @@ -207,7 +207,7 @@ public function joinRoles(Member $member): ?PromiseInterface return ($this->endpoint->getIndex($member->id) === false) ? null - : $member->setroles([$this->civ13->role_ids['SS14 Verified']], 'SS14 verified join'); + : $this->civ13->replaceDiscordRoles($member, [$this->civ13->role_ids['SS14 Verified']], 'SS14 verified join'); } /** The verified record matching `$key` => `$value` (e.g. `discord`, `ss14`), or null. */ diff --git a/tests/windows/hosted-verifier.php b/tests/windows/hosted-verifier.php index b4ebab84..c9d87d5e 100644 --- a/tests/windows/hosted-verifier.php +++ b/tests/windows/hosted-verifier.php @@ -25,6 +25,35 @@ final class HostedDiscord extends Discord\Discord { public function __construct(private React\EventLoop\LoopInterface $testLoop) {} public function getLoop(): React\EventLoop\LoopInterface { return $this->testLoop; } } +final class HostedRoleMember extends Discord\Parts\User\Member { + public array $writes = []; + public function addRole($role, ?string $reason = null): React\Promise\PromiseInterface { + $this->writes[] = ['add', $role, $reason]; + return React\Promise\resolve($this); + } + public function removeRole($role, ?string $reason = null): React\Promise\PromiseInterface { + $this->writes[] = ['remove', $role, $reason]; + return React\Promise\resolve($this); + } + public function setRoles($roles, ?string $reason = null): React\Promise\PromiseInterface { + $this->writes[] = ['replace', $roles, $reason]; + return React\Promise\resolve($this); + } +} +function exerciseRoleWrites(Civ13\Civ13 $bot, HostedRoleMember $member): void { + foreach ([ + $bot->addDiscordRole($member, '123', 'role test'), + $bot->removeDiscordRole($member, '123', 'role test'), + $bot->replaceDiscordRoles($member, ['456'], 'role test'), + ] as $promise) { + $called = false; + $promise->then(static function ($value) use (&$called, $member): void { + check($value === $member); + $called = true; + }); + check($called); + } +} $bot=(new ReflectionClass(Civ13\Civ13::class))->newInstanceWithoutConstructor(); $bot->loop=new React\EventLoop\StreamSelectLoop(); $bot->discord=new HostedDiscord($bot->loop); $bot->logger=new Monolog\Logger('offline-hosted',[new Monolog\Handler\NullHandler()]); @@ -80,5 +109,37 @@ public function getLoop(): React\EventLoop\LoopInterface { return $this->testLoo $profile=['options'=>['role_ids'=>[],'channel_ids'=>[]],'gitdir'=>__DIR__,'state_dir'=>__DIR__,'verification_source'=>['url'=>Civ13\HostedVerificationSource::URL,'mode'=>'read-only']]; check(Civ13\PortableProfile::runtimeOptions($profile)['verification_read_only']); foreach(['http://localhost/verified','http://civ13.com:55555/verified?token=x','https://other.invalid/verified'] as $url) { $profile['verification_source']['url']=$url;rejects(fn()=>Civ13\PortableProfile::runtimeOptions($profile)); } -echo "PASS asynchronous hosted GET, real setup, legacy write/listener barriers, stale lookup/role-timer denial, concurrent coalescing and unrelated timer progress, bounded retry, schema/size/identity/removal/empty contracts\n"; +$roleMember = (new ReflectionClass(HostedRoleMember::class))->newInstanceWithoutConstructor(); +// A fresh or stale replica must never acquire role-write access. +$bot->verifier = $verifier; +check(!$bot->canWriteDiscordRoles()); +$bot->enabled_gameservers = [new class { + public function bancheck(string $ckey, bool $bypass, bool $useCache): bool { return $ckey === 'fixtureplayer'; } + public function permabancheck(string $ckey): bool { return $ckey === 'fixtureplayer'; } +}]; +check($bot->bancheck('fixtureplayer') && !$bot->bancheck('otherplayer')); +check($bot->permabancheck('fixtureplayer') && !$bot->permabancheck('otherplayer')); +exerciseRoleWrites($bot, $roleMember); +check($roleMember->writes === []); +$standalone = (new ReflectionClass(Civ13\Civ13::class))->newInstanceWithoutConstructor(); +$standalone->portable_state_dir = __DIR__; +check(!$standalone->canWriteDiscordRoles()); +exerciseRoleWrites($standalone, $roleMember); +$standalone->addRoles($roleMember, '123'); +$standalone->removeRoles($roleMember, '123'); +$standalone->setRoles($roleMember, '123', '456'); +check($roleMember->writes === []); +$standalone->portable_state_dir = ''; +$standalone->options['verification_read_only'] = true; +exerciseRoleWrites($standalone, $roleMember); +check($roleMember->writes === []); +$standalone->options['verification_read_only'] = false; +check($standalone->canWriteDiscordRoles()); +exerciseRoleWrites($standalone, $roleMember); +check($roleMember->writes === [ + ['add', '123', 'role test'], + ['remove', '123', 'role test'], + ['replace', ['456'], 'role test'], +]); +echo "PASS asynchronous hosted GET, real setup, legacy write/listener and Discord role authority barriers, stale lookup/role-timer denial, concurrent coalescing and unrelated timer progress, bounded retry, schema/size/identity/removal/empty contracts\n"; require __DIR__.'/../local/verification-authority.php';