316 lines
14 KiB
Dart
316 lines
14 KiB
Dart
/// Admin presentation: system settings (password policy and token expiry).
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../core/theme/app_theme.dart';
|
|
import '../../../data/repositories/admin_repository_impl.dart';
|
|
import '../../auth/bloc/auth_bloc.dart';
|
|
|
|
/// Admin-only screen to view and update password policy and JWT expiry settings.
|
|
class SettingsScreen extends StatefulWidget {
|
|
const SettingsScreen({super.key, required this.adminRepo});
|
|
|
|
final AdminRepositoryImpl adminRepo;
|
|
|
|
@override
|
|
State<SettingsScreen> createState() => _SettingsScreenState();
|
|
}
|
|
|
|
/// Loads settings on init and persists changes via [AdminRepositoryImpl].
|
|
class _SettingsScreenState extends State<SettingsScreen> {
|
|
SystemSettings? _settings;
|
|
bool _loading = true;
|
|
String? _error;
|
|
bool _saving = false;
|
|
|
|
final _minLengthController = TextEditingController();
|
|
final _accessExpiryController = TextEditingController();
|
|
final _refreshExpiryController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_minLengthController.dispose();
|
|
_accessExpiryController.dispose();
|
|
_refreshExpiryController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
/// Fetches current system settings and populates form controllers.
|
|
Future<void> _load() async {
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
final s = await widget.adminRepo.getSettings();
|
|
if (mounted) {
|
|
setState(() {
|
|
_settings = s;
|
|
_minLengthController.text = s.passwordMinLength.toString();
|
|
_accessExpiryController.text = s.accessTokenExpiry;
|
|
_refreshExpiryController.text = s.refreshTokenExpiry;
|
|
_loading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() {
|
|
_error = e.toString().replaceFirst('Exception: ', '');
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Validates form values and submits updated settings to the API.
|
|
Future<void> _save() async {
|
|
final minLen = int.tryParse(_minLengthController.text.trim());
|
|
if (minLen == null || minLen < 1 || minLen > 128) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Password minimum length must be between 1 and 128')),
|
|
);
|
|
return;
|
|
}
|
|
final accessExpiry = _accessExpiryController.text.trim();
|
|
final refreshExpiry = _refreshExpiryController.text.trim();
|
|
if (accessExpiry.isEmpty || refreshExpiry.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Token expiry values are required (e.g. 15m, 7d)')),
|
|
);
|
|
return;
|
|
}
|
|
setState(() => _saving = true);
|
|
try {
|
|
final updated = SystemSettings(
|
|
passwordMinLength: minLen,
|
|
passwordRequireUppercase: _settings!.passwordRequireUppercase,
|
|
passwordRequireLowercase: _settings!.passwordRequireLowercase,
|
|
passwordRequireNumber: _settings!.passwordRequireNumber,
|
|
passwordRequireSpecial: _settings!.passwordRequireSpecial,
|
|
accessTokenExpiry: accessExpiry,
|
|
refreshTokenExpiry: refreshExpiry,
|
|
);
|
|
final s = await widget.adminRepo.updateSettings(updated);
|
|
if (mounted) {
|
|
setState(() {
|
|
_settings = s;
|
|
_saving = false;
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Settings saved')),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() => _saving = false);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(e.toString().replaceFirst('Exception: ', ''))),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final isAdmin = context.read<AuthBloc>().state.isAdmin;
|
|
if (!isAdmin) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Settings')),
|
|
body: const Center(child: Text('Access denied. Admin only.')),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Settings'),
|
|
flexibleSpace: Container(
|
|
decoration: const BoxDecoration(gradient: AppTheme.goldGradient),
|
|
),
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back_rounded),
|
|
onPressed: () => context.pop(),
|
|
),
|
|
),
|
|
body: _loading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _error != null
|
|
? Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(_error!),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: _load, child: const Text('Retry')),
|
|
],
|
|
),
|
|
),
|
|
)
|
|
: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
ExpansionTile(
|
|
initiallyExpanded: false,
|
|
leading: Icon(Icons.password_rounded, color: AppColors.goldPrimary),
|
|
title: Text(
|
|
'Password policy',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.goldPrimary,
|
|
),
|
|
),
|
|
subtitle: const Text('Minimum length and character requirements'),
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextField(
|
|
controller: _minLengthController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Minimum length',
|
|
border: OutlineInputBorder(),
|
|
helperText: 'Minimum password length (e.g. 8)',
|
|
),
|
|
keyboardType: TextInputType.number,
|
|
),
|
|
const SizedBox(height: 8),
|
|
CheckboxListTile(
|
|
title: const Text('Require uppercase letter'),
|
|
value: _settings!.passwordRequireUppercase,
|
|
onChanged: (v) => setState(() {
|
|
_settings = SystemSettings(
|
|
passwordMinLength: _settings!.passwordMinLength,
|
|
passwordRequireUppercase: v ?? true,
|
|
passwordRequireLowercase: _settings!.passwordRequireLowercase,
|
|
passwordRequireNumber: _settings!.passwordRequireNumber,
|
|
passwordRequireSpecial: _settings!.passwordRequireSpecial,
|
|
accessTokenExpiry: _settings!.accessTokenExpiry,
|
|
refreshTokenExpiry: _settings!.refreshTokenExpiry,
|
|
);
|
|
}),
|
|
),
|
|
CheckboxListTile(
|
|
title: const Text('Require lowercase letter'),
|
|
value: _settings!.passwordRequireLowercase,
|
|
onChanged: (v) => setState(() {
|
|
_settings = SystemSettings(
|
|
passwordMinLength: _settings!.passwordMinLength,
|
|
passwordRequireUppercase: _settings!.passwordRequireUppercase,
|
|
passwordRequireLowercase: v ?? true,
|
|
passwordRequireNumber: _settings!.passwordRequireNumber,
|
|
passwordRequireSpecial: _settings!.passwordRequireSpecial,
|
|
accessTokenExpiry: _settings!.accessTokenExpiry,
|
|
refreshTokenExpiry: _settings!.refreshTokenExpiry,
|
|
);
|
|
}),
|
|
),
|
|
CheckboxListTile(
|
|
title: const Text('Require number'),
|
|
value: _settings!.passwordRequireNumber,
|
|
onChanged: (v) => setState(() {
|
|
_settings = SystemSettings(
|
|
passwordMinLength: _settings!.passwordMinLength,
|
|
passwordRequireUppercase: _settings!.passwordRequireUppercase,
|
|
passwordRequireLowercase: _settings!.passwordRequireLowercase,
|
|
passwordRequireNumber: v ?? true,
|
|
passwordRequireSpecial: _settings!.passwordRequireSpecial,
|
|
accessTokenExpiry: _settings!.accessTokenExpiry,
|
|
refreshTokenExpiry: _settings!.refreshTokenExpiry,
|
|
);
|
|
}),
|
|
),
|
|
CheckboxListTile(
|
|
title: const Text('Require special character'),
|
|
value: _settings!.passwordRequireSpecial,
|
|
onChanged: (v) => setState(() {
|
|
_settings = SystemSettings(
|
|
passwordMinLength: _settings!.passwordMinLength,
|
|
passwordRequireUppercase: _settings!.passwordRequireUppercase,
|
|
passwordRequireLowercase: _settings!.passwordRequireLowercase,
|
|
passwordRequireNumber: _settings!.passwordRequireNumber,
|
|
passwordRequireSpecial: v ?? true,
|
|
accessTokenExpiry: _settings!.accessTokenExpiry,
|
|
refreshTokenExpiry: _settings!.refreshTokenExpiry,
|
|
);
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 8),
|
|
ExpansionTile(
|
|
initiallyExpanded: false,
|
|
leading: Icon(Icons.timer_outlined, color: AppColors.goldPrimary),
|
|
title: Text(
|
|
'Token expiration',
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
fontWeight: FontWeight.w600,
|
|
color: AppColors.goldPrimary,
|
|
),
|
|
),
|
|
subtitle: const Text('Access and refresh token validity'),
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
'Use values like 15m, 1h, 7d (minutes, hours, days). New tokens will use these values.',
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _accessExpiryController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Access token expiry',
|
|
border: OutlineInputBorder(),
|
|
helperText: 'e.g. 15m, 1h',
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: _refreshExpiryController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Refresh token expiry',
|
|
border: OutlineInputBorder(),
|
|
helperText: 'e.g. 7d, 30d',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 24),
|
|
FilledButton(
|
|
onPressed: _saving ? null : _save,
|
|
child: _saving
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('Save settings'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|