feat: implement full API - all admin/user routes, RBAC, sync service, collector, MySQL config, superadmin hanbin credentials
9d66536 Download scripts/seed_roles.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/scripts/seed_roles.py
- Command line
-
hf download hf://spaces/binhugg2/api/scripts/seed_roles.py
-
curl -L -o seed_roles.py https://huggingface.co/spaces/binhugg2/api/resolve/main/scripts/seed_roles.py
3.18 kB
| import asyncio | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| from sqlalchemy import select | |
| from app.database import async_session | |
| from app.models.role import Role, AdminRole | |
| from app.models.permission import Permission, RolePermission | |
| ROLES = [ | |
| { | |
| "name": "超级管理员", | |
| "code": "super_admin", | |
| "description": "全部权限,含角色管理", | |
| "all_permissions": True, | |
| }, | |
| { | |
| "name": "管理员", | |
| "code": "admin", | |
| "description": "除角色管理外的所有权限", | |
| "exclude_codes": [ | |
| "role:menu", "role:list", "role:create", "role:update", "role:delete", | |
| "role:btn:assign_perm", | |
| ], | |
| }, | |
| { | |
| "name": "编辑", | |
| "code": "editor", | |
| "description": "内容管理,不能动系统配置", | |
| "include_codes": [ | |
| "vod:menu", "vod:list", "vod:detail", "vod:create", "vod:update", | |
| "vod:btn:hide", "vod:btn:recommend", "vod:btn:delete", "vod:btn:audit", | |
| "category:menu", "category:list", "category:update", "category:btn:map", | |
| "sync:menu", "sync:list", "sync:btn:trigger", "sync:btn:cancel", | |
| ], | |
| }, | |
| { | |
| "name": "查看者", | |
| "code": "viewer", | |
| "description": "只读所有页面", | |
| "include_patterns": [":list", ":detail", ":config", ":menu"], | |
| }, | |
| ] | |
| async def seed(): | |
| async with async_session() as db: | |
| existing = await db.execute(select(Role)) | |
| if existing.scalars().first(): | |
| print("Roles already seeded, skipping.") | |
| return | |
| all_perm_result = await db.execute(select(Permission)) | |
| all_perms = {p.code: p.id for p in all_perm_result.scalars().all()} | |
| for role_data in ROLES: | |
| role = Role( | |
| name=role_data["name"], | |
| code=role_data["code"], | |
| description=role_data["description"], | |
| is_builtin=True, | |
| status=1, | |
| ) | |
| db.add(role) | |
| await db.flush() | |
| if role_data.get("all_permissions"): | |
| perm_ids = list(all_perms.values()) | |
| elif "exclude_codes" in role_data: | |
| perm_ids = [ | |
| pid for code, pid in all_perms.items() | |
| if code not in role_data["exclude_codes"] | |
| ] | |
| elif "include_codes" in role_data: | |
| perm_ids = [ | |
| all_perms[c] for c in role_data["include_codes"] | |
| if c in all_perms | |
| ] | |
| elif "include_patterns" in role_data: | |
| perm_ids = [] | |
| for code, pid in all_perms.items(): | |
| if any(pat in code for pat in role_data["include_patterns"]): | |
| perm_ids.append(pid) | |
| else: | |
| perm_ids = [] | |
| for pid in perm_ids: | |
| db.add(RolePermission(role_id=role.id, permission_id=pid)) | |
| await db.commit() | |
| print(f"Seeded {len(ROLES)} roles with permissions.") | |
| if __name__ == "__main__": | |
| asyncio.run(seed()) |