Files
leaudit-platform-frontend/sql/test_user_setup.sql
T

46 lines
1.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 为测试用户添加默认角色的脚本
-- 测试用户信息:sub='001', username='testuser1'
-- 首先查找测试用户
DO $$
DECLARE
test_user_id uuid;
existing_role_count integer;
BEGIN
-- 查找测试用户的ID
SELECT id INTO test_user_id
FROM sso_users
WHERE sub = '001' AND username = 'testuser1';
IF test_user_id IS NOT NULL THEN
-- 检查用户是否已经有common角色(role_id = 2
SELECT COUNT(*) INTO existing_role_count
FROM user_role
WHERE user_id = test_user_id AND role_id = 2;
IF existing_role_count = 0 THEN
-- 为用户添加common角色
INSERT INTO user_role (user_id, role_id)
VALUES (test_user_id, 2);
RAISE NOTICE '已为测试用户(%)添加默认角色(common)', test_user_id;
ELSE
RAISE NOTICE '测试用户(%)已经拥有默认角色(common)', test_user_id;
END IF;
ELSE
RAISE NOTICE '测试用户不存在,请先创建用户';
END IF;
END $$;
-- 验证结果
SELECT
u.id,
u.sub,
u.username,
u.nick_name,
r.role_key,
r.role_name
FROM sso_users u
LEFT JOIN user_role ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.sub = '001' AND u.username = 'testuser1';