forked from sr2/cloud-api
Orgs can only grant permissions to groups that they themselves have been granted access to. Super admin bypasses not added, flagged as todos.
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
"""
|
|
Database models for organisation module
|
|
|
|
Models:
|
|
- Organisation:
|
|
- id[PK], name, status, intake_questionnaire, root_user_id[FK], billing_contact_id[FK], security_contact_id[FK], owner_contact_id[FK]
|
|
- user_rel: ORM relationship to User via OrgUsers relationship table
|
|
- group_rel: ORM relationship to Group, backprops Group.org_rel
|
|
- root_user_rel: ORM relationship to User with root_user_id FK
|
|
- root_user_email: Calc property root_user_rel.email
|
|
- billing_contact_rel: ORM relationship to Contact with billing_contact FK
|
|
- security_contact_rel: ORM relationship to Contact with security_contact FK
|
|
- owner_contact_rel: ORM relationship to Contact with owner_contact FK
|
|
- OrgUsers: org_id[FK][PK], user_id[FK][PK]
|
|
"""
|
|
|
|
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from src.database import Base
|
|
|
|
|
|
class Organisation(Base):
|
|
__tablename__ = "organisation"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String, unique=True)
|
|
status = Column(String, default="partial")
|
|
intake_questionnaire = Column(JSON)
|
|
|
|
root_user_id = Column(Integer, ForeignKey("user.id"))
|
|
|
|
billing_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
security_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
owner_contact_id = Column(Integer, ForeignKey("contact.id"))
|
|
|
|
user_rel = relationship(
|
|
"User", secondary="orgusers", back_populates="organisation_rel"
|
|
)
|
|
|
|
group_rel = relationship("Group", back_populates="org_rel")
|
|
root_user_rel = relationship("User", foreign_keys="Organisation.root_user_id")
|
|
|
|
@property
|
|
def root_user_email(self):
|
|
return self.root_user_rel.email if self.root_user_rel else None
|
|
|
|
billing_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.billing_contact_id"
|
|
)
|
|
security_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.security_contact_id"
|
|
)
|
|
owner_contact_rel = relationship(
|
|
"Contact", foreign_keys="Organisation.owner_contact_id"
|
|
)
|
|
|
|
permission_rel = relationship(
|
|
"Permission", secondary="org_permissions", back_populates="org_rel"
|
|
)
|
|
|
|
|
|
class OrgUsers(Base):
|
|
__tablename__ = "orgusers"
|
|
|
|
org_id = Column(
|
|
Integer, ForeignKey("organisation.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
user_id = Column(
|
|
Integer, ForeignKey("user.id", ondelete="CASCADE"), primary_key=True
|
|
)
|