Periodic checks of character ownership hashes.

Fix migration naming from merge.
This commit is contained in:
Adarnof 2017-09-17 02:31:46 -04:00
parent 02f2968ee5
commit 5ce5f37867
8 changed files with 67 additions and 26 deletions

View File

@ -87,6 +87,10 @@ CELERYBEAT_SCHEDULE = {
'task': 'eveonline.tasks.run_model_update', 'task': 'eveonline.tasks.run_model_update',
'schedule': crontab(minute=0, hour="*/6"), 'schedule': crontab(minute=0, hour="*/6"),
}, },
'check_all_character_ownership': {
'task': 'authentication.tasks.check_all_character_ownership',
'schedule': crontab(hour='*/4'),
}
} }
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)

36
authentication/tasks.py Normal file
View File

@ -0,0 +1,36 @@
from alliance_auth.celeryapp import app
from esi.models import Token
from esi.errors import TokenExpiredError, TokenInvalidError
from authentication.models import CharacterOwnership
import logging
logger = logging.getLogger(__name__)
@app.task
def check_character_ownership(owner_hash):
tokens = Token.objects.filter(character_owner_hash=owner_hash)
if tokens:
for t in tokens:
old_hash = t.character_owner_hash
try:
t.update_token_data(commit=False)
except (TokenExpiredError, TokenInvalidError):
t.delete()
continue
if t.character_owner_hash == old_hash:
break
else:
logger.info('Character %s has changed ownership. Revoking %s tokens.' % (t.character_name, tokens.count()))
tokens.delete()
else:
logger.info('No tokens found with owner hash %s. Revoking ownership.' % owner_hash)
CharacterOwnership.objects.filter(owner_hash=owner_hash).delete()
@app.task
def check_all_character_ownership():
for c in CharacterOwnership.objects.all().only('owner_hash'):
check_character_ownership.delay(c.owner_hash)

View File

@ -12,6 +12,7 @@ from django.contrib.auth.models import User
from alliance_auth.tests.auth_utils import AuthUtils from alliance_auth.tests.auth_utils import AuthUtils
from authentication.models import CharacterOwnership, UserProfile, State, get_guest_state from authentication.models import CharacterOwnership, UserProfile, State, get_guest_state
from authentication.backends import StateBackend from authentication.backends import StateBackend
from authentication.tasks import check_character_ownership
from eveonline.models import EveCharacter, EveCorporationInfo, EveAllianceInfo from eveonline.models import EveCharacter, EveCorporationInfo, EveAllianceInfo
from esi.models import Token from esi.models import Token
@ -136,6 +137,28 @@ class CharacterOwnershipTestCase(TestCase):
self.user = User.objects.get(pk=self.user.pk) self.user = User.objects.get(pk=self.user.pk)
self.assertIsNone(self.user.profile.main_character) self.assertIsNone(self.user.profile.main_character)
@mock.patch('esi.models.Token.update_token_data')
def test_character_ownership_check(self, update_token_data):
t = Token.objects.create(
user=self.user,
character_id=self.character.character_id,
character_name=self.character.character_name,
character_owner_hash='1',
)
co = CharacterOwnership.objects.get(owner_hash='1')
check_character_ownership(co.owner_hash)
self.assertTrue(CharacterOwnership.objects.filter(owner_hash='1').exists())
t.character_owner_hash = '2'
t.save()
check_character_ownership(co.owner_hash)
self.assertFalse(CharacterOwnership.objects.filter(owner_hash='1').exists())
t.delete()
co = CharacterOwnership.objects.create(user=self.user, character=self.character, owner_hash='3')
check_character_ownership(co.owner_hash)
self.assertFalse(CharacterOwnership.objects.filter(owner_hash='3').exists())
class StateTestCase(TestCase): class StateTestCase(TestCase):
@classmethod @classmethod

View File

@ -8,7 +8,7 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('hrapplications', '0001_initial'), ('hrapplications', '0002_choices_for_questions'),
] ]
operations = [ operations = [

View File

@ -10,7 +10,7 @@ from sortedm2m.operations import AlterSortedManyToManyField
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('hrapplications', '0002_make_strings_more_stringy'), ('hrapplications', '0003_make_strings_more_stringy'),
] ]
operations = [ operations = [

View File

@ -18,7 +18,7 @@ def delete_permissions(apps, schema_editor):
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('hrapplications', '0003_sorted_questions'), ('hrapplications', '0004_sorted_questions'),
] ]
operations = [ operations = [

View File

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-22 23:35
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('services', '0002_auto_20161016_0135'),
]
operations = [
migrations.AlterField(
model_name='groupcache',
name='service',
field=models.CharField(choices=[('discourse', 'discourse'), ('discord', 'discord')], max_length=254, unique=True),
),
]

View File

@ -6,7 +6,6 @@ from alliance_auth.celeryapp import app
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from notifications import notify from notifications import notify
from services.modules.discord.manager import DiscordOAuthManager, DiscordApiBackoff from services.modules.discord.manager import DiscordOAuthManager, DiscordApiBackoff
from .models import DiscordUser from .models import DiscordUser
@ -59,7 +58,6 @@ class DiscordTasks:
@staticmethod @staticmethod
@app.task(bind=True, name='discord.update_groups') @app.task(bind=True, name='discord.update_groups')
@only_one
def update_groups(task_self, pk): def update_groups(task_self, pk):
user = User.objects.get(pk=pk) user = User.objects.get(pk=pk)
logger.debug("Updating discord groups for user %s" % user) logger.debug("Updating discord groups for user %s" % user)