mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-13 22:32:15 +00:00
Move blacklist checking out of the Application model
This commit is contained in:
44
hr/models.py
44
hr/models.py
@@ -33,49 +33,7 @@ class Application(models.Model):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def blacklist_values(self):
|
def blacklist_values(self):
|
||||||
"""
|
return blacklist_values(self.user)
|
||||||
Returns a list of blacklist values that apply to the application
|
|
||||||
"""
|
|
||||||
|
|
||||||
blacklist = []
|
|
||||||
bl_items = Blacklist.objects.filter(models.Q(expiry_date__gt=datetime.now()) | models.Q(expiry_date=None))
|
|
||||||
|
|
||||||
# Check Reddit blacklists
|
|
||||||
if installed('reddit'):
|
|
||||||
reddit_uids = self.user.redditaccount_set.all().values_list('username', flat=True)
|
|
||||||
objs = bl_items.filter(type=BLACKLIST_TYPE_REDDIT, value__in=reddit_uids)
|
|
||||||
blacklist.extend(objs)
|
|
||||||
|
|
||||||
# Check email blacklists
|
|
||||||
blacklist.extend(bl_items.filter(type=BLACKLIST_TYPE_EMAIL, value=self.user.email.lower()))
|
|
||||||
|
|
||||||
# Check Auth blacklists
|
|
||||||
blacklist.extend(bl_items.filter(type=BLACKLIST_TYPE_AUTH, value=self.user.username.lower()))
|
|
||||||
|
|
||||||
# Check EVE Related blacklists
|
|
||||||
evechars = EVEPlayerCharacter.objects.filter(eveaccount__user=self.user).select_related('corporation__alliance')
|
|
||||||
|
|
||||||
# Check Character blacklists
|
|
||||||
characters = evechars.values_list('name', flat=True)
|
|
||||||
objs = bl_items.filter(type=BLACKLIST_TYPE_CHARACTER, value__in=characters)
|
|
||||||
blacklist.extend(objs)
|
|
||||||
|
|
||||||
# Check Corporation blacklists
|
|
||||||
corporations = evechars.values_list('corporation__name', flat=True)
|
|
||||||
objs = bl_items.filter(type=BLACKLIST_TYPE_CORPORATION, value__in=corporations)
|
|
||||||
blacklist.extend(objs)
|
|
||||||
|
|
||||||
# Check Alliance blacklists
|
|
||||||
alliances = evechars.values_list('corporation__alliance__name', flat=True)
|
|
||||||
objs = bl_items.filter(type=BLACKLIST_TYPE_ALLIANCE, value__in=alliances)
|
|
||||||
blacklist.extend(objs)
|
|
||||||
|
|
||||||
# Check API Key blacklists
|
|
||||||
keys = self.user.eveaccount_set.all().values_list('api_user_id', flat=True)
|
|
||||||
objs = bl_items.filter(type=BLACKLIST_TYPE_APIUSERID, value__in=keys)
|
|
||||||
blacklist.extend(objs)
|
|
||||||
|
|
||||||
return blacklist
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_action(self):
|
def last_action(self):
|
||||||
|
|||||||
20
hr/tasks.py
Normal file
20
hr/tasks.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from django.conf import settings
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from celery.decorators import task
|
||||||
|
from hr.utils import blacklist_values
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
@task(ignore_result=True)
|
||||||
|
def blacklist_check():
|
||||||
|
log = blacklist_check.get_logger()
|
||||||
|
|
||||||
|
users = User.objects.filter(active=True)
|
||||||
|
|
||||||
|
for u in users:
|
||||||
|
if users.groups.count() > 0:
|
||||||
|
# Has groups
|
||||||
|
val = blacklist_values(user)
|
||||||
|
if len(val) > 0:
|
||||||
|
# Report possible issue
|
||||||
|
log.warn("Suspect User: %s, %s entries found" % (u.username, len(val)))
|
||||||
48
hr/utils.py
Normal file
48
hr/utils.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
from hr.app_defines import *
|
||||||
|
from hr.models import Blacklist
|
||||||
|
|
||||||
|
def blacklist_values(user):
|
||||||
|
"""
|
||||||
|
Returns a list of blacklist values that apply to the application
|
||||||
|
"""
|
||||||
|
|
||||||
|
blacklist = []
|
||||||
|
bl_items = Blacklist.objects.filter(models.Q(expiry_date__gt=datetime.now()) | models.Q(expiry_date=None))
|
||||||
|
|
||||||
|
# Check Reddit blacklists
|
||||||
|
if installed('reddit'):
|
||||||
|
reddit_uids = user.redditaccount_set.all().values_list('username', flat=True)
|
||||||
|
objs = bl_items.filter(type=BLACKLIST_TYPE_REDDIT, value__in=reddit_uids)
|
||||||
|
blacklist.extend(objs)
|
||||||
|
|
||||||
|
# Check email blacklists
|
||||||
|
blacklist.extend(bl_items.filter(type=BLACKLIST_TYPE_EMAIL, value=user.email.lower()))
|
||||||
|
|
||||||
|
# Check Auth blacklists
|
||||||
|
blacklist.extend(bl_items.filter(type=BLACKLIST_TYPE_AUTH, value=user.username.lower()))
|
||||||
|
|
||||||
|
# Check EVE Related blacklists
|
||||||
|
evechars = EVEPlayerCharacter.objects.filter(eveaccount__user=user).select_related('corporation__alliance')
|
||||||
|
|
||||||
|
# Check Character blacklists
|
||||||
|
characters = evechars.values_list('name', flat=True)
|
||||||
|
objs = bl_items.filter(type=BLACKLIST_TYPE_CHARACTER, value__in=characters)
|
||||||
|
blacklist.extend(objs)
|
||||||
|
|
||||||
|
# Check Corporation blacklists
|
||||||
|
corporations = evechars.values_list('corporation__name', flat=True)
|
||||||
|
objs = bl_items.filter(type=BLACKLIST_TYPE_CORPORATION, value__in=corporations)
|
||||||
|
blacklist.extend(objs)
|
||||||
|
|
||||||
|
# Check Alliance blacklists
|
||||||
|
alliances = evechars.values_list('corporation__alliance__name', flat=True)
|
||||||
|
objs = bl_items.filter(type=BLACKLIST_TYPE_ALLIANCE, value__in=alliances)
|
||||||
|
blacklist.extend(objs)
|
||||||
|
|
||||||
|
# Check API Key blacklists
|
||||||
|
keys = user.eveaccount_set.all().values_list('api_user_id', flat=True)
|
||||||
|
objs = bl_items.filter(type=BLACKLIST_TYPE_APIUSERID, value__in=keys)
|
||||||
|
blacklist.extend(objs)
|
||||||
|
|
||||||
|
return blacklist
|
||||||
|
|
||||||
Reference in New Issue
Block a user