Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MySQLdb # Library for connecting to a MySQL Database
- import getpass # This library and any call that starts with 'getpass.' is just so that the input
- # from the keyboard is not displayed back on the terminal.
- #Variables for connecting to the database. The variable names explain their meaning.
- sqlHost = sqlHost
- sqlUser = sqlUser
- sqlPass = sqlPass
- dataB = dataBase
- #This creates a connection object to the database. Basically just saying hello to the database
- # and authenticating with it.
- conn = MySQLdb.connect(host = sqlHost,
- user = sqlUser,
- passwd = sqlPass,
- db = dataB)
- cursor = conn.cursor() # This line creates another object and is rightfully named. If you've worked
- # with databases before you know that you end up with a cursor for a prompt,
- # much like a command line. This is that cursor, you pass it SQL statements
- # and it executes them.
- # Class for simple user management.
- class Admin_Com():
- def newUser():
- userName = raw_input("Enter user Name: ")
- passWord = getpass.getpass("Enter User Password: ")
- # The below command adds a user into the authentication table.
- newUserCommand = 'INSERT into authentication(userName,hash) VALUES(' + "'" + userName +"'" + ',' + "'" + passWord + "'" + ")"
- cursor.execute(newUserCommand)
- main() # Loops back to main for more options.
- def deleteUser():
- print "Enter user name to delete: "
- userName = raw_input()
- # Pretty simple command here as well. Searches the database for the userName from input and
- # Removes that entry.
- deleteUserCommand = "DELETE from authentication WHERE userName = " + "'" + userName + "'"
- cursor.execute(deleteUserCommand)
- print "User deleted successfully."
- main()# Loops back to main for more options.
- def changePass():
- print "Enter user name to change: "
- userName = raw_input()
- passWord = getpass.getpass("Enter new password: ")
- passWord = Hash.md5Hash_hex(passWord)
- # This SQL statement searchs the table for the input userName and replaces the associated
- # password with the new password
- changePassCommand = "UPDATE authentication set hash = " + "'" + passWord + "'" + " where userName = " + "'" + userName + "'"
- cursor.execute(changePassCommand)
- main()# Loops back to main for more options.
- def main():
- userChoice = 0
- userSelection = """1: Add user 2: Delete user
- 3: Change user password 4: Quit"""
- print "User administration for PyMMO"
- print "What would you like to do?"
- print userSelection
- userChoice = raw_input("$: ")
- userChoice = int(userChoice)
- while (userChoice != 4): # Simple while loop to direct the user input from the above choices.
- if (userChoice == 1):
- newUser()
- elif (userChoice == 2):
- deleteUser()
- elif (userChoice == 3):
- changePass()
- exit()
- if __name__ == '__main__':main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement