Skip to content

Commit 0b95f2c

Browse files
authored
Update asynccasbin to casbin async api (#199)
* Update asynccasbin to casbin async api * remove pymysql dependency * update the test-user test to admin
1 parent 7409285 commit 0b95f2c

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

backend/app/common/rbac.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
class RBAC:
1717
@staticmethod
18-
async def enforcer() -> casbin.Enforcer:
18+
async def enforcer() -> casbin.AsyncEnforcer:
1919
"""
2020
获取 casbin 执行器
2121
2222
:return:
2323
"""
2424
adapter = casbin_async_sqlalchemy_adapter.Adapter(async_engine, db_class=CasbinRule)
25-
enforcer = casbin.Enforcer(RBAC_MODEL_CONF, adapter)
25+
enforcer = casbin.AsyncEnforcer(RBAC_MODEL_CONF, adapter)
2626
await enforcer.load_policy()
2727
return enforcer
2828

backend/app/services/casbin_service.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ async def get_casbin_list(*, ptype: str, sub: str) -> Select:
2525
@staticmethod
2626
async def get_policy_list():
2727
enforcer = await RBAC.enforcer()
28-
data = await enforcer.get_policy()
28+
data = enforcer.get_policy()
2929
return data
3030

3131
@staticmethod
3232
async def get_policy_list_by_role(*, role: str):
3333
enforcer = await RBAC.enforcer()
34-
data = await enforcer.get_filtered_named_policy('p', 0, role)
34+
data = enforcer.get_filtered_named_policy('p', 0, role)
3535
return data
3636

3737
@staticmethod
@@ -53,7 +53,7 @@ async def create_policies(*, ps: list[CreatePolicy]):
5353
@staticmethod
5454
async def update_policy(*, old: UpdatePolicy, new: UpdatePolicy):
5555
enforcer = await RBAC.enforcer()
56-
_p = await enforcer.has_policy(old.sub, old.path, old.method)
56+
_p = enforcer.has_policy(old.sub, old.path, old.method)
5757
if not _p:
5858
raise errors.NotFoundError(msg='权限不存在')
5959
data = await enforcer.update_policy([old.sub, old.path, old.method], [new.sub, new.path, new.method])
@@ -70,7 +70,7 @@ async def update_policies(*, old: list[UpdatePolicy], new: list[UpdatePolicy]):
7070
@staticmethod
7171
async def delete_policy(*, p: DeletePolicy):
7272
enforcer = await RBAC.enforcer()
73-
_p = await enforcer.has_policy(p.sub, p.path, p.method)
73+
_p = enforcer.has_policy(p.sub, p.path, p.method)
7474
if not _p:
7575
raise errors.NotFoundError(msg='权限不存在')
7676
data = await enforcer.remove_policy(p.sub, p.path, p.method)
@@ -115,7 +115,7 @@ async def create_groups(*, gs: list[CreateUserRole]):
115115
@staticmethod
116116
async def delete_group(*, g: DeleteUserRole):
117117
enforcer = await RBAC.enforcer()
118-
_g = await enforcer.has_grouping_policy(g.uuid, g.role)
118+
_g = enforcer.has_grouping_policy(g.uuid, g.role)
119119
if not _g:
120120
raise errors.NotFoundError(msg='权限不存在')
121121
data = await enforcer.remove_grouping_policy(g.uuid, g.role)

backend/app/services/menu_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ async def get_user_menu_tree(*, request: Request):
4444
if roles:
4545
for role in roles:
4646
menu_ids.extend([menu.id for menu in role.menus])
47-
menu_select = await MenuDao.get_role_menus(db, request.user.is_superuser, menu_ids)
48-
menu_tree = await get_tree_data(menu_select)
47+
menu_select = await MenuDao.get_role_menus(db, request.user.is_superuser, menu_ids)
48+
menu_tree = await get_tree_data(menu_select)
4949
return menu_tree
5050

5151
@staticmethod

backend/app/tests/api_v1/test_auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
def test_login(client: TestClient) -> None:
99
data = {
10-
'username': 'test',
11-
'password': 'test',
10+
'username': 'admin',
11+
'password': '123456',
1212
}
1313
response = client.post(f'{settings.API_V1_STR}/auth/login', json=data)
1414
assert response.status_code == 200

backend/app/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def client() -> Generator:
2525

2626
@pytest.fixture(scope='module')
2727
def token_headers(client: TestClient) -> Dict[str, str]:
28-
return get_token_headers(client=client, username='test', password='test')
28+
return get_token_headers(client=client, username='admin', password='123456')

backend/sql/init_test_data.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ VALUES (1, 'test', 0, 0, null, null, null, 1, 0, null, '2023-06-26 17:13:45', nu
33

44
insert into fba.sys_menu (id, title, name, level, sort, icon, path, menu_type, component, perms, status, `show`, cache, remark, parent_id, created_time, updated_time)
55
values (1, '测试', 'test', 0, 0, '', null, 0, null, null, 0, 0, 1, null, null, '2023-07-27 19:14:10', '2023-07-27 19:14:52'),
6-
(2, '仪表盘', 'dashboard', 0, 0, 'IconDashboard', 'dashboard', 1, '/dashboard/workplace/index.vue', null, 1, 1, 1, null, null, '2023-07-27 19:15:45', null),
7-
(3, '工作台', 'Workplace', 0, 0, null, 'workplace', 0, null, null, 1, 1, 1, null, 2, '2023-07-27 19:17:59', null),
6+
(2, '仪表盘', 'dashboard', 0, 0, 'IconDashboard', 'dashboard', 0, null, null, 1, 1, 1, null, null, '2023-07-27 19:15:45', null),
7+
(3, '工作台', 'Workplace', 0, 0, null, 'workplace', 1, '/dashboard/workplace/index.vue', null, 1, 1, 1, null, 2, '2023-07-27 19:17:59', null),
88
(4, 'arco官网', 'arcoWebsite', 0, 888, 'IconLink', 'https://arco.design', 1, null, null, 1, 1, 1, null, null, '2023-07-27 19:19:23', null),
99
(5, '日志', 'log', 0, 66, 'IconBug', 'log', 0, null, null, 1, 1, 1, null, null, '2023-07-27 19:19:59', null),
1010
(6, '登录日志', 'Login', 0, 0, null, 'login', 1, '/log/login/index.vue', null, 1, 1, 1, null, 5, '2023-07-27 19:20:56', null),
@@ -26,8 +26,9 @@ VALUES (1, 'test', 2, 1, null, '2023-06-26 17:13:45', null);
2626
INSERT INTO fba.sys_role_menu (id, role_id, menu_id)
2727
VALUES (1, 1, 1);
2828

29+
-- 密码明文:123456
2930
INSERT INTO fba.sys_user (id, uuid, username, nickname, password, salt, email, is_superuser, is_staff, status, is_multi_login, avatar, phone, join_time, last_login_time, dept_id, created_time, updated_time)
30-
VALUES (1, 'af4c804f-3966-4949-ace2-3bb7416ea926', 'test', 'test', '$2b$12$eYLKJttq2/e66er1VK7NFezNIYkAu.XTGrJppJpqOW1DHmbcBf5ou', 'NvxNx', 'test@example.com', 1, 1, 1, 0, null, null, '2023-06-26 17:13:45', null, 1, '2023-06-26 17:13:45', null);
31+
VALUES (1, 'af4c804f-3966-4949-ace2-3bb7416ea926', 'admin', '用户88888', '$2b$12$RJXAtJodRw37ZQGxTPlu0OH.aN5lNXG6yvC4Tp9GIQEBmMY/YCc.m', 'bcNjV', 'admin@example.com', 1, 1, 1, 0, null, null, '2023-06-26 17:13:45', null, 1, '2023-06-26 17:13:45', null);
3132

3233
INSERT INTO fba.sys_user_role (id, user_id, role_id)
3334
VALUES (1, 1, 1);

requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ aiosmtplib==1.1.6
33
alembic==1.7.4
44
APScheduler==3.8.1
55
asgiref==3.7.2
6-
asynccasbin==1.1.8
76
asyncmy==0.2.5
87
bcrypt==3.2.2
9-
casbin_async_sqlalchemy_adapter==1.2.0
8+
casbin==1.23.0
9+
casbin_async_sqlalchemy_adapter==1.3.0
1010
cryptography==41.0.2
1111
email-validator==1.1.3
1212
Faker==9.7.1
@@ -22,7 +22,6 @@ path==15.1.2
2222
pre-commit==3.2.2
2323
psutil==5.9.5
2424
pydantic==1.10.5
25-
pymysql==0.9.3
2625
pytest==7.2.2
2726
pytest-pretty==1.2.0
2827
python-jose==3.3.0

0 commit comments

Comments
 (0)