From f98dcb307935a4761d5afb678814b888b4fe7387 Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Sun, 8 Jun 2025 20:16:02 +0800 Subject: [PATCH] Add OAuth2 user to auto bind a role --- backend/app/admin/crud/crud_user.py | 5 +++++ backend/plugin/oauth2/plugin.toml | 2 +- backend/sql/mysql/init_test_data.sql | 5 ++++- backend/sql/postgresql/init_test_data.sql | 5 ++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backend/app/admin/crud/crud_user.py b/backend/app/admin/crud/crud_user.py index 25f03074c..6f9f9b98d 100644 --- a/backend/app/admin/crud/crud_user.py +++ b/backend/app/admin/crud/crud_user.py @@ -94,6 +94,11 @@ async def add_by_oauth2(self, db: AsyncSession, obj: AddOAuth2UserParam) -> None dict_obj = obj.model_dump() dict_obj.update({'is_staff': True, 'salt': salt}) new_user = self.model(**dict_obj) + + stmt = select(Role) + role = await db.execute(stmt) + new_user.roles = [role.scalars().first()] # 默认绑定第一个角色 + db.add(new_user) async def update(self, db: AsyncSession, input_user: User, obj: UpdateUserParam) -> int: diff --git a/backend/plugin/oauth2/plugin.toml b/backend/plugin/oauth2/plugin.toml index 6e5686137..0f3094d5c 100644 --- a/backend/plugin/oauth2/plugin.toml +++ b/backend/plugin/oauth2/plugin.toml @@ -1,6 +1,6 @@ [plugin] summary = 'OAuth 2.0' -version = '0.0.2' +version = '0.0.3' description = '通过 OAuth 2.0 的方式登录系统' author = 'wu-clan' diff --git a/backend/sql/mysql/init_test_data.sql b/backend/sql/mysql/init_test_data.sql index af1086ab0..8d09300c8 100644 --- a/backend/sql/mysql/init_test_data.sql +++ b/backend/sql/mysql/init_test_data.sql @@ -29,7 +29,10 @@ insert into sys_role (id, name, status, is_filter_scopes, remark, created_time, values (1, 'test', 1, 1, null, '2023-06-26 17:13:45', null); insert into sys_role_menu (id, role_id, menu_id) -values (1, 1, 1); +values (1, 1, 1), + (2, 1, 2), + (3, 1, 3), + (4, 1, 4); insert into 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', 'admin', '用户88888', '$2b$12$8y2eNucX19VjmZ3tYhBLcOsBwy9w1IjBQE4SSqwMDL5bGQVp2wqS.', 0x24326224313224387932654E7563583139566A6D5A33745968424C634F, 'admin@example.com', 1, 1, 1, 1, null, null, '2023-06-26 17:13:45', '2024-11-18 13:53:57', 1, '2023-06-26 17:13:45', '2024-11-18 13:53:57'); diff --git a/backend/sql/postgresql/init_test_data.sql b/backend/sql/postgresql/init_test_data.sql index 1c6c78972..e107e358e 100644 --- a/backend/sql/postgresql/init_test_data.sql +++ b/backend/sql/postgresql/init_test_data.sql @@ -29,7 +29,10 @@ insert into sys_role (id, name, status, is_filter_scopes, remark, created_time, values (1, 'test', 1, 1, null, '2023-06-26 17:13:45', null); insert into sys_role_menu (id, role_id, menu_id) -values (1, 1, 1); +values (1, 1, 1), + (2, 1, 2), + (3, 1, 3), + (4, 1, 4); insert into 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', 'admin', '用户88888', '$2b$12$8y2eNucX19VjmZ3tYhBLcOsBwy9w1IjBQE4SSqwMDL5bGQVp2wqS.', '0x24326224313224387932654E7563583139566A6D5A33745968424C634F', 'admin@example.com', 1, 1, 1, 1, null, null, '2023-06-26 17:13:45', '2024-11-18 13:53:57', 1, '2023-06-26 17:13:45', '2024-11-18 13:53:57');