You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by gc...@apache.org on 2025/01/07 18:02:36 UTC

(allura) 01/03: create return_to redirect helper

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

gcruz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 9a017ce70b62cae58e5916856d1e152f44a805a3
Author: Dave Brondsema <db...@slashdotmedia.com>
AuthorDate: Mon Jan 6 16:49:31 2025 -0500

    create return_to redirect helper
---
 Allura/allura/lib/custom_middleware.py |  9 +--------
 Allura/allura/lib/helpers.py           | 15 +++++++++++++++
 Allura/allura/lib/plugin.py            | 10 +---------
 3 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/Allura/allura/lib/custom_middleware.py b/Allura/allura/lib/custom_middleware.py
index 9e937a66d..b32bece97 100644
--- a/Allura/allura/lib/custom_middleware.py
+++ b/Allura/allura/lib/custom_middleware.py
@@ -157,14 +157,7 @@ class LoginRedirectMiddleware:
         is_api_request = environ.get('PATH_INFO', '').startswith('/rest/')
         if status[:3] == '401' and not is_api_request and not is_ajax(Request(environ)):
             login_url = tg.config.get('auth.login_url', '/auth/')
-            if environ['REQUEST_METHOD'] == 'GET':
-                return_to = environ['PATH_INFO']
-                if environ.get('QUERY_STRING'):
-                    return_to += '?' + environ['QUERY_STRING']
-                location = tg.url(login_url, dict(return_to=return_to))
-            else:
-                # Don't try to re-post; the body has been lost.
-                location = tg.url(login_url)
+            location = h.url_return_to(login_url, environ=environ)
             r = exc.HTTPFound(location=location, headers={'X-Robots-Tag': 'noindex,follow'})
             return r(environ, start_response)
         start_response(status, headers, exc_info)
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index a8a903da7..96658f835 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -45,6 +45,7 @@ import tg
 import six
 import cchardet as chardet
 import pkg_resources
+import webob
 from formencode.validators import FancyValidator
 from dateutil.parser import parse
 from bson import ObjectId
@@ -551,6 +552,20 @@ def absurl(url):
     return host + url
 
 
+def url_return_to(url: str, request: webob.Request | None = None, environ: dict | None = None) -> str:
+    if not environ:
+        environ = request.environ
+    if environ['REQUEST_METHOD'] == 'GET':
+        return_to = environ['PATH_INFO']
+        if environ.get('QUERY_STRING'):
+            return_to += '?' + environ['QUERY_STRING']
+        location = tg.url(url, dict(return_to=return_to))
+    else:
+        # Don't try to re-post; the body has been lost.
+        location = tg.url(url)
+    return location
+
+
 def diff_text(t1, t2, differ=None):
     t1_lines = t1.replace('\r', '').split('\n')
     t2_lines = t2.replace('\r', '').split('\n')
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 6f48524cb..437357be2 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -151,15 +151,7 @@ class AuthenticationProvider:
             return M.User.anonymous()
 
         if self.session.get('pwd-expired') and request.path not in self.pwd_expired_allowed_urls:
-            if self.request.environ['REQUEST_METHOD'] == 'GET':
-                return_to = self.request.environ['PATH_INFO']
-                if self.request.environ.get('QUERY_STRING'):
-                    return_to += '?' + self.request.environ['QUERY_STRING']
-                location = tg.url(self.pwd_expired_allowed_urls[0], dict(return_to=return_to))
-            else:
-                # Don't try to re-post; the body has been lost.
-                location = tg.url(self.pwd_expired_allowed_urls[0])
-            redirect(location)
+            redirect(h.url_return_to(self.pwd_expired_allowed_urls[0], self.request))
 
         return user