mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Added find_user function
This commit is contained in:
parent
60f10fa72e
commit
03dc07ff9b
2 changed files with 16 additions and 13 deletions
23
database.py
23
database.py
|
@ -71,17 +71,12 @@ def login(username, password, enable_exceptions=False):
|
|||
# Create a new session
|
||||
session = Session()
|
||||
# Find the matching user
|
||||
users = session.query(User).filter_by(username=username).all()
|
||||
# No user with a matching username found
|
||||
if len(users) == 0:
|
||||
if enable_exceptions:
|
||||
raise NoUsersMatchingError("No users with the specified username found.")
|
||||
else:
|
||||
return session, None
|
||||
db_user = users[0]
|
||||
db_user = session.query(User).filter_by(username=username).first()
|
||||
# Verify that the user exists
|
||||
if db_user is not None:
|
||||
return session, None
|
||||
# Test the password and return the session and the user if successful
|
||||
if bcrypt.hashpw(password.encode("utf8"), db_user.password) == db_user.password:
|
||||
# TODO: Maybe there's a better way to do this?
|
||||
return session, db_user
|
||||
else:
|
||||
if enable_exceptions:
|
||||
|
@ -93,6 +88,16 @@ def login(username, password, enable_exceptions=False):
|
|||
def init_royal_db():
|
||||
create_user("test", "test", True)
|
||||
|
||||
|
||||
def find_user(username):
|
||||
"""Find the user with the specified username and return the session and the user object."""
|
||||
# Create a new session
|
||||
session = Session()
|
||||
# Find the matching user
|
||||
db_user = session.query(User).filter_by(username=username).first()
|
||||
# Return the session and the user
|
||||
return session, db_user
|
||||
|
||||
session = Session()
|
||||
# Generate the database if it's empty
|
||||
if session.query(User).first() is None:
|
||||
|
|
|
@ -540,10 +540,8 @@ Sintassi: `/toggleroyal <username>`"""
|
|||
if len(arguments) != 1:
|
||||
await update.message.reply(bot, "⚠ Sintassi del comando non valida.\n`/toggleroyal <username>`", parse_mode="Markdown")
|
||||
return
|
||||
# Create a new database session
|
||||
session = database.Session()
|
||||
# Find the user
|
||||
user = session.query(database.User).filter_by(username=arguments[0]).first()
|
||||
# Find the specified user
|
||||
session, user = database.find_user(arguments[0])
|
||||
# Check if the user exists
|
||||
if user is None:
|
||||
await update.message.reply(bot, "⚠ L'utente specificato non esiste.")
|
||||
|
|
Loading…
Reference in a new issue