-- 检查用户权限配置
-- 用于排查登录日志和操作日志权限差异问题

-- 1. 检查菜单权限配置
SELECT 
    menu_id,
    menu_name,
    perms,
    status
FROM sys_menu 
WHERE perms IN ('monitor:logininfor:list', 'monitor:operlog:list')
ORDER BY perms
LIMIT 1000;

-- 2. 检查角色权限关联
SELECT 
    r.role_id,
    r.role_name,
    r.role_key,
    m.menu_name,
    m.perms
FROM sys_role r
INNER JOIN sys_role_menu rm ON r.role_id = rm.role_id
INNER JOIN sys_menu m ON rm.menu_id = m.menu_id
WHERE m.perms IN ('monitor:logininfor:list', 'monitor:operlog:list')
ORDER BY r.role_id, m.perms
LIMIT 1000;

-- 3. 检查用户角色关联
SELECT 
    u.user_id,
    u.user_name,
    r.role_name,
    r.role_key
FROM sys_user u
LEFT JOIN sys_user_role ur ON u.user_id = ur.user_id
LEFT JOIN sys_role r ON ur.role_id = r.role_id
WHERE u.user_name = 'admin'  -- 替换为你的用户名
ORDER BY u.user_id
LIMIT 1000;

-- 4. 检查用户的所有权限（通过角色）
SELECT DISTINCT
    u.user_name,
    m.perms
FROM sys_user u
INNER JOIN sys_user_role ur ON u.user_id = ur.user_id
INNER JOIN sys_role r ON ur.role_id = r.role_id
INNER JOIN sys_role_menu rm ON r.role_id = rm.role_id
INNER JOIN sys_menu m ON rm.menu_id = m.menu_id
WHERE u.user_name = 'admin'  -- 替换为你的用户名
  AND m.perms IN ('monitor:logininfor:list', 'monitor:operlog:list')
ORDER BY m.perms
LIMIT 1000;

-- 5. 如果发现缺少权限，可以使用以下SQL添加权限
-- 注意：需要先确认 menu_id 和 role_id
-- INSERT INTO sys_role_menu (role_id, menu_id) 
-- SELECT role_id, menu_id 
-- FROM sys_role r, sys_menu m 
-- WHERE r.role_key = 'admin'  -- 替换为你的角色
--   AND m.perms = 'monitor:logininfor:list';

