File size: 4,579 Bytes
9d66536 | 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 | 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) |