def deleteUser(userBackend: CSVBackend, username: str, yes: bool): """ Deletes a user from the backend. :param userBackend: :param username: :param yes: """ # First ask for confirmation if the server is not running. temp = "\nPlease make sure that the AlertR Server is not running " temp += "while deleting a user.\nOtherwise it can lead to an " temp += "inconsistent state and a corrupted database.\n" temp += "Are you sure to continue?" print(temp) if not userConfirmation(yes): sys.exit(0) if username is None: allUsernames = listUsers(userBackend) while True: userOptionStr = input("Choose number of username to delete: ") userOption = 0 try: userOption = int(userOptionStr) except Exception: pass if 0 < userOption <= len(allUsernames): username = allUsernames[userOption - 1] break else: print("Invalid option. Please retry.") # Check if the username exists. if not userBackend.userExists(username): logging.error("[%s]: Username '%s' does not exist." % (fileName, username)) sys.exit(1) if userBackend.deleteUser(username): userBackend.writeUserdata() logging.info("[%s]: Deleting user successful." % fileName) else: logging.error("[%s]: Deleting user failed." % fileName) sys.exit(1)
updateEmailNotification = (str( configRoot.find("update").find("general").attrib[ "emailNotification"]).upper() == "TRUE") # email notification works only if smtp is activated if (updateEmailNotification is True and smtpActivated is False): raise ValueError("Update check can not have email " + "notification activated when smtp is not activated.") # configure user credentials backend userBackendMethod = str( configRoot.find("storage").find("userBackend").attrib[ "method"]).upper() if userBackendMethod == "CSV": globalData.userBackend = CSVBackend(globalData.userBackendCsvFile) else: raise ValueError("No valid user backend method in config file.") # configure storage backend (check which backend is configured) userBackendMethod = str( configRoot.find("storage").find("storageBackend").attrib[ "method"]).upper() if userBackendMethod == "SQLITE": globalData.storage = Sqlite(globalData.storageBackendSqliteFile, globalData.version) elif userBackendMethod == "MYSQL": backendUsername = str(configRoot.find("storage").find(
def modifyUser(userBackend: CSVBackend, username: str, password: str, nodeType: str, instance: str, yes: bool, updater: Updater): """ Modifies a user in the backend. :param userBackend: :param username: :param password: :param nodeType: :param instance: :param yes: :param updater: """ # First ask for confirmation if the server is not running. temp = "\nPlease make sure that the AlertR Server is not running " temp += "while modifying a user.\nOtherwise it can lead to an " temp += "inconsistent state and a corrupted database.\n" temp += "Are you sure to continue?" print(temp) if not userConfirmation(yes): sys.exit(0) # If we have no username given, ask for one. if username is None: allUsernames = listUsers(userBackend) while True: userOptionStr = input("Choose number of username to modify: ") userOption = 0 try: userOption = int(userOptionStr) except Exception: pass if 0 < userOption <= len(allUsernames): username = allUsernames[userOption - 1] break else: print("Invalid option. Please retry.") # Check if the username exists. if not userBackend.userExists(username): logging.error("[%s]: Username '%s' does not exist." % (fileName, username)) sys.exit(1) # Ask for value to change if none was given via argument. if password is None and nodeType is None and instance is None: print("1. Change password.") print("2. Change instance and node type.") userOption = 0 while True: userOptionStr = input("Please choose an option: ") try: userOption = int(userOptionStr) except Exception: pass if 0 < userOption < 3: break else: print("Invalid option. Please retry.") # Change password. if userOption == 1: while True: pw1 = getpass.getpass("Please enter password:\n") pw2 = getpass.getpass("Please verify password:\n") if pw1 == pw2: password = pw1 break else: print("Passwords do not match. Please retry.") # Change instance and node type. elif userOption == 2: # Give user a list to choose from when we are not in offline mode. if updater is not None: nodeType, instance = chooseNodeTypeAndInstance(updater) # If no node type and instance is chosen until now, ask user. if nodeType not in validNodeTypes: while True: nodeType = input("Please enter node type:\n").lower() if nodeType in validNodeTypes: break else: print( "Only valid node types are: 'sensor', 'manager', 'alert'. Please retry." ) if instance is None: instance = input("Please enter instance:\n") # Change password. if password is not None: if userBackend.changePassword(username, password): userBackend.writeUserdata() logging.info("[%s]: Changing password of user successful." % fileName) else: logging.error("[%s]: Changing password of user failed." % fileName) sys.exit(1) # Change node type and instance. if (nodeType is None or instance is None) and nodeType != instance: logging.error( "[%s]: Node type and instance have to be modified together." % fileName) sys.exit(1) elif nodeType is not None and instance is not None: nodeType = nodeType.lower() if nodeType not in validNodeTypes: logging.error("[%s]: Node type '%s' invalid." % (fileName, nodeType)) sys.exit(1) if userBackend.changeNodeTypeAndInstance(username, nodeType, instance): userBackend.writeUserdata() logging.info( "[%s]: Changing node type and instance of user successful." % fileName) else: logging.error( "[%s]: Changing node type and instance of user failed." % fileName) sys.exit(1)
def addUser(userBackend: CSVBackend, username: str, password: str, nodeType: str, instance: str, yes: bool, updater: Updater): """ Adds a user to the backend. :param userBackend: :param username: :param password: :param nodeType: :param instance: :param yes: :param updater: """ # First ask for confirmation if the server is not running. temp = "\nPlease make sure that the AlertR Server is not running " temp += "while adding a user.\nOtherwise it can lead to an " temp += "inconsistent state and a corrupted database.\n" temp += "Are you sure to continue?" print(temp) if not userConfirmation(yes): sys.exit(0) if username is None: username = input("Please enter username:\n") if password is None: while True: pw1 = getpass.getpass("Please enter password:\n") pw2 = getpass.getpass("Please verify password:\n") if pw1 == pw2: password = pw1 break else: print("Passwords do not match. Please retry.") # If we have no instance given via argument # and we have online repository information, give the user a list # to choose from. if instance is None and updater is not None: nodeType, instance = chooseNodeTypeAndInstance(updater) # If no node type and instance is chosen until now, ask user. if nodeType not in validNodeTypes: while True: nodeType = input("Please enter node type:\n").lower() if nodeType in validNodeTypes: break else: print( "Only valid node types are: 'sensor', 'manager', 'alert'. Please retry." ) if instance is None: instance = input("Please enter instance:\n") if userBackend.addUser(username, password, nodeType, instance): userBackend.writeUserdata() logging.info("[%s]: Adding user successful." % fileName) else: logging.error("[%s]: Adding user failed." % fileName) sys.exit(1)