models: add SessionIndex model (#41949)

This commit is contained in:
Benjamin Dauvergne 2020-04-24 12:49:10 +02:00
parent be52f6c2ec
commit 7b5ad08ad8
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,28 @@
# Generated by Django 2.2.12 on 2020-04-24 05:14
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('mellon', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='SessionIndex',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('session_index', models.TextField(verbose_name='SAML SessionIndex')),
('session_key', models.CharField(max_length=40, verbose_name='Django session key')),
('saml_identifier', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mellon.UserSAMLIdentifier', verbose_name='SAML identifier')),
],
options={
'verbose_name': 'SAML SessionIndex',
'verbose_name_plural': 'SAML SessionIndexes',
'unique_together': {('saml_identifier', 'session_index', 'session_key')},
},
),
]

View File

@ -15,6 +15,8 @@
from __future__ import unicode_literals
from importlib import import_module
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
@ -38,3 +40,30 @@ class UserSAMLIdentifier(models.Model):
verbose_name = _('user SAML identifier')
verbose_name_plural = _('users SAML identifiers')
unique_together = (('issuer', 'name_id'),)
class SessionIndex(models.Model):
session_index = models.TextField(_('SAML SessionIndex'))
session_key = models.CharField(_('Django session key'), max_length=40)
saml_identifier = models.ForeignKey(
verbose_name=_('SAML identifier'),
to=UserSAMLIdentifier,
on_delete=models.CASCADE)
@staticmethod
def cleanup(cls):
session_engine = import_module(settings.SESSION_ENGINE)
store = session_engine.SessionStore()
ids = []
for si in cls.objects.all():
if not store.exists(si.session_key):
ids.append(si.id)
cls.objects.filter(id__in=ids).delete()
class Meta:
verbose_name = _('SAML SessionIndex')
verbose_name_plural = _('SAML SessionIndexes')
unique_together = (
('saml_identifier', 'session_index', 'session_key'),
)