Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of the authenticated users?

I would like to display the list of the authenticated users.

On the documentation: http://docs.djangoproject.com/en/dev/topics/auth/

class models.User
is_authenticated()¶
Always returns True. This is a way to tell if the user has been authenticated. ...

You can know on the template side is the current User is authenticated or not:

{% if user.is_authenticated %} {% endif %}

But I didn't found the way the get the list of the authenticated users.

Any idea?

like image 421
GeReinhart Avatar asked Apr 27 '10 16:04

GeReinhart


People also ask

How do I check authenticated users?

See Authenticated UsersConnect to Fireware Web UI. Select System Status > Authentication List. A list of all users authenticated to the Firebox appears.

Is authenticated user Django?

Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project's needs.

Is active in Django?

If is_active is True (default), returns only active users, or if False , returns only inactive users. Use None to return all users irrespective of active state.


2 Answers

Going along with rz's answer, you could query the Session model for non-expired sessions, then turn the session data into users. Once you've got that you could turn it into a template tag which could render the list on any given page.

(This is all untested, but hopefully will be close to working).

Fetch all the logged in users...

from django.contrib.auth.models import User from django.contrib.sessions.models import Session from django.utils import timezone  def get_all_logged_in_users():     # Query all non-expired sessions     # use timezone.now() instead of datetime.now() in latest versions of Django     sessions = Session.objects.filter(expire_date__gte=timezone.now())     uid_list = []      # Build a list of user ids from that query     for session in sessions:         data = session.get_decoded()         uid_list.append(data.get('_auth_user_id', None))      # Query all logged in users based on id list     return User.objects.filter(id__in=uid_list) 

Using this, you can make a simple inclusion template tag...

from django import template from wherever import get_all_logged_in_users register = template.Library()  @register.inclusion_tag('templatetags/logged_in_user_list.html') def render_logged_in_user_list():     return { 'users': get_all_logged_in_users() } 

logged_in_user_list.html

{% if users %} <ul class="user-list">     {% for user in users %}     <li>{{ user }}</li>     {% endfor %} </ul> {% endif %} 

Then in your main page you can simply use it where you like...

{% load your_library_name %} {% render_logged_in_user_list %} 

EDIT

For those talking about the 2-week persistent issue, I'm assuming that anyone wanting to have an "active users" type of listing will be making use of the SESSION_EXPIRE_AT_BROWSER_CLOSE setting, though I recognize this isn't always the case.

like image 151
3 revs, 2 users 97% Avatar answered Oct 14 '22 04:10

3 revs, 2 users 97%


Most reliable solution would only be the something you store when user logs in or logs out. I saw this solution and i think its worth sharing.

models.py

from django.contrib.auth.signals import user_logged_in, user_logged_out  class LoggedUser(models.Model):     user = models.ForeignKey(User, primary_key=True)      def __unicode__(self):         return self.user.username      def login_user(sender, request, user, **kwargs):         LoggedUser(user=user).save()      def logout_user(sender, request, user, **kwargs):         try:             u = LoggedUser.objects.get(user=user)             u.delete()         except LoggedUser.DoesNotExist:             pass      user_logged_in.connect(login_user)     user_logged_out.connect(logout_user) 

views.py

logged_users = LoggedUser.objects.all().order_by('username') 
like image 42
A.J. Avatar answered Oct 14 '22 04:10

A.J.