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.
187 lines
6.4 KiB
187 lines
6.4 KiB
import 'package:flutter/material.dart';
|
|
import 'package:youmazgestion/Components/app_bar.dart';
|
|
import 'package:youmazgestion/Services/app_database.dart';
|
|
import 'package:youmazgestion/Models/role.dart';
|
|
|
|
class HandleUserRole extends StatefulWidget {
|
|
const HandleUserRole({super.key});
|
|
|
|
@override
|
|
State<HandleUserRole> createState() => _HandleUserRoleState();
|
|
}
|
|
|
|
class _HandleUserRoleState extends State<HandleUserRole> {
|
|
final db = AppDatabase.instance;
|
|
|
|
List<Map<String, dynamic>> roles = [];
|
|
List<dynamic> permissions = [];
|
|
Map<int, Map<String, bool>> rolePermissionsMap = {};
|
|
|
|
final TextEditingController _roleController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_initData();
|
|
}
|
|
|
|
Future<void> _initData() async {
|
|
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;
|
|
permissions = perms;
|
|
rolePermissionsMap = tempRolePermissionsMap;
|
|
});
|
|
}
|
|
|
|
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, String permission, bool enabled) async {
|
|
final perm = permissions.firstWhere((p) => p.name == permission);
|
|
|
|
if (enabled) {
|
|
await db.assignPermission(roleId, perm.id!);
|
|
} else {
|
|
await db.removePermission(roleId, perm.id!);
|
|
}
|
|
|
|
setState(() {
|
|
rolePermissionsMap[roleId]![permission] = enabled;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: const 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)
|
|
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: 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é'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|