예제 #1
0
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)
예제 #2
0
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)