feat: admin panel

This commit is contained in:
Per Stark
2025-01-23 14:35:13 +01:00
parent 16e0611a88
commit 703480e8b3
6 changed files with 81 additions and 16 deletions

View File

@@ -18,7 +18,8 @@ use crate::{
page_data!(AdminPanelData, "auth/admin_panel.html", {
user: User,
settings: SystemSettings,
analytics: Analytics
analytics: Analytics,
users: i64,
});
pub async fn show_admin_panel(
@@ -27,23 +28,29 @@ pub async fn show_admin_panel(
) -> Result<impl IntoResponse, HtmlError> {
// Early return if the user is not authenticated
let user = match auth.current_user {
Some(user) => user,
None => return Ok(Redirect::to("/").into_response()),
Some(user) if user.admin => user,
_ => return Ok(Redirect::to("/").into_response()),
};
let settings = SystemSettings::get_current(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let analytics = Analytics::get_current(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let users_count = Analytics::get_users_amount(&state.surreal_db_client)
.await
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
let output = render_template(
AdminPanelData::template_name(),
AdminPanelData {
user,
settings,
analytics,
users: users_count,
},
state.templates.clone(),
)?;

View File

@@ -1,5 +1,6 @@
use crate::storage::types::file_info::deserialize_flexible_id;
use crate::storage::types::{file_info::deserialize_flexible_id, user::User, StoredObject};
use serde::{Deserialize, Serialize};
use tracing::info;
use crate::{error::AppError, storage::db::SurrealDbClient};
@@ -7,6 +8,7 @@ use crate::{error::AppError, storage::db::SurrealDbClient};
pub struct Analytics {
#[serde(deserialize_with = "deserialize_flexible_id")]
pub id: String,
pub page_loads: i64,
pub visitors: i64,
}
@@ -20,6 +22,7 @@ impl Analytics {
.content(Analytics {
id: "current".to_string(),
visitors: 0,
page_loads: 0,
})
.await?;
@@ -47,4 +50,30 @@ impl Analytics {
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
}
pub async fn increment_page_loads(db: &SurrealDbClient) -> Result<Self, AppError> {
let updated: Option<Self> = db
.client
.query("UPDATE type::thing('analytics', 'current') SET page_loads += 1 RETURN AFTER")
.await?
.take(0)?;
updated.ok_or(AppError::Validation("Failed to update analytics".into()))
}
pub async fn get_users_amount(db: &SurrealDbClient) -> Result<i64, AppError> {
#[derive(Debug, Deserialize)]
struct CountResult {
count: i64,
}
let result: Option<CountResult> = db
.client
.query("SELECT count() as count FROM type::table($table) GROUP ALL")
.bind(("table", User::table_name()))
.await?
.take(0)?;
Ok(result.map(|r| r.count).unwrap_or(0))
}
}

View File

@@ -11,7 +11,7 @@
<label class="label">
<span class="label-text">Email</span>
</label>
<input type="email" name="email" value="{{ user.email }}" class="input text-gray-100! input-bordered w-full"
<input type="email" name="email" value="{{ user.email }}" class="input text-primary-content input-bordered w-full"
disabled />
</div>
<div class="form-control">
@@ -20,8 +20,8 @@
</label>
{% block api_key_section %}
{% if user.api_key %}
<input type="text" name="api-key" value="{{ user.api_key }}" class="input text-gray-100! input-bordered w-full"
disabled />
<input type="text" name="api-key" value="{{ user.api_key }}"
class="input text-primary-content input-bordered w-full" disabled />
<a href="https://www.icloud.com/shortcuts/66985f7b98a74aaeac6ba29c3f1f0960"
class="btn btn-accent mt-4 w-full">Download
iOS

View File

@@ -1,13 +1,40 @@
{% extends 'body_base.html' %}
{% block main %}
<main class="container justify-center flex-grow flex mx-auto mt-4 p-5">
Hello
{% if user.admin %}
admin
{% else %}
user
{% endif %}
<main class="container flex-grow flex flex-col mx-auto mt-4 p-5 space-y-6">
<h1 class="text-3xl font-bold">Admin Dashboard</h1>
{{settings}}
<div class="stats stats-vertical lg:stats-horizontal shadow">
<div class="stat">
<div class="stat-title">Page loads</div>
<div class="stat-value text-secondary">{{analytics.page_loads}}</div>
<div class="stat-desc">Amount of page loads</div>
</div>
<div class="stat">
<div class="stat-title">Unique visitors</div>
<div class="stat-value text-primary">{{analytics.visitors}}</div>
<div class="stat-desc">Amount of unique visitors</div>
</div>
<div class="stat">
<div class="stat-title">Users</div>
<div class="stat-value text-accent">{{users}}</div>
<div class="stat-desc">Amount of registered users</div>
</div>
</div>
<!-- Settings in Fieldset -->
<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
<fieldset class="fieldset p-4 shadow rounded-box">
<legend class="fieldset-legend">Registration</legend>
<label class="fieldset-label">
<input type="checkbox" class="checkbox" hx-post="/toggle-registrations" hx-target="#registration-status" {% if
settings.registrations_enabled %}checked{% endif %} />
Enable Registrations
</label>
<div id="registration-status" class="text-sm mt-2"></div>
</fieldset>
</div>
</main>
{% endblock %}

View File

@@ -37,7 +37,7 @@
</p>
</label>
</div>
<div class="mt-2 text-error" id="signup-result"></div>
<div class="mt-4 text-error" id="signup-result"></div>
<div class="form-control mt-6">
<button id="submit-btn" class="btn btn-primary w-full">
Create Account

View File

@@ -19,7 +19,9 @@
<summary>Account</summary>
<ul class="bg-base-100 rounded-t-none p-2">
<li><a hx-boost="true" class="" href="/account">Account</a></li>
{% if user.admin %}
<li><a hx-boost="true" class="" href="/admin">Admin</a></li>
{% endif %}
<li><a hx-boost="true" href="/signout">Sign out</a></li>
</ul>
</details>