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)
|
|
|
|
password = Column(String)
|
|
|
|
|
2022-03-23 13:25:00 +00:00
|
|
|
capabilities: list[UserCapability] = relationship(
|
|
|
|
"UserCapability", lazy="joined", cascade="all, delete-orphan"
|
|
|
|
)
|
|
|
|
certificates: list[Certificate] = relationship(
|
|
|
|
"Certificate", lazy="select"
|
|
|
|
)
|
|
|
|
distinguished_names: list[DistinguishedName] = relationship(
|
|
|
|
"DistinguishedName", lazy="select"
|
|
|
|
)
|
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-17 17:06:00 +00:00
|
|
|
cn_only = Column(Boolean, default=True)
|
|
|
|
country = Column(String(2))
|
|
|
|
state = Column(String)
|
|
|
|
city = Column(String)
|
|
|
|
organization = Column(String)
|
|
|
|
organizational_unit = Column(String)
|
|
|
|
email = Column(String)
|
|
|
|
common_name = Column(String)
|
|
|
|
|
|
|
|
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"))
|
|
|
|
dn_id = Column(Integer, ForeignKey("distinguished_names.id"))
|
|
|
|
expiry = Column(DateTime, default=datetime.datetime.now)
|
2022-03-22 16:29:02 +00:00
|
|
|
|
|
|
|
distinguished_name = relationship("DistinguishedName", lazy="joined")
|