diff options
Diffstat (limited to 'gn_auth/auth/authorisation')
| -rw-r--r-- | gn_auth/auth/authorisation/resources/models.py | 19 | ||||
| -rw-r--r-- | gn_auth/auth/authorisation/users/views.py | 3 |
2 files changed, 18 insertions, 4 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__( diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py index dc2c1e4..a706067 100644 --- a/gn_auth/auth/authorisation/users/views.py +++ b/gn_auth/auth/authorisation/users/views.py @@ -328,7 +328,8 @@ def user_resources() -> Response: conn, the_token.user, start_at=int(_request_params.get("start", 0)), - count=int(_request_params.get("length", 0))) + count=int(_request_params.get("length", 0)), + text_filter=_request_params.get("text_filter", "")) return jsonify({ "resources": [asdict(resource) for resource in _resources], "total-records": _total_records, |
