Files
Hyungi Ahn 2b1c7bfb88 feat: 다수 기능 개선 - 순찰, 출근, 작업분석, 모바일 UI 등
- 순찰/점검 기능 개선 (zone-detail 페이지 추가)
- 출근/근태 시스템 개선 (연차 조회, 근무현황)
- 작업분석 대분류 그룹화 및 마이그레이션 스크립트
- 모바일 네비게이션 UI 추가
- NAS 배포 도구 및 문서 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 14:41:01 +09:00

157 lines
4.7 KiB
JavaScript

// /js/auth-check.js
// auth.js의 함수들을 직접 구현 (모듈 의존성 제거)
function isLoggedIn() {
const token = localStorage.getItem('token');
return token && token !== 'undefined' && token !== 'null';
}
function getUser() {
const user = localStorage.getItem('user');
return user ? JSON.parse(user) : null;
}
function clearAuthData() {
localStorage.removeItem('token');
localStorage.removeItem('user');
localStorage.removeItem('userPageAccess'); // 페이지 권한 캐시도 삭제
}
/**
* 현재 페이지의 page_key를 URL 경로로부터 추출
* 예: /pages/work/tbm.html -> work.tbm
* /pages/admin/accounts.html -> admin.accounts
* /pages/dashboard.html -> dashboard
*/
function getCurrentPageKey() {
const path = window.location.pathname;
// /pages/로 시작하는지 확인
if (!path.startsWith('/pages/')) {
return null;
}
// /pages/ 이후 경로 추출
const pagePath = path.substring(7); // '/pages/' 제거
// .html 제거
const withoutExt = pagePath.replace('.html', '');
// 슬래시를 점으로 변환
const pageKey = withoutExt.replace(/\//g, '.');
return pageKey;
}
/**
* 사용자의 페이지 접근 권한 확인 (캐시 활용)
*/
async function checkPageAccess(pageKey) {
const currentUser = getUser();
// Admin은 모든 페이지 접근 가능
if (currentUser.role === 'Admin' || currentUser.role === 'System Admin') {
return true;
}
// 프로필 페이지는 모든 사용자 접근 가능
if (pageKey && pageKey.startsWith('profile.')) {
return true;
}
// 대시보드는 모든 사용자 접근 가능
if (pageKey === 'dashboard') {
return true;
}
try {
// 캐시된 권한 확인
const cached = localStorage.getItem('userPageAccess');
let accessiblePages = null;
if (cached) {
const cacheData = JSON.parse(cached);
// 캐시가 5분 이내인 경우 사용
if (Date.now() - cacheData.timestamp < 5 * 60 * 1000) {
accessiblePages = cacheData.pages;
}
}
// 캐시가 없으면 API 호출
if (!accessiblePages) {
const response = await fetch(`${window.API_BASE_URL}/users/${currentUser.user_id}/page-access`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
if (!response.ok) {
console.error('페이지 권한 조회 실패:', response.status);
return false;
}
const data = await response.json();
accessiblePages = data.data.pageAccess || [];
// 캐시 저장
localStorage.setItem('userPageAccess', JSON.stringify({
pages: accessiblePages,
timestamp: Date.now()
}));
}
// 해당 페이지에 대한 접근 권한 확인
const pageAccess = accessiblePages.find(p => p.page_key === pageKey);
return pageAccess && pageAccess.can_access === 1;
} catch (error) {
console.error('페이지 권한 체크 오류:', error);
return false;
}
}
// 즉시 실행 함수로 스코프를 보호하고 로직을 실행
(async function() {
if (!isLoggedIn()) {
console.log('🚨 인증되지 않은 사용자. 로그인 페이지로 이동합니다.');
clearAuthData(); // 만약을 위해 한번 더 정리
window.location.href = '/index.html';
return; // 이후 코드 실행 방지
}
const currentUser = getUser();
// 사용자 정보가 유효한지 확인 (토큰은 있지만 유저 정보가 깨졌을 경우)
if (!currentUser || !currentUser.username) {
console.error('🚨 사용자 정보가 유효하지 않습니다. 강제 로그아웃 처리합니다.');
clearAuthData();
window.location.href = '/index.html';
return;
}
const userRole = currentUser.role || currentUser.access_level || '사용자';
console.log(`${currentUser.username}(${userRole})님 인증 성공.`);
// 페이지 접근 권한 체크 (Admin은 건너뛰기)
if (currentUser.role !== 'Admin' && currentUser.role !== 'System Admin') {
const pageKey = getCurrentPageKey();
if (pageKey) {
console.log(`🔍 페이지 권한 체크: ${pageKey}`);
const hasAccess = await checkPageAccess(pageKey);
if (!hasAccess) {
console.error(`🚫 페이지 접근 권한이 없습니다: ${pageKey}`);
alert('이 페이지에 접근할 권한이 없습니다.');
window.location.href = '/pages/dashboard.html';
return;
}
console.log(`✅ 페이지 접근 권한 확인됨: ${pageKey}`);
}
}
// 역할 기반 메뉴 제어 로직은 각 컴포넌트 로더(load-navbar.js 등)로 이전함.
// 전역 변수 할당(window.currentUser) 제거.
})();