From 2f1b40873eff28997c2297d9ef429d6caeac82ad Mon Sep 17 00:00:00 2001 From: "b.razafimandimbihery" Date: Tue, 27 May 2025 17:00:03 +0300 Subject: [PATCH] last commit tsu mety --- lib/Views/gestionRole.dart | 188 ++++++++++++++++++++++--------------- 1 file changed, 114 insertions(+), 74 deletions(-) diff --git a/lib/Views/gestionRole.dart b/lib/Views/gestionRole.dart index c8f84c2..399d455 100644 --- a/lib/Views/gestionRole.dart +++ b/lib/Views/gestionRole.dart @@ -14,9 +14,9 @@ class _HandleUserRoleState extends State { final db = AppDatabase.instance; List> roles = []; - Map permissionsMap = {}; + List permissions = []; + Map> rolePermissionsMap = {}; - int? selectedRoleId; final TextEditingController _roleController = TextEditingController(); @override @@ -29,11 +29,22 @@ class _HandleUserRoleState extends State { final roleList = await db.database.then((db) => db.query('roles')); final perms = await db.getAllPermissions(); + Map> tempRolePermissionsMap = {}; + + for (var role in roleList) { + final roleId = role['id'] as int; + final rolePerms = await db.getPermissionsForRole(roleId); + + tempRolePermissionsMap[roleId] = { + for (var perm in perms) + perm.name: rolePerms.any((rp) => rp.name == perm.name) + }; + } + setState(() { roles = roleList; - for (var perm in perms) { - permissionsMap[perm.name] = false; - } + permissions = perms; + rolePermissionsMap = tempRolePermissionsMap; }); } @@ -46,33 +57,17 @@ class _HandleUserRoleState extends State { await _initData(); } - Future _loadRolePermissions(int roleId) async { - final rolePerms = await db.getPermissionsForRole(roleId); - final allPerms = await db.getAllPermissions(); - - setState(() { - selectedRoleId = roleId; - permissionsMap = { - for (var perm in allPerms) - perm.name: rolePerms.any((rp) => rp.name == perm.name) - }; - }); - } - - Future _onPermissionToggle(String permission, bool enabled) async { - if (selectedRoleId == null) return; - - final allPerms = await db.getAllPermissions(); - final perm = allPerms.firstWhere((p) => p.name == permission); + Future _onPermissionToggle(int roleId, String permission, bool enabled) async { + final perm = permissions.firstWhere((p) => p.name == permission); if (enabled) { - await db.assignPermission(selectedRoleId!, perm.id!); + await db.assignPermission(roleId, perm.id!); } else { - await db.removePermission(selectedRoleId!, perm.id!); + await db.removePermission(roleId, perm.id!); } setState(() { - permissionsMap[permission] = enabled; + rolePermissionsMap[roleId]![permission] = enabled; }); } @@ -84,59 +79,104 @@ class _HandleUserRoleState extends State { padding: const EdgeInsets.all(16.0), child: Column( children: [ - // ✅ Champ pour saisir un nouveau rôle - Row( - children: [ - Expanded( - child: TextField( - controller: _roleController, - decoration: const InputDecoration( - labelText: 'Nouveau rôle', - border: OutlineInputBorder(), + // 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, - child: const Text('Créer'), + 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), - - // 🔽 Sélection d'un rôle - DropdownButton( - isExpanded: true, - hint: const Text("Choisir un rôle"), - value: selectedRoleId, - items: roles.map((role) { - return DropdownMenuItem( - value: role['id'] as int, - child: Text(role['designation'] ?? ''), - ); - }).toList(), - onChanged: (value) { - if (value != null) _loadRolePermissions(value); - }, - ), - - const SizedBox(height: 16), - - // ✅ Permissions associées - if (selectedRoleId != null) + // Tableau des rôles et permissions + if (roles.isNotEmpty && permissions.isNotEmpty) Expanded( - child: ListView( - children: permissionsMap.entries.map((entry) { - return CheckboxListTile( - title: Text(entry.key), - value: entry.value, - onChanged: (bool? value) { - _onPermissionToggle(entry.key, value ?? false); - }, - ); - }).toList(), + 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: 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'] as int; + return DataRow( + cells: [ + DataCell(Text(role['designation'] ?? '')), + ...permissions.map((perm) { + final isChecked = rolePermissionsMap[roleId]?[perm.name] ?? false; + return DataCell( + Checkbox( + value: isChecked, + onChanged: (bool? value) { + _onPermissionToggle(roleId, perm.name, value ?? false); + }, + ), + ); + }).toList(), + ], + ); + }).toList(), + ), + ), + ), + ), + ), + ) + else + const Expanded( + child: Center( + child: Text('Aucun rôle ou permission trouvé'), ), ), ],