Add IP bans

Users can be banned by IP address now, either by singular IP or in an IP
range. If they are banned and attempt to post, they will be met with a
"you are banned until X date" screen.

There are a few loose threads with this, and IP bans may be obsolete if
I decide to go the accounts-required-for-posting route. But I think this
is a good start for 4chan style posting.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-06-20 15:26:35 -07:00
parent b4df8b9756
commit 28ccd7d73b
7 changed files with 190 additions and 30 deletions

View File

@@ -264,32 +264,50 @@ def report_created(sender, instance, created, **kwargs):
# board =
class RangeBan(models.Model):
# Starting IP address of the ban
start = models.GenericIPAddressField()
# Ending IP address of the ban
end = models.GenericIPAddressField()
# The reason for this ban
ban_reason = models.TextField(blank=False)
# The time that this ban was created.
created = models.DateTimeField(auto_now_add=True)
# Expiration date of this ban. If it is null, it is permanent.
expires = models.DateTimeField(null=True)
class Ban(models.Model):
# IP address of the ban
ip = models.GenericIPAddressField()
class BanCommon(models.Model):
# The reason for this ban
ban_reason = models.TextField(blank=False)
# Board that this ban is for. If null, then all boards.
# Even though this is nullable, we just want to delete all reports for a
# board if that board is deleted.
board = models.ForeignKey("Board", on_delete=models.CASCADE, null=True)
board = models.ForeignKey("Board", on_delete=models.CASCADE, null=True, blank=True)
# The time that this ban was created.
created = models.DateTimeField(auto_now_add=True)
# Expiration date of this ban. If it is null, it is permanent.
expires = models.DateTimeField(null=True)
expires = models.DateTimeField(null=True, blank=True)
class Meta:
abstract = True
class RangeBan(BanCommon):
# Starting IP address of the ban
start = models.GenericIPAddressField()
# Ending IP address of the ban
end = models.GenericIPAddressField()
def clean(self):
import ipaddress
start = ipaddress.ip_address(self.start)
end = ipaddress.ip_address(self.end)
if start.version != end.version:
raise ValidationError(
_("Start and end IP address must be the same protocol (IPv4 or IPv6)")
)
if start >= end:
raise ValidationError(_("Start IP must be lower than end IP"))
def __str__(self):
return f"Range ban for {self.start}-{self.end}"
class Ban(BanCommon):
# IP address of the ban
ip = models.GenericIPAddressField(unique=True)
def __str__(self):
return f"Ban for {self.ip}"
class BanTemplate(models.Model):