feat: middleware guards — permission-based handler access control#35
Merged
Conversation
Add Guard interface and three guard middleware classes under src/Bot/Middleware/Guards/: - RequiresRole: checks user's Mattermost roles (e.g. system_admin) - RequiresPermission: maps permissions to roles (e.g. manage_team) - ChannelMember: verifies user is a member of the post's channel All guards follow the existing cache-backed pattern from AdminOnly with configurable TTL via mattermost.bot.guard_cache_ttl. Full Pest BDD test coverage included.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds permission-based middleware guards under
src/Bot/Middleware/Guards/:Guardinterface — contract withauthorize(userId, channelId): boolfor custom guardsRequiresRole— short-circuits if user lacks a specific Mattermost role (e.g.system_admin)RequiresPermission— maps permission names to roles (e.g.manage_team→team_admin|system_admin)ChannelMember— verifies user is a member of the post's channel viaGET /channels/{id}/members/{userId}All guards follow the existing cache-backed pattern from
AdminOnlywith configurable TTL viamattermost.bot.guard_cache_ttl(default 300s).Closes #13
Test plan
RequiresRole— 7 tests: allows/blocks by role, caches lookups, handles API failures, passes non-PostCreated events, drops empty userId, implements Guard interfaceRequiresPermission— 8 tests: allows/blocks by permission→role mapping, denies unknown permissions, caches, handles API failures, passes non-PostCreated, implements GuardChannelMember— 9 tests: allows members, blocks non-members, caches per user+channel, checks different channels separately, handles API failures, passes non-PostCreated, drops empty userId/channelId, implements Guardpint --test— passedpest— 292 passed, 0 failedphpstan analyse(level 8) — 0 errorsrector --dry-run— no changesDemo: pending — orchestrator will post