# Copyright (c) 2025, JINGROW and contributors
# For license information, please see license.txt

"""
User 相关白名单函数 - 转发到 SaaS 端
"""

import jingrow
import requests
import logging
from jingrow.config import Config
from jingrow.utils.auth import get_request_session_cookie

logger = logging.getLogger(__name__)


@jingrow.whitelist()
def get_all_roles(**kwargs):
    """获取所有角色 - 转发到 SaaS 端"""
    url = f"{Config.jingrow_server_url}/api/action/jingrow.core.pagetype.user.user.get_all_roles"
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    
    session_cookie = get_request_session_cookie()
    if session_cookie:
        headers["Cookie"] = f"sid={session_cookie}"
    
    resp = requests.post(url, headers=headers, json=kwargs, timeout=30)
    if resp.status_code == 200:
        return resp.json().get("message", [])
    logger.error(f"[user.get_all_roles] SaaS error: {resp.status_code}")
    return []
