- .env.development.example과 lib/core/config/environment.dart, lib/core/permissions/permission_manager.dart에서 PERMISSION__ 폴백을 view 전용으로 좁히고 기본 정책을 명시적으로 거부하도록 재정비했다 - lib/core/navigation/*, lib/core/routing/app_router.dart, lib/widgets/app_shell.dart, lib/main.dart에서 메뉴 매니페스트·카탈로그를 도입해 /menus 응답을 캐싱하고 라우터·사이드바·Breadcrumb가 동일 menu_code/route_path를 쓰도록 리팩터링했다 - lib/core/permissions/permission_resources.dart와 그룹 권한/메뉴 마스터 모듈을 menu_code 기반 CRUD 및 Catalog 경로 정합성 검사로 전환하고 PermissionSynchronizer·PermissionBootstrapper를 확장했다 - test/helpers/test_permissions.dart, test/widgets/app_shell_test.dart 등 신규 구조를 반영하는 테스트·골든과 doc/frontend_menu_permission_tasks.md 문서를 보강했다
133 lines
3.9 KiB
Dart
133 lines
3.9 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
|
|
import 'package:superport_v2/core/config/feature_flags.dart';
|
|
|
|
/// 환경 설정 로더
|
|
///
|
|
/// - .env.development / .env.production 파일을 로드하여 런타임 설정을 주입한다.
|
|
/// - `--dart-define=ENV=production` 형태로 빌드/실행 시 환경을 지정한다.
|
|
/// - 주요 키: `API_BASE_URL`, `FEATURE_*` 플래그들.
|
|
class Environment {
|
|
Environment._();
|
|
|
|
/// 현재 환경명 (development | production)
|
|
static late final String envName;
|
|
|
|
/// API 서버 베이스 URL
|
|
static late final String baseUrl;
|
|
|
|
/// 프로덕션 여부
|
|
static late final bool isProduction;
|
|
|
|
/// 환경 변수에서 파싱한 리소스별 권한 집합.
|
|
static final Map<String, Set<String>> _permissions = {};
|
|
|
|
/// 환경 초기화
|
|
///
|
|
/// - 기본 환경은 development이며, `ENV` dart-define 으로 변경 가능
|
|
/// - 해당 환경의 .env 파일을 로드하고 핵심 값을 추출한다.
|
|
static Future<void> initialize() async {
|
|
const envFromDefine = String.fromEnvironment(
|
|
'ENV',
|
|
defaultValue: 'development',
|
|
);
|
|
envName = envFromDefine.toLowerCase();
|
|
isProduction = envName == 'production';
|
|
|
|
final candidates = ['.env.$envName', 'assets/.env.$envName'];
|
|
var initialized = false;
|
|
|
|
for (final fileName in candidates) {
|
|
if (initialized) {
|
|
break;
|
|
}
|
|
try {
|
|
await dotenv.load(fileName: fileName);
|
|
initialized = true;
|
|
} catch (e) {
|
|
if (kDebugMode) {
|
|
// 웹 번들 자산(.env.*)까지 순차 시도 후 실패 시에만 기본값으로 폴백한다.
|
|
// ignore: avoid_print
|
|
print('[Environment] $fileName 로드 실패: $e');
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!initialized) {
|
|
dotenv.testLoad();
|
|
}
|
|
|
|
baseUrl = dotenv.maybeGet('API_BASE_URL') ?? 'http://localhost:8080';
|
|
FeatureFlags.initialize();
|
|
_loadPermissions();
|
|
}
|
|
|
|
/// 기능 플래그 조회 (기본 false)
|
|
static bool flag(String key, {bool defaultValue = false}) {
|
|
final v = dotenv.maybeGet(key);
|
|
if (v == null) return defaultValue;
|
|
switch (v.trim().toLowerCase()) {
|
|
case '1':
|
|
case 'y':
|
|
case 'yes':
|
|
case 'true':
|
|
return true;
|
|
case '0':
|
|
case 'n':
|
|
case 'no':
|
|
case 'false':
|
|
return false;
|
|
default:
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/// `.env` 파일에서 `PERMISSION__*` 키를 파싱해 권한 맵을 구성한다.
|
|
static void _loadPermissions() {
|
|
_permissions.clear();
|
|
for (final entry in dotenv.env.entries) {
|
|
const prefix = 'PERMISSION__';
|
|
if (!entry.key.startsWith(prefix)) {
|
|
continue;
|
|
}
|
|
final resource = entry.key.substring(prefix.length).toLowerCase();
|
|
// 콤마 구분 문자열을 소문자/trim 처리해 비교를 일관되게 맞춘다.
|
|
final values = entry.value
|
|
.split(',')
|
|
.map((token) => token.trim().toLowerCase())
|
|
.where((token) => token.isNotEmpty)
|
|
.toSet();
|
|
_permissions[resource] = values;
|
|
}
|
|
}
|
|
|
|
/// 환경에 설정된 권한이 있는 경우 해당 액션 허용 여부를 반환한다.
|
|
static bool hasPermission(String resource, String action) {
|
|
final actions = _permissions[resource.toLowerCase()];
|
|
if (actions == null || actions.isEmpty) {
|
|
return false;
|
|
}
|
|
if (actions.contains('all')) {
|
|
// all 키워드는 모든 액션 허용을 의미한다.
|
|
return true;
|
|
}
|
|
return actions.contains(action.toLowerCase());
|
|
}
|
|
|
|
/// 테스트에서 환경 권한 맵을 직접 오버라이드하기 위한 헬퍼.
|
|
@visibleForTesting
|
|
static void setTestPermissions(Map<String, Set<String>> permissions) {
|
|
_permissions
|
|
..clear()
|
|
..addAll(
|
|
permissions.map(
|
|
(key, value) => MapEntry(
|
|
key.toLowerCase(),
|
|
value.map((action) => action.toLowerCase()).toSet(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|