From 7e2b64549002305c2015dbf5e56096d62a1b2f9a Mon Sep 17 00:00:00 2001 From: 4cuck <40539855+4cuck@users.noreply.github.com> Date: Tue, 17 Oct 2023 00:34:10 +0200 Subject: [PATCH] add admin mode --- .env.example | 4 ++-- server/src/admin.rs | 25 +++++++++++++++++-------- server/src/lib.rs | 1 + server/templates/admin.html.j2 | 5 ++++- src/main.rs | 2 ++ 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 26cb5f7..331e9f3 100644 --- a/.env.example +++ b/.env.example @@ -43,7 +43,7 @@ ORIGIN_SOURCE_GIT_BRANCH="master" ORIGIN_SOURCE_GIT_URL="https://github.com/zedeus/nitter.git" # session lifetime for host logins SESSION_TTL_SECONDS="2592000" -# admin hosts to get full access -ADMIN_HOSTS="example.com,foo.net" +# admin instance domains +ADMIN_DOMAINS="example.com,foo.net" # token name used for logging in - has to be a valid DNS TXT key LOGIN_TOKEN_NAME="localhost" \ No newline at end of file diff --git a/server/src/admin.rs b/server/src/admin.rs index faefab7..034b21a 100644 --- a/server/src/admin.rs +++ b/server/src/admin.rs @@ -43,6 +43,7 @@ use crate::LOGIN_URL; pub struct ActiveLogin { /// Hosts this session has access to. hosts: HashSet, + admin: bool, } const LOGIN_KEY: &'static str = "LOGIN"; @@ -85,8 +86,9 @@ pub async fn login( Form(input): Form, ) -> Result { tracing::debug!(login=?input); + let domain = input.domain.trim(); let host = host::Entity::find() - .filter(host::Column::Domain.eq(input.domain.trim())) + .filter(host::Column::Domain.eq(domain)) .one(db) .await?; @@ -113,7 +115,7 @@ pub async fn login( Err(_) => { let mut ids = HashSet::with_capacity(1); ids.insert(host.id); - ActiveLogin { hosts: ids } + ActiveLogin { hosts: ids, admin: config.admin_domains.iter().any(|e| e == domain) } } }; session.insert(LOGIN_KEY, session_value)?; @@ -273,7 +275,7 @@ pub async fn overview( ) -> Result { tracing::info!(?session); - let hosts = get_all_login_hosts(&session, db).await?; + let (login,hosts) = get_all_login_hosts(&session, db).await?; let mut context = tera::Context::new(); let res = { @@ -284,6 +286,7 @@ pub async fn overview( let time = guard.last_update.format("%Y.%m.%d %H:%M").to_string(); context.insert("last_updated", &time); context.insert("instances", &hosts); + context.insert("is_admin", &login.admin); let res = Html(template.render("admin.html.j2", &context)?).into_response(); drop(guard); @@ -377,14 +380,20 @@ pub async fn errors_view( async fn get_all_login_hosts( session: &Session, db: &DatabaseConnection, -) -> Result> { +) -> Result<(ActiveLogin, Vec)> { let login = get_session_login(&session)?; - let host_res = host::Entity::find() + let host_res = match login.admin { + true => host::Entity::find() + .filter(host::Column::Enabled.eq(true)) + .all(db) + .await?, + false => host::Entity::find() .filter(host::Column::Id.is_in(login.hosts.iter().map(|v| *v))) .all(db) - .await?; - Ok(host_res) + .await?, + }; + Ok((login,host_res)) } /// Get wanted [host::Model] for current [Session] if valid for this user @@ -395,7 +404,7 @@ async fn get_specific_login_host( ) -> Result { let login = get_session_login(&session)?; - if !login.hosts.contains(&wanted_host_id) { + if !login.hosts.contains(&wanted_host_id) && !login.admin { return Err(ServerError::MissingPermission); } diff --git a/server/src/lib.rs b/server/src/lib.rs index 81a8108..7c311dd 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -40,6 +40,7 @@ pub struct Config { pub max_age: usize, pub session_ttl_seconds: u64, pub login_token_name: String, + pub admin_domains: Vec, } #[derive(Clone, axum::extract::FromRef)] diff --git a/server/templates/admin.html.j2 b/server/templates/admin.html.j2 index de7f08c..68902f7 100644 --- a/server/templates/admin.html.j2 +++ b/server/templates/admin.html.j2 @@ -14,7 +14,8 @@

Admin Interface

Logout Add more instances

-

Instances

you are logged in for.

+

Instances

+ {% if is_admin %}

Admin mode.

{% endif %}
@@ -24,9 +25,11 @@ {% for instance in instances -%} + {# #} + {%- endfor %}
{{instance.domain}}HistoryErrors
diff --git a/src/main.rs b/src/main.rs index 4dac232..8588b31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,12 +166,14 @@ fn read_server_config(instance_ping_interval: usize) -> miette::Result