2021-09-22 18:29:59 +02:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
from posthog.models.utils import UUIDModel, sane_repr
|
2023-11-02 18:14:23 +01:00
|
|
|
from posthog.models.organization import OrganizationMembership
|
2021-09-22 18:29:59 +02:00
|
|
|
|
|
|
|
|
2022-02-02 17:16:35 +01:00
|
|
|
# We call models that grant a user access to some grouping of users a "membership"
|
2021-09-22 18:29:59 +02:00
|
|
|
class ExplicitTeamMembership(UUIDModel):
|
|
|
|
class Level(models.IntegerChoices):
|
|
|
|
"""Keep in sync with OrganizationMembership.Level (only difference being organizations having an Owner)."""
|
|
|
|
|
|
|
|
MEMBER = 1, "member"
|
|
|
|
ADMIN = 8, "administrator"
|
|
|
|
|
2024-08-22 11:42:25 +02:00
|
|
|
team = models.ForeignKey(
|
2021-09-22 18:29:59 +02:00
|
|
|
"posthog.Team",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="explicit_memberships",
|
|
|
|
related_query_name="explicit_membership",
|
|
|
|
)
|
2024-08-22 11:42:25 +02:00
|
|
|
parent_membership = models.ForeignKey(
|
2021-09-22 18:29:59 +02:00
|
|
|
"posthog.OrganizationMembership",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="explicit_team_memberships",
|
|
|
|
related_query_name="explicit_team_membership",
|
|
|
|
)
|
2024-08-22 11:42:25 +02:00
|
|
|
level = models.PositiveSmallIntegerField(default=Level.MEMBER, choices=Level.choices)
|
|
|
|
joined_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated_at = models.DateTimeField(auto_now=True)
|
2021-09-22 18:29:59 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
2023-10-26 12:38:15 +02:00
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=["team", "parent_membership"],
|
|
|
|
name="unique_explicit_team_membership",
|
|
|
|
)
|
2021-09-22 18:29:59 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.Level(self.level))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def effective_level(self) -> "OrganizationMembership.Level":
|
2022-09-05 14:38:54 +02:00
|
|
|
"""If organization level is higher than project level, then that takes precedence over explicit project level."""
|
2021-09-22 18:29:59 +02:00
|
|
|
return max(self.level, self.parent_membership.level)
|
|
|
|
|
|
|
|
__repr__ = sane_repr("team", "parent_membership", "level")
|