-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Add IP address banning and unbanning functionality #19213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -38,9 +38,24 @@ <h3 class="card-title"><code>IpAddress</code> Record</h3> | |||||
| <dd class="col-sm-8">{{ ip_address.ban_date }}</dd> | ||||||
|
|
||||||
| <dt class="col-sm-4">Ban Reason:</dt> | ||||||
| <dd class="col-sm-8">{{ ip_address.ban_reason.value }}</dd> | ||||||
| <dd class="col-sm-8">{{ ip_address.ban_reason.value if ip_address.ban_reason is not none else 'None' }}</dd> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was reading more about Jinja2 filters recently, and I think this could also be expressed as:
Suggested change
but that means that the Maybe we should instead change the BanReason from an Enum to a StrEnum, like we do in other places, and thus don't need to refer to it by .value? A larger refactor, but probably worthwhile to consider as a follow-up. |
||||||
| </dl> | ||||||
| </div> <!-- /.card-body --> | ||||||
| <div class="card-footer"> | ||||||
| {% if request.has_permission(Permissions.AdminIpAddressesWrite) %} | ||||||
| {% if not ip_address.is_banned %} | ||||||
| <form method="POST" action="{{ request.route_path('admin.ip_address.ban', ip_address=ip_address.ip_address) }}"> | ||||||
| <input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}"> | ||||||
| <button type="submit" class="btn btn-danger">Ban this IP address</button> | ||||||
| </form> | ||||||
| {% else %} | ||||||
| <form method="POST" action="{{ request.route_path('admin.ip_address.unban', ip_address=ip_address.ip_address) }}"> | ||||||
| <input name="csrf_token" type="hidden" value="{{ request.session.get_csrf_token() }}"> | ||||||
| <button type="submit" class="btn btn-warning">Unban this IP address</button> | ||||||
| </form> | ||||||
| {% endif %} | ||||||
| {% endif %} | ||||||
| </div> <!-- /.card-footer --> | ||||||
| </div> <!-- /.card --> | ||||||
|
|
||||||
| <div class="card"> | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """ | ||
| Add ADMINISTRATIVE to BanReason enum | ||
|
|
||
| Revision ID: 31ac9b5e1e8b | ||
| Revises: a6cae8e65f1a | ||
| Create Date: 2025-12-22 18:19:43.751813 | ||
| """ | ||
|
|
||
|
|
||
| from alembic import op | ||
| from alembic_postgresql_enum import TableReference | ||
|
|
||
| revision = "31ac9b5e1e8b" | ||
| down_revision = "a6cae8e65f1a" | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.sync_enum_values( | ||
| enum_schema="public", | ||
| enum_name="banreason", | ||
| new_values=["authentication-attempts", "administrative"], | ||
| affected_columns=[ | ||
| TableReference( | ||
| table_schema="public", | ||
| table_name="ip_addresses", | ||
| column_name="ban_reason", | ||
| ) | ||
| ], | ||
| enum_values_to_rename=[], | ||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.sync_enum_values( | ||
| enum_schema="public", | ||
| enum_name="banreason", | ||
| new_values=["authentication-attempts"], | ||
| affected_columns=[ | ||
| TableReference( | ||
| table_schema="public", | ||
| table_name="ip_addresses", | ||
| column_name="ban_reason", | ||
| ) | ||
| ], | ||
| enum_values_to_rename=[], | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: worth adding a test case and handling for unbanning an already-unbanned entry, or are we fine with doing the unban operation regardless of current state?