add a single_log_out parametters on Ticket for query optimisation

This commit is contained in:
Valentin Samir 2015-05-28 14:02:17 +02:00
parent c9629f65ae
commit 7e2917e977
3 changed files with 49 additions and 14 deletions

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('cas_server', '0014_auto_20150528_0012'),
]
operations = [
migrations.AddField(
model_name='proxygrantingticket',
name='single_log_out',
field=models.BooleanField(default=False),
preserve_default=True,
),
migrations.AddField(
model_name='proxyticket',
name='single_log_out',
field=models.BooleanField(default=False),
preserve_default=True,
),
migrations.AddField(
model_name='serviceticket',
name='single_log_out',
field=models.BooleanField(default=False),
preserve_default=True,
),
]

View file

@ -17,10 +17,10 @@ from django.db import models
from django.contrib import messages from django.contrib import messages
from picklefield.fields import PickledObjectField from picklefield.fields import PickledObjectField
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
import re import re
import os import os
import time
import random import random
import string import string
@ -69,13 +69,13 @@ class User(models.Model):
"""Sending SLO request to all services the user logged in""" """Sending SLO request to all services the user logged in"""
async_list = [] async_list = []
session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10)) session = FuturesSession(executor=ThreadPoolExecutor(max_workers=10))
for ticket in ServiceTicket.objects.filter(user=self, validate=True): ticket_classes = [ServiceTicket, ProxyTicket, ProxyGrantingTicket]
async_list.append(ticket.logout(request, session)) for ticket_class in ticket_classes:
ticket.delete() for ticket in ticket_class.objects.filter(
for ticket in ProxyTicket.objects.filter(user=self, validate=True): user=self,
async_list.append(ticket.logout(request, session)) validate=True,
ticket.delete() single_log_out=True
for ticket in ProxyGrantingTicket.objects.filter(user=self, validate=True): ):
async_list.append(ticket.logout(request, session)) async_list.append(ticket.logout(request, session))
ticket.delete() ticket.delete()
for future in async_list: for future in async_list:
@ -112,7 +112,8 @@ class User(models.Model):
attributs=service_attributs, attributs=service_attributs,
service=service, service=service,
renew=renew, renew=renew,
service_pattern=service_pattern service_pattern=service_pattern,
single_log_out=service_pattern.single_log_out
) )
ticket.save() ticket.save()
return ticket return ticket
@ -306,13 +307,14 @@ class Ticket(models.Model):
service_pattern = models.ForeignKey(ServicePattern, related_name="%(class)s") service_pattern = models.ForeignKey(ServicePattern, related_name="%(class)s")
creation = models.DateTimeField(auto_now_add=True) creation = models.DateTimeField(auto_now_add=True)
renew = models.BooleanField(default=False) renew = models.BooleanField(default=False)
single_log_out = models.BooleanField(default=False)
def __unicode__(self): def __unicode__(self):
return u"Ticket(%s, %s)" % (self.user, self.service) return u"Ticket(%s, %s)" % (self.user, self.service)
def logout(self, request, session): def logout(self, request, session):
"""Send a SLO request to the ticket service""" """Send a SLO request to the ticket service"""
if self.validate and self.service_pattern.single_log_out: if self.validate and self.single_log_out:
xml = """<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xml = """<samlp:LogoutRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
ID="%(id)s" Version="2.0" IssueInstant="%(datetime)s"> ID="%(id)s" Version="2.0" IssueInstant="%(datetime)s">
<saml:NameID xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"></saml:NameID> <saml:NameID xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"></saml:NameID>
@ -320,7 +322,7 @@ class Ticket(models.Model):
</samlp:LogoutRequest>""" % \ </samlp:LogoutRequest>""" % \
{ {
'id' : os.urandom(20).encode("hex"), 'id' : os.urandom(20).encode("hex"),
'datetime' : int(time.time()), 'datetime' : timezone.now().isoformat(),
'ticket': self.value 'ticket': self.value
} }
headers = {'Content-Type': 'text/xml'} headers = {'Content-Type': 'text/xml'}

View file

@ -288,7 +288,8 @@ def ps_validate(request, ticket_type=None):
pticket = models.ProxyGrantingTicket.objects.create( pticket = models.ProxyGrantingTicket.objects.create(
user=ticket.user, user=ticket.user,
service=pgt_url, service=pgt_url,
service_pattern=pattern service_pattern=pattern,
single_log_out=pattern.single_log_out
) )
url = utils.update_url(pgt_url, {'pgtIou':proxyid, 'pgtId':pticket.value}) url = utils.update_url(pgt_url, {'pgtIou':proxyid, 'pgtId':pticket.value})
try: try: