Updated with new Basemodel for Audit

This commit is contained in:
Saurab-Shrestha 2024-02-25 14:46:12 +05:45
parent 8271fedb78
commit edd55a5dcc
5 changed files with 46 additions and 17 deletions

View File

@ -106,7 +106,6 @@ def prompt_completion(
db: Session = Depends(deps.get_db),
current_user: models.User = Security(
deps.get_current_user,
deps.get_active_subscription,
),
) -> OpenAICompletion | StreamingResponse:
try:

View File

@ -26,5 +26,16 @@ def list_companies(
"""
Retrieve a list of companies with pagination support.
"""
logs = crud.audit.get_multi(db, skip=skip, limit=limit)
logs = crud.audit.get_multi_desc(db, skip=skip, limit=limit)
logs = [
schemas.Audit(
id=dep.id,
model=dep.model,
username=(crud.user.get_by_id(db, id=dep.user_id).fullname),
details=dep.details,
action=dep.action,
timestamp=dep.timestamp,
)
for dep in logs
]
return logs

View File

@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
LDAP_SERVER = settings.LDAP_SERVER
# LDAP_ENABLE = settings.LDAP_ENABLE
LDAP_ENABLE = False
LDAP_ENABLE = True
router = APIRouter(prefix="/auth", tags=["auth"])
@ -116,17 +116,25 @@ def login_access_token(
"""
OAuth2 compatible token login, get an access token for future requests
"""
def ad_auth(LDAP_ENABLE):
if LDAP_ENABLE:
existing_user = crud.user.get_by_email(db, email=form_data.username)
if existing_user:
if existing_user.user_role.role.name == "SUPER_ADMIN":
pass
return True
else:
ldap = ldap_login(db=db, username=form_data.username, password=form_data.password)
else:
ldap = ldap_login(db=db, username=form_data.username, password=form_data.password)
ad_user_register(db=db, email=form_data.username,fullname=ldap, password=form_data.password)
ad_user_register(db=db, email=form_data.username, fullname=ldap, password=form_data.password)
return True
return False
if not (ad_auth(LDAP_ENABLE)):
raise HTTPException(
status_code=403,
detail="Invalid Credentials!!!",
)
user = crud.user.authenticate(
db, email=form_data.username, password=form_data.password
@ -254,7 +262,7 @@ def register(
user_role_name = role_name or Role.GUEST["name"]
user_role = create_user_role(db, user, user_role_name, company)
log_audit(model='user_roles', action='creation',
details={"status": '201', 'detail': "User role created successfully.", }, user_id=current_user.id)
details={'detail': "User role created successfully.", }, user_id=current_user.id)
except Exception as e:
print(traceback.format_exc())
raise HTTPException(

View File

@ -1,12 +1,18 @@
from typing import Optional
from typing import Optional, List
from private_gpt.users.crud.base import CRUDBase
from private_gpt.users.models.audit import Audit
from private_gpt.users.schemas.audit import AuditCreate, AuditUpdate
from sqlalchemy import desc
from sqlalchemy.orm import Session
class CRUDAudit(CRUDBase[Audit, AuditCreate, AuditUpdate]):
def get_multi_desc(
self, db: Session, *, skip: int = 0, limit: int = 100
) -> List[Audit]:
return db.query(self.model).order_by(desc(self.model.timestamp)).offset(skip).limit(limit).all()
def get_by_id(self, db: Session, *, id: str) -> Optional[Audit]:
return db.query(self.model).filter(Audit.id == id).first()

View File

@ -26,5 +26,10 @@ class AuditInDB(AuditBase):
class Config:
orm_mode = True
class Audit(AuditBase):
pass
class Audit(BaseModel):
id: int
model: str
username: str
action: str
details: dict
timestamp: Optional[datetime]