Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. import MySQLdb # Library for connecting to a MySQL Database
  2. import getpass # This library and any call that starts with 'getpass.' is  just so that the input
  3.                # from the keyboard is not displayed back on the terminal.
  4. #Variables for connecting to the database. The variable names explain their meaning.
  5. sqlHost = sqlHost
  6. sqlUser = sqlUser
  7. sqlPass = sqlPass
  8. dataB = dataBase
  9.  
  10. #This creates a connection object to the database. Basically just saying hello to the database
  11. # and authenticating with it.
  12. conn = MySQLdb.connect(host = sqlHost,
  13.                        user = sqlUser,
  14.                       passwd = sqlPass,
  15.                       db = dataB)
  16. cursor = conn.cursor() # This line creates another object and is rightfully named. If you've worked
  17.                        # with databases before you know that you end up with a cursor for a prompt,
  18.                        # much like a command line. This is that cursor, you pass it SQL statements
  19.                        # and it executes them.
  20.  
  21. # Class for simple user management.
  22. class Admin_Com():
  23.     def newUser():
  24.         userName = raw_input("Enter user Name: ")
  25.  
  26.         passWord = getpass.getpass("Enter User Password: ")
  27.  
  28.         # The below command adds a user into the authentication table.
  29.         newUserCommand = 'INSERT into authentication(userName,hash) VALUES(' + "'" + userName +"'" + ',' + "'" + passWord + "'" + ")"
  30.         cursor.execute(newUserCommand)
  31.         main() # Loops back to main for more options.
  32.  
  33.     def deleteUser():
  34.         print "Enter user name to delete: "
  35.         userName = raw_input()
  36.        
  37.         # Pretty simple command here as well. Searches the database for the userName from input and
  38.         # Removes that entry.
  39.         deleteUserCommand = "DELETE from authentication WHERE userName = " + "'" + userName + "'"
  40.         cursor.execute(deleteUserCommand)
  41.         print "User deleted successfully."
  42.         main()# Loops back to main for more options.
  43.  
  44.     def changePass():
  45.         print "Enter user name to change: "
  46.         userName = raw_input()
  47.    
  48.         passWord = getpass.getpass("Enter new password: ")
  49.         passWord = Hash.md5Hash_hex(passWord)
  50.    
  51.         # This SQL statement searchs the table for the input userName and replaces the associated
  52.         # password with the new password
  53.         changePassCommand = "UPDATE authentication set hash = " + "'" + passWord + "'" + " where userName = " + "'" + userName + "'"
  54.         cursor.execute(changePassCommand)
  55.         main()# Loops back to main for more options.
  56.  
  57.    
  58. def main():
  59.     userChoice = 0
  60.     userSelection = """1: Add user                 2: Delete user
  61. 3: Change user password     4: Quit"""
  62.  
  63.     print "User administration for PyMMO"
  64.     print "What would you like to do?"
  65.     print userSelection
  66.     userChoice = raw_input("$: ")
  67.     userChoice = int(userChoice)
  68.     while (userChoice != 4): # Simple while loop to direct the user input from the above choices.
  69.         if (userChoice == 1):
  70.             newUser()
  71.         elif (userChoice == 2):
  72.             deleteUser()
  73.         elif (userChoice == 3):
  74.             changePass()
  75.     exit()
  76. if __name__ == '__main__':main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement