about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/resources
diff options
context:
space:
mode:
Diffstat (limited to 'gn_auth/auth/authorisation/resources')
-rw-r--r--gn_auth/auth/authorisation/resources/models.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/gn_auth/auth/authorisation/resources/models.py b/gn_auth/auth/authorisation/resources/models.py
index f0647c3..8c9abc7 100644
--- a/gn_auth/auth/authorisation/resources/models.py
+++ b/gn_auth/auth/authorisation/resources/models.py
@@ -189,20 +189,24 @@ def user_resources(
         conn: db.DbConnection,
         user: User,
         start_at: int = 0,
-        count: int = 0
+        count: int = 0,
+        text_filter: str = ""
 ) -> tuple[Sequence[Resource], int]:
     """List the resources available to the user"""
+    text_filter = text_filter.strip()
     query_template = ("SELECT %%COLUMNS%%  "
              "FROM user_roles AS ur "
              "INNER JOIN resources AS r ON ur.resource_id=r.resource_id "
              "INNER JOIN resource_categories AS rc "
              "ON r.resource_category_id=rc.resource_category_id "
-             "WHERE ur.user_id=? %%LIMITS%%")
+             "WHERE ur.user_id=? %%LIKE%% %%LIMITS%%")
     with db.cursor(conn) as cursor:
         cursor.execute(
             query_template.replace(
                 "%%COLUMNS%%", "COUNT(DISTINCT(r.resource_id)) AS count"
             ).replace(
+                "%%LIKE%%", ""
+            ).replace(
                 "%%LIMITS%%", ""),
             (str(user.user_id),))
         _total_records = int(cursor.fetchone()["count"])
@@ -213,9 +217,18 @@ def user_resources(
                 "r.resource_category_id, r.public, r.created_by, r.created_at, "
                 "rc.resource_category_key, rc.resource_category_description"
             ).replace(
+                "%%LIKE%%",
+                ("" if text_filter == "" else (
+                    "AND (r.resource_name LIKE ? OR "
+                    "rc.resource_category_key LIKE ? OR "
+                    "rc.resource_category_description LIKE ? )"))
+            ).replace(
                 "%%LIMITS%%",
                 ("" if count <= 0 else f"LIMIT {count} OFFSET {start_at}")),
-            (str(user.user_id),))
+            (str(user.user_id),) + (
+                tuple() if text_filter == "" else
+                tuple(f"%{text_filter}%" for _ in range(0, 3))
+            ))
         rows = cursor.fetchall() or []
 
         _creators_ = __fetch_creators__(