Reputation: 432
I'm currently using a dajaxice call in a template, and I'd like to be able to retrieve the currently logged in user within the python function my dajax calls.
I know that the dajax passes a 'request' parameter to the python function which contains the session ID, but I've had no luck ascertaining the type of variable 'request' is. I've tried converting it to a string, decoding it as a JSON string, and multiple other methods.
All I'd like to be able to do is find the currently logged in user within the python function which dajax calls, without passing it in from the actual template, as that's unreliable security-wise.
A broken down version of my relevant code:
Dajax Call:
Dajaxice.fortura_test.rbac_test(rbac_callback,{});
rbac_test():
@dajaxice_register
def rbac_test(request):
import re;
from django.contrib.sessions.models import Session
from django.contrib.auth.models import User
request_string = str(request);
request_list = [];
for x in request_string:
request_list.append(x);
request_string = ''.join(request_list);
m = re.match(r"'sessionid'\: '([A-Za-z0-9]+)'",request_string);
if m is not None:
session_key = m.group(0);
session = Session.objects.get(session_key=session_key)
uid = session.get_decoded().get('_auth_user_id')
user = User.objects.get(pk=uid)
return user.id
else:
return 'No match';
The method above I'm currently using doesn't quite work, but I feel like there should be a better way than how I'm approaching this at the moment.
Any help would be appreciated.
Upvotes: 0
Views: 158
Reputation: 53998
If that is a normal Django view then request
is Django's HTTPRequest object and you can get the currently logged in user using request.user
which will return a contrib.auth.models.User
instance (you can make sure they are logged in by using request.user.is_autenticated()
)
Upvotes: 1