Skip to content

Commit

Permalink
Merge pull request #174 from mila-iqia/redirect-when-not-connected
Browse files Browse the repository at this point in the history
Redirect to login page when not connected
  • Loading branch information
soline-b authored Oct 20, 2023
2 parents aa4009c + 46f4b9b commit a24b269
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 195 deletions.
108 changes: 27 additions & 81 deletions clockwork_web/browser_routes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,13 @@ def route_set_nbr_items_per_page():
# Check if nbr_items_per_page is a positive integer
if type(nbr_items_per_page) == int and nbr_items_per_page > 0:

if current_user.is_authenticated():
# If it is, update this number in the current user's settings and
# retrieve the status code and status message associated to this
# operation
(
status_code,
status_message,
) = current_user.settings_nbr_items_per_page_set(nbr_items_per_page)
else:
# Otherwise, return an error
return (
render_template_with_user_settings(
"error.html",
error_msg=gettext("The user is not authenticated."),
previous_request_args=previous_request_args,
),
403, # Forbidden
)
# If it is, update this number in the current user's settings and
# retrieve the status code and status message associated to this
# operation
(
status_code,
status_message,
) = current_user.settings_nbr_items_per_page_set(nbr_items_per_page)

if status_code == 200:
# If a success has been return, redirect to the settings page
Expand Down Expand Up @@ -192,18 +181,7 @@ def route_set_dark_mode():

# Set the dark mode value to True in the current user's web settings and
# retrieve the status code and status message associated to the operation
if current_user.is_authenticated():
(status_code, status_message) = current_user.settings_dark_mode_enable()
else:
# Otherwise, return an error
return (
render_template_with_user_settings(
"error.html",
error_msg=gettext("The user is not authenticated."),
previous_request_args=previous_request_args,
),
403, # Forbidden
)
(status_code, status_message) = current_user.settings_dark_mode_enable()

if status_code == 200:
# If a success has been returned
Expand Down Expand Up @@ -234,20 +212,10 @@ def route_unset_dark_mode():

# Initialize the request arguments (it is further transferred to the HTML)
previous_request_args = {}
if current_user.is_authenticated():
# Set the dark mode value to False in the current user's web settings and
# retrieve the status code and status message associated to the operation
(status_code, status_message) = current_user.settings_dark_mode_disable()
else:
# Otherwise, return an error
return (
render_template_with_user_settings(
"error.html",
error_msg=gettext("The user is not authenticated."),
previous_request_args=previous_request_args,
),
403, # Forbidden
)

# Set the dark mode value to False in the current user's web settings and
# retrieve the status code and status message associated to the operation
(status_code, status_message) = current_user.settings_dark_mode_disable()

if status_code == 200:
# If a success has been returned
Expand Down Expand Up @@ -493,25 +461,13 @@ def route_set_date_format():
if date_format:
# Check if the date format is supported
if date_format in get_available_date_formats():
if current_user.is_authenticated():
# If the requested date format is expected, update the preferred
# date format of the current user and retrieve the status code
# and status message associated to this operation
(
status_code,
status_message,
) = current_user.settings_date_format_set(date_format)

else:
# Otherwise, return an error
return (
render_template_with_user_settings(
"error.html",
error_msg=gettext("The user is not authenticated."),
previous_request_args=previous_request_args,
),
403, # Forbidden
)
# If the requested date format is expected, update the preferred
# date format of the current user and retrieve the status code
# and status message associated to this operation
(
status_code,
status_message,
) = current_user.settings_date_format_set(date_format)

if status_code == 200:
# If a success has been return, redirect to the home page
Expand Down Expand Up @@ -575,24 +531,14 @@ def route_set_time_format():
if time_format:
# Check if the date format is supported
if time_format in get_available_time_formats():
if current_user.is_authenticated():
# If the requested time format is expected, update the preferred
# time format of the current user and retrieve the status code
# and status message associated to this operation
(
status_code,
status_message,
) = current_user.settings_time_format_set(time_format)
else:
# Otherwise, return an error
return (
render_template_with_user_settings(
"error.html",
error_msg=gettext("The user is not authenticated."),
previous_request_args=previous_request_args,
),
403, # Forbidden
)

# If the requested time format is expected, update the preferred
# time format of the current user and retrieve the status code
# and status message associated to this operation
(
status_code,
status_message,
) = current_user.settings_time_format_set(time_format)

if status_code == 200:
# If a success has been return, redirect to the home page
Expand Down
4 changes: 2 additions & 2 deletions clockwork_web/login_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ def route_callback():
from flask import current_app

current_app.logger.debug(
"called login_user(user) for user with email %s, user.is_authenticated() is %s",
"called login_user(user) for user with email %s, user.is_authenticated is %s",
user.mila_email_username,
user.is_authenticated(),
user.is_authenticated,
)
# Send user back to homepage
return redirect(url_for("index"))
Expand Down
4 changes: 2 additions & 2 deletions clockwork_web/server_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def have_same_users(user1: str, user2: str):
# Initialize Babel
def get_locale():
# If the user is authenticated
if current_user and current_user.is_authenticated():
if current_user and current_user.is_authenticated:

return current_user.get_language()

Expand Down Expand Up @@ -250,7 +250,7 @@ def index():
where people can click on the "login" button on the web interface.
"""

if current_user.is_authenticated():
if current_user.is_authenticated:
app.logger.debug("in route for '/'; redirecting to jobs/")
return redirect("jobs/")
else:
Expand Down
4 changes: 2 additions & 2 deletions clockwork_web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h4><span>{{ gettext("Alpha version!") }}</span></h4>
{% endif %}
</a>
</div>
{% if current_user.is_authenticated() %}
{% if current_user.is_authenticated %}
<div class="col">
<nav class="navbar-expand-lg">
<div class="container-fluid">
Expand Down Expand Up @@ -126,7 +126,7 @@ <h4><span>{{ gettext("Alpha version!") }}</span></h4>
</div>

{% block form %}
{% if request.path != '/jobs/dashboard' and current_user.is_authenticated() %}
{% if request.path != '/jobs/dashboard' and current_user.is_authenticated %}
<!-- Search jobs form -->
<div id="formBlock">
<div class="search_button formCollapse {{ 'collapse' if request.path != '/jobs/search' }} {{ 'show' if request.path == '/jobs/search' }}" id="mainCollapse">
Expand Down
12 changes: 0 additions & 12 deletions clockwork_web/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ def boolean(value):
"actions"
] = False

# If we don't set those two values ourselves, we are going
# to have users being asked to login every time they click
# on a link or refresh the pages.
def is_authenticated(self):
return True

def is_active(self):
return self.status == "enabled"

def get_id(self):
return self.mila_email_username

Expand Down Expand Up @@ -350,6 +341,3 @@ def get_web_settings(self):
A dictionary presenting the default web settings.
"""
return self.web_settings

def is_authenticated(self):
return False
Loading

0 comments on commit a24b269

Please sign in to comment.