Compare commits
No commits in common. "94fbab278cd8949a45073e338d96550d18f7dcff" and "02225cdf09ae3ec4d1f38d93b3efaf4d6de54313" have entirely different histories.
94fbab278c
...
02225cdf09
11 changed files with 149 additions and 172 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from . import models, schemata
|
||||
from . import models, schemas
|
||||
from .connection import Connection
|
||||
|
||||
__all__ = ["Connection", "models", "schemata"]
|
||||
__all__ = ["Connection", "models", "schemas"]
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from __future__ import annotations
|
|||
from sqlalchemy import (Column, DateTime, ForeignKey, Integer, String,
|
||||
UniqueConstraint)
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import Session, relationship
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
ORMBaseModel = declarative_base()
|
||||
|
||||
|
|
@ -29,7 +29,6 @@ class User(ORMBaseModel):
|
|||
|
||||
name = Column(String, primary_key=True, index=True)
|
||||
password = Column(String, nullable=False)
|
||||
email = Column(String)
|
||||
|
||||
country = Column(String(2))
|
||||
state = Column(String)
|
||||
|
|
@ -37,6 +36,8 @@ class User(ORMBaseModel):
|
|||
organization = Column(String)
|
||||
organizational_unit = Column(String)
|
||||
|
||||
email = Column(String)
|
||||
|
||||
capabilities: list[UserCapability] = relationship(
|
||||
"UserCapability", lazy="joined", cascade="all, delete-orphan"
|
||||
)
|
||||
|
|
@ -44,23 +45,6 @@ class User(ORMBaseModel):
|
|||
"Device", lazy="select", back_populates="owner"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_db(
|
||||
cls,
|
||||
db: Session,
|
||||
name: str,
|
||||
) -> User | None:
|
||||
"""
|
||||
Load user from database by name.
|
||||
"""
|
||||
|
||||
return (
|
||||
db
|
||||
.query(cls)
|
||||
.filter(cls.name == name)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
class Device(ORMBaseModel):
|
||||
__tablename__ = "devices"
|
||||
|
|
@ -73,7 +57,7 @@ class Device(ORMBaseModel):
|
|||
expiry = Column(DateTime)
|
||||
|
||||
owner: User = relationship(
|
||||
"User", lazy="joined", back_populates="devices"
|
||||
"User", lazy="joined", back_populates="distinguished_names"
|
||||
)
|
||||
|
||||
UniqueConstraint(
|
||||
|
|
|
|||
|
|
@ -2,8 +2,11 @@
|
|||
Pydantic representation of database contents.
|
||||
"""
|
||||
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from passlib.context import CryptContext
|
||||
|
|
@ -11,23 +14,65 @@ from pydantic import BaseModel, Field, validator
|
|||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import models
|
||||
from .device import Device
|
||||
from .user_capability import UserCapability
|
||||
from . import models
|
||||
|
||||
##########
|
||||
# table: user_capabilities
|
||||
##########
|
||||
|
||||
|
||||
class UserCapability(Enum):
|
||||
admin = "admin"
|
||||
login = "login"
|
||||
issue = "issue"
|
||||
renew = "renew"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.value
|
||||
|
||||
@classmethod
|
||||
def from_value(cls, value) -> UserCapability:
|
||||
"""
|
||||
Create UserCapability from various formats
|
||||
"""
|
||||
|
||||
if isinstance(value, cls):
|
||||
# value is already a UserCapability, use that
|
||||
return value
|
||||
|
||||
elif isinstance(value, models.UserCapability):
|
||||
# create from db format
|
||||
return cls(value.capability)
|
||||
|
||||
else:
|
||||
# create from string representation
|
||||
return cls(str(value))
|
||||
|
||||
@property
|
||||
def model(self) -> models.UserCapability:
|
||||
return models.UserCapability(
|
||||
capability=self.value,
|
||||
)
|
||||
|
||||
|
||||
##########
|
||||
# table: users
|
||||
##########
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
name: str
|
||||
|
||||
country: str
|
||||
state: str
|
||||
city: str
|
||||
organization: str
|
||||
organizational_unit: str
|
||||
|
||||
email: str
|
||||
|
||||
capabilities: list[UserCapability] = []
|
||||
|
||||
country: str | None = Field(default=None, repr=False)
|
||||
state: str | None = Field(default=None, repr=False)
|
||||
city: str | None = Field(default=None, repr=False)
|
||||
organization: str | None = Field(default=None, repr=False)
|
||||
organizational_unit: str | None = Field(default=None, repr=False)
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
|
@ -63,8 +108,8 @@ class User(UserBase):
|
|||
Load user from database by name.
|
||||
"""
|
||||
|
||||
if (db_user := models.User.from_db(db, name)) is None:
|
||||
return None
|
||||
db_user = models.User(name=name)
|
||||
db.refresh(db_user)
|
||||
|
||||
return cls.from_orm(db_user)
|
||||
|
||||
|
|
@ -83,7 +128,6 @@ class User(UserBase):
|
|||
db_user = models.User(
|
||||
name=user.name,
|
||||
password=crypt_context.hash(user.password),
|
||||
email=user.email,
|
||||
capabilities=[
|
||||
capability.model
|
||||
for capability in user.capabilities
|
||||
|
|
@ -98,7 +142,7 @@ class User(UserBase):
|
|||
|
||||
except IntegrityError:
|
||||
# user already existed
|
||||
return None
|
||||
pass
|
||||
|
||||
def is_admin(self) -> bool:
|
||||
return UserCapability.admin in self.capabilities
|
||||
|
|
@ -113,7 +157,10 @@ class User(UserBase):
|
|||
Authenticate with name/password against users in database.
|
||||
"""
|
||||
|
||||
if (db_user := models.User.from_db(db, self.name)) is None:
|
||||
db_user = models.User(name=self.name)
|
||||
db.refresh(db_user)
|
||||
|
||||
if db_user is None:
|
||||
# nonexistent user, fake doing password verification
|
||||
crypt_context.dummy_verify()
|
||||
return False
|
||||
|
|
@ -134,8 +181,8 @@ class User(UserBase):
|
|||
Update this user in the database.
|
||||
"""
|
||||
|
||||
if (db_user := models.User.from_db(db, self.name)) is None:
|
||||
return None
|
||||
db_user = models.User(name=self.name)
|
||||
db.refresh(db_user)
|
||||
|
||||
for capability in db_user.capabilities:
|
||||
db.delete(capability)
|
||||
|
|
@ -155,10 +202,84 @@ class User(UserBase):
|
|||
Delete this user from the database.
|
||||
"""
|
||||
|
||||
if (db_user := models.User.from_db(db, self.name)) is None:
|
||||
db_user = models.User(name=self.name)
|
||||
db.refresh(db_user)
|
||||
|
||||
if db_user is None:
|
||||
# nonexistent user
|
||||
return False
|
||||
|
||||
db.delete(db_user)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
##########
|
||||
# table: devices
|
||||
##########
|
||||
|
||||
|
||||
class DeviceBase(BaseModel):
|
||||
name: str
|
||||
type: str
|
||||
expiry: datetime
|
||||
|
||||
|
||||
class DeviceCreate(DeviceBase):
|
||||
owner_name: str
|
||||
|
||||
|
||||
class Device(DeviceBase):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
db: Session,
|
||||
device: DeviceCreate,
|
||||
) -> Device | None:
|
||||
"""
|
||||
Create a new device in the database.
|
||||
"""
|
||||
|
||||
try:
|
||||
db_device = models.Device(
|
||||
owner_name=device.owner_name,
|
||||
|
||||
name=device.name,
|
||||
type=device.type,
|
||||
expiry=device.expiry,
|
||||
)
|
||||
|
||||
db.add(db_device)
|
||||
db.commit()
|
||||
db.refresh(db_device)
|
||||
|
||||
return cls.from_orm(db_device)
|
||||
|
||||
except IntegrityError:
|
||||
# device already existed
|
||||
pass
|
||||
|
||||
def delete(
|
||||
self,
|
||||
db: Session,
|
||||
) -> bool:
|
||||
"""
|
||||
Delete this device from the database.
|
||||
"""
|
||||
|
||||
db_device = models.Device(
|
||||
# owner_name=
|
||||
name=self.name,
|
||||
)
|
||||
db.refresh(db_device)
|
||||
|
||||
if db_device is None:
|
||||
# nonexistent device
|
||||
return False
|
||||
|
||||
db.delete(db_device)
|
||||
db.commit()
|
||||
return True
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
from .device import Device, DeviceBase, DeviceCreate
|
||||
from .user import User, UserBase, UserCreate
|
||||
from .user_capability import UserCapability
|
||||
|
||||
__all__ = ["Device", "DeviceBase", "DeviceCreate",
|
||||
"User", "UserBase", "UserCreate", "UserCapability"]
|
||||
|
|
@ -1,79 +0,0 @@
|
|||
"""
|
||||
Pydantic representation of database contents.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from .. import models
|
||||
|
||||
|
||||
class DeviceBase(BaseModel):
|
||||
name: str
|
||||
type: str
|
||||
expiry: datetime
|
||||
|
||||
|
||||
class DeviceCreate(DeviceBase):
|
||||
owner_name: str
|
||||
|
||||
|
||||
class Device(DeviceBase):
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
@classmethod
|
||||
def create(
|
||||
cls,
|
||||
db: Session,
|
||||
device: DeviceCreate,
|
||||
) -> Device | None:
|
||||
"""
|
||||
Create a new device in the database.
|
||||
"""
|
||||
|
||||
try:
|
||||
db_device = models.Device(
|
||||
owner_name=device.owner_name,
|
||||
|
||||
name=device.name,
|
||||
type=device.type,
|
||||
expiry=device.expiry,
|
||||
)
|
||||
|
||||
db.add(db_device)
|
||||
db.commit()
|
||||
db.refresh(db_device)
|
||||
|
||||
return cls.from_orm(db_device)
|
||||
|
||||
except IntegrityError:
|
||||
# device already existed
|
||||
return None
|
||||
|
||||
def delete(
|
||||
self,
|
||||
db: Session,
|
||||
) -> bool:
|
||||
"""
|
||||
Delete this device from the database.
|
||||
"""
|
||||
|
||||
db_device = models.Device(
|
||||
# owner_name=
|
||||
name=self.name,
|
||||
)
|
||||
db.refresh(db_device)
|
||||
|
||||
if db_device is None:
|
||||
# nonexistent device
|
||||
return False
|
||||
|
||||
db.delete(db_device)
|
||||
db.commit()
|
||||
return True
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
"""
|
||||
Pydantic representation of database contents.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
|
||||
from .. import models
|
||||
|
||||
|
||||
class UserCapability(Enum):
|
||||
admin = "admin"
|
||||
login = "login"
|
||||
issue = "issue"
|
||||
renew = "renew"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return self.value
|
||||
|
||||
@classmethod
|
||||
def from_value(cls, value) -> UserCapability:
|
||||
"""
|
||||
Create UserCapability from various formats
|
||||
"""
|
||||
|
||||
if isinstance(value, cls):
|
||||
# value is already a UserCapability, use that
|
||||
return value
|
||||
|
||||
elif isinstance(value, models.UserCapability):
|
||||
# create from db format
|
||||
return cls(value.capability)
|
||||
|
||||
else:
|
||||
# create from string representation
|
||||
return cls(str(value))
|
||||
|
||||
@property
|
||||
def model(self) -> models.UserCapability:
|
||||
return models.UserCapability(
|
||||
capability=self.value,
|
||||
)
|
||||
|
|
@ -14,7 +14,7 @@ from fastapi import FastAPI
|
|||
|
||||
from .config import Config, Settings
|
||||
from .db import Connection
|
||||
from .db.schemata import User
|
||||
from .db.schemas import User
|
||||
from .routers import main_router
|
||||
|
||||
settings = Settings.get()
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from sqlalchemy.orm import Session
|
|||
|
||||
from ..config import Config
|
||||
from ..db import Connection
|
||||
from ..db.schemata import User
|
||||
from ..db.schemas import User
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/authenticate")
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
|||
|
||||
from ..config import Config
|
||||
from ..db import Connection
|
||||
from ..db.schemata import User, UserCapability, UserCreate
|
||||
from ..db.schemas import User, UserCapability, UserCreate
|
||||
from ._common import Responses, get_current_user
|
||||
|
||||
router = APIRouter(prefix="/admin", tags=["admin"])
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..db import Connection
|
||||
from ..db.schemata import DistinguishedName, DistinguishedNameCreate, User
|
||||
from ..db.schemas import DistinguishedName, DistinguishedNameCreate, User
|
||||
from ._common import Responses, get_current_user_if_admin_or_self
|
||||
|
||||
router = APIRouter(prefix="/dn")
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from sqlalchemy.orm import Session
|
|||
|
||||
from ..config import Config
|
||||
from ..db import Connection
|
||||
from ..db.schemata import User, UserCapability, UserCreate
|
||||
from ..db.schemas import User, UserCapability, UserCreate
|
||||
from ._common import Responses, get_current_user, get_current_user_if_admin
|
||||
|
||||
router = APIRouter(prefix="/user", tags=["user"])
|
||||
|
|
|
|||
Loading…
Reference in a new issue