change username

This commit is contained in:
Spekulaas 2023-10-25 22:13:34 +02:00
parent c14a360418
commit 359b1d8af1
3 changed files with 51 additions and 2 deletions

View File

@ -35,6 +35,7 @@ class User:
# check if username is already taken
if self.db.fetchUserByUsername(input_username):
print("Username already taken")
return False
# create sig for user
@ -73,3 +74,20 @@ class User:
print('Password updated')
else:
print('Something went wrong while trying to update password..')
def updateAccount(self):
# Get new username
new_username = input('Enter your new username: ')
# check if username is already taken
if self.db.fetchUserByUsername(new_username):
print("Username already taken")
return False
private_key_bytes = Signature.privateKeyToBytes(self.private_key)
if self.db.changeUsername(private_key_bytes, new_username) == True:
print('Username updated')
else:
print('Something went wrong while trying to update username..')

View File

@ -53,7 +53,7 @@ class DatabaseHelper:
def createUser(self, private_key, public_key, username, password):
if not self.conn:
return False
return None
try:
self.cursor.execute("INSERT INTO `users` (private_key, public_key, username, password) VALUES (?, ?, ?, ?)", (private_key, public_key, username, password,))
self.commit()
@ -76,6 +76,37 @@ class DatabaseHelper:
print(error)
return None
def changeUsername(self, user_privatekey, username):
if not self.conn:
return None
# Execute the query
try:
self.cursor.execute("UPDATE `users` SET username = ? WHERE `private_key` = ?", (username, user_privatekey,))
self.commit()
return True
except sqlite3.Error as error:
print(error)
return None
def deleteUser(self, user_privatekey):
# Check if the database is open
if not self.conn:
return None
# Execute the query
try:
self.cursor.execute("DELETE FROM `users` WHERE `private_key` = ?", (user_privatekey, ))
self.commit()
return True
except sqlite3.Error as error:
print(error)
return None
def fetchUserByUsername(self, username):
if not self.conn:
return None

View File

@ -164,7 +164,7 @@ class MenuHelper:
print(user.private_key)
case "Change username":
print("TODO")
user.updateAccount()
case "Change password":
user.updatePassword()