import sys
import os
from mod_python import Cookie, apache
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
this=os.path.dirname(__file__)
project=os.path.join(this, "../PATH/TO/YOURAPP")
app=os.path.dirname(project)
sys.path.append(project)
sys.path.append(app)
from mxwnet import settings
from django.contrib.sessions.models import Session
def send_to_login(req):
# Maybe better use Django settings !?
req.headers_out['Location'] = "/accounts/login/?next=%s" % req.uri
return apache.HTTP_MOVED_PERMANENTLY
def fixuphandler(req):
try:
raw_cookie = req.headers_in["Cookie"]
except:
req.log_error("No cookie within this request\n")
return send_to_login(req)
cookies = Cookie.get_cookies(req)
if cookies.has_key('sessionid'):
sessionid = cookies['sessionid'].value
try:
s = Session.objects.get(pk=sessionid)
except:
return send_to_login(req)
try:
obj = s.get_decoded()
except:
return send_to_login(req)
if obj.has_key('_auth_user_id'):
return apache.OK
else:
return send_to_login(req)
else:
# If the cookie (if any) has no sessionid, return the user to the login box.
req.log_error("No sessionid within this cookie\n")
return send_to_login(req)
# This should never happen. Log it, in case
req.log_error("ERROR!!!!! We should not reach this point!!!")
Comments