From 98963c1442f9710e098e2b09f0098ebcc568dcec Mon Sep 17 00:00:00 2001 From: Sidonie Bouthors Date: Fri, 19 Sep 2025 13:45:19 +0200 Subject: [PATCH] feat: explicitely warn on unauthorized command usages --- src/commands.rs | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index a3d1f53..84408e9 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -79,7 +79,7 @@ fn require_authorization() -> Endpoint<'static, DependencyMap, HandlerResult, Dp }; if !authorized { - log::warn!("Chat {} tried to use the commmand {}", msg.chat.id, command.shortand()) + log::warn!("Unauthorized Chat {} tried to use the commmand {}", msg.chat.id, command.shortand()) } authorized @@ -91,20 +91,32 @@ fn require_authorization() -> Endpoint<'static, DependencyMap, HandlerResult, Dp /// /// Required dependencies: `teloxide_core::types::message::Message`, `sqlx_sqlite::SqlitePool` fn require_admin() -> Endpoint<'static, DependencyMap, HandlerResult, DpHandlerDescription> { - dptree::entry().filter_async(|msg: Message, db: Arc| async move { - let Some(user) = msg.from else { - return false; - }; - - let id = user.id.to_string(); - sqlx::query!( - "SELECT COUNT(*) AS is_admin FROM admins WHERE telegram_id = $1", - id - ) - .fetch_one(db.as_ref()) - .await - .is_ok_and(|r| r.is_admin > 0) - }) + dptree::entry().filter_async( + |command: Command, msg: Message, db: Arc| async move { + let Some(user) = msg.from else { + return false; + }; + + let id = user.id.to_string(); + let is_admin = sqlx::query!( + "SELECT COUNT(*) AS is_admin FROM admins WHERE telegram_id = $1", + id + ) + .fetch_one(db.as_ref()) + .await + .is_ok_and(|r| r.is_admin > 0); + + if !is_admin { + log::warn!( + "Non-admin User {} tried to use the admin command {}", + id, + command.shortand() + ); + } + + is_admin + }, + ) } // --------------------------- AVAILABLE COMMANDS -----------------------------