feat: implement full API - all admin/user routes, RBAC, sync service, collector, MySQL config, superadmin hanbin credentials
9d66536 Download app/services/auth_admin.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/app/services/auth_admin.py
- Command line
-
hf download hf://spaces/binhugg2/api/app/services/auth_admin.py
-
curl -L -o auth_admin.py https://huggingface.co/spaces/binhugg2/api/resolve/main/app/services/auth_admin.py
4.58 kB
| from sqlalchemy import select | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.models.admin import Admin | |
| from app.models.role import Role, AdminRole | |
| from app.models.permission import Permission, RolePermission | |
| from app.utils.auth import hash_password, verify_password, create_access_token | |
| async def authenticate_admin(db: AsyncSession, username: str, password: str) -> Admin | None: | |
| result = await db.execute(select(Admin).where(Admin.username == username)) | |
| admin = result.scalar_one_or_none() | |
| if not admin or not admin.is_active or not verify_password(password, admin.password_hash): | |
| return None | |
| return admin | |
| async def get_admin_roles(db: AsyncSession, admin_id: int) -> list[str]: | |
| result = await db.execute( | |
| select(Role.code) | |
| .join(AdminRole, AdminRole.role_id == Role.id) | |
| .where(AdminRole.admin_id == admin_id, Role.status == 1) | |
| ) | |
| return [r[0] for r in result.all()] | |
| async def get_admin_permissions(db: AsyncSession, admin_id: int) -> list[str]: | |
| result = await db.execute( | |
| select(Permission.code) | |
| .join(RolePermission, RolePermission.permission_id == Permission.id) | |
| .join(AdminRole, AdminRole.role_id == RolePermission.role_id) | |
| .where(AdminRole.admin_id == admin_id, Permission.status == 1) | |
| .distinct() | |
| ) | |
| return [p[0] for p in result.all()] | |
| async def get_menu_tree(db: AsyncSession, admin_id: int) -> list[dict]: | |
| perm_codes = await get_admin_permissions(db, admin_id) | |
| roles = await get_admin_roles(db, admin_id) | |
| is_super = "super_admin" in roles | |
| result = await db.execute( | |
| select(Permission) | |
| .where(Permission.type == "menu", Permission.status == 1) | |
| .order_by(Permission.sort_order) | |
| ) | |
| all_menus = result.scalars().all() | |
| menus = [] | |
| for m in all_menus: | |
| if not is_super: | |
| menu_page_codes = [ | |
| c.code for c in await _get_menu_pages(db, m.id) | |
| if c.code in perm_codes | |
| ] | |
| if not menu_page_codes: | |
| continue | |
| menus.append({ | |
| "name": code_to_route_name(m.code), | |
| "path": m.path, | |
| "component": "BasicLayout", | |
| "redirect": m.path, | |
| "meta": { | |
| "title": m.name, | |
| "icon": m.icon, | |
| "order": m.sort_order, | |
| }, | |
| "children": [ | |
| { | |
| "name": code_to_route_name(m.code) + "Index", | |
| "path": m.path, | |
| "component": m.component, | |
| "meta": { | |
| "title": m.name, | |
| "icon": m.icon, | |
| "hideInMenu": True, | |
| "keepAlive": False, | |
| "menuVisibleWithForbidden": True, | |
| }, | |
| } | |
| ], | |
| }) | |
| return menus | |
| async def get_permission_tree(db: AsyncSession) -> list[dict]: | |
| result = await db.execute( | |
| select(Permission).order_by(Permission.sort_order) | |
| ) | |
| all_perms = result.scalars().all() | |
| perm_map: dict[int, dict] = {} | |
| roots = [] | |
| for p in all_perms: | |
| node = build_perm_node(p) | |
| perm_map[p.id] = node | |
| for p in all_perms: | |
| if p.parent_id and p.parent_id in perm_map: | |
| if "children" not in perm_map[p.parent_id]: | |
| perm_map[p.parent_id]["children"] = [] | |
| perm_map[p.parent_id]["children"].append(perm_map[p.id]) | |
| elif not p.parent_id: | |
| roots.append(perm_map[p.id]) | |
| return roots | |
| async def _get_menu_pages(db: AsyncSession, menu_id: int) -> list[Permission]: | |
| result = await db.execute( | |
| select(Permission) | |
| .where(Permission.parent_id == menu_id, Permission.type == "page", Permission.status == 1) | |
| ) | |
| return list(result.scalars().all()) | |
| def build_menu_node(p: Permission) -> dict: | |
| return { | |
| "name": code_to_route_name(p.code), | |
| "path": p.path or "", | |
| "component": p.component, | |
| "redirect": p.redirect, | |
| "meta": { | |
| "title": p.name, | |
| "icon": p.icon, | |
| "order": p.sort_order, | |
| "hideInMenu": False, | |
| "keepAlive": False, | |
| }, | |
| } | |
| def build_perm_node(p: Permission) -> dict: | |
| return { | |
| "id": p.id, | |
| "code": p.code, | |
| "name": p.name, | |
| "type": p.type, | |
| } | |
| def code_to_route_name(code: str) -> str: | |
| parts = code.replace(":", "_").split("_") | |
| return "".join(p.capitalize() for p in parts) |