Skip to content

Commit

Permalink
[REF] website_require_login: Extract path matching
Browse files Browse the repository at this point in the history
Also create only one instance of Path(path)
  • Loading branch information
SirAionTech committed Dec 9, 2024
1 parent f2754aa commit 35dab4e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions website_require_login/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ Contributors
~~~~~~~~~~~~

* Ooops404 <https://ooops404.com>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
21 changes: 17 additions & 4 deletions website_require_login/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 Advitus MB
# Copyright 2024 Simone Rubino - Aion Tech
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0).
from pathlib import Path

Expand All @@ -23,6 +24,18 @@ def _serve_fallback(cls):
return res
return super()._serve_fallback()

@classmethod
def _require_login_get_matching_path(cls, path, search_paths):
"""Return which one of `search_paths` is a parent of `path`."""
path_inst = Path(path)
for search_path in search_paths:
if search_path == path or Path(search_path) in path_inst.parents:
matching_path = search_path
break
else:
matching_path = None
return matching_path

@classmethod
def _check_require_auth(cls):
# if not website request - skip
Expand All @@ -42,7 +55,7 @@ def _check_require_auth(cls):
.mapped("path")
)
path = request.httprequest.path
for auth_path in auth_paths:
if auth_path == path or Path(auth_path) in Path(path).parents:
redirect_path = "/web/login?redirect=%s" % path
return request.redirect(redirect_path, code=302)
auth_path = cls._require_login_get_matching_path(path, auth_paths)
if auth_path:
redirect_path = "/web/login?redirect=%s" % path
return request.redirect(redirect_path, code=302)
3 changes: 3 additions & 0 deletions website_require_login/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
* Ooops404 <https://ooops404.com>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>
5 changes: 4 additions & 1 deletion website_require_login/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -410,6 +409,10 @@ <h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Ooops404 &lt;<a class="reference external" href="https://ooops404.com">https://ooops404.com</a>&gt;</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down

0 comments on commit 35dab4e

Please sign in to comment.