Skip to content

Commit

Permalink
refactor: set the 'is_rie' var in the global jinja context
Browse files Browse the repository at this point in the history
This avoids code redundancy.
  • Loading branch information
azmeuk committed Oct 9, 2023
1 parent f80501f commit cd8f2e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
2 changes: 2 additions & 0 deletions web/flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from flask_babel import Babel
from flask_migrate import Migrate
from flask_wtf.csrf import CSRFProtect
from flaskr.utils import is_rie

from .common.extensions import cache

Expand Down Expand Up @@ -58,6 +59,7 @@ def global_processor():
"config": app.config,
"beta": app.config["BETA"],
"documentation_link": app.config["DOCUMENTATION_LINK"],
"is_rie": is_rie(),
"LANGUAGES": LANGUAGES,
**app.config["WORDINGS"],
}
Expand Down
24 changes: 1 addition & 23 deletions web/flaskr/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
from flaskr.models import MeetingFilesExternal
from flaskr.models import User
from flaskr.utils import retry_join_meeting
from netaddr import IPAddress
from netaddr import IPNetwork
from sqlalchemy import exc
from webdav3.client import Client as webdavClient
from webdav3.exceptions import WebDavException
Expand Down Expand Up @@ -377,16 +375,10 @@ def index():
def home():
if has_user_session():
return redirect(url_for("routes.welcome"))
is_rie = any(
[
IPAddress(request.remote_addr) in IPNetwork(network_ip)
for network_ip in current_app.config["RIE_NETWORK_IPS"]
]
)

stats = get_meetings_stats()
return render_template(
"index.html",
is_rie=is_rie,
stats=stats,
mail_meeting=current_app.config["MAIL_MEETING"],
max_participants=current_app.config["MAX_PARTICIPANTS"],
Expand Down Expand Up @@ -1262,12 +1254,6 @@ def ncdownload(isexternal, mfid, mftoken):
methods=["GET"],
)
def signin_mail_meeting(meeting_fake_id, expiration, h):
is_rie = any(
[
IPAddress(request.remote_addr) in IPNetwork(network_ip)
for network_ip in current_app.config["RIE_NETWORK_IPS"]
]
)
meeting = get_mail_meeting(meeting_fake_id)
wordings = current_app.config["WORDINGS"]

Expand Down Expand Up @@ -1298,7 +1284,6 @@ def signin_mail_meeting(meeting_fake_id, expiration, h):
expiration=expiration,
user_id="fakeuserId",
h=h,
is_rie=is_rie,
role="moderator",
)

Expand All @@ -1307,12 +1292,6 @@ def signin_mail_meeting(meeting_fake_id, expiration, h):
"/meeting/signin/<meeting_fake_id>/creator/<int:user_id>/hash/<h>", methods=["GET"]
)
def signin_meeting(meeting_fake_id, user_id, h):
is_rie = any(
[
IPAddress(request.remote_addr) in IPNetwork(network_ip)
for network_ip in current_app.config["RIE_NETWORK_IPS"]
]
)
meeting = get_meeting_from_meeting_id_and_user_id(meeting_fake_id, user_id)
wordings = current_app.config["WORDINGS"]
if meeting is None:
Expand Down Expand Up @@ -1340,7 +1319,6 @@ def signin_meeting(meeting_fake_id, user_id, h):
meeting_fake_id=meeting_fake_id,
user_id=user_id,
h=h,
is_rie=is_rie,
role=role,
)

Expand Down
18 changes: 18 additions & 0 deletions web/flaskr/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from flask import current_app
from flask import request
from netaddr import IPAddress
from netaddr import IPNetwork


def secret_key():
Expand All @@ -11,3 +14,18 @@ def retry_join_meeting(referrer, role, fullname, fullname_suffix):
or (role in ("attendee", "moderator") and fullname)
or (role == "authenticated" and fullname and fullname_suffix)
)


def is_rie():
"""
Checks wether the request was made from inside the state network
"Réseau Interministériel de l’État"
"""
if not request.remote_addr:
return False

return any(
IPAddress(request.remote_addr) in IPNetwork(network_ip)
for network_ip in current_app.config.get("RIE_NETWORK_IPS", [])
if network_ip
)

0 comments on commit cd8f2e5

Please sign in to comment.