Joywork/ajax/getSupersetGuestToken.php
2026-07-15 16:33:08 +03:00

92 lines
3.0 KiB
PHP
Raw Permalink 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.

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
require_once($_SERVER['DOCUMENT_ROOT']."/engine/classes/BiAnalytics.php");
if (!isset($_SESSION['id']) || !$_SESSION['id']) {
http_response_code(401);
echo json_encode(array('error' => 'Unauthorized'));
exit;
}
$company_id = isset($_SESSION['agency_id']) ? (int)$_SESSION['agency_id'] : 1;
$user_id = (int)$_SESSION['id'];
//$_SESSION['role_superset'] = 'manager';
if (isset($_SESSION['role_superset']) && !empty($_SESSION['role_superset'])) {
$user_role = $_SESSION['role_superset'];
} else {
if (!empty($_SESSION['agency']) || !empty($_SESSION['users_admin'])) {
$user_role = 'company_head';
} elseif (!empty($_SESSION['manager'])) {
$user_role = 'manager';
} else {
$user_role = 'agent';
}
$_SESSION['role_superset'] = $user_role;
}
$dashboard_uuid = isset($_GET['dashboard_id']) ? $_GET['dashboard_id'] : '';
// Проверка доступа к дашборду
$dashboard_uuid_esc = mysql_real_escape_string($dashboard_uuid);
$company_id_esc = $company_id;
$user_id_esc = $user_id;
if ($user_id == $company_id) {
$sql_check = "
SELECT 1 FROM bi_analytics_dashboard_access
WHERE dashboard_uuid = '$dashboard_uuid_esc'
AND company_id = $company_id_esc
AND is_active = 1
LIMIT 1
";
} else {
$role_level = 0;
if ($user_role == 'agent') $role_level = 1;
elseif ($user_role == 'manager') $role_level = 2;
elseif ($user_role == 'company_head') $role_level = 3;
$sql_check = "
SELECT 1 FROM bi_analytics_dashboard_access
WHERE dashboard_uuid = '$dashboard_uuid_esc'
AND company_id = $company_id_esc
AND is_active = 1
AND (user_id = $user_id_esc OR user_id = 0)
AND role_required <= $role_level
LIMIT 1
";
}
$result_check = mysql_query($sql_check);
if (!$result_check || mysql_num_rows($result_check) == 0) {
http_response_code(403);
echo json_encode(array(
'error' => 'Dashboard access denied',
'role_level' => $user_role
));
exit;
}
// Информация о пользователе
$userInfo = array(
'first_name' => isset($_SESSION['first_name']) ? $_SESSION['first_name'] : 'User',
'last_name' => isset($_SESSION['last_name']) ? $_SESSION['last_name'] : (string)$user_id,
'email' => isset($_SESSION['email']) ? $_SESSION['email'] : null,
);
$bi = new BiAnalytics();
$result = $bi->getGuestToken($dashboard_uuid, $company_id, $user_id, $user_role, $userInfo);
if (isset($result['token'])) {
header('Content-Type: application/json');
echo json_encode(array('token' => $result['token']));
} else {
http_response_code(500);
echo json_encode(array('error' => 'Failed to get guest token', 'details' => $result['error']));
}
file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/log/superset_guest.log',
date('Y-m-d H:i:s') . "\n" . json_encode($result) . "\n\n", FILE_APPEND);