You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.5 KiB
177 lines
5.5 KiB
import 'package:flutter/material.dart';
|
|
import 'package:youmazgestion/Components/app_bar.dart';
|
|
import 'package:youmazgestion/Models/Permission.dart';
|
|
import 'package:youmazgestion/Services/app_database.dart';
|
|
import 'package:youmazgestion/Models/role.dart';
|
|
|
|
class RolePermissionsPage extends StatefulWidget {
|
|
final Role role;
|
|
|
|
const RolePermissionsPage({super.key, required this.role});
|
|
|
|
@override
|
|
State<RolePermissionsPage> createState() => _RolePermissionsPageState();
|
|
}
|
|
|
|
class _RolePermissionsPageState extends State<RolePermissionsPage> {
|
|
final db = AppDatabase.instance;
|
|
List<Permission> permissions = [];
|
|
List<Map<String, dynamic>> menus = [];
|
|
Map<int, Map<String, bool>> menuPermissionsMap = {};
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initData();
|
|
}
|
|
|
|
Future<void> _initData() async {
|
|
final perms = await db.getAllPermissions();
|
|
final menuList = await db.database.then((db) => db.query('menu'));
|
|
|
|
Map<int, Map<String, bool>> tempMenuPermissionsMap = {};
|
|
|
|
for (var menu in menuList) {
|
|
final menuId = menu['id'] as int;
|
|
final menuPerms = await db.getPermissionsForRoleAndMenu(
|
|
widget.role.id!, menuId);
|
|
|
|
tempMenuPermissionsMap[menuId] = {
|
|
for (var perm in perms)
|
|
perm.name: menuPerms.any((mp) => mp.name == perm.name)
|
|
};
|
|
}
|
|
|
|
setState(() {
|
|
permissions = perms;
|
|
menus = menuList;
|
|
menuPermissionsMap = tempMenuPermissionsMap;
|
|
});
|
|
}
|
|
|
|
Future<void> _onPermissionToggle(
|
|
int menuId, String permission, bool enabled) async {
|
|
final perm = permissions.firstWhere((p) => p.name == permission);
|
|
|
|
if (enabled) {
|
|
await db.assignRoleMenuPermission(
|
|
widget.role.id!, menuId, perm.id!);
|
|
} else {
|
|
await db.removeRoleMenuPermission(
|
|
widget.role.id!, menuId, perm.id!);
|
|
}
|
|
|
|
setState(() {
|
|
menuPermissionsMap[menuId]![permission] = enabled;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: CustomAppBar(
|
|
title: "Permissions - ${widget.role.designation}",
|
|
// showBackButton: true,
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Gestion des permissions pour le rôle: ${widget.role.designation}',
|
|
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
const Text(
|
|
'Sélectionnez les permissions pour chaque menu:',
|
|
style: TextStyle(fontSize: 14, color: Colors.grey),
|
|
),
|
|
const SizedBox(height: 20),
|
|
if (permissions.isNotEmpty && menus.isNotEmpty)
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: menus.length,
|
|
itemBuilder: (context, index) {
|
|
final menu = menus[index];
|
|
final menuId = menu['id'] as int;
|
|
final menuName = menu['name'] as String;
|
|
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 15),
|
|
elevation: 3,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(12.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
menuName,
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Wrap(
|
|
spacing: 10,
|
|
runSpacing: 10,
|
|
children: permissions.map((perm) {
|
|
final isChecked = menuPermissionsMap[menuId]?[perm.name] ?? false;
|
|
return FilterChip(
|
|
label: perm.name,
|
|
selected: isChecked,
|
|
onSelected: (bool value) {
|
|
_onPermissionToggle(menuId, perm.name, value);
|
|
},
|
|
);
|
|
}).toList(),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
)
|
|
else
|
|
const Expanded(
|
|
child: Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class FilterChip extends StatelessWidget {
|
|
final String label;
|
|
final bool selected;
|
|
final ValueChanged<bool> onSelected;
|
|
|
|
const FilterChip({
|
|
super.key,
|
|
required this.label,
|
|
required this.selected,
|
|
required this.onSelected,
|
|
});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ChoiceChip(
|
|
label: Text(label),
|
|
selected: selected,
|
|
onSelected: onSelected,
|
|
selectedColor: Colors.blue,
|
|
labelStyle: TextStyle(
|
|
color: selected ? Colors.white : Colors.black,
|
|
),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(20),
|
|
),
|
|
);
|
|
}
|
|
}
|