canaille-globuzma/canaille/utils/__init__.py

79 lines
2 KiB
Python
Raw Normal View History

2021-12-01 10:44:15 +00:00
import base64
import hashlib
import json
2023-03-29 18:14:28 +00:00
import re
2021-12-20 22:57:27 +00:00
from canaille.models import User
from flask import current_app
from flask import request
from flask_babel import gettext as _
2020-11-16 13:42:24 +00:00
2021-12-01 10:44:15 +00:00
def obj_to_b64(obj):
return base64.b64encode(json.dumps(obj).encode("utf-8")).decode("utf-8")
def b64_to_obj(string):
return json.loads(base64.b64decode(string.encode("utf-8")).decode("utf-8"))
def profile_hash(*args):
return hashlib.sha256(
current_app.config["SECRET_KEY"].encode("utf-8")
+ obj_to_b64(args).encode("utf-8")
).hexdigest()
def login_placeholder():
2021-12-03 17:37:25 +00:00
user_filter = current_app.config["LDAP"].get("USER_FILTER", User.DEFAULT_FILTER)
placeholders = []
if "cn={login}" in user_filter:
placeholders.append(_("John Doe"))
if "uid={login}" in user_filter:
placeholders.append(_("jdoe"))
if "mail={login}" in user_filter or not placeholders:
placeholders.append(_("john@doe.com"))
return _(" or ").join(placeholders)
2021-12-08 09:00:36 +00:00
def default_fields():
read = set()
write = set()
for acl in current_app.config["ACL"].values():
2021-12-13 22:53:41 +00:00
if "FILTER" not in acl:
2021-12-08 09:00:36 +00:00
read |= set(acl.get("READ", []))
write |= set(acl.get("WRITE", []))
return read, write
def get_current_domain():
if current_app.config.get("SERVER_NAME"):
return current_app.config.get("SERVER_NAME")
return request.host
def get_current_mail_domain():
if current_app.config["SMTP"].get("FROM_ADDR"):
return current_app.config["SMTP"]["FROM_ADDR"].split("@")[-1]
return get_current_domain()
2023-03-29 18:14:28 +00:00
def validate_uri(value):
regex = re.compile(
r"^(?:[A-Z0-9\\.-]+)s?://" # scheme + ://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain...
r"[A-Z0-9\\.-]+|" # hostname...
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # ...or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)
return re.match(regex, value) is not None