feat: add access control logging and feedback for increment command and button

This commit is contained in:
Andre Beging
2025-09-30 10:51:22 +02:00
parent fba72a679d
commit fe3d852434

8
bot.py
View File

@@ -151,10 +151,16 @@ async def remove_counter(update: Update, context: ContextTypes.DEFAULT_TYPE):
async def increment_counter(update: Update, context: ContextTypes.DEFAULT_TYPE): async def increment_counter(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id chat_id = update.effective_chat.id
if not context.bot_data['access'](chat_id): if not context.bot_data['access'](chat_id):
logger.info("Access denied for chat %s on /increment", chat_id)
try:
await update.message.reply_text("Zugriff verweigert: Dieser Chat ist nicht freigeschaltet.")
except Exception:
pass
return return
counters = context.bot_data['counters'] counters = context.bot_data['counters']
# If no argument: show inline keyboard with counters # If no argument: show inline keyboard with counters
if not context.args: if not context.args:
logger.debug("/increment without args in chat %s -> showing keyboard (%d counters)", chat_id, len(counters))
if not counters: if not counters:
await update.message.reply_text("Keine Counter vorhanden. Lege einen an mit /add <name>.") await update.message.reply_text("Keine Counter vorhanden. Lege einen an mit /add <name>.")
return return
@@ -195,6 +201,7 @@ async def handle_increment_button(update: Update, context: ContextTypes.DEFAULT_
counters = context.bot_data['counters'] counters = context.bot_data['counters']
chat_id = query.message.chat_id if query.message else None chat_id = query.message.chat_id if query.message else None
if chat_id is not None and not context.bot_data['access'](chat_id): if chat_id is not None and not context.bot_data['access'](chat_id):
logger.info("Access denied for chat %s on increment button %s", chat_id, name)
return return
key = norm_key(name) key = norm_key(name)
if key not in counters: if key not in counters:
@@ -202,6 +209,7 @@ async def handle_increment_button(update: Update, context: ContextTypes.DEFAULT_
return return
counters[key] += 1 counters[key] += 1
save_counters(counters) save_counters(counters)
logger.debug("Increment via button: %s -> %d", key, counters[key])
# Rebuild keyboard to reflect new values # Rebuild keyboard to reflect new values
buttons = [] buttons = []
row: list = [] row: list = []