Selaa lähdekoodia

HUE-87. Incorporating review feedback, adding access to 'last access time'

vinithra 15 vuotta sitten
vanhempi
commit
1dfb6870fb
3 muutettua tiedostoa jossa 21 lisäystä ja 16 poistoa
  1. 2 2
      README.testing
  2. 13 11
      desktop/core/src/desktop/auth/views.py
  3. 6 3
      desktop/core/src/desktop/log/access.py

+ 2 - 2
README.testing

@@ -52,13 +52,13 @@ E.g.
   build/env/bin/hue test specific useradmin.tests:test_user_admin
 
 Start up pdb on test failures:
-  build/env/bin/hue test <args> --pdb --pdb-failure
+  build/env/bin/hue test <args> --pdb --pdb-failure -s
 
 
 Special environment variables
 =============================
 
-DESKTOP_LOG_LEVEL=<level>
+DESKTOP_LOGLEVEL=<level>
   level can be DEBUG, INFO, WARN, ERROR, or CRITICAL
 
   When specified, the console logger is set to the given log level. A console

+ 13 - 11
desktop/core/src/desktop/auth/views.py

@@ -28,8 +28,7 @@ from django.http import HttpResponseRedirect
 from desktop.auth.backend import AllowFirstUserDjangoBackend
 from desktop.lib.django_util import render_json, render
 from desktop.lib.django_util import login_notrequired
-from desktop.log.access import access_warn, recent_access_map, remote_ip_map
-from userman.models import UserProfile
+from desktop.log.access import access_warn, remote_ip_map, recent_access_time_map
 
 LOG = logging.getLogger(__name__)
 
@@ -63,16 +62,19 @@ def login_form(request):
 
 
 def get_current_users():
-  """Return current users by inspecting sessions"""
-  _current_users = { }
+  """Return dictionary of User objects and
+  a dictionary of the user's IP address and last access time"""
+  current_users = { }
   for session in Session.objects.all():
-    uid = session.get_decoded().get('_auth_user_id')
-    if uid != None:
-      userobj = User.objects.get(pk=uid)
-      profile = UserProfile.objects.get(user=userobj)
-      _current_users[userobj] = remote_ip_map.get(userobj.username, { })
-
-  return _current_users
+    uid = session.get_decoded().get(django.contrib.auth.SESSION_KEY)
+    if uid is not None:
+      try:
+        userobj = User.objects.get(pk=uid)
+      except User.DoesNotExist:
+        LOG.debug("User with id=%d does not exist" % uid)
+      current_users[userobj] = {'ip':remote_ip_map.get(userobj.username, ''), 'time':recent_access_time_map.get(userobj.username, '')}
+
+  return current_users
 
 
 @login_notrequired

+ 6 - 3
desktop/core/src/desktop/log/access.py

@@ -52,9 +52,12 @@ recent_access_map = { }
 _recent_access_map_lk = threading.Lock()
 _per_user_lk = { }      # Indexed by username
 
-# Store the IP address of users
+# Store a map of usernames and their IP addresses
 remote_ip_map = { }
 
+# Store a map of usernames and last access times
+recent_access_time_map = { }
+
 # Max number of records per user per view to keep
 _USER_ACCESS_HISTORY_SIZE = desktop.conf.USER_ACCESS_HISTORY_SIZE.get()
 
@@ -100,8 +103,9 @@ class AccessInfo(dict):
         app_dict = { }
         _per_user_lk[user] = threading.Lock()
         recent_access_map[user] = app_dict
-        # Update the IP address of the user
+        # Update the IP address and last access time of the user
         remote_ip_map[user] = self['remote_ip']
+        recent_access_time_map[user] = self['time']
       finally:
         _recent_access_map_lk.release()
 
@@ -138,7 +142,6 @@ def log_page_hit(request, view_func, level=None):
     level = logging.INFO
   ai = AccessInfo(request)
   ai.log(level)
-  ai['remote_ip'] = request.META.get('REMOTE_ADDR', '-')
   # Find the app
   app_re_match = _MODULE_RE.match(view_func.__module__)
   app = app_re_match and app_re_match.group(0) or '-'