File size: 7,105 Bytes
54eb2ce 4c719e5 54eb2ce 4c719e5 54eb2ce 4c719e5 54eb2ce 4c719e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | """
Admin controller for managing users and service catalog.
Simple authentication with hardcoded admin:admin credentials.
"""
from typing import Optional
from sqlalchemy import select, delete, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from src.model import User, ServiceCatalog, ResourceCatalog
from src.lib.enum import ServiceType
# Simple admin credentials (hardcoded for now)
ADMIN_USERNAME = "admin"
ADMIN_PASSWORD = "admin"
class AdminController:
"""Controller for admin operations."""
@staticmethod
def verify_admin_credentials(username: str, password: str) -> bool:
"""Verify admin credentials."""
return username == ADMIN_USERNAME and password == ADMIN_PASSWORD
@staticmethod
async def get_all_users(session: AsyncSession) -> list[User]:
"""Get all users."""
result = await session.execute(select(User).order_by(User.created_at.desc()))
return result.scalars().all()
@staticmethod
async def get_all_services(session: AsyncSession) -> list[ServiceCatalog]:
"""Get all services from catalog."""
result = await session.execute(
select(ServiceCatalog).order_by(
ServiceCatalog.service_type, ServiceCatalog.name
)
)
return result.scalars().all()
@staticmethod
async def get_service_by_id(
session: AsyncSession, service_id: int
) -> Optional[ServiceCatalog]:
"""Get service by ID."""
return await session.get(ServiceCatalog, service_id)
@staticmethod
async def create_service(
session: AsyncSession,
name: str,
slug: str,
service_type: ServiceType,
description: Optional[str] = None,
is_active: bool = True,
) -> ServiceCatalog:
"""Create a new service in the catalog."""
service = ServiceCatalog(
name=name,
slug=slug,
service_type=service_type,
description=description,
is_active=is_active,
)
session.add(service)
await session.commit()
await session.refresh(service)
return service
@staticmethod
async def delete_service(session: AsyncSession, service_id: int) -> bool:
"""Delete a service from the catalog."""
service = await session.get(ServiceCatalog, service_id)
if service:
await session.delete(service)
await session.commit()
return True
return False
@staticmethod
async def toggle_service_status(
session: AsyncSession, service_id: int
) -> Optional[ServiceCatalog]:
"""Toggle service active status."""
service = await session.get(ServiceCatalog, service_id)
if service:
service.is_active = not service.is_active
await session.commit()
await session.refresh(service)
return service
return None
@staticmethod
async def get_user_count(session: AsyncSession) -> int:
"""Get total user count."""
result = await session.execute(select(func.count()).select_from(User))
return result.scalar_one()
@staticmethod
async def get_service_count(session: AsyncSession) -> int:
"""Get total service count."""
result = await session.execute(select(func.count()).select_from(ServiceCatalog))
return result.scalar_one()
# ==================== Resource Catalog Methods ====================
@staticmethod
async def get_all_resources(session: AsyncSession) -> list[ResourceCatalog]:
"""Get all resources from catalog with service information."""
result = await session.execute(
select(ResourceCatalog)
.options(selectinload(ResourceCatalog.service))
.order_by(ResourceCatalog.service_id, ResourceCatalog.name)
)
return result.scalars().all()
@staticmethod
async def get_resources_by_service(
session: AsyncSession, service_id: int
) -> list[ResourceCatalog]:
"""Get all resources for a specific service."""
result = await session.execute(
select(ResourceCatalog)
.where(ResourceCatalog.service_id == service_id)
.order_by(ResourceCatalog.name)
)
return result.scalars().all()
@staticmethod
async def get_resource_by_id(
session: AsyncSession, resource_id: int
) -> Optional[ResourceCatalog]:
"""Get resource by ID."""
result = await session.execute(
select(ResourceCatalog)
.options(selectinload(ResourceCatalog.service))
.where(ResourceCatalog.id == resource_id)
)
return result.scalar_one_or_none()
@staticmethod
async def get_resource_by_slug(
session: AsyncSession, slug: str
) -> Optional[ResourceCatalog]:
"""Get resource by slug."""
result = await session.execute(
select(ResourceCatalog)
.options(selectinload(ResourceCatalog.service))
.where(ResourceCatalog.slug == slug)
)
return result.scalar_one_or_none()
@staticmethod
async def create_resource(
session: AsyncSession,
name: str,
slug: str,
service_id: int,
description: Optional[str] = None,
is_active: bool = True,
) -> ResourceCatalog:
"""Create a new resource in the catalog."""
resource = ResourceCatalog(
name=name,
slug=slug,
service_id=service_id,
description=description,
is_active=is_active,
)
session.add(resource)
await session.commit()
await session.refresh(resource)
return resource
@staticmethod
async def delete_resource(session: AsyncSession, resource_id: int) -> bool:
"""Delete a resource from the catalog."""
resource = await session.get(ResourceCatalog, resource_id)
if resource:
await session.delete(resource)
await session.commit()
return True
return False
@staticmethod
async def toggle_resource_status(
session: AsyncSession, resource_id: int
) -> Optional[ResourceCatalog]:
"""Toggle resource active status."""
resource = await session.get(ResourceCatalog, resource_id)
if resource:
resource.is_active = not resource.is_active
await session.commit()
await session.refresh(resource)
return resource
return None
@staticmethod
async def get_resource_count(session: AsyncSession) -> int:
"""Get total resource count."""
result = await session.execute(select(func.count()).select_from(ResourceCatalog))
return result.scalar_one()
|