Refacto : on ne verifie plus le role admin mais les permissions du role
This commit is contained in:
@ -9,6 +9,7 @@ import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/utils/permission_gate.dart';
|
||||
import 'package:em2rp/models/role_model.dart';
|
||||
import 'package:em2rp/views/widgets/custom_app_bar.dart';
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
|
||||
class UserManagementPage extends StatefulWidget {
|
||||
const UserManagementPage({super.key});
|
||||
@ -110,7 +111,20 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
||||
final lastNameController = TextEditingController();
|
||||
final emailController = TextEditingController();
|
||||
final phoneController = TextEditingController();
|
||||
String selectedRole = 'ADMIN';
|
||||
String? selectedRoleId;
|
||||
List<RoleModel> availableRoles = [];
|
||||
bool isLoadingRoles = true;
|
||||
|
||||
Future<void> _loadRoles() async {
|
||||
final snapshot =
|
||||
await FirebaseFirestore.instance.collection('roles').get();
|
||||
availableRoles = snapshot.docs
|
||||
.map((doc) => RoleModel.fromMap(doc.data(), doc.id))
|
||||
.toList();
|
||||
selectedRoleId =
|
||||
availableRoles.isNotEmpty ? availableRoles.first.id : null;
|
||||
isLoadingRoles = false;
|
||||
}
|
||||
|
||||
InputDecoration buildInputDecoration(String label, IconData icon) {
|
||||
return InputDecoration(
|
||||
@ -130,164 +144,162 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
||||
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => Dialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Container(
|
||||
width: 400,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
builder: (context) => FutureBuilder(
|
||||
future: _loadRoles(),
|
||||
builder: (context, snapshot) {
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Container(
|
||||
width: 400,
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Icon(Icons.person_add, color: AppColors.rouge),
|
||||
const SizedBox(width: 12),
|
||||
Text(
|
||||
'Nouvel utilisateur',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
color: AppColors.noir,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: firstNameController,
|
||||
decoration:
|
||||
buildInputDecoration('Prénom', Icons.person_outline),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: lastNameController,
|
||||
decoration: buildInputDecoration('Nom', Icons.person),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: emailController,
|
||||
decoration:
|
||||
buildInputDecoration('Email', Icons.email_outlined),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: phoneController,
|
||||
decoration: buildInputDecoration(
|
||||
'Téléphone', Icons.phone_outlined),
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
DropdownButtonFormField<String>(
|
||||
value: selectedRole,
|
||||
decoration: buildInputDecoration(
|
||||
'Rôle', Icons.admin_panel_settings_outlined),
|
||||
items: ['ADMIN', 'CREW'].map((String role) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: role,
|
||||
child: Text(role),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
if (newValue != null) {
|
||||
selectedRole = newValue;
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 12),
|
||||
),
|
||||
child: const Text(
|
||||
'Annuler',
|
||||
style: TextStyle(color: AppColors.gris),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (emailController.text.isEmpty ||
|
||||
firstNameController.text.isEmpty ||
|
||||
lastNameController.text.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
'Veuillez remplir tous les champs obligatoires'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final newUser = UserModel(
|
||||
uid: '', // Sera généré par Firebase
|
||||
firstName: firstNameController.text,
|
||||
lastName: lastNameController.text,
|
||||
email: emailController.text,
|
||||
phoneNumber: phoneController.text,
|
||||
role: selectedRole,
|
||||
profilePhotoUrl: '',
|
||||
);
|
||||
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
await Provider.of<UsersProvider>(context, listen: false)
|
||||
.createUserWithEmailInvite(context, newUser);
|
||||
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
scaffoldMessenger.showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Invitation envoyée avec succès'),
|
||||
backgroundColor: Colors.green,
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.person_add, color: AppColors.rouge),
|
||||
const SizedBox(width: 12),
|
||||
Text(
|
||||
'Nouvel utilisateur',
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(
|
||||
color: AppColors.noir,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Erreur lors de la création: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.rouge,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24, vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: firstNameController,
|
||||
decoration: buildInputDecoration(
|
||||
'Prénom', Icons.person_outline),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: lastNameController,
|
||||
decoration: buildInputDecoration('Nom', Icons.person),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: emailController,
|
||||
decoration: buildInputDecoration(
|
||||
'Email', Icons.email_outlined),
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: phoneController,
|
||||
decoration: buildInputDecoration(
|
||||
'Téléphone', Icons.phone_outlined),
|
||||
keyboardType: TextInputType.phone,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
isLoadingRoles
|
||||
? const CircularProgressIndicator()
|
||||
: DropdownButtonFormField<String>(
|
||||
value: selectedRoleId,
|
||||
decoration: buildInputDecoration('Rôle',
|
||||
Icons.admin_panel_settings_outlined),
|
||||
items: availableRoles.map((role) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: role.id,
|
||||
child: Text(role.name),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (String? newValue) {
|
||||
if (newValue != null) {
|
||||
selectedRoleId = newValue;
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
child: const Text(
|
||||
'Inviter',
|
||||
style: TextStyle(color: AppColors.blanc),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
style: TextButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 12),
|
||||
),
|
||||
child: const Text(
|
||||
'Annuler',
|
||||
style: TextStyle(color: AppColors.gris),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
if (emailController.text.isEmpty ||
|
||||
firstNameController.text.isEmpty ||
|
||||
lastNameController.text.isEmpty ||
|
||||
selectedRoleId == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
'Veuillez remplir tous les champs obligatoires'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final newUser = UserModel(
|
||||
uid: '', // Sera généré par Firebase
|
||||
firstName: firstNameController.text,
|
||||
lastName: lastNameController.text,
|
||||
email: emailController.text,
|
||||
phoneNumber: phoneController.text,
|
||||
role: selectedRoleId!,
|
||||
profilePhotoUrl: '',
|
||||
);
|
||||
await Provider.of<UsersProvider>(context,
|
||||
listen: false)
|
||||
.createUserWithEmailInvite(context, newUser,
|
||||
roleId: selectedRoleId);
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Erreur lors de la création: ${e.toString()}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.rouge,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 24, vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
child: const Text(
|
||||
'Inviter',
|
||||
style: TextStyle(color: AppColors.blanc),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user