Beispiel #1
0
 def change_administrator_password(self):
     _db = Database(self._logger, False, False, self._env)
     admin_uid = _db.admin_user_id
     if admin_uid > 0:
         new_password = input("New administrator password: "******"admin", new_password)
         except DatabaseException:
             print(
                 "Database exception: unable to reset administrators password."
             )
     else:
         print(
             "Invalid administrator user ID, database could be corrupted.")
         print(
             "Consider creating a new empty database and starting over.\n")
Beispiel #2
0
class ConsoleUserManager(ConsoleMenu):
    MAIN_MENU_CHOICES = [
        "List Users", "Create User", "List Permissions for User",
        "Assign User Permission", "Revoke User Permission", "List Event Types",
        "Reset Password", "Back to Main Menu"
    ]

    def __init__(self, environment, user_id, session_token, use_logger=None):
        self._db = Database(env=environment, logger=use_logger)
        self._session_token = session_token
        self._user_id = user_id

    def main_menu(self):
        print("User Administration Menu\n")
        x = 1
        choices = ConsoleUserManager.MAIN_MENU_CHOICES
        for each in choices:
            print("{0}.) ".format(x) + each)
            x += 1

        choice = self.select_item(1, len(choices) + 1)

        if choice == 1:
            self.list_users()
        elif choice == 2:
            self.create_user()
        elif choice == 3:
            query_user_id = int(input("List permissions for user_id: "))
            self.list_permissions_for_user_id(query_user_id)
        elif choice == 4:
            query_user_id = int(
                input("Assign view-event-id permission for user_id: "))
            query_event_type_id = int(input("Event type id: "))
            self.assign_permission_for_user_id(query_user_id, "view",
                                               query_event_type_id)
        elif choice == 5:
            print("Not yet implemented.")
        elif choice == 6:
            event_types = db.list_event_types()
            print("ID\tEvent Type")
            for each_event in event_types:
                print("{0}\t{1}".format(each_event[0], each_event[1]))
        elif choice == 7:
            username = input("Reset password for user with email/username: "******"New password: "******"\nPress Enter to continue")
        self.main_menu()

    def reset_password(self, email, new_passwd):
        try:
            self._db.reset_password(email, new_passwd)
            print("Password successfully changed.")
        except DatabaseException:
            print("Database Exception: Could not change password.")

    def list_users(self):
        try:
            user_list = self._db.list_analyst_user_info()
            # user_id, email_address, full_name, last_logged_in
            print("User ID\tLogin/E-mail\tFull Name\tLast Logged In\n\n")
            for each_user in user_list:
                print("{0}\t{1}\t{2}\t{3}".format(each_user[0], each_user[1],
                                                  each_user[2], each_user[3]))
            print("\n\n")
        except DatabaseException:
            print("Database error.")

    def create_user(self, email=None, passwd=None, name=None):
        if email is None:
            email = input("E-mail address: ")
        if passwd is None:
            passwd = input("Password: "******"Name: ")

        try:
            new_user = self._db.create_user(email, passwd, name, "console")
            print("Created new user with ID: {0}".format(new_user[0]))
        except DatabaseException:
            print("Could not create a new user.")

    def list_permissions_for_user_id(self, uid):
        try:
            user_permissions = self._db.list_permissions_for_user_id(uid)
            print("ACL ID\tPermission\tEvent Type ID\tCreated\tRevoked\n")
            for each_permission in user_permissions:
                print("{0}\t{1}\t{2}\t{3}\t{4}".format(each_permission[0],
                                                       each_permission[2],
                                                       each_permission[1],
                                                       each_permission[6],
                                                       "NO"))
        except DatabaseException:
            print("Could not create a new user.")

    def assign_permission_for_user_id(self,
                                      uid,
                                      permission,
                                      event_type_id=None):
        if permission is "view-event-type" and event_type_id:
            event_type = None
            all_event_types = self._db.list_event_types()
            for each_event_type in all_event_types:
                if each_event_type[0] == event_type_id:
                    event_type = each_event_type[1]
                    break
            if event_type:
                logged_event_type_id = None
                for each_event_type in all_event_types:
                    if each_event_type[1] is "Add Permission":
                        logged_event_type_id = each_event_type[0]
                        break
                try:
                    event_id = self._db.log_event(
                        {
                            "assigned_by": "Administrator",
                            "ip_addr": "console",
                            "event_type": event_type,
                            "event_type_id": event_type_id
                        }, logged_event_type_id, 0, uid)
                    new_acl_id = self._db.assign_permission_for_user_id(
                        uid, permission, event_id, event_type_id)
                    print("Assigned new permission ID {0} to user_id {1}.".
                          format(new_acl_id, uid))
                except DatabaseException:
                    print("Could not assign a new permission.")

    def revoke_permission_for_user_id(self, uid, permission_id):
        pass