| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import enum |
| 4 |
import typing as t |
| 5 |
import uuid |
| 6 |
|
| 7 |
from globus_sdk._internal.remarshal import strseq_iter |
| 8 |
from globus_sdk._missing import MISSING, MissingType |
| 9 |
from globus_sdk._payload import GlobusPayload |
| 10 |
|
| 11 |
T = t.TypeVar("T") |
| 12 |
|
| 13 |
|
| 14 |
class GroupRole(enum.Enum): |
| 15 |
member = "member" |
| 16 |
manager = "manager" |
| 17 |
admin = "admin" |
| 18 |
|
| 19 |
|
| 20 |
_GROUP_ROLE_T = t.Union[GroupRole, t.Literal["member", "manager", "admin"]] |
| 21 |
|
| 22 |
|
| 23 |
class GroupMemberVisibility(enum.Enum): |
| 24 |
members = "members" |
| 25 |
managers = "managers" |
| 26 |
|
| 27 |
|
| 28 |
_GROUP_MEMBER_VISIBILITY_T = t.Union[ |
| 29 |
GroupMemberVisibility, t.Literal["members", "managers"] |
| 30 |
] |
| 31 |
|
| 32 |
|
| 33 |
class GroupVisibility(enum.Enum): |
| 34 |
authenticated = "authenticated" |
| 35 |
private = "private" |
| 36 |
|
| 37 |
|
| 38 |
_GROUP_VISIBILITY_T = t.Union[GroupVisibility, t.Literal["authenticated", "private"]] |
| 39 |
|
| 40 |
|
| 41 |
class GroupRequiredSignupFields(enum.Enum): |
| 42 |
institution = "institution" |
| 43 |
current_project_name = "current_project_name" |
| 44 |
address = "address" |
| 45 |
city = "city" |
| 46 |
state = "state" |
| 47 |
country = "country" |
| 48 |
address1 = "address1" |
| 49 |
address2 = "address2" |
| 50 |
zip = "zip" |
| 51 |
phone = "phone" |
| 52 |
department = "department" |
| 53 |
field_of_science = "field_of_science" |
| 54 |
|
| 55 |
|
| 56 |
_GROUP_REQUIRED_SIGNUP_FIELDS_T = t.Union[ |
| 57 |
GroupRequiredSignupFields, |
| 58 |
t.Literal[ |
| 59 |
"institution", |
| 60 |
"current_project_name", |
| 61 |
"address", |
| 62 |
"city", |
| 63 |
"state", |
| 64 |
"country", |
| 65 |
"address1", |
| 66 |
"address2", |
| 67 |
"zip", |
| 68 |
"phone", |
| 69 |
"department", |
| 70 |
"field_of_science", |
| 71 |
], |
| 72 |
] |
| 73 |
|
| 74 |
|
| 75 |
def _typename(obj: t.Any) -> str: |
| 76 |
if isinstance(obj, type) and issubclass(obj, enum.Enum): |
| 77 |
return obj.__name__ |
| 78 |
return str(obj) |
| 79 |
|
| 80 |
|
| 81 |
def _fmt_union(obj: t.Any) -> str: |
| 82 |
return " | ".join(_typename(x) for x in t.get_args(obj)) |
| 83 |
|
| 84 |
|
| 85 |
def _docstring_fixer(cls: type[T]) -> type[T]: |
| 86 |
""" |
| 87 |
These type aliases are not always rendered correctly by sphinx autodoc. |
| 88 |
|
| 89 |
Therefore, we state the types explicitly using a doc modifier, so |
| 90 |
that we can reformat them in a sphinx-friendly way. |
| 91 |
""" |
| 92 |
if cls.__doc__ is not None: |
| 93 |
cls.__doc__ = cls.__doc__.format( |
| 94 |
_GROUP_ROLE_T=_fmt_union(_GROUP_ROLE_T), |
| 95 |
_GROUP_MEMBER_VISIBILITY_T=_fmt_union(_GROUP_MEMBER_VISIBILITY_T), |
| 96 |
_GROUP_VISIBILITY_T=_fmt_union(_GROUP_VISIBILITY_T), |
| 97 |
_GROUP_REQUIRED_SIGNUP_FIELDS_T=_fmt_union(_GROUP_REQUIRED_SIGNUP_FIELDS_T), |
| 98 |
) |
| 99 |
return cls |
| 100 |
|
| 101 |
|
| 102 |
class BatchMembershipActions(GlobusPayload): |
| 103 |
""" |
| 104 |
An object used to represent a batch action on memberships of a group. |
| 105 |
`Perform actions on group members |
| 106 |
<https://groups.api.globus.org/redoc#operation/group_membership_post_actions_v2_groups__group_id__post>`_. |
| 107 |
""" |
| 108 |
|
| 109 |
def accept_invites( |
| 110 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 111 |
) -> BatchMembershipActions: |
| 112 |
""" |
| 113 |
Accept invites for identities. The identities must belong to |
| 114 |
the identity set of authenticated user. |
| 115 |
|
| 116 |
:param identity_ids: The identities for whom to accept invites |
| 117 |
""" |
| 118 |
self.setdefault("accept", []).extend( |
| 119 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 120 |
) |
| 121 |
return self |
| 122 |
|
| 123 |
def add_members( |
| 124 |
self, |
| 125 |
identity_ids: t.Iterable[uuid.UUID | str], |
| 126 |
*, |
| 127 |
role: _GROUP_ROLE_T = "member", |
| 128 |
) -> BatchMembershipActions: |
| 129 |
""" |
| 130 |
Add a list of identities to a group with the given role. |
| 131 |
|
| 132 |
:param identity_ids: The identities to add to the group |
| 133 |
:param role: The role for the new group members |
| 134 |
""" |
| 135 |
self.setdefault("add", []).extend( |
| 136 |
{"identity_id": identity_id, "role": role} |
| 137 |
for identity_id in strseq_iter(identity_ids) |
| 138 |
) |
| 139 |
return self |
| 140 |
|
| 141 |
def approve_pending( |
| 142 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 143 |
) -> BatchMembershipActions: |
| 144 |
""" |
| 145 |
Approve a list of identities with pending join requests. |
| 146 |
|
| 147 |
:param identity_ids: The identities to approve as members of the group |
| 148 |
""" |
| 149 |
self.setdefault("approve", []).extend( |
| 150 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 151 |
) |
| 152 |
return self |
| 153 |
|
| 154 |
def change_roles( |
| 155 |
self, |
| 156 |
role: _GROUP_ROLE_T, |
| 157 |
identity_ids: t.Iterable[uuid.UUID | str], |
| 158 |
) -> BatchMembershipActions: |
| 159 |
""" |
| 160 |
Assign a new role to a list of identities. |
| 161 |
|
| 162 |
:param role: The new role to assign. |
| 163 |
:param identity_ids: The identities to assign to the new role. |
| 164 |
""" |
| 165 |
self.setdefault("change_role", []).extend( |
| 166 |
{"role": role, "identity_id": identity_id} for identity_id in identity_ids |
| 167 |
) |
| 168 |
return self |
| 169 |
|
| 170 |
def decline_invites( |
| 171 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 172 |
) -> BatchMembershipActions: |
| 173 |
""" |
| 174 |
Decline an invitation for a given set of identities. |
| 175 |
|
| 176 |
:param identity_ids: The identities for whom invitations should be declined |
| 177 |
""" |
| 178 |
self.setdefault("decline", []).extend( |
| 179 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 180 |
) |
| 181 |
return self |
| 182 |
|
| 183 |
def invite_members( |
| 184 |
self, |
| 185 |
identity_ids: t.Iterable[uuid.UUID | str], |
| 186 |
*, |
| 187 |
role: _GROUP_ROLE_T = "member", |
| 188 |
) -> BatchMembershipActions: |
| 189 |
""" |
| 190 |
Invite a list of identities to a group with the given role. |
| 191 |
|
| 192 |
:param identity_ids: The identities to invite to the group |
| 193 |
:param role: The role for the invited group members |
| 194 |
""" |
| 195 |
self.setdefault("invite", []).extend( |
| 196 |
{"identity_id": identity_id, "role": role} |
| 197 |
for identity_id in strseq_iter(identity_ids) |
| 198 |
) |
| 199 |
return self |
| 200 |
|
| 201 |
def join(self, identity_ids: t.Iterable[uuid.UUID | str]) -> BatchMembershipActions: |
| 202 |
""" |
| 203 |
Join a group with the given identities. The identities must be in the |
| 204 |
authenticated users identity set. |
| 205 |
|
| 206 |
:param identity_ids: The identities to use to join the group |
| 207 |
""" |
| 208 |
self.setdefault("join", []).extend( |
| 209 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 210 |
) |
| 211 |
return self |
| 212 |
|
| 213 |
def leave( |
| 214 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 215 |
) -> BatchMembershipActions: |
| 216 |
""" |
| 217 |
Leave a group that one of the identities in the authenticated user's |
| 218 |
identity set is a member of. |
| 219 |
|
| 220 |
:param identity_ids: The identities to remove from the group |
| 221 |
""" |
| 222 |
self.setdefault("leave", []).extend( |
| 223 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 224 |
) |
| 225 |
return self |
| 226 |
|
| 227 |
def reject_join_requests( |
| 228 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 229 |
) -> BatchMembershipActions: |
| 230 |
""" |
| 231 |
Reject identities which have requested to join the group. |
| 232 |
|
| 233 |
:param identity_ids: The identities to reject from the group |
| 234 |
""" |
| 235 |
self.setdefault("reject", []).extend( |
| 236 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 237 |
) |
| 238 |
return self |
| 239 |
|
| 240 |
def remove_members( |
| 241 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 242 |
) -> BatchMembershipActions: |
| 243 |
""" |
| 244 |
Remove members from a group. This must be done as an admin or manager |
| 245 |
of the group. |
| 246 |
|
| 247 |
:param identity_ids: The identities to remove from the group |
| 248 |
""" |
| 249 |
self.setdefault("remove", []).extend( |
| 250 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 251 |
) |
| 252 |
return self |
| 253 |
|
| 254 |
def request_join( |
| 255 |
self, identity_ids: t.Iterable[uuid.UUID | str] |
| 256 |
) -> BatchMembershipActions: |
| 257 |
""" |
| 258 |
Request to join a group. |
| 259 |
|
| 260 |
:param identity_ids: The identities to use to request membership in the group |
| 261 |
""" |
| 262 |
self.setdefault("request_join", []).extend( |
| 263 |
{"identity_id": identity_id} for identity_id in strseq_iter(identity_ids) |
| 264 |
) |
| 265 |
return self |
| 266 |
|
| 267 |
|
| 268 |
@_docstring_fixer |
| 269 |
class GroupPolicies(GlobusPayload): |
| 270 |
""" |
| 271 |
An object used to represent the policy settings of a group. |
| 272 |
This may be used to set or modify group settings. |
| 273 |
|
| 274 |
See also: |
| 275 |
`API documentation on setting the policies for the group. \ |
| 276 |
<https://groups.api.globus.org/redoc#operation/update_policies_v2_groups__group_id__policies_put>`_ |
| 277 |
|
| 278 |
:param is_high_assurance: Whether the group is high assurance or not |
| 279 |
:param group_visibility: The visibility of the group |
| 280 |
:type group_visibility: {_GROUP_VISIBILITY_T} |
| 281 |
:param group_members_visibility: The visibility of the group members |
| 282 |
:type group_members_visibility: {_GROUP_MEMBER_VISIBILITY_T} |
| 283 |
:param join_requests: Whether the group allows join requests or not |
| 284 |
:param signup_fields: The fields required for signup in the group |
| 285 |
:type signup_fields: typing.Iterable[{_GROUP_REQUIRED_SIGNUP_FIELDS_T}] |
| 286 |
:param authentication_assurance_timeout: The session timeout for high assurance |
| 287 |
group policy enforcement |
| 288 |
""" |
| 289 |
|
| 290 |
def __init__( |
| 291 |
self, |
| 292 |
*, |
| 293 |
is_high_assurance: bool, |
| 294 |
group_visibility: _GROUP_VISIBILITY_T, |
| 295 |
group_members_visibility: _GROUP_MEMBER_VISIBILITY_T, |
| 296 |
join_requests: bool, |
| 297 |
signup_fields: t.Iterable[_GROUP_REQUIRED_SIGNUP_FIELDS_T], |
| 298 |
authentication_assurance_timeout: int | None | MissingType = MISSING, |
| 299 |
) -> None: |
| 300 |
super().__init__() |
| 301 |
self["is_high_assurance"] = is_high_assurance |
| 302 |
self["group_visibility"] = group_visibility |
| 303 |
self["group_members_visibility"] = group_members_visibility |
| 304 |
self["join_requests"] = join_requests |
| 305 |
self["signup_fields"] = list(signup_fields) |
| 306 |
self["authentication_assurance_timeout"] = authentication_assurance_timeout |