From 9ef9e8f1da2b3e1e9364fa64b2944aea63654328 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Mon, 18 Sep 2023 10:43:04 +0200 Subject: [PATCH] Add auth data to DB --- IM/InfrastructureList.py | 66 ++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/IM/InfrastructureList.py b/IM/InfrastructureList.py index edc11a481..f02a56b6b 100644 --- a/IM/InfrastructureList.py +++ b/IM/InfrastructureList.py @@ -63,7 +63,7 @@ def get_inf_ids(auth=None): if auth: # In this case only loads the auth data to improve performance inf_ids = [] - for inf_id in InfrastructureList._get_inf_ids_from_db(): + for inf_id in InfrastructureList._get_inf_ids_from_db(auth): inf = None # Check if the infrastructure is in memory if (inf_id in InfrastructureList.infrastructure_list and @@ -153,10 +153,11 @@ def init_table(): InfrastructureList.logger.debug("Creating the IM database!.") if db.db_type == DataBase.MYSQL: db.execute("CREATE TABLE inf_list(rowid INTEGER NOT NULL AUTO_INCREMENT UNIQUE," - " id VARCHAR(255) PRIMARY KEY, deleted INTEGER, date TIMESTAMP, data LONGBLOB)") + " id VARCHAR(255) PRIMARY KEY, deleted INTEGER, date TIMESTAMP, data LONGBLOB," + " auth LONGBLOB)") elif db.db_type == DataBase.SQLITE: db.execute("CREATE TABLE inf_list(id VARCHAR(255) PRIMARY KEY, deleted INTEGER," - " date TIMESTAMP, data LONGBLOB)") + " date TIMESTAMP, data LONGBLOB, auth LONGBLOB)") db.close() return True else: @@ -175,25 +176,30 @@ def _get_data_from_db(db_url, inf_id=None, auth=None): db = DataBase(db_url) if db.connect(): inf_list = {} + data_field = "data" + if auth: + data_field = "auth" if inf_id: if db.db_type == DataBase.MONGO: - res = db.find("inf_list", {"id": inf_id}, {"data": True}) + res = db.find("inf_list", {"id": inf_id}, {data_field: True, "deleted": True}) else: - res = db.select("select data from inf_list where id = %s", (inf_id,)) + res = db.select("select " + data_field + ",deleted from inf_list where id = %s", (inf_id,)) else: if db.db_type == DataBase.MONGO: - res = db.find("inf_list", {"deleted": 0}, {"data": True}, [('_id', -1)]) + res = db.find("inf_list", {"deleted": 0}, {data_field: True, "deleted": True}, [('_id', -1)]) else: - res = db.select("select data from inf_list where deleted = 0 order by rowid desc") + res = db.select("select " + data_field + ",deleted from inf_list where deleted = 0 order by rowid desc") if len(res) > 0: for elem in res: if db.db_type == DataBase.MONGO: - data = elem['data'] + data = elem[data_field] + deleted = elem["deleted"] else: data = elem[0] + deleted = elem[1] try: if auth: - inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth(data) + inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize_auth(inf_id, deleted, data) else: inf = IM.InfrastructureInfo.InfrastructureInfo.deserialize(data) inf_list[inf.id] = inf @@ -239,15 +245,51 @@ def _save_data_to_db(db_url, inf_list, inf_id=None): return None @staticmethod - def _get_inf_ids_from_db(): + def _gen_where_from_auth(auth, get_all): + like = "" + if auth: + for elem in auth.getAuthInfo('InfrastructureManager'): + if elem.get("username"): + if like: + like += " and " + like += "auth like '%%" + elem.get("username") + "%%'" + + if like: + return "where deleted = 0 and auth is null or (" + like + ")" + elif get_all: + return "" + else: + return "where deleted = 0" + + @staticmethod + def _gen_filter_from_auth(auth, get_all): + like = "" + if auth: + for elem in auth.getAuthInfo('InfrastructureManager'): + if elem.get("username"): + if like: + like += "|" + like += elem.get("username") + + if like: + return {"deleted": 0, "auth": {"$regex": like}} + elif get_all: + return {} + else: + return {"deleted": 0} + + @staticmethod + def _get_inf_ids_from_db(auth=None, get_all=False): try: db = DataBase(Config.DATA_DB) if db.connect(): inf_list = [] if db.db_type == DataBase.MONGO: - res = db.find("inf_list", {"deleted": 0}, {"id": True}, [('id', -1)]) + filter = InfrastructureList._gen_filter_from_auth(auth, get_all) + res = db.find("inf_list", filter, {"id": True}, [('id', -1)]) else: - res = db.select("select id from inf_list where deleted = 0 order by rowid desc") + where = InfrastructureList._gen_where_from_auth(auth, get_all) + res = db.select("select id from inf_list %s order by rowid desc" % where) for elem in res: if db.db_type == DataBase.MONGO: inf_list.append(elem['id'])