Skip to content

Commit b415dc8

Browse files
authored
Add OAuth2 user to auto bind a role (#656)
1 parent 05b74d7 commit b415dc8

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

backend/app/admin/crud/crud_user.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ async def add_by_oauth2(self, db: AsyncSession, obj: AddOAuth2UserParam) -> None
9494
dict_obj = obj.model_dump()
9595
dict_obj.update({'is_staff': True, 'salt': salt})
9696
new_user = self.model(**dict_obj)
97+
98+
stmt = select(Role)
99+
role = await db.execute(stmt)
100+
new_user.roles = [role.scalars().first()] # 默认绑定第一个角色
101+
97102
db.add(new_user)
98103

99104
async def update(self, db: AsyncSession, input_user: User, obj: UpdateUserParam) -> int:

backend/plugin/oauth2/plugin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[plugin]
22
summary = 'OAuth 2.0'
3-
version = '0.0.2'
3+
version = '0.0.3'
44
description = '通过 OAuth 2.0 的方式登录系统'
55
author = 'wu-clan'
66

backend/sql/mysql/init_test_data.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ insert into sys_role (id, name, status, is_filter_scopes, remark, created_time,
2929
values (1, 'test', 1, 1, null, '2023-06-26 17:13:45', null);
3030

3131
insert into sys_role_menu (id, role_id, menu_id)
32-
values (1, 1, 1);
32+
values (1, 1, 1),
33+
(2, 1, 2),
34+
(3, 1, 3),
35+
(4, 1, 4);
3336

3437
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)
3538
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');

backend/sql/postgresql/init_test_data.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ insert into sys_role (id, name, status, is_filter_scopes, remark, created_time,
2929
values (1, 'test', 1, 1, null, '2023-06-26 17:13:45', null);
3030

3131
insert into sys_role_menu (id, role_id, menu_id)
32-
values (1, 1, 1);
32+
values (1, 1, 1),
33+
(2, 1, 2),
34+
(3, 1, 3),
35+
(4, 1, 4);
3336

3437
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)
3538
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');

0 commit comments

Comments
 (0)