|
|
|
@ -14,9 +14,9 @@ class _HandleUserRoleState extends State<HandleUserRole> { |
|
|
|
final db = AppDatabase.instance; |
|
|
|
|
|
|
|
List<Map<String, dynamic>> roles = []; |
|
|
|
Map<String, bool> permissionsMap = {}; |
|
|
|
List<dynamic> permissions = []; |
|
|
|
Map<int, Map<String, bool>> rolePermissionsMap = {}; |
|
|
|
|
|
|
|
int? selectedRoleId; |
|
|
|
final TextEditingController _roleController = TextEditingController(); |
|
|
|
|
|
|
|
@override |
|
|
|
@ -29,11 +29,22 @@ class _HandleUserRoleState extends State<HandleUserRole> { |
|
|
|
final roleList = await db.database.then((db) => db.query('roles')); |
|
|
|
final perms = await db.getAllPermissions(); |
|
|
|
|
|
|
|
Map<int, Map<String, bool>> 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<HandleUserRole> { |
|
|
|
await _initData(); |
|
|
|
} |
|
|
|
|
|
|
|
Future<void> _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<void> _onPermissionToggle(String permission, bool enabled) async { |
|
|
|
if (selectedRoleId == null) return; |
|
|
|
|
|
|
|
final allPerms = await db.getAllPermissions(); |
|
|
|
final perm = allPerms.firstWhere((p) => p.name == permission); |
|
|
|
Future<void> _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<HandleUserRole> { |
|
|
|
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<int>( |
|
|
|
isExpanded: true, |
|
|
|
hint: const Text("Choisir un rôle"), |
|
|
|
value: selectedRoleId, |
|
|
|
items: roles.map((role) { |
|
|
|
return DropdownMenuItem<int>( |
|
|
|
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é'), |
|
|
|
), |
|
|
|
), |
|
|
|
], |
|
|
|
|