Skip to content

Commit 7409285

Browse files
authored
Add api and casbin related interfaces (#198)
* Add get all apis and more interface * Add casbin get related interface * typo * update function name
1 parent 5aacc16 commit 7409285

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

backend/app/api/v1/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
router = APIRouter()
1616

1717

18+
@router.get('/all', summary='获取所有接口', dependencies=[DependsJwtAuth])
19+
async def get_all_apis():
20+
data = await ApiService.get_all()
21+
return await response_base.success(data=data)
22+
23+
1824
@router.get('/{pk}', summary='获取接口详情', dependencies=[DependsJwtAuth])
1925
async def get_api(pk: int):
2026
api = await ApiService.get(pk=pk)
2127
return await response_base.success(data=api)
2228

2329

2430
@router.get('', summary='(模糊条件)分页获取所有接口', dependencies=[DependsJwtAuth, PageDepends])
25-
async def get_all_apis(
31+
async def get_api_list(
2632
db: CurrentSession,
2733
name: Annotated[str | None, Query()] = None,
2834
method: Annotated[str | None, Query()] = None,

backend/app/api/v1/casbin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ async def get_all_policies():
4141
return await response_base.success(data=policies)
4242

4343

44+
@router.get('/policy/{role}/all', summary='获取指定角色的所有P权限规则', dependencies=[DependsJwtAuth])
45+
async def get_role_policies(role: str):
46+
policies = await CasbinService.get_policy_list_by_role(role=role)
47+
return await response_base.success(data=policies)
48+
49+
4450
@router.post('/policy', summary='添加P权限规则', dependencies=[DependsRBAC])
4551
async def create_policy(p: CreatePolicy):
4652
"""

backend/app/crud/crud_api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
from typing import NoReturn
3+
from typing import NoReturn, Sequence
44

55
from sqlalchemy import Select, select, desc, delete, and_
66
from sqlalchemy.ext.asyncio import AsyncSession
@@ -14,7 +14,7 @@ class CRUDApi(CRUDBase[Api, CreateApi, UpdateApi]):
1414
async def get(self, db: AsyncSession, pk: int) -> Api | None:
1515
return await self.get_(db, pk=pk)
1616

17-
async def get_all(self, name: str = None, method: str = None, path: str = None) -> Select:
17+
async def get_list(self, name: str = None, method: str = None, path: str = None) -> Select:
1818
se = select(self.model).order_by(desc(self.model.created_time))
1919
where_list = []
2020
if name:
@@ -27,6 +27,10 @@ async def get_all(self, name: str = None, method: str = None, path: str = None)
2727
se = se.where(and_(*where_list))
2828
return se
2929

30+
async def get_all(self, db: AsyncSession) -> Sequence[Api]:
31+
apis = await db.execute(select(self.model))
32+
return apis.scalars().all()
33+
3034
async def get_by_name(self, db: AsyncSession, name: str) -> Api | None:
3135
api = await db.execute(select(self.model).where(self.model.name == name))
3236
return api.scalars().first()

backend/app/services/api_service.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
from typing import Sequence
4+
35
from sqlalchemy import Select
46

57
from backend.app.common.exception import errors
@@ -20,7 +22,13 @@ async def get(*, pk: int) -> Api:
2022

2123
@staticmethod
2224
async def get_select(*, name: str = None, method: str = None, path: str = None) -> Select:
23-
return await ApiDao.get_all(name=name, method=method, path=path)
25+
return await ApiDao.get_list(name=name, method=method, path=path)
26+
27+
@staticmethod
28+
async def get_all() -> Sequence[Api]:
29+
async with async_db_session() as db:
30+
apis = await ApiDao.get_all(db)
31+
return apis
2432

2533
@staticmethod
2634
async def create(*, obj: CreateApi) -> None:

backend/app/services/casbin_service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ async def get_policy_list():
2828
data = await enforcer.get_policy()
2929
return data
3030

31+
@staticmethod
32+
async def get_policy_list_by_role(*, role: str):
33+
enforcer = await RBAC.enforcer()
34+
data = await enforcer.get_filtered_named_policy('p', 0, role)
35+
return data
36+
3137
@staticmethod
3238
async def create_policy(*, p: CreatePolicy):
3339
enforcer = await RBAC.enforcer()

0 commit comments

Comments
 (0)