stijnhau / moderation-bundle
User moderation for Symfony apps: temporary or permanent bans and mutes with reasons, admin role changes, an audit log, and ban enforcement at login and mid-session.
Package info
github.com/stijnhau/moderation-bundle
Type:symfony-bundle
pkg:composer/stijnhau/moderation-bundle
Requires
- php: >=8.4
- doctrine/doctrine-bundle: ^3.4
- doctrine/orm: ^3.8
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- symfony/clock: ^8.1
- symfony/config: ^8.1
- symfony/dependency-injection: ^8.1
- symfony/framework-bundle: ^8.1
- symfony/http-kernel: ^8.1
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^13.4
- symfony/browser-kit: ^8.1
- symfony/event-dispatcher: ^8.1
- symfony/security-bundle: ^8.1
- symfony/translation: ^8.1
- symfony/yaml: ^8.1
Suggests
- symfony/security-bundle: For ban enforcement: the login user checker and ending banned users' open sessions
Provides
None
Conflicts
None
Replaces
None
README
User moderation for Symfony apps, shared by CityWars, Crime-Life and F1Manager: temporary or permanent bans and mutes with a reason, granting and revoking admin, one audit log of who did what, and ban enforcement at login and in open sessions.
| Piece | What it does |
|---|---|
Model\ModerationTrait + ModeratableUserInterface |
Ban/mute state on the app's user entity: banned at/until/reason, muted at/until/reason. A ban with no end is permanent. |
Service\Moderation |
ban(), unban(), mute(), unmute(), grantAdmin(), revokeAdmin(), each with its rules and saved together with its audit log entry. assertNotMuted() is for the app's forum, message and chat guards. |
Model\AbstractAuditLogEntry, Service\AuditLog, Query\AuditLogReader |
The audit log: action (your own names), actor (null for the system), subject user plus a frozen description, JSON details, and time. Moderation records its own actions; record anything else with AuditLog::record(). |
Security\BannedUserChecker |
Refuses banned users at login (as the firewall's user_checker, or called from yours). |
Security\ActiveBanListener |
Logs a banned user out on their next request, adds a flash, and redirects to login. BannedUserLoggedOutEvent lets you adjust the response. |
The admin screens are the app's own.
Install
composer require stijnhau/moderation-bundle
-
The user entity implements
ModeratableUserInterfaceanduse ModerationTrait;(you addgetModerationName(),isAdmin(),grantAdmin(),revokeAdmin()). -
Map an audit log entry:
#[ORM\Entity] class AuditLogEntry extends AbstractAuditLogEntry {}. -
Configure
config/packages/stijnhau_moderation.yaml:stijnhau_moderation: user_class: App\Entity\User require_reason: true protect_admins: true # admins can't ban or mute other admins audit_log: entry_class: App\Entity\AuditLogEntry # ~: no audit log per_page: 50 actions: # your audit log's names for these actions banned: user.banned unbanned: user.unbanned muted: user.muted unmuted: user.unmuted admin_granted: admin.granted admin_revoked: admin.revoked enforcement: enabled: true redirect_route: app_login priority: -10 # kernel.request, after the firewall message_permanent: stijnhau_moderation.banned_permanent # `security` domain, %reason% message_temporary: stijnhau_moderation.banned_until # also %until%
-
security.yaml:user_checker: Stijnhau\ModerationBundle\Security\BannedUserChecker, or call it from your own checker. -
bin/console make:migration.
Time comes from the clock service. Errors are ModerationExceptions, with keys
in the stijnhau_moderation domain (en/nl/fr included).
License
MIT, see LICENSE.