You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2019/05/21 17:23:09 UTC

[allura] 01/01: [#8287] script to backfill previous_login_details

This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch db/8287
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 79b1bce385d029c0b3612b4c506a088fc2bf3119
Author: Dave Brondsema <da...@brondsema.net>
AuthorDate: Tue May 21 12:56:46 2019 -0400

    [#8287] script to backfill previous_login_details
---
 Allura/allura/lib/plugin.py                        |  1 -
 Allura/allura/model/auth.py                        |  6 ++-
 .../scripts/backfill_previous_login_details.py     | 55 ++++++++++++++++++++++
 3 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index c52453f..73786a0 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -204,7 +204,6 @@ class AuthenticationProvider(object):
         else:
             self.session['username'] = user.username
             h.auditlog_user('Successful login', user=user)
-        user.backfill_login_details(self)
         self.after_login(user, self.request)
 
         if 'rememberme' in self.request.params:
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index 2a15a45..3520dcd 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -303,6 +303,10 @@ class User(MappedClass, ActivityNode, ActivityObject, SearchIndexable):
         # but allow for anything to be stored, like other headers, geo info, frequency of use, etc
     }])
 
+    def __repr__(self):
+        return (u'<User username={s.username!r} display_name={s.display_name!r} _id={s._id!r} '
+                u'disabled={s.disabled!r} pending={s.pending!r}>'.format(s=self))
+
     def index(self):
         provider = plugin.AuthenticationProvider.get(None)  # no need in request here
         localization = '%s/%s' % (
@@ -370,8 +374,6 @@ class User(MappedClass, ActivityNode, ActivityObject, SearchIndexable):
             self.previous_login_details.append(detail)
 
     def backfill_login_details(self, auth_provider):
-        if self.previous_login_details:
-            return
         # ".*" at start of regex and the DOTALL flag is needed only for the test, which uses mim
         # Fixed in ming f9f69d3c, so once we upgrade to 0.6.1+ we can remove it
         msg_regex = re.compile(r'.*^({})'.format('|'.join([re.escape(line_prefix)
diff --git a/Allura/allura/scripts/backfill_previous_login_details.py b/Allura/allura/scripts/backfill_previous_login_details.py
new file mode 100644
index 0000000..b05a6ac
--- /dev/null
+++ b/Allura/allura/scripts/backfill_previous_login_details.py
@@ -0,0 +1,55 @@
+#       Licensed to the Apache Software Foundation (ASF) under one
+#       or more contributor license agreements.  See the NOTICE file
+#       distributed with this work for additional information
+#       regarding copyright ownership.  The ASF licenses this file
+#       to you under the Apache License, Version 2.0 (the
+#       "License"); you may not use this file except in compliance
+#       with the License.  You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#       Unless required by applicable law or agreed to in writing,
+#       software distributed under the License is distributed on an
+#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#       KIND, either express or implied.  See the License for the
+#       specific language governing permissions and limitations
+#       under the License.
+from __future__ import absolute_import, division, print_function, unicode_literals
+
+import logging
+
+import argparse
+
+from ming.orm import session
+
+from allura.scripts import ScriptTask
+from allura import model as M
+from allura.lib.plugin import AuthenticationProvider
+from allura.lib.utils import chunked_find
+
+log = logging.getLogger('allura.scripts.backfill_previous_login_details')
+
+
+class BackfillPreviousLoginDetails(ScriptTask):
+
+    @classmethod
+    def parser(cls):
+        return argparse.ArgumentParser(description="Backfill previous login details from audit logs.  "
+                                                   "Needed if auth.hibp_failure_force_pwd_change is enabled")
+
+    @classmethod
+    def execute(cls, options):
+        auth_provider = AuthenticationProvider.get(None)
+        for i, chunk in enumerate(chunked_find(M.User, {})):
+            log.info('Backfilling login details for chunk #%s', i)
+            for u in chunk:
+                try:
+                    u.backfill_login_details(auth_provider)
+                    session(u).flush(u)
+                except Exception:
+                    log.exception('Error backfilling on user %s', u)
+        log.info('Finished backfilling previous login details')
+
+
+if __name__ == '__main__':
+    BackfillPreviousLoginDetails.main()