Feat: ban users - #74
Draft
khanzadimahdi wants to merge 1 commit into
Draft
Conversation
An administrator can suspend an account from the dashboard, and the ban takes effect at once — including for access tokens handed out before it. - user.BannedAt records when the ban started; IsBanned/SetBanned derive from it, and re-saving an already banned user does not move the date. Stored as a nullable timestamp so lifting a ban writes an explicit null - The Authenticate middleware turns away every request from a banned user with 403 and a body carrying code "user_banned", so a client can tell a ban apart from an ordinary permission denial. It translates the message itself: it runs before the Localize middleware, so it falls back through the requested language, the user's own, then the default - Login refuses after the password check, so a wrong password cannot be used to probe which accounts are banned. Refresh refuses too, since the refresh token outlives the access token by days - The dashboard's user endpoints read and write the flag; the moment of the ban is the server's to decide and is never taken from the client Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
An administrator can suspend an account from the dashboard, and the ban takes effect at once — including for access tokens that were handed out before it.
The model
user.BannedAtrecords when the ban started.IsBanned()andSetBanned(bool)derive from it, and re-saving an already banned user does not move the date. In Mongo it is a nullable timestamp rather than a zero time, so lifting a ban writes an explicitnullinstead of leaving the old date behind.Where a ban is enforced
Three places, because each one is a different way back in:
Authenticatemiddleware turns away every authenticated request. This is the one that makes a ban immediate — an access token issued a minute before the ban still fails.All three answer 403 with the same body shape:
{"code": "user_banned", "message": "…"}403rather than401: the credentials are fine, the account is not, and retrying with a fresh token would change nothing. Thecodefield is what lets a client tell a ban apart from an ordinary permission denial — both are 403s, but a permission denial is a dead end for one action while a ban ends the session.Notes for review
Localizemiddleware. It falls back through the explicitly requested language → the user's ownLanguageCode→ the translator's default. This is whyNewAuthenticateMiddlewarenow takes atranslator— that's the one-argument change across all 53 call sites inblog.go, and it's most of this diff's line count.getUserreturnsbannedwithoutomitempty: "not banned" is a meaningfulfalse, not an absent value.banned_atrides along so the dashboard can show since when.updateUsertakesbannedas intent only; the timestamp is the server's to decide and is never read from the client.user.User→ domain conversions in the Mongo repository were near-identical copies; they're now onetoDomainhelper. That's whyrepository.goshows more deletions than the feature needs.Relationship to the other PRs
Split out of one working branch alongside contact-us and notes. All three are cut from
mainand can be merged in any order, but each touches the translation files andinfrastructure/ioc/providers/blog.go. This one'sblog.gochange is thetranslatorargument on everyNewAuthenticateMiddlewarecall — when it conflicts with a sibling PR's new route, the resolution is to keep the new route and addtranslatorto it. Re-rungo generateafter merging.Verification
go build ./...,go vet ./...andgo test ./...all pass on this branch alone.🤖 Generated with Claude Code