diff options
| author | Frederick Muriuki Muriithi | 2026-02-10 11:40:55 -0600 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-02-10 11:43:42 -0600 |
| commit | 95e2c62795441f6361daa086f72917b6606eb68a (patch) | |
| tree | 8d4415198231bdb2a25d9edbf62bb8a9bc714385 | |
| parent | 2fe5d90776edd1bc62a6eeaa492d3efb2974b158 (diff) | |
| download | gn-auth-95e2c62795441f6361daa086f72917b6606eb68a.tar.gz | |
Authorisation Check: New function to check user has view access.
| -rw-r--r-- | gn_auth/auth/authorisation/resources/checks.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/resources/checks.py b/gn_auth/auth/authorisation/resources/checks.py index 225468c..80e7f5b 100644 --- a/gn_auth/auth/authorisation/resources/checks.py +++ b/gn_auth/auth/authorisation/resources/checks.py @@ -147,3 +147,33 @@ def can_delete( user_id, system_resource(conn).resource_id, "(AND system:system-wide:data:delete)")) + + +def can_view( + conn: authdb.DbConnection, + user_id: uuid.UUID, + resource_id: uuid.UUID +) -> bool: + """Check whether user is allowed view a resource and/or its data.""" + with authdb.cursor(conn) as cursor: + cursor.execute("SELECT public FROM resources WHERE resource_id=?", + (str(resource_id),)) + row = cursor.fetchone() + is_public = bool(row) and bool(int(row["public"])) + + return ( + is_public# The resource is public, everyone can view! + or + authorised_for_spec( + # resource-level view access: user has view access to his resource. + conn, + user_id, + resource_id, + "(OR group:resource:view-resource system:resource:view)") + or + authorised_for_spec( + # system-wide view access: user can view any/all resource(s). + conn, + user_id, + system_resource(conn).resource_id, + "(OR system:system-wide:data:view system:resource:view)")) |
