1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-30 15:04:18 +00:00

Improved password change command

This commit is contained in:
Steffo 2017-03-10 11:52:20 +01:00
parent e506249bd3
commit 2c11a0dcb0
2 changed files with 15 additions and 9 deletions

View file

@ -13,6 +13,13 @@ import database
b = telegram.Bot(royalbotconfig.telegram_token) b = telegram.Bot(royalbotconfig.telegram_token)
def currently_logged_in(update):
"""Find the database user which sent the update."""
session = database.Session()
user = session.query(database.User).filter_by(telegram_id=update.message.sent_from.user_id).first()
return session, user
async def diario(bot, update, arguments): async def diario(bot, update, arguments):
"""Aggiungi una frase al diario Royal Games. """Aggiungi una frase al diario Royal Games.
@ -125,7 +132,7 @@ Sintassi: `/sync <username> <password>`"""
# Handle duplicate # Handle duplicate
logged_user.telegram_id = update.message.sent_from.user_id logged_user.telegram_id = update.message.sent_from.user_id
session.commit() session.commit()
await update.message.chat.send_message(bot, f"Sincronizzazione riuscita: {logged_user}") await update.message.chat.send_message(bot, f"Sincronizzazione riuscita!\nSei loggato come `{logged_user}`.")
else: else:
await update.message.chat.send_message(bot, "⚠ L'account è già stato sincronizzato.") await update.message.chat.send_message(bot, "⚠ L'account è già stato sincronizzato.")
else: else:
@ -135,17 +142,16 @@ Sintassi: `/sync <username> <password>`"""
async def changepassword(bot, update, arguments): async def changepassword(bot, update, arguments):
"""Cambia la tua password del Database Royal Games. """Cambia la tua password del Database Royal Games.
Sintassi: `/changepassword <username> <oldpassword> <newpassword>`""" Sintassi: `/changepassword <newpassword>`"""
if len(arguments) != 3: if len(arguments) != 2:
await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/sync <username> <password>`") await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/changepassword <oldpassword> <newpassword>`")
return return
# TODO: this can be improved # TODO: this can be improved, maybe?
# Try to login session, logged_user = currently_logged_in(update)
_, logged_user = database.login(arguments[0], arguments[1])
# Check if the login is successful # Check if the login is successful
if logged_user is not None: if logged_user is not None:
# Change the password # Change the password
database.change_password(logged_user.username, arguments[2]) database.change_password(logged_user.username, arguments[1])
await update.message.chat.send_message(bot, f"Il cambio password è riuscito!\n\n_Info per smanettoni: la tua password è hashata nel database come_ `{logged_user.password}`.") await update.message.chat.send_message(bot, f"Il cambio password è riuscito!\n\n_Info per smanettoni: la tua password è hashata nel database come_ `{logged_user.password}`.")
else: else:
await update.message.chat.send_message(bot, "⚠ Username o password non validi.") await update.message.chat.send_message(bot, "⚠ Username o password non validi.")

View file

@ -65,7 +65,7 @@ def login(username, password, enable_exceptions=False):
# Create a new session # Create a new session
session = Session() session = Session()
# Find the matching user # Find the matching user
users = session.query(User).filter(User.username == username).all() users = session.query(User).filter_by(username=username).all()
# No user with a matching username found # No user with a matching username found
if len(users) == 0: if len(users) == 0:
if enable_exceptions: if enable_exceptions: