nextcloudregister/webapp.py
2020-10-17 22:04:55 +02:00

207 lines
6.2 KiB
Python

from configparser import ConfigParser
from flask import (
Flask,
request,
render_template,
Response,
)
from nextcloudregister.lib import (
CONFIG_PATH,
NextcloudApi,
NextcloudApiCannotSendEmail,
NextcloudApiException,
NextcloudApiInvalidInputData,
NextcloudApiNoEmailPassword,
NextcloudApiPermissionDenied,
NextcloudApiUsernamealreadyExists,
NextcloudApiUnknownError,
)
config = ConfigParser()
config.read(CONFIG_PATH)
app = Flask(__name__)
base_uri = config.get("web", "base_uri", fallback="")
base_uri = base_uri + ("" if base_uri.endswith("/") else "/")
@app.route(base_uri, methods=["GET", "POST"])
def form_manager():
if request.method == "POST":
return form_post()
else:
return form_get()
def form_get(data=None, error=None, info=None, success=False):
context = {
"base_uri": base_uri,
"data": data or {},
"disable": success,
"error": error,
"eula": config.get("web", "eula", fallback=""),
"favicon": config.get("web", "favicon", fallback=""),
"info": info,
"instance_link": config.get("web", "instance_link", fallback=""),
"max_accounts": config.getint("rules", "max_accounts", fallback=0),
"org_link": config.get("web", "org_link", fallback=""),
"success": success,
}
context["mandatory_password"] = config.getboolean(
"rules",
"mandatory_password",
fallback=True,
)
context["mandatory_email"] = config.getboolean(
"rules",
"mandatory_email",
fallback=False,
)
count_accounts = 0
api = NextcloudApi()
try:
count_accounts = api.count_accounts()
except NextcloudApiException:
context["max_accounts"] = 0
if count_accounts >= context["max_accounts"] and not success:
context["disable"] = True
context["error"] = (
"Tout les comptes disponibles sur cette instance ont deja été "
"distribués."
)
else:
context["count_accounts"] = context["max_accounts"] - count_accounts
return render_template("form.html", **context)
def form_post():
mandatory_email = config.getboolean(
"rules",
"mandatory_email",
fallback=False,
)
mandatory_password = config.getboolean(
"rules",
"mandatory_password",
fallback=False,
)
# validate mandatory fields
if not request.form.get("username"):
return form_get(
data=request.form,
error="Un nom d'utilisateur est obligatoire pour vous inscrire."
)
if mandatory_email and not request.form.get("email"):
return form_get(
data=request.form,
error="Une adresse mail est obligatoire pour vous inscrire."
)
if mandatory_password and not request.form.get("password1"):
return form_get(
data=request.form,
error="Un mot de passe est obligatoire pour vous inscrire."
)
# validate password
if request.form.get("password1"):
password1 = request.form.get("password1")
password2 = request.form.get("password2")
if not password1 == password2:
return form_get(
data=request.form,
error="Les mots de passe ne correspondent pas."
)
api = NextcloudApi()
try:
api.create_account(
username=request.form.get("username"),
password=request.form.get("password1"),
email=request.form.get("email")
)
except NextcloudApiCannotSendEmail:
return form_get(
data=request.form,
error="Impossible d'envoyer un email pour la creation du compte."
)
except NextcloudApiInvalidInputData:
return form_get(
data=request.form,
error=(
"Erreur interne. Merci de contacter l'administrateur de "
"l'instance Nextcloud."
)
)
except NextcloudApiNoEmailPassword:
return form_get(
data=request.form,
error=(
"Une addresse email ou un mot de passe sont obligatoires pour "
"créer un compte Nextcloud."
)
)
except NextcloudApiPermissionDenied:
return form_get(
data=request.form,
error=(
"Erreur interne. Merci de contacter l'administrateur de "
"l'instance Nextcloud. (Permission denied)"
)
)
except NextcloudApiUsernamealreadyExists:
return form_get(
data=request.form,
error=(
"Le nom d'utilisateur que vous avez choisi est déjà utilisé "
"sur cette instance Nextcloud."
)
)
except NextcloudApiUnknownError as err:
return form_get(
data=request.form,
error=err.hint
)
except NextcloudApiException as err:
return form_get(
data=request.form,
error=(
"Erreur interne. Merci de contacter l'administrateur de "
"l'instance Nextcloud. (%s)" % err
)
)
except Exception:
return form_get(
data=request.form,
error=(
"Erreur interne. Merci de contacter l'administrateur de "
"l'instance Nextcloud."
)
)
if config.get("web", "instance_link", fallback=""):
return form_get(
info=(
"L'inscription est un succes, vous allez maintenat être "
"redirigé vers votre instance Nextcloud."
),
success=True,
)
else:
return form_get(
info="Vous êtes maintenat inscrit sur cette instance Nextcloud",
success=True,
)
@app.route(base_uri + "style.css")
def style():
context = {
"color": config.get("web", "color")
}
css_content = render_template("style.css", **context)
return Response(css_content, mimetype="text/css")
application = app