|
| 1 | +""" |
| 2 | +user.py |
| 3 | +
|
| 4 | +Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. |
| 5 | +Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch |
| 6 | +Last updated: 08/12/2025 |
| 7 | +""" |
| 8 | + |
| 9 | +import logging |
| 10 | +import cs3.identity.group.v1beta1.resources_pb2 as cs3igr |
| 11 | +import cs3.identity.group.v1beta1.group_api_pb2 as cs3ig |
| 12 | +import cs3.identity.user.v1beta1.resources_pb2 as cs3iur |
| 13 | +from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub |
| 14 | + |
| 15 | +from .config import Config |
| 16 | +from .statuscodehandler import StatusCodeHandler |
| 17 | + |
| 18 | + |
| 19 | +class Group: |
| 20 | + """ |
| 21 | + Group class to handle group related API calls with CS3 Gateway API. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + config: Config, |
| 27 | + log: logging.Logger, |
| 28 | + gateway: GatewayAPIStub, |
| 29 | + status_code_handler: StatusCodeHandler, |
| 30 | + ) -> None: |
| 31 | + """ |
| 32 | + Initializes the Group class with logger, auth, and gateway stub, |
| 33 | +
|
| 34 | + :param log: Logger instance for logging. |
| 35 | + :param gateway: GatewayAPIStub instance for interacting with CS3 Gateway. |
| 36 | + :param auth: An instance of the auth class. |
| 37 | + """ |
| 38 | + self._log: logging.Logger = log |
| 39 | + self._gateway: GatewayAPIStub = gateway |
| 40 | + self._config: Config = config |
| 41 | + self._status_code_handler: StatusCodeHandler = status_code_handler |
| 42 | + |
| 43 | + def get_group(self, opaque_id, idp) -> cs3igr.Group: |
| 44 | + """ |
| 45 | + Get the group information provided the opaque_id. |
| 46 | +
|
| 47 | + :param opaque_id: Opaque group id. |
| 48 | + :return: Group information. |
| 49 | + :raises: return NotFoundException (Group not found) |
| 50 | + :raises: AuthenticationException (Operation not permitted) |
| 51 | + :raises: UnknownException (Unknown error) |
| 52 | + """ |
| 53 | + req = cs3ig.GetGroupRequest(group_id=cs3igr.GroupId(opaque_id=opaque_id, idp=idp)) |
| 54 | + res = self._gateway.GetGroup(request=req) |
| 55 | + self._status_code_handler.handle_errors(res.status, "get group", f'opaque_id="{opaque_id}"') |
| 56 | + self._log.debug(f'msg="Invoked GetGroup" opaque_id="{res.group.id.opaque_id}" trace="{res.status.trace}"') |
| 57 | + return res.group |
| 58 | + |
| 59 | + def get_group_by_claim(self, claim, value) -> cs3igr.Group: |
| 60 | + """ |
| 61 | + Get the group information provided the claim and value. |
| 62 | +
|
| 63 | + :param claim: Claim to search for. |
| 64 | + :param value: Value to search for. |
| 65 | + :return: Group information. |
| 66 | + :raises: NotFoundException (Group not found) |
| 67 | + :raises: AuthenticationException (Operation not permitted) |
| 68 | + :raises: UnknownException (Unknown error) |
| 69 | + """ |
| 70 | + req = cs3ig.GetGroupByClaimRequest(claim=claim, value=value, skip_fetching_members=False) |
| 71 | + res = self._gateway.GetGroupByClaim(request=req) |
| 72 | + self._status_code_handler.handle_errors(res.status, "get group by claim", f'claim="{claim}" value="{value}"') |
| 73 | + self._log.debug(f'msg="Invoked GetGroupByClaim" opaque_id="{res.group.id.opaque_id}" trace="{res.status.trace}"') |
| 74 | + return res.group |
| 75 | + |
| 76 | + def has_member(self, group_opaque_id, user_opaque_id, idp) -> bool: |
| 77 | + """ |
| 78 | + Check if a user is a member of a group. |
| 79 | +
|
| 80 | + :param group_opaque_id: Group opaque id. |
| 81 | + :param user_opaque_id: User opaque id. |
| 82 | + :return: True if the user is a member of the group, False otherwise. |
| 83 | + :raises: NotFoundException (Group not found) |
| 84 | + :raises: AuthenticationException (Operation not permitted) |
| 85 | + :raises: UnknownException (Unknown error) |
| 86 | + """ |
| 87 | + req = cs3ig.HasMemberRequest(group_id=cs3igr.GroupId(opaque_id=group_opaque_id, idp=idp), user_id=cs3iur.UserId(opaque_id=user_opaque_id, idp=idp)) |
| 88 | + res = self._gateway.HasMember(request=req) |
| 89 | + self._status_code_handler.handle_errors(res.status, "has member", f'group_id="{group_opaque_id}" user_id="{user_opaque_id}"') |
| 90 | + self._log.debug(f'msg="Invoked HasMember" group_id="{group_opaque_id}" user_id="{user_opaque_id}" trace="{res.status.trace}"') |
| 91 | + return res.ok |
| 92 | + |
| 93 | + def get_members(self, opaque_id, idp) -> list[str]: |
| 94 | + """ |
| 95 | + Get the groups the user is a part of. |
| 96 | +
|
| 97 | + :param opaque_id: Opaque group id. |
| 98 | + :return: A list of the groups the user is part of. |
| 99 | + :raises: NotFoundException (User not found) |
| 100 | + :raises: AuthenticationException (Operation not permitted) |
| 101 | + :raises: UnknownException (Unknown error) |
| 102 | + """ |
| 103 | + req = cs3ig.GetMembersRequest(group_id=cs3igr.GroupId(idp=idp, opaque_id=opaque_id)) |
| 104 | + res = self._gateway.GetUserGroups(request=req) |
| 105 | + self._status_code_handler.handle_errors(res.status, "get user groups", f'opaque_id="{opaque_id}"') |
| 106 | + self._log.debug(f'msg="Invoked GetUserGroups" opaque_id="{opaque_id}" trace="{res.status.trace}"') |
| 107 | + return res.groups |
| 108 | + |
| 109 | + def find_groups(self, auth_token: tuple, filters) -> list[cs3igr.Group]: |
| 110 | + """ |
| 111 | + Find a group based on a filter. |
| 112 | +
|
| 113 | + :param auth_token: tuple in the form ('x-access-token', <token>) (see auth.get_token/auth.check_token) |
| 114 | + :param filters: Filters to search for. |
| 115 | + :return: a list of group(s). |
| 116 | + :raises: NotFoundException (Group not found) |
| 117 | + :raises: AuthenticationException (Operation not permitted) |
| 118 | + :raises: UnknownException (Unknown error) |
| 119 | + """ |
| 120 | + req = cs3ig.FindGroupsRequest(filters=filters) |
| 121 | + res = self._gateway.FindGroups(request=req, metadata=[auth_token]) |
| 122 | + self._status_code_handler.handle_errors(res.status, "find groups") |
| 123 | + self._log.debug(f'msg="Invoked FindGroups" filter="{filter}" trace="{res.status.trace}"') |
| 124 | + return res.groups |
0 commit comments