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.
210 lines
7.7 KiB
210 lines
7.7 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';
|
|
import 'package:youmazgestion/Services/stock_managementDatabase.dart';
|
|
|
|
class HandleUserRole extends StatefulWidget {
|
|
const HandleUserRole({super.key});
|
|
|
|
@override
|
|
State<HandleUserRole> createState() => _HandleUserRoleState();
|
|
}
|
|
|
|
class _HandleUserRoleState extends State<HandleUserRole> {
|
|
final db = AppDatabase.instance;
|
|
|
|
List<Role> roles = [];
|
|
List<Permission> permissions = [];
|
|
List<Map<String, dynamic>> menus = [];
|
|
Map<int, Map<int, Map<String, bool>>> roleMenuPermissionsMap = {};
|
|
|
|
final TextEditingController _roleController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initData();
|
|
}
|
|
|
|
Future<void> _initData() async {
|
|
final roleList = await db.getRoles();
|
|
final perms = await db.getAllPermissions();
|
|
final menuList = await db.database.then((db) => db.query('menu'));
|
|
|
|
Map<int, Map<int, Map<String, bool>>> tempRoleMenuPermissionsMap = {};
|
|
|
|
for (var role in roleList) {
|
|
final roleId = role.id!;
|
|
tempRoleMenuPermissionsMap[roleId] = {};
|
|
|
|
for (var menu in menuList) {
|
|
final menuId = menu['id'] as int;
|
|
final menuPerms = await db.getPermissionsForRoleAndMenu(roleId, menuId);
|
|
|
|
tempRoleMenuPermissionsMap[roleId]![menuId] = {
|
|
for (var perm in perms)
|
|
perm.name: menuPerms.any((mp) => mp.name == perm.name)
|
|
};
|
|
}
|
|
}
|
|
|
|
setState(() {
|
|
roles = roleList;
|
|
permissions = perms;
|
|
menus = menuList;
|
|
roleMenuPermissionsMap = tempRoleMenuPermissionsMap;
|
|
});
|
|
}
|
|
|
|
Future<void> _addRole() async {
|
|
String designation = _roleController.text.trim();
|
|
if (designation.isEmpty) return;
|
|
|
|
await db.createRole(Role(designation: designation));
|
|
_roleController.clear();
|
|
await _initData();
|
|
}
|
|
|
|
Future<void> _onPermissionToggle(int roleId, int menuId, String permission, bool enabled) async {
|
|
final perm = permissions.firstWhere((p) => p.name == permission);
|
|
|
|
if (enabled) {
|
|
await db.assignRoleMenuPermission(roleId, menuId, perm.id!);
|
|
} else {
|
|
await db.removeRoleMenuPermission(roleId, menuId, perm.id!);
|
|
}
|
|
|
|
setState(() {
|
|
roleMenuPermissionsMap[roleId]![menuId]![permission] = enabled;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: CustomAppBar(title: "Gestion des rôles"),
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Column(
|
|
children: [
|
|
// Ajout de rôle
|
|
Card(
|
|
elevation: 6,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _roleController,
|
|
decoration: InputDecoration(
|
|
labelText: 'Nouveau rôle',
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
ElevatedButton(
|
|
onPressed: _addRole,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.blue,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
),
|
|
child: const Text('Ajouter'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
// Tableau des rôles et permissions
|
|
if (roles.isNotEmpty && permissions.isNotEmpty && menus.isNotEmpty)
|
|
Expanded(
|
|
child: Card(
|
|
elevation: 6,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minWidth: MediaQuery.of(context).size.width - 32,
|
|
),
|
|
child: Column(
|
|
children: menus.map((menu) {
|
|
final menuId = menu['id'] as int;
|
|
return Column(
|
|
children: [
|
|
Text(
|
|
menu['name'],
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
DataTable(
|
|
columnSpacing: 20,
|
|
columns: [
|
|
const DataColumn(
|
|
label: Text(
|
|
'Rôles',
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
...permissions.map((perm) => DataColumn(
|
|
label: Text(
|
|
perm.name,
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
)).toList(),
|
|
],
|
|
rows: roles.map((role) {
|
|
final roleId = role.id!;
|
|
return DataRow(
|
|
cells: [
|
|
DataCell(Text(role.designation)),
|
|
...permissions.map((perm) {
|
|
final isChecked = roleMenuPermissionsMap[roleId]?[menuId]?[perm.name] ?? false;
|
|
return DataCell(
|
|
Checkbox(
|
|
value: isChecked,
|
|
onChanged: (bool? value) {
|
|
_onPermissionToggle(roleId, menuId, perm.name, value ?? false);
|
|
},
|
|
),
|
|
);
|
|
}).toList(),
|
|
],
|
|
);
|
|
}).toList(),
|
|
),
|
|
],
|
|
);
|
|
}).toList(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
else
|
|
const Expanded(
|
|
child: Center(
|
|
child: Text('Aucun rôle, permission ou menu trouvé'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|