Files
aler_dashboard_git/auth.php
T
2026-07-09 16:31:30 +08:00

164 lines
5.3 KiB
PHP

<?php
session_start();
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Pragma: no-cache");
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
// 兼容低版本str_contains
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle === '' || strpos($haystack, $needle) !== false;
}
}
$expireTime = 1800;
// 未登录直接跳转登录页
if (empty($_SESSION['user_id']) || empty($_SESSION['login_time']) || (time() - $_SESSION['login_time']) > $expireTime) {
session_destroy();
header("Location: login.php");
exit;
}
$_SESSION['login_time'] = time();
// 登录用户基础信息
$loginUserId = $_SESSION['user_id'];
$userName = htmlspecialchars($_SESSION['username'] ?? '');
$isAdmin = intval($_SESSION['is_admin'] ?? 0);
$userRoleId = intval($_SESSION['role_id'] ?? 0);
// ====================== 双库固定连接 ======================
$dbHost = "10.150.117.190";
$dbUser = "root";
$dbPass = "hp93000";
// 业务库 alert_mail_stat(工单、SMTP、大盘)
$dbAlertName = "alert_mail_stat";
$connAlert = mysqli_connect($dbHost, $dbUser, $dbPass, $dbAlertName);
if (!$connAlert) {
die("业务库alert_mail_stat连接失败:" . mysqli_connect_error());
}
mysqli_set_charset($connAlert, "utf8mb4");
// 权限库 monitor(用户、角色、权限、消息)
$dbMonitorName = "monitor";
$connMonitor = mysqli_connect($dbHost, $dbUser, $dbPass, $dbMonitorName);
if (!$connMonitor) {
die("权限库monitor连接失败:" . mysqli_connect_error());
}
mysqli_set_charset($connMonitor, "utf8mb4");
// ========================================================================
// ========== 新增:读取角色真实名称 ==========
$roleRealName = "未知角色";
if ($userRoleId > 0) {
$getRoleSql = "SELECT role_name FROM sys_role WHERE id = $userRoleId";
$roleResult = mysqli_query($connMonitor, $getRoleSql);
if ($roleResult && $roleRow = mysqli_fetch_assoc($roleResult)) {
$roleRealName = $roleRow['role_name'];
}
}
// 超级管理员强制覆盖文字
if ($isAdmin === 1) {
$roleRealName = "超级管理员";
}
// ===========================================
// 1. 登录时一次性加载当前角色全部权限,存入全局变量(侧边栏共用)
$allowPages = [];
if ($userRoleId > 0) {
$permSql = "SELECT page_key FROM sys_role_permission WHERE role_id = $userRoleId";
$permRes = mysqli_query($connMonitor, $permSql);
if ($permRes) {
while ($row = mysqli_fetch_assoc($permRes)) {
$allowPages[] = $row['page_key'];
}
}
}
// 2. 获取当前访问页面文件名(去掉.php后缀)
$currentPage = basename($_SERVER['SCRIPT_NAME'], '.php');
// 3. 非管理员自动拦截无权限页面(全局统一校验,无需每个页面重复写)
if ($isAdmin !== 1 && !in_array($currentPage, $allowPages)) {
echo "<script>alert('无访问权限!');history.back();</script>";
exit;
}
// 读取右上角未读消息数量
$unreadMsgCount = 0;
$msgSql = "SELECT COUNT(id) cnt FROM sys_user_msg WHERE user_id = $loginUserId AND is_read = 0";
$msgRes = mysqli_query($connMonitor, $msgSql);
if ($msgRes) {
$msgRow = mysqli_fetch_assoc($msgRes);
$unreadMsgCount = intval($msgRow['cnt']);
}
// 读取系统基础配置
$sysConfig = [];
$configSql = "SELECT k, v FROM sys_config";
$configRes = mysqli_query($connAlert, $configSql);
if ($configRes) {
while ($row = mysqli_fetch_assoc($configRes)) {
$sysConfig[$row['k']] = $row['v'];
}
}
$currentLang = $sysConfig['lang'] ?? "zh";
$sidebarRawLinks = $sysConfig['quick_link'] ?? "";
// 多语言字典
$langMap = [
'zh' => [
'html_lang' => 'zh-CN',
'page_title' => '运维监控平台',
'monitor_panel' => '监控面板',
'alert_overview' => '告警总览',
'quick_link' => '快捷外链',
'system_manage' => '系统管理',
'notify_config' => '消息通知配置',
'role_manage' => '角色管理',
'user_manage' => '用户管理',
'current_user' => '当前用户:',
'sys_time' => '系统时间:',
'refresh' => '刷新页面',
'logout' => '退出登录',
'unread_msg' => '未读消息'
],
'en' => [
'html_lang' => 'en',
'page_title' => "Monitor Platform",
'monitor_panel' => "Monitor Panel",
'alert_overview' => "Alert Dashboard",
'quick_link' => "Quick Link",
'system_manage' => "System Manage",
'notify_config' => "Notify Config",
'role_manage' => "Role Manage",
'user_manage' => "User Manage",
'current_user' => "User: ",
'sys_time' => "Time: ",
'refresh' => "Refresh",
'logout' => "Logout",
'unread_msg' => "Unread Msg"
]
];
$t = $langMap[$currentLang];
// 解析侧边栏外链
$sidebarLinkList = [];
if (!empty($sidebarRawLinks)) {
$lines = explode("\n", $sidebarRawLinks);
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) continue;
$item = explode("|", $line);
if (count($item) === 2) {
$sidebarLinkList[] = [
'name' => trim($item[0]),
'url' => trim($item[1])
];
}
}
}
// 【已删除冲突无用函数 hasPagePerm】
?>