From 0b95f2c61ebcba9c7b05376dc86a0a9f19562a5e Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Tue, 8 Aug 2023 17:27:20 +0800 Subject: [PATCH] Update asynccasbin to casbin async api (#199) * Update asynccasbin to casbin async api * remove pymysql dependency * update the test-user test to admin --- backend/app/common/rbac.py | 4 ++-- backend/app/services/casbin_service.py | 10 +++++----- backend/app/services/menu_service.py | 4 ++-- backend/app/tests/api_v1/test_auth.py | 4 ++-- backend/app/tests/conftest.py | 2 +- backend/sql/init_test_data.sql | 7 ++++--- requirements.txt | 5 ++--- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/backend/app/common/rbac.py b/backend/app/common/rbac.py index 349e29d4..a158e70e 100644 --- a/backend/app/common/rbac.py +++ b/backend/app/common/rbac.py @@ -15,14 +15,14 @@ from backend.app.models.sys_casbin_rule import CasbinRule class RBAC: @staticmethod - async def enforcer() -> casbin.Enforcer: + async def enforcer() -> casbin.AsyncEnforcer: """ 获取 casbin 执行器 :return: """ adapter = casbin_async_sqlalchemy_adapter.Adapter(async_engine, db_class=CasbinRule) - enforcer = casbin.Enforcer(RBAC_MODEL_CONF, adapter) + enforcer = casbin.AsyncEnforcer(RBAC_MODEL_CONF, adapter) await enforcer.load_policy() return enforcer diff --git a/backend/app/services/casbin_service.py b/backend/app/services/casbin_service.py index a9493b9d..064e3cc6 100644 --- a/backend/app/services/casbin_service.py +++ b/backend/app/services/casbin_service.py @@ -25,13 +25,13 @@ class CasbinService: @staticmethod async def get_policy_list(): enforcer = await RBAC.enforcer() - data = await enforcer.get_policy() + data = enforcer.get_policy() return data @staticmethod async def get_policy_list_by_role(*, role: str): enforcer = await RBAC.enforcer() - data = await enforcer.get_filtered_named_policy('p', 0, role) + data = enforcer.get_filtered_named_policy('p', 0, role) return data @staticmethod @@ -53,7 +53,7 @@ class CasbinService: @staticmethod async def update_policy(*, old: UpdatePolicy, new: UpdatePolicy): enforcer = await RBAC.enforcer() - _p = await enforcer.has_policy(old.sub, old.path, old.method) + _p = enforcer.has_policy(old.sub, old.path, old.method) if not _p: raise errors.NotFoundError(msg='权限不存在') data = await enforcer.update_policy([old.sub, old.path, old.method], [new.sub, new.path, new.method]) @@ -70,7 +70,7 @@ class CasbinService: @staticmethod async def delete_policy(*, p: DeletePolicy): enforcer = await RBAC.enforcer() - _p = await enforcer.has_policy(p.sub, p.path, p.method) + _p = enforcer.has_policy(p.sub, p.path, p.method) if not _p: raise errors.NotFoundError(msg='权限不存在') data = await enforcer.remove_policy(p.sub, p.path, p.method) @@ -115,7 +115,7 @@ class CasbinService: @staticmethod async def delete_group(*, g: DeleteUserRole): enforcer = await RBAC.enforcer() - _g = await enforcer.has_grouping_policy(g.uuid, g.role) + _g = enforcer.has_grouping_policy(g.uuid, g.role) if not _g: raise errors.NotFoundError(msg='权限不存在') data = await enforcer.remove_grouping_policy(g.uuid, g.role) diff --git a/backend/app/services/menu_service.py b/backend/app/services/menu_service.py index c00ac93b..aabb5573 100644 --- a/backend/app/services/menu_service.py +++ b/backend/app/services/menu_service.py @@ -44,8 +44,8 @@ class MenuService: if roles: for role in roles: menu_ids.extend([menu.id for menu in role.menus]) - menu_select = await MenuDao.get_role_menus(db, request.user.is_superuser, menu_ids) - menu_tree = await get_tree_data(menu_select) + menu_select = await MenuDao.get_role_menus(db, request.user.is_superuser, menu_ids) + menu_tree = await get_tree_data(menu_select) return menu_tree @staticmethod diff --git a/backend/app/tests/api_v1/test_auth.py b/backend/app/tests/api_v1/test_auth.py index 3b24633a..345a8b7a 100644 --- a/backend/app/tests/api_v1/test_auth.py +++ b/backend/app/tests/api_v1/test_auth.py @@ -7,8 +7,8 @@ from backend.app.core.conf import settings def test_login(client: TestClient) -> None: data = { - 'username': 'test', - 'password': 'test', + 'username': 'admin', + 'password': '123456', } response = client.post(f'{settings.API_V1_STR}/auth/login', json=data) assert response.status_code == 200 diff --git a/backend/app/tests/conftest.py b/backend/app/tests/conftest.py index f412605b..74f646d7 100644 --- a/backend/app/tests/conftest.py +++ b/backend/app/tests/conftest.py @@ -25,4 +25,4 @@ def client() -> Generator: @pytest.fixture(scope='module') def token_headers(client: TestClient) -> Dict[str, str]: - return get_token_headers(client=client, username='test', password='test') + return get_token_headers(client=client, username='admin', password='123456') diff --git a/backend/sql/init_test_data.sql b/backend/sql/init_test_data.sql index d82ba2a6..603d144a 100644 --- a/backend/sql/init_test_data.sql +++ b/backend/sql/init_test_data.sql @@ -3,8 +3,8 @@ VALUES (1, 'test', 0, 0, null, null, null, 1, 0, null, '2023-06-26 17:13:45', nu 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) 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'), - (2, '仪表盘', 'dashboard', 0, 0, 'IconDashboard', 'dashboard', 1, '/dashboard/workplace/index.vue', null, 1, 1, 1, null, null, '2023-07-27 19:15:45', null), - (3, '工作台', 'Workplace', 0, 0, null, 'workplace', 0, null, null, 1, 1, 1, null, 2, '2023-07-27 19:17:59', null), + (2, '仪表盘', 'dashboard', 0, 0, 'IconDashboard', 'dashboard', 0, null, null, 1, 1, 1, null, null, '2023-07-27 19:15:45', null), + (3, '工作台', 'Workplace', 0, 0, null, 'workplace', 1, '/dashboard/workplace/index.vue', null, 1, 1, 1, null, 2, '2023-07-27 19:17:59', null), (4, 'arco官网', 'arcoWebsite', 0, 888, 'IconLink', 'https://arco.design', 1, null, null, 1, 1, 1, null, null, '2023-07-27 19:19:23', null), (5, '日志', 'log', 0, 66, 'IconBug', 'log', 0, null, null, 1, 1, 1, null, null, '2023-07-27 19:19:59', null), (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); INSERT INTO fba.sys_role_menu (id, role_id, menu_id) VALUES (1, 1, 1); +-- 密码明文:123456 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) -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); +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); INSERT INTO fba.sys_user_role (id, user_id, role_id) VALUES (1, 1, 1); diff --git a/requirements.txt b/requirements.txt index 70a7bb21..c3bdba91 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,10 +3,10 @@ aiosmtplib==1.1.6 alembic==1.7.4 APScheduler==3.8.1 asgiref==3.7.2 -asynccasbin==1.1.8 asyncmy==0.2.5 bcrypt==3.2.2 -casbin_async_sqlalchemy_adapter==1.2.0 +casbin==1.23.0 +casbin_async_sqlalchemy_adapter==1.3.0 cryptography==41.0.2 email-validator==1.1.3 Faker==9.7.1 @@ -22,7 +22,6 @@ path==15.1.2 pre-commit==3.2.2 psutil==5.9.5 pydantic==1.10.5 -pymysql==0.9.3 pytest==7.2.2 pytest-pretty==1.2.0 python-jose==3.3.0