/
role.py
85 lines (67 loc) · 2.38 KB
/
role.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from typing import Any, List, Mapping, Optional
from typing_extensions import TypedDict
from sentry import features
from sentry.api.serializers import Serializer
from sentry.models.user import User
from sentry.roles.manager import OrganizationRole, Role, TeamRole
class BaseRoleSerializerResponse(TypedDict):
id: str
name: str
desc: str
scopes: List[str]
allowed: bool
isAllowed: bool
isRetired: bool
class OrganizationRoleSerializerResponse(BaseRoleSerializerResponse):
is_global: bool
isGlobal: bool
minimumTeamRole: str
class TeamRoleSerializerResponse(BaseRoleSerializerResponse):
isMinimumRoleFor: Optional[str]
class RoleSerializer(Serializer):
def __init__(self, **kwargs):
"""
Remove this when deleting "organization:team-roles" flag
"""
self.organization = kwargs["organization"]
def serialize(
self,
obj: Role,
attrs: Mapping[str, Any],
user: User,
**kwargs: Any,
) -> BaseRoleSerializerResponse:
has_team_roles = features.has("organizations:team-roles", self.organization)
is_retired_role = has_team_roles and obj.is_retired
allowed_roles = kwargs.get("allowed_roles") or ()
return {
"id": str(obj.id),
"name": obj.name,
"desc": obj.desc,
"scopes": obj.scopes,
"allowed": obj in allowed_roles, # backward compatibility
"isAllowed": obj in allowed_roles,
"isRetired": is_retired_role,
}
class OrganizationRoleSerializer(RoleSerializer):
def serialize(
self, obj: OrganizationRole, attrs, user, **kwargs
) -> OrganizationRoleSerializerResponse:
serialized = super().serialize(obj, attrs, user, **kwargs)
serialized.update(
{
"is_global": obj.is_global, # backward compatibility
"isGlobal": obj.is_global,
"minimumTeamRole": obj.get_minimum_team_role().id,
}
)
return serialized
class TeamRoleSerializer(RoleSerializer):
def serialize(self, obj: TeamRole, attrs, user, **kwargs) -> TeamRoleSerializerResponse:
serialized = super().serialize(obj, attrs, user, **kwargs)
serialized.update(
{
"isMinimumRoleFor": obj.is_minimum_role_for,
}
)
return serialized