mirror of
https://github.com/dgtlmoon/changedetection.io.git
synced 2025-11-30 21:33:20 +00:00
Some checks failed
Build and push containers / metadata (push) Has been cancelled
Build and push containers / build-push-containers (push) Has been cancelled
Publish Python 🐍distribution 📦 to PyPI and TestPyPI / Build distribution 📦 (push) Has been cancelled
ChangeDetection.io App Test / lint-code (push) Has been cancelled
Publish Python 🐍distribution 📦 to PyPI and TestPyPI / Test the built 📦 package works basically. (push) Has been cancelled
Publish Python 🐍distribution 📦 to PyPI and TestPyPI / Publish Python 🐍 distribution 📦 to PyPI (push) Has been cancelled
ChangeDetection.io App Test / test-application-3-10 (push) Has been cancelled
ChangeDetection.io App Test / test-application-3-11 (push) Has been cancelled
ChangeDetection.io App Test / test-application-3-12 (push) Has been cancelled
ChangeDetection.io App Test / test-application-3-13 (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
36 lines
1.6 KiB
Python
36 lines
1.6 KiB
Python
import os
|
|
from functools import wraps
|
|
from flask import current_app, redirect, request
|
|
from loguru import logger
|
|
|
|
def login_optionally_required(func):
|
|
"""
|
|
If password authentication is enabled, verify the user is logged in.
|
|
To be used as a decorator for routes that should optionally require login.
|
|
This version is blueprint-friendly as it uses current_app instead of directly accessing app.
|
|
"""
|
|
@wraps(func)
|
|
def decorated_view(*args, **kwargs):
|
|
from flask import current_app
|
|
import flask_login
|
|
from flask_login import current_user
|
|
|
|
# Access datastore through the app config
|
|
datastore = current_app.config['DATASTORE']
|
|
has_password_enabled = datastore.data['settings']['application'].get('password') or os.getenv("SALTED_PASS", False)
|
|
|
|
# Permitted
|
|
if request.endpoint and 'static_content' in request.endpoint and request.view_args and request.view_args.get('group') == 'styles':
|
|
return func(*args, **kwargs)
|
|
# Permitted
|
|
elif request.endpoint and 'diff_history_page' in request.endpoint and datastore.data['settings']['application'].get('shared_diff_access'):
|
|
return func(*args, **kwargs)
|
|
elif request.method in flask_login.config.EXEMPT_METHODS:
|
|
return func(*args, **kwargs)
|
|
elif current_app.config.get('LOGIN_DISABLED'):
|
|
return func(*args, **kwargs)
|
|
elif has_password_enabled and not current_user.is_authenticated:
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
return func(*args, **kwargs)
|
|
return decorated_view |