models.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. from django.db import models
  2. from django.utils.text import slugify
  3. from django.utils.translation import gettext_lazy as _
  4. from organizations.base import (
  5. OrganizationBase,
  6. OrganizationUserBase,
  7. OrganizationOwnerBase,
  8. OrganizationInvitationBase,
  9. )
  10. from organizations.abstract import SharedBaseModel
  11. from organizations.fields import SlugField
  12. from organizations.signals import user_added
  13. # Defines which scopes belong to which role
  14. # Credit to sentry/conf/server.py
  15. ROLES = (
  16. {
  17. "id": "member",
  18. "name": "Member",
  19. "desc": "Members can view and act on events, as well as view most other data within the organization.",
  20. "scopes": set(
  21. [
  22. "event:read",
  23. "event:write",
  24. "event:admin",
  25. "project:releases",
  26. "project:read",
  27. "org:read",
  28. "member:read",
  29. "team:read",
  30. ]
  31. ),
  32. },
  33. {
  34. "id": "admin",
  35. "name": "Admin",
  36. "desc": "Admin privileges on any teams of which they're a member. They can create new teams and projects, as well as remove teams and projects which they already hold membership on (or all teams, if open membership is on). Additionally, they can manage memberships of teams that they are members of.",
  37. "scopes": set(
  38. [
  39. "event:read",
  40. "event:write",
  41. "event:admin",
  42. "org:read",
  43. "member:read",
  44. "project:read",
  45. "project:write",
  46. "project:admin",
  47. "project:releases",
  48. "team:read",
  49. "team:write",
  50. "team:admin",
  51. "org:integrations",
  52. ]
  53. ),
  54. },
  55. {
  56. "id": "manager",
  57. "name": "Manager",
  58. "desc": "Gains admin access on all teams as well as the ability to add and remove members.",
  59. "is_global": True,
  60. "scopes": set(
  61. [
  62. "event:read",
  63. "event:write",
  64. "event:admin",
  65. "member:read",
  66. "member:write",
  67. "member:admin",
  68. "project:read",
  69. "project:write",
  70. "project:admin",
  71. "project:releases",
  72. "team:read",
  73. "team:write",
  74. "team:admin",
  75. "org:read",
  76. "org:write",
  77. "org:integrations",
  78. ]
  79. ),
  80. },
  81. {
  82. "id": "owner",
  83. "name": "Organization Owner",
  84. "desc": "Unrestricted access to the organization, its data, and its settings. Can add, modify, and delete projects and members, as well as make billing and plan changes.",
  85. "is_global": True,
  86. "scopes": set(
  87. [
  88. "org:read",
  89. "org:write",
  90. "org:admin",
  91. "org:integrations",
  92. "member:read",
  93. "member:write",
  94. "member:admin",
  95. "team:read",
  96. "team:write",
  97. "team:admin",
  98. "project:read",
  99. "project:write",
  100. "project:admin",
  101. "project:releases",
  102. "event:read",
  103. "event:write",
  104. "event:admin",
  105. ]
  106. ),
  107. },
  108. )
  109. class OrganizationUserRole(models.IntegerChoices):
  110. MEMBER = 0, "Member"
  111. ADMIN = 1, "Admin"
  112. MANAGER = 2, "Manager"
  113. OWNER = 3, "Owner" # Many users can be owner but only one primary owner
  114. @classmethod
  115. def from_string(cls, string: str):
  116. for status in cls:
  117. if status.label.lower() == string.lower():
  118. return status
  119. @classmethod
  120. def get_role(cls, role: int):
  121. return ROLES[role]
  122. class Organization(SharedBaseModel, OrganizationBase):
  123. slug = SlugField(
  124. max_length=200,
  125. blank=False,
  126. editable=True,
  127. populate_from="name",
  128. unique=True,
  129. help_text=_("The name in all lowercase, suitable for URL identification"),
  130. )
  131. is_accepting_events = models.BooleanField(
  132. default=True, help_text="Used for throttling at org level"
  133. )
  134. open_membership = models.BooleanField(
  135. default=True, help_text="Allow any organization member to join any team"
  136. )
  137. scrub_ip_addresses = models.BooleanField(
  138. default=True,
  139. help_text="Default for whether projects should script IP Addresses",
  140. )
  141. def slugify_function(self, content):
  142. reserved_words = [
  143. "login",
  144. "register",
  145. "app",
  146. "profile",
  147. "organizations",
  148. "settings",
  149. "issues",
  150. "performance",
  151. "_health",
  152. "rest-auth",
  153. "api",
  154. "accept",
  155. "stripe",
  156. "admin",
  157. "__debug__",
  158. ]
  159. slug = slugify(content)
  160. if slug in reserved_words:
  161. return slug + "-1"
  162. return slug
  163. def add_user(self, user, role=OrganizationUserRole.MEMBER):
  164. """
  165. Adds a new user and if the first user makes the user an admin and
  166. the owner.
  167. """
  168. users_count = self.users.all().count()
  169. if users_count == 0:
  170. role = OrganizationUserRole.OWNER
  171. org_user = self._org_user_model.objects.create(
  172. user=user, organization=self, role=role
  173. )
  174. if users_count == 0:
  175. self._org_owner_model.objects.create(
  176. organization=self, organization_user=org_user
  177. )
  178. # User added signal
  179. user_added.send(sender=self, user=user)
  180. return org_user
  181. @property
  182. def owners(self):
  183. return self.users.filter(
  184. organizations_ext_organizationuser__role=OrganizationUserRole.OWNER
  185. )
  186. @property
  187. def email(self):
  188. """ Used to identify billing contact for stripe. """
  189. billing_contact = self.owner.organization_user.user
  190. return billing_contact.email
  191. def get_user_scopes(self, user):
  192. org_user = self.organization_users.get(user=user)
  193. return org_user.get_scopes()
  194. class OrganizationUser(SharedBaseModel, OrganizationUserBase):
  195. user = models.ForeignKey(
  196. "users.User",
  197. blank=True,
  198. null=True,
  199. on_delete=models.CASCADE,
  200. related_name="organizations_ext_organizationuser",
  201. )
  202. role = models.PositiveSmallIntegerField(choices=OrganizationUserRole.choices)
  203. email = models.EmailField(
  204. blank=True, null=True, help_text="Email for pending invite"
  205. )
  206. class Meta(OrganizationOwnerBase.Meta):
  207. unique_together = (("user", "organization"), ("email", "organization"))
  208. def __str__(self, *args, **kwargs):
  209. if self.user:
  210. return super().__str__(*args, **kwargs)
  211. return self.email
  212. def get_email(self):
  213. if self.user:
  214. return self.user.email
  215. return self.email
  216. def get_role(self):
  217. return self.get_role_display().lower()
  218. def get_scopes(self):
  219. role = OrganizationUserRole.get_role(self.role)
  220. return role["scopes"]
  221. def accept_invite(self, user):
  222. self.user = user
  223. self.email = None
  224. self.save()
  225. @property
  226. def pending(self):
  227. return self.user_id is None
  228. @property
  229. def is_active(self):
  230. """ Non pending means active """
  231. return not self.pending
  232. class OrganizationOwner(OrganizationOwnerBase):
  233. """ Only usage is for billing contact currently """
  234. class OrganizationInvitation(OrganizationInvitationBase):
  235. """ Required to exist for django-organizations """