2012-06-27 23:28:35 +02:00
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.conf import settings
|
|
|
|
from django_cas.backends import CASBackend
|
2013-09-05 22:20:52 +02:00
|
|
|
from django.db import models, connection
|
2012-07-11 17:39:20 +02:00
|
|
|
|
|
|
|
from gestioncof.models import CofProfile
|
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class COFCASBackend(CASBackend):
|
|
|
|
def authenticate(self, ticket, service):
|
|
|
|
"""Authenticates CAS ticket and retrieves user data"""
|
|
|
|
user = super(COFCASBackend, self).authenticate(ticket, service)
|
2012-07-11 17:39:20 +02:00
|
|
|
try:
|
|
|
|
profile = user.get_profile()
|
|
|
|
except CofProfile.DoesNotExist:
|
|
|
|
profile, created = CofProfile.objects.get_or_create(user = user)
|
|
|
|
profile.save()
|
2012-06-27 23:28:35 +02:00
|
|
|
if not profile.login_clipper:
|
|
|
|
profile.login_clipper = user.username
|
|
|
|
profile.save()
|
|
|
|
if not user.email:
|
|
|
|
user.email = settings.CAS_EMAIL_FORMAT % profile.login_clipper
|
|
|
|
user.save()
|
2012-07-11 17:39:20 +02:00
|
|
|
if profile.is_buro and not user.is_superuser:
|
|
|
|
user.is_superuser = True
|
|
|
|
user.is_staff = True
|
|
|
|
user.save()
|
2012-06-27 23:28:35 +02:00
|
|
|
return user
|
|
|
|
|
|
|
|
def context_processor (request):
|
|
|
|
'''Append extra data to the context of the given request'''
|
|
|
|
data = {
|
|
|
|
"user": request.user,
|
|
|
|
"site": Site.objects.get_current(),
|
|
|
|
}
|
|
|
|
return data
|
2013-09-05 22:20:52 +02:00
|
|
|
|
|
|
|
def lock_table(model):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
table = model._meta.db_table
|
|
|
|
cursor.execute("LOCK TABLES %s WRITE" % table)
|
|
|
|
row = cursor.fetchone()
|
|
|
|
return row
|
|
|
|
|
|
|
|
def unlock_table(model):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
cursor.execute("UNLOCK TABLES")
|
|
|
|
row = cursor.fetchone()
|
|
|
|
return row
|