diff --git a/app.py b/app.py index 9fc046f..5809570 100644 --- a/app.py +++ b/app.py @@ -981,14 +981,15 @@ def admin_dashboard(): def admin_add_site(): """Add a new site to sites.conf. - JSON body: {site, pin, nc_user?, nc_pass?} + JSON body: {site, pin, nc_user, nc_pass} Site must be exactly 4 digits and must not already exist. + NC User and NC Pass are required. """ data = request.get_json(force=True) site = data.get("site", "").strip() pin = data.get("pin", "").strip() - nc_user = data.get("nc_user", "").strip() or None - nc_pass = data.get("nc_pass", "").strip() or None + nc_user = data.get("nc_user", "").strip() + nc_pass = data.get("nc_pass", "").strip() if not re.fullmatch(r"\d{4}", site): return jsonify({"ok": False, "error": "Site must be exactly 4 digits"}), 400 @@ -996,6 +997,12 @@ def admin_add_site(): if not pin: return jsonify({"ok": False, "error": "PIN is required"}), 400 + if not nc_user: + return jsonify({"ok": False, "error": "NC User is required"}), 400 + + if not nc_pass: + return jsonify({"ok": False, "error": "NC Pass is required"}), 400 + # Reject characters that would corrupt sites.conf (colon is the # field delimiter, newlines would inject extra lines) for field_name, value in [("PIN", pin), ("NC User", nc_user), ("NC Pass", nc_pass)]: @@ -1011,7 +1018,18 @@ def admin_add_site(): sites[site] = {"pin": pin, "nc_user": nc_user, "nc_pass": nc_pass} save_sites(sites) - return jsonify({"ok": True}) + + # Auto-provision the Nextcloud user + nc_msg = None + if all([NC_URL, NC_USER, NC_PASS]): + if nc_user_exists(nc_user): + nc_msg = f"NC user '{nc_user}' already exists" + elif nc_create_user(nc_user, nc_pass): + nc_msg = f"NC user '{nc_user}' created" + else: + nc_msg = f"Site saved, but failed to create NC user '{nc_user}'" + + return jsonify({"ok": True, "nc_msg": nc_msg}) @app.route("/admin/api/sites/", methods=["PUT"]) diff --git a/templates/admin.html b/templates/admin.html index d722768..9eeaa92 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -302,8 +302,8 @@
- - + +
@@ -324,7 +324,7 @@ Not found {% endif %} {% else %} - Global + Not set {% endif %}
@@ -391,8 +391,8 @@ const nc_user = document.getElementById('add-nc-user').value.trim(); const nc_pass = document.getElementById('add-nc-pass').value.trim(); - if (!site || !pin) { - showBanner('Site number and PIN are required', 'error'); + if (!site || !pin || !nc_user || !nc_pass) { + showBanner('All fields are required', 'error'); return; } @@ -404,7 +404,8 @@ }); const data = await resp.json(); if (data.ok) { - window.location.reload(); + if (data.nc_msg) showBanner(data.nc_msg, 'success'); + setTimeout(() => window.location.reload(), 1500); } else { showBanner(data.error || 'Failed to add site', 'error'); } @@ -485,8 +486,8 @@ const nc_user = document.getElementById('edit-nc-user').value.trim(); const nc_pass = document.getElementById('edit-nc-pass').value.trim(); - if (!pin) { - showBanner('PIN is required', 'error'); + if (!pin || !nc_user || !nc_pass) { + showBanner('PIN, NC User, and NC Pass are required', 'error'); return; }