last commit tsu mety
This commit is contained in:
parent
6ad6608324
commit
2f1b40873e
@ -14,9 +14,9 @@ class _HandleUserRoleState extends State<HandleUserRole> {
|
|||||||
final db = AppDatabase.instance;
|
final db = AppDatabase.instance;
|
||||||
|
|
||||||
List<Map<String, dynamic>> roles = [];
|
List<Map<String, dynamic>> roles = [];
|
||||||
Map<String, bool> permissionsMap = {};
|
List<dynamic> permissions = [];
|
||||||
|
Map<int, Map<String, bool>> rolePermissionsMap = {};
|
||||||
|
|
||||||
int? selectedRoleId;
|
|
||||||
final TextEditingController _roleController = TextEditingController();
|
final TextEditingController _roleController = TextEditingController();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -29,11 +29,22 @@ class _HandleUserRoleState extends State<HandleUserRole> {
|
|||||||
final roleList = await db.database.then((db) => db.query('roles'));
|
final roleList = await db.database.then((db) => db.query('roles'));
|
||||||
final perms = await db.getAllPermissions();
|
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(() {
|
setState(() {
|
||||||
roles = roleList;
|
roles = roleList;
|
||||||
for (var perm in perms) {
|
permissions = perms;
|
||||||
permissionsMap[perm.name] = false;
|
rolePermissionsMap = tempRolePermissionsMap;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,33 +57,17 @@ class _HandleUserRoleState extends State<HandleUserRole> {
|
|||||||
await _initData();
|
await _initData();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadRolePermissions(int roleId) async {
|
Future<void> _onPermissionToggle(int roleId, String permission, bool enabled) async {
|
||||||
final rolePerms = await db.getPermissionsForRole(roleId);
|
final perm = permissions.firstWhere((p) => p.name == permission);
|
||||||
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);
|
|
||||||
|
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
await db.assignPermission(selectedRoleId!, perm.id!);
|
await db.assignPermission(roleId, perm.id!);
|
||||||
} else {
|
} else {
|
||||||
await db.removePermission(selectedRoleId!, perm.id!);
|
await db.removePermission(roleId, perm.id!);
|
||||||
}
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
permissionsMap[permission] = enabled;
|
rolePermissionsMap[roleId]![permission] = enabled;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,59 +79,104 @@ class _HandleUserRoleState extends State<HandleUserRole> {
|
|||||||
padding: const EdgeInsets.all(16.0),
|
padding: const EdgeInsets.all(16.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
// ✅ Champ pour saisir un nouveau rôle
|
// Ajout de rôle
|
||||||
Row(
|
Card(
|
||||||
|
elevation: 6,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _roleController,
|
controller: _roleController,
|
||||||
decoration: const InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: 'Nouveau rôle',
|
labelText: 'Nouveau rôle',
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 10),
|
const SizedBox(width: 10),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: _addRole,
|
onPressed: _addRole,
|
||||||
child: const Text('Créer'),
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Colors.blue,
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: const Text('Ajouter'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
const SizedBox(height: 20),
|
const SizedBox(height: 20),
|
||||||
|
// Tableau des rôles et permissions
|
||||||
// 🔽 Sélection d'un rôle
|
if (roles.isNotEmpty && permissions.isNotEmpty)
|
||||||
DropdownButton<int>(
|
Expanded(
|
||||||
isExpanded: true,
|
child: Card(
|
||||||
hint: const Text("Choisir un rôle"),
|
elevation: 6,
|
||||||
value: selectedRoleId,
|
shape: RoundedRectangleBorder(
|
||||||
items: roles.map((role) {
|
borderRadius: BorderRadius.circular(10),
|
||||||
return DropdownMenuItem<int>(
|
),
|
||||||
value: role['id'] as int,
|
child: Padding(
|
||||||
child: Text(role['designation'] ?? ''),
|
padding: const EdgeInsets.all(16.0),
|
||||||
);
|
child: SingleChildScrollView(
|
||||||
}).toList(),
|
scrollDirection: Axis.horizontal,
|
||||||
onChanged: (value) {
|
child: ConstrainedBox(
|
||||||
if (value != null) _loadRolePermissions(value);
|
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);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
||||||
const SizedBox(height: 16),
|
|
||||||
|
|
||||||
// ✅ Permissions associées
|
|
||||||
if (selectedRoleId != null)
|
|
||||||
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(),
|
}).toList(),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
const Expanded(
|
||||||
|
child: Center(
|
||||||
|
child: Text('Aucun rôle ou permission trouvé'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user