allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [2/3] git commit: [#7480] ticket:610 Track login info
Date Mon, 21 Jul 2014 14:58:09 GMT
[#7480] ticket:610 Track login info


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/2664ba72
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/2664ba72
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/2664ba72

Branch: refs/heads/master
Commit: 2664ba72792b6bc80fe26f033ad1e10476eefb79
Parents: e297862
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Thu Jul 17 13:08:53 2014 +0300
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Mon Jul 21 14:57:46 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py                 |  1 +
 Allura/allura/model/auth.py                 | 15 +++++++++++++++
 Allura/allura/tests/functional/test_auth.py | 14 ++++++++++++++
 3 files changed, 30 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/2664ba72/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index eb1fb43..a5c3f65 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -144,6 +144,7 @@ class AuthenticationProvider(object):
             self.session.save()
             g.zarkov_event('login', user=user)
             g.statsUpdater.addUserLogin(user)
+            user.track_login(self.request)
             return user
         except exc.HTTPUnauthorized:
             self.logout()

http://git-wip-us.apache.org/repos/asf/allura/blob/2664ba72/Allura/allura/model/auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index ed3c4d0..ff4acea 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -274,6 +274,21 @@ class User(MappedClass, ActivityNode, ActivityObject):
 
     # Statistics
     stats_id = FieldProperty(S.ObjectId, if_missing=None)
+    last_access = FieldProperty(dict(
+        login_date=S.DateTime,
+        login_ip=str,
+        login_ua=str,
+        session_date=S.DateTime,
+        session_ip=str,
+        session_ua=str))
+
+    def track_login(self, req):
+        user_ip = req.headers.get('X_FORWARDED_FOR', req.remote_addr)
+        user_agent = req.headers.get('User-Agent')
+        self.last_access['login_date'] = datetime.utcnow()
+        self.last_access['login_ip'] = user_ip
+        self.last_access['login_ua'] = user_agent
+        session(self).flush(self)
 
     def can_send_user_message(self):
         """Return true if User is permitted to send a mesage to another user.

http://git-wip-us.apache.org/repos/asf/allura/blob/2664ba72/Allura/allura/tests/functional/test_auth.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_auth.py b/Allura/allura/tests/functional/test_auth.py
index 1c41e8a..a200352 100644
--- a/Allura/allura/tests/functional/test_auth.py
+++ b/Allura/allura/tests/functional/test_auth.py
@@ -78,6 +78,20 @@ class TestAuth(TestController):
             username='test-usera', password='foo'))
         assert 'Invalid login' in str(r), r.showbrowser()
 
+    def test_track_login(self):
+        user = M.User.by_username('test-user')
+        assert_equal(user.last_access['login_date'], None)
+        assert_equal(user.last_access['login_ip'], None)
+        assert_equal(user.last_access['login_ua'], None)
+
+        self.app.post('/auth/do_login', params=dict(
+            username='test-user', password='foo'),
+            headers={'X_FORWARDED_FOR': 'addr', 'User-Agent': 'browser'})
+        user = M.User.by_username('test-user')
+        assert_not_equal(user.last_access['login_date'], None)
+        assert_equal(user.last_access['login_ip'], 'addr')
+        assert_equal(user.last_access['login_ua'], 'browser')
+
     @td.with_user_project('test-admin')
     def test_prefs(self):
         r = self.app.get('/auth/preferences/',


Mime
View raw message