fix: admin panel — passwordless create, show generated password, user table

This commit is contained in:
Claus Lohmar 2026-07-08 13:49:26 +01:00
parent 993514d92e
commit f45132962c

45
main.go
View file

@ -184,10 +184,16 @@ func adminHandler(w http.ResponseWriter, r *http.Request) {
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// If JSON format requested, return raw API response
if r.URL.Query().Get("format") == "json" {
w.Header().Set("Content-Type", "application/json")
w.Write(body)
return
}
tmpl := template.Must(template.New("admin").Parse(adminHTML))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl.Execute(w, map[string]interface{}{
"Users": string(body),
"ApiError": resp.StatusCode != 200,
})
@ -355,7 +361,9 @@ const adminHTML = `<!DOCTYPE html>
.btn-primary:hover { background: #2d3748; }
input, select { padding: 0.4rem 0.6rem; border: 1px solid #e2e8f0; border-radius: 4px; font-size: 0.9rem; margin-right: 0.5rem; }
.form-row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.success { background: #c6f6d5; color: #276749; padding: 0.75rem; border-radius: 6px; margin-bottom: 1rem; }
.error { background: #fed7d7; color: #c53030; padding: 0.75rem; border-radius: 6px; margin-bottom: 1rem; }
.password-box { background: #1a1a2e; color: #63b3ed; padding: 0.75rem; border-radius: 4px; font-family: monospace; margin-top: 0.5rem; }
</style>
</head>
<body>
@ -373,7 +381,6 @@ const adminHTML = `<!DOCTYPE html>
<input name="username" placeholder="Username" required>
<input name="display_name" placeholder="Display Name" required>
<input name="email" placeholder="Email" type="email" required>
<input name="password" placeholder="Password" type="password" required>
<select name="groups" multiple size="3">
<option value="users">users</option>
<option value="drive">drive</option>
@ -387,28 +394,56 @@ const adminHTML = `<!DOCTYPE html>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</form>
<div id="createResult"></div>
</div>
<div class="card">
<h2>Users</h2>
<pre>{{.Users}}</pre>
<table>
<thead><tr><th>Username</th><th>Display Name</th><th>Email</th><th>Groups</th><th>Action</th></tr></thead>
<tbody id="usersTable"></tbody>
</table>
</div>
</div>
<script>
async function loadUsers() {
const resp = await fetch('/config?format=json');
const users = await resp.json();
const tbody = document.getElementById('usersTable');
tbody.innerHTML = users.map(u => '<tr><td>' + u.username + '</td><td>' + (u.display_name||'') + '</td><td>' + (u.email||'') + '</td><td>' + (u.groups||[]).join(', ') + '</td><td><button class="btn btn-danger" onclick="deleteUser(\'' + u.username + '\')">Delete</button></td></tr>').join('');
}
async function createUser(e) {
e.preventDefault();
const form = e.target;
const data = Object.fromEntries(new FormData(form));
data.groups = Array.from(form.querySelector('[name=groups]').selectedOptions).map(o => o.value);
if (!data.groups.length) data.groups = ['users'];
const resp = await fetch('/config', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({users: [data]})
});
if (resp.ok) { alert('User created'); location.reload(); }
else { alert('Error: ' + await resp.text()); }
const result = await resp.json();
const div = document.getElementById('createResult');
if (result.success) {
const pwd = (result.users && result.users[0] && result.users[0].placeholder_password) || 'check email';
div.innerHTML = '<div class="success">User created! <div class="password-box">Password: ' + pwd + '</div></div>';
loadUsers();
} else {
div.innerHTML = '<div class="error">Error: ' + JSON.stringify(result) + '</div>';
}
}
async function deleteUser(username) {
if (!confirm('Delete ' + username + '?')) return;
const resp = await fetch('/config?username=' + username, {method: 'DELETE'});
if (resp.ok) { loadUsers(); }
else { alert('Delete failed: ' + await resp.text()); }
}
loadUsers();
</script>
</body>
</html>`