kiwi-vpn/api/kiwi_vpn_api/db/models.py

107 lines
2.7 KiB
Python
Raw Normal View History

2022-03-20 03:45:40 +00:00
"""
SQLAlchemy representation of database contents.
"""
2022-03-19 23:56:11 +00:00
from __future__ import annotations
2022-03-17 17:06:00 +00:00
import datetime
from sqlalchemy import (Boolean, Column, DateTime, ForeignKey, Integer, String,
UniqueConstraint)
2022-03-17 22:47:31 +00:00
from sqlalchemy.ext.declarative import declarative_base
2022-03-19 23:56:11 +00:00
from sqlalchemy.orm import Session, relationship
2022-03-17 17:06:00 +00:00
2022-03-17 22:47:31 +00:00
ORMBaseModel = declarative_base()
2022-03-17 17:06:00 +00:00
class User(ORMBaseModel):
__tablename__ = "users"
name = Column(String, primary_key=True, index=True)
2022-03-23 15:00:35 +00:00
password = Column(String, nullable=False)
2022-03-17 17:06:00 +00:00
2022-03-23 13:25:00 +00:00
capabilities: list[UserCapability] = relationship(
"UserCapability", lazy="joined", cascade="all, delete-orphan"
)
certificates: list[Certificate] = relationship(
2022-03-23 15:30:22 +00:00
"Certificate", lazy="select", back_populates="owner"
2022-03-23 13:25:00 +00:00
)
distinguished_names: list[DistinguishedName] = relationship(
2022-03-23 15:30:22 +00:00
"DistinguishedName", lazy="select", back_populates="owner"
2022-03-23 13:25:00 +00:00
)
2022-03-17 17:06:00 +00:00
2022-03-19 23:56:11 +00:00
@classmethod
def load(cls, db: Session, name: str) -> User | None:
2022-03-20 03:45:40 +00:00
"""
Load user from database by name.
"""
2022-03-19 23:56:11 +00:00
return (db
.query(User)
.filter(User.name == name)
.first())
2022-03-17 17:06:00 +00:00
class UserCapability(ORMBaseModel):
__tablename__ = "user_capabilities"
user_name = Column(
String,
ForeignKey("users.name"),
primary_key=True,
index=True,
)
capability = Column(String, primary_key=True)
class DistinguishedName(ORMBaseModel):
__tablename__ = "distinguished_names"
id = Column(Integer, primary_key=True, autoincrement=True)
2022-03-22 16:29:02 +00:00
owner_name = Column(String, ForeignKey("users.name"))
2022-03-23 15:00:35 +00:00
cn_only = Column(Boolean, default=True, nullable=False)
2022-03-17 17:06:00 +00:00
country = Column(String(2))
state = Column(String)
city = Column(String)
organization = Column(String)
organizational_unit = Column(String)
email = Column(String)
2022-03-23 15:00:35 +00:00
common_name = Column(String, nullable=False)
2022-03-17 17:06:00 +00:00
2022-03-23 15:30:22 +00:00
owner: User = relationship(
"User", lazy="joined", back_populates="distinguished_names"
)
2022-03-17 17:06:00 +00:00
UniqueConstraint(
country,
state,
city,
organization,
organizational_unit,
email,
common_name,
)
class Certificate(ORMBaseModel):
__tablename__ = "certificates"
id = Column(Integer, primary_key=True, autoincrement=True)
owner_name = Column(String, ForeignKey("users.name"))
2022-03-23 15:00:35 +00:00
dn_id = Column(
Integer,
ForeignKey("distinguished_names.id"),
nullable=False,
)
2022-03-17 17:06:00 +00:00
expiry = Column(DateTime, default=datetime.datetime.now)
2022-03-22 16:29:02 +00:00
2022-03-23 15:00:35 +00:00
distinguished_name: DistinguishedName = relationship(
"DistinguishedName", lazy="joined"
)
2022-03-23 15:30:22 +00:00
owner: User = relationship(
"User", lazy="joined", back_populates="certificates"
)