diff options
author | BonfaceKilz | 2021-10-13 13:10:40 +0300 |
---|---|---|
committer | BonfaceKilz | 2021-10-25 14:00:14 +0300 |
commit | 026a0f4e46a1909b16433bf1620f1b778fa6b913 (patch) | |
tree | 636c0f8229c5ea0a2fa0b4f8426eb8ac6f681809 | |
parent | f42d0d5b04f2974e6d93155ef7b936653a9d6248 (diff) | |
download | genenetwork2-026a0f4e46a1909b16433bf1620f1b778fa6b913.tar.gz |
resource_manager: Add `get_user_membership` method
-rw-r--r-- | wqflask/wqflask/resource_manager.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/wqflask/wqflask/resource_manager.py b/wqflask/wqflask/resource_manager.py index 0b1805d8..dfd853b2 100644 --- a/wqflask/wqflask/resource_manager.py +++ b/wqflask/wqflask/resource_manager.py @@ -1,8 +1,10 @@ +import redis import json +from typing import Dict @@ -11,4 +13,31 @@ import json +def get_user_membership(conn: redis.Redis, user_id: str, + group_id: str) -> Dict: + """Return a dictionary that indicates whether the `user_id` is a + member or admin of `group_id`. + + Args: + - conn: a Redis Connection with the responses decoded. + - user_id: a user's unique id + e.g. '8ad942fe-490d-453e-bd37-56f252e41603' + - group_id: a group's unique id + e.g. '7fa95d07-0e2d-4bc5-b47c-448fdc1260b2' + + Returns: + A dict indicating whether the user is an admin or a member of + the group: {"member": True, "admin": False} + + """ + results = {"member": False, "admin": False} + for key, value in conn.hgetall('groups').items(): + if key == group_id: + group_info = json.loads(value) + if user_id in group_info.get("admins"): + results["admin"] = True + if user_id in group_info.get("members"): + results["member"] = True + break + return results admin_status = check_owner_or_admin(resource_id=resource_id) |