Created
May 10, 2013 18:18
-
-
Save mitgr81/5556319 to your computer and use it in GitHub Desktop.
Drupal authorization in django-tastypie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Sessions(models.Model): | |
id = models.IntegerField(primary_key=True, db_column='uid') | |
session_id = models.CharField(max_length=64, db_column='sid') | |
class Meta: | |
db_table = u'sessions' | |
managed = False # Don't let Django create the tables for this model. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tastypie.authorization import Authorization | |
from drupal_models import Sessions | |
class HackyDrupalAuthorization(Authorization): | |
def __get_uid(self, bundle): | |
for name, cookie in bundle.request.COOKIES.items(): | |
if "SESS" not in name: | |
continue | |
sess = Sessions.objects.using('__name_of_your_drupal_database_in_settings__').filter(sid=cookie) | |
if sess: | |
uid = sess[0].uid | |
break | |
else: | |
uid = 0 | |
return uid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment