Ajout d'utilisateur OK
Ajout bouton de deconnexion
This commit is contained in:
90
em2rp/firestore.rules
Normal file
90
em2rp/firestore.rules
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
rules_version = '2';
|
||||||
|
|
||||||
|
service cloud.firestore {
|
||||||
|
match /databases/{database}/documents {
|
||||||
|
// Fonction pour vérifier si l'utilisateur est authentifié
|
||||||
|
function isAuthenticated() {
|
||||||
|
return request.auth != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUserRole() {
|
||||||
|
let userData = get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
|
||||||
|
return userData != null ? userData.role : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour vérifier si l'utilisateur est un admin
|
||||||
|
function isAdmin() {
|
||||||
|
return isAuthenticated() && getUserRole() == 'ADMIN';
|
||||||
|
}
|
||||||
|
|
||||||
|
function isOwner(userId) {
|
||||||
|
return isAuthenticated() && request.auth.uid == userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nouvelle fonction pour vérifier si un CREW est assigné à un événement du client
|
||||||
|
function isAssignedToClientEvent(clientId) {
|
||||||
|
let events = getAfter(/databases/$(database)/documents/events)
|
||||||
|
.where("clientId", "==", clientId)
|
||||||
|
.where("assignedUsers." + request.auth.uid, "==", true).limit(1);
|
||||||
|
return events.size() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour vérifier si c'est le premier utilisateur
|
||||||
|
function isFirstUser() {
|
||||||
|
return !exists(/databases/$(database)/documents/users);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour vérifier si c'est une mise à jour de l'UID
|
||||||
|
function isUidUpdate() {
|
||||||
|
return request.resource.data.diff(resource.data).affectedKeys().hasOnly(['uid']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour la collection users
|
||||||
|
match /users/{userId} {
|
||||||
|
allow read: if isAuthenticated() && (isAdmin() || isOwner(userId));
|
||||||
|
// Permettre la création si admin OU si l'utilisateur crée son propre document
|
||||||
|
allow create: if isAdmin() || (isAuthenticated() && request.auth.uid == userId);
|
||||||
|
allow update: if isAdmin() ||
|
||||||
|
(isOwner(userId) &&
|
||||||
|
request.resource.data.diff(resource.data).affectedKeys()
|
||||||
|
.hasOnly(['phoneNumber', 'profilePhotoUrl', 'firstName', 'lastName', 'role']));
|
||||||
|
allow delete: if isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour la collection clients
|
||||||
|
match /clients/{clientId} {
|
||||||
|
// Lecture :
|
||||||
|
// - Les admins peuvent tout voir
|
||||||
|
// - Les CREW ne peuvent voir que les clients liés à leurs événements
|
||||||
|
allow read: if isAdmin() ||
|
||||||
|
(getUserRole() == 'CREW' && isAssignedToClientEvent(clientId));
|
||||||
|
|
||||||
|
// Création, modification et suppression : Seuls les admins
|
||||||
|
allow create, update, delete: if isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour la collection events (prestations)
|
||||||
|
match /events/{eventId} {
|
||||||
|
allow read: if isAdmin() ||
|
||||||
|
(isAuthenticated() && (resource.data.assignedUsers[request.auth.uid] == true));
|
||||||
|
allow create, update: if isAdmin();
|
||||||
|
allow delete: if isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour la collection quotes (devis)
|
||||||
|
match /quotes/{quoteId} {
|
||||||
|
allow read, write: if isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour la collection invoices (factures)
|
||||||
|
match /invoices/{invoiceId} {
|
||||||
|
allow read, write: if isAdmin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Règles pour les autres collections
|
||||||
|
match /{document=**} {
|
||||||
|
// Par défaut, refuser l'accès
|
||||||
|
allow read, write: if false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
em2rp/lib/config/env.dart
Normal file
17
em2rp/lib/config/env.dart
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
class Env {
|
||||||
|
static const bool isDevelopment = true;
|
||||||
|
|
||||||
|
// Configuration de l'auto-login en développement
|
||||||
|
static const String devAdminEmail = 'paul.fournel@em2events.fr';
|
||||||
|
static const String devAdminPassword =
|
||||||
|
'votre_mot_de_passe'; // À remplacer par le vrai mot de passe
|
||||||
|
|
||||||
|
// URLs et endpoints
|
||||||
|
static const String baseUrl = 'https://em2rp-951dc.firebaseapp.com';
|
||||||
|
|
||||||
|
// Configuration Firebase
|
||||||
|
static const String firebaseProjectId = 'em2rp-951dc';
|
||||||
|
|
||||||
|
// Autres configurations
|
||||||
|
static const int apiTimeout = 30000; // 30 secondes
|
||||||
|
}
|
@ -12,6 +12,8 @@ import 'views/user_management_page.dart';
|
|||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'providers/local_user_provider.dart';
|
import 'providers/local_user_provider.dart';
|
||||||
import 'services/user_service.dart';
|
import 'services/user_service.dart';
|
||||||
|
import 'pages/auth/reset_password_page.dart';
|
||||||
|
import 'config/env.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
@ -30,7 +32,7 @@ void main() async {
|
|||||||
ChangeNotifierProvider<LocalUserProvider>(
|
ChangeNotifierProvider<LocalUserProvider>(
|
||||||
create: (context) => LocalUserProvider()),
|
create: (context) => LocalUserProvider()),
|
||||||
|
|
||||||
// // Injection des Providers en utilisant UserService
|
// Injection des Providers en utilisant UserService
|
||||||
ChangeNotifierProvider<UsersProvider>(
|
ChangeNotifierProvider<UsersProvider>(
|
||||||
create: (context) => UsersProvider(context.read<UserService>()),
|
create: (context) => UsersProvider(context.read<UserService>()),
|
||||||
),
|
),
|
||||||
@ -73,14 +75,74 @@ class MyApp extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
home: const AutoLoginWrapper(),
|
||||||
routes: {
|
routes: {
|
||||||
'/login': (context) => const LoginPage(),
|
'/login': (context) => const LoginPage(),
|
||||||
'/calendar': (context) => const AuthGuard(child: CalendarPage()),
|
'/calendar': (context) => const AuthGuard(child: CalendarPage()),
|
||||||
'/my_account': (context) => const AuthGuard(child: MyAccountPage()),
|
'/my_account': (context) => const AuthGuard(child: MyAccountPage()),
|
||||||
'/user_management': (context) =>
|
'/user_management': (context) =>
|
||||||
const AuthGuard(requiredRole: "ADMIN", child: UserManagementPage()),
|
const AuthGuard(requiredRole: "ADMIN", child: UserManagementPage()),
|
||||||
|
'/reset_password': (context) {
|
||||||
|
final args = ModalRoute.of(context)!.settings.arguments
|
||||||
|
as Map<String, dynamic>;
|
||||||
|
return ResetPasswordPage(
|
||||||
|
email: args['email'] as String,
|
||||||
|
actionCode: args['actionCode'] as String,
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
initialRoute: '/login',
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AutoLoginWrapper extends StatefulWidget {
|
||||||
|
const AutoLoginWrapper({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<AutoLoginWrapper> createState() => _AutoLoginWrapperState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AutoLoginWrapperState extends State<AutoLoginWrapper> {
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_autoLogin();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _autoLogin() async {
|
||||||
|
try {
|
||||||
|
final localAuthProvider =
|
||||||
|
Provider.of<LocalUserProvider>(context, listen: false);
|
||||||
|
|
||||||
|
// Vérifier si l'utilisateur est déjà connecté
|
||||||
|
if (FirebaseAuth.instance.currentUser == null && Env.isDevelopment) {
|
||||||
|
// Connexion automatique en mode développement
|
||||||
|
await localAuthProvider.signInWithEmailAndPassword(
|
||||||
|
Env.devAdminEmail,
|
||||||
|
Env.devAdminPassword,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Charger les données utilisateur
|
||||||
|
await localAuthProvider.loadUserData();
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(context).pushReplacementNamed('/calendar');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Auto login failed: $e');
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(context).pushReplacementNamed('/login');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return const Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
|
||||||
|
|
||||||
enum Permission {
|
enum Permission {
|
||||||
// Permissions liées aux prestations
|
// Permissions liées aux prestations
|
||||||
viewAllEvents, // Voir toutes les prestations
|
viewAllEvents, // Voir toutes les prestations
|
||||||
|
149
em2rp/lib/pages/auth/reset_password_page.dart
Normal file
149
em2rp/lib/pages/auth/reset_password_page.dart
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import '../../providers/users_provider.dart';
|
||||||
|
|
||||||
|
class ResetPasswordPage extends StatefulWidget {
|
||||||
|
final String email;
|
||||||
|
final String actionCode;
|
||||||
|
|
||||||
|
const ResetPasswordPage({
|
||||||
|
Key? key,
|
||||||
|
required this.email,
|
||||||
|
required this.actionCode,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
_ResetPasswordPageState createState() => _ResetPasswordPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ResetPasswordPageState extends State<ResetPasswordPage> {
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
final _passwordController = TextEditingController();
|
||||||
|
final _confirmPasswordController = TextEditingController();
|
||||||
|
bool _isLoading = false;
|
||||||
|
String? _errorMessage;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_passwordController.dispose();
|
||||||
|
_confirmPasswordController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _resetPassword() async {
|
||||||
|
if (!_formKey.currentState!.validate()) return;
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_isLoading = true;
|
||||||
|
_errorMessage = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Vérifier le code d'action
|
||||||
|
await FirebaseAuth.instance.verifyPasswordResetCode(widget.actionCode);
|
||||||
|
|
||||||
|
// Réinitialiser le mot de passe
|
||||||
|
await FirebaseAuth.instance.confirmPasswordReset(
|
||||||
|
code: widget.actionCode,
|
||||||
|
newPassword: _passwordController.text,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Connecter l'utilisateur automatiquement
|
||||||
|
await FirebaseAuth.instance.signInWithEmailAndPassword(
|
||||||
|
email: widget.email,
|
||||||
|
password: _passwordController.text,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (mounted) {
|
||||||
|
Navigator.of(context).pushReplacementNamed('/home');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
setState(() {
|
||||||
|
_errorMessage = 'Une erreur est survenue. Veuillez réessayer.';
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
if (mounted) {
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: const Text('Créer votre mot de passe'),
|
||||||
|
),
|
||||||
|
body: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
'Bienvenue ! Veuillez créer votre mot de passe pour continuer.',
|
||||||
|
style: Theme.of(context).textTheme.titleMedium,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
TextFormField(
|
||||||
|
controller: _passwordController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Nouveau mot de passe',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
obscureText: true,
|
||||||
|
validator: (value) {
|
||||||
|
if (value == null || value.isEmpty) {
|
||||||
|
return 'Veuillez entrer un mot de passe';
|
||||||
|
}
|
||||||
|
if (value.length < 6) {
|
||||||
|
return 'Le mot de passe doit contenir au moins 6 caractères';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextFormField(
|
||||||
|
controller: _confirmPasswordController,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Confirmer le mot de passe',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
obscureText: true,
|
||||||
|
validator: (value) {
|
||||||
|
if (value != _passwordController.text) {
|
||||||
|
return 'Les mots de passe ne correspondent pas';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
if (_errorMessage != null) ...[
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
Text(
|
||||||
|
_errorMessage!,
|
||||||
|
style: TextStyle(
|
||||||
|
color: Theme.of(context).colorScheme.error,
|
||||||
|
),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: _isLoading ? null : _resetPassword,
|
||||||
|
child: _isLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const Text('Créer le mot de passe'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -22,12 +22,61 @@ class LocalUserProvider with ChangeNotifier {
|
|||||||
|
|
||||||
/// Charge les données de l'utilisateur actuel
|
/// Charge les données de l'utilisateur actuel
|
||||||
Future<void> loadUserData() async {
|
Future<void> loadUserData() async {
|
||||||
if (_auth.currentUser == null) return;
|
if (_auth.currentUser == null) {
|
||||||
DocumentSnapshot userDoc =
|
print('No current user in Auth');
|
||||||
await _firestore.collection('users').doc(_auth.currentUser!.uid).get();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
print('Loading user data for: ${_auth.currentUser!.uid}');
|
||||||
|
try {
|
||||||
|
DocumentSnapshot userDoc = await _firestore
|
||||||
|
.collection('users')
|
||||||
|
.doc(_auth.currentUser!.uid)
|
||||||
|
.get();
|
||||||
|
|
||||||
if (userDoc.exists) {
|
if (userDoc.exists) {
|
||||||
setUser(UserModel.fromMap(
|
print('User document found in Firestore');
|
||||||
userDoc.data() as Map<String, dynamic>, userDoc.id));
|
final userData = userDoc.data() as Map<String, dynamic>;
|
||||||
|
print('User data: $userData');
|
||||||
|
|
||||||
|
// Si le document n'a pas d'UID, l'ajouter
|
||||||
|
if (!userData.containsKey('uid')) {
|
||||||
|
await userDoc.reference.update({'uid': _auth.currentUser!.uid});
|
||||||
|
userData['uid'] = _auth.currentUser!.uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUser(UserModel.fromMap(userData, userDoc.id));
|
||||||
|
print('User data loaded successfully');
|
||||||
|
} else {
|
||||||
|
print('No user document found in Firestore');
|
||||||
|
// Créer un document utilisateur par défaut
|
||||||
|
final defaultUser = UserModel(
|
||||||
|
uid: _auth.currentUser!.uid,
|
||||||
|
email: _auth.currentUser!.email ?? '',
|
||||||
|
firstName: '',
|
||||||
|
lastName: '',
|
||||||
|
role: 'USER',
|
||||||
|
phoneNumber: '',
|
||||||
|
profilePhotoUrl: '',
|
||||||
|
);
|
||||||
|
|
||||||
|
await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
|
||||||
|
'uid': _auth.currentUser!.uid,
|
||||||
|
'email': _auth.currentUser!.email,
|
||||||
|
'firstName': '',
|
||||||
|
'lastName': '',
|
||||||
|
'role': 'USER',
|
||||||
|
'phoneNumber': '',
|
||||||
|
'profilePhotoUrl': '',
|
||||||
|
'createdAt': FieldValue.serverTimestamp(),
|
||||||
|
});
|
||||||
|
|
||||||
|
setUser(defaultUser);
|
||||||
|
print('Default user document created');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error loading user data: $e');
|
||||||
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import '../models/user_model.dart';
|
import '../models/user_model.dart';
|
||||||
import '../services/user_service.dart';
|
import '../services/user_service.dart';
|
||||||
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
class UsersProvider with ChangeNotifier {
|
class UsersProvider with ChangeNotifier {
|
||||||
final UserService _userService;
|
final UserService _userService;
|
||||||
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||||
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||||
List<UserModel> _users = [];
|
List<UserModel> _users = [];
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
|
|
||||||
@ -16,28 +21,146 @@ class UsersProvider with ChangeNotifier {
|
|||||||
Future<void> fetchUsers() async {
|
Future<void> fetchUsers() async {
|
||||||
_isLoading = true;
|
_isLoading = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_users = await _userService.fetchUsers();
|
final snapshot = await _firestore.collection('users').get();
|
||||||
} finally {
|
_users = snapshot.docs
|
||||||
|
.map((doc) => UserModel.fromMap(doc.data(), doc.id))
|
||||||
|
.toList();
|
||||||
|
} catch (e) {
|
||||||
|
print('Error fetching users: $e');
|
||||||
|
}
|
||||||
|
|
||||||
_isLoading = false;
|
_isLoading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Mise à jour d'un utilisateur
|
/// Mise à jour d'un utilisateur
|
||||||
Future<void> updateUser(UserModel user) async {
|
Future<void> updateUser(UserModel user) async {
|
||||||
await _userService.updateUser(user);
|
try {
|
||||||
await fetchUsers();
|
await _firestore.collection('users').doc(user.uid).update({
|
||||||
|
'firstName': user.firstName,
|
||||||
|
'lastName': user.lastName,
|
||||||
|
'email': user.email,
|
||||||
|
'phoneNumber': user.phoneNumber,
|
||||||
|
'role': user.role,
|
||||||
|
'profilePhotoUrl': user.profilePhotoUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
final index = _users.indexWhere((u) => u.uid == user.uid);
|
||||||
|
if (index != -1) {
|
||||||
|
_users[index] = user;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error updating user: $e');
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Suppression d'un utilisateur
|
/// Suppression d'un utilisateur
|
||||||
Future<void> deleteUser(String uid) async {
|
Future<void> deleteUser(String uid) async {
|
||||||
await _userService.deleteUser(uid);
|
try {
|
||||||
await fetchUsers();
|
await _firestore.collection('users').doc(uid).delete();
|
||||||
|
_users.removeWhere((user) => user.uid == uid);
|
||||||
|
notifyListeners();
|
||||||
|
} catch (e) {
|
||||||
|
print('Error deleting user: $e');
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Réinitialisation du mot de passe
|
/// Réinitialisation du mot de passe
|
||||||
Future<void> resetPassword(String email) async {
|
Future<void> resetPassword(String email) async {
|
||||||
await _userService.resetPassword(email);
|
await _userService.resetPassword(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> createUserWithEmailInvite(UserModel user) async {
|
||||||
|
String? authUid;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Vérifier l'état de l'authentification
|
||||||
|
final currentUser = _auth.currentUser;
|
||||||
|
print('Current user: ${currentUser?.email}');
|
||||||
|
|
||||||
|
if (currentUser == null) {
|
||||||
|
throw Exception('Aucun utilisateur connecté');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vérifier le rôle de l'utilisateur actuel
|
||||||
|
final currentUserDoc =
|
||||||
|
await _firestore.collection('users').doc(currentUser.uid).get();
|
||||||
|
print('Current user role: ${currentUserDoc.data()?['role']}');
|
||||||
|
|
||||||
|
if (currentUserDoc.data()?['role'] != 'ADMIN') {
|
||||||
|
throw Exception(
|
||||||
|
'Seuls les administrateurs peuvent créer des utilisateurs');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Créer l'utilisateur dans Firebase Authentication
|
||||||
|
final userCredential = await _auth.createUserWithEmailAndPassword(
|
||||||
|
email: user.email,
|
||||||
|
password: 'TemporaryPassword123!', // Mot de passe temporaire
|
||||||
|
);
|
||||||
|
|
||||||
|
authUid = userCredential.user!.uid;
|
||||||
|
print('User created in Auth with UID: $authUid');
|
||||||
|
|
||||||
|
// Créer le document dans Firestore avec l'UID de Auth comme ID
|
||||||
|
await _firestore.collection('users').doc(authUid).set({
|
||||||
|
'uid': authUid,
|
||||||
|
'firstName': user.firstName,
|
||||||
|
'lastName': user.lastName,
|
||||||
|
'email': user.email,
|
||||||
|
'phoneNumber': user.phoneNumber,
|
||||||
|
'role': user.role,
|
||||||
|
'profilePhotoUrl': user.profilePhotoUrl,
|
||||||
|
'createdAt': FieldValue.serverTimestamp(),
|
||||||
|
});
|
||||||
|
|
||||||
|
print('User document created in Firestore with Auth UID');
|
||||||
|
|
||||||
|
// Envoyer un email de réinitialisation de mot de passe
|
||||||
|
await _auth.sendPasswordResetEmail(
|
||||||
|
email: user.email,
|
||||||
|
actionCodeSettings: ActionCodeSettings(
|
||||||
|
url: 'http://localhost:63337/finishSignUp?email=${user.email}',
|
||||||
|
handleCodeInApp: true,
|
||||||
|
androidPackageName: 'com.em2rp.app',
|
||||||
|
androidInstallApp: true,
|
||||||
|
androidMinimumVersion: '12',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
print('Password reset email sent');
|
||||||
|
|
||||||
|
// Ajouter l'utilisateur à la liste locale
|
||||||
|
final newUser = UserModel(
|
||||||
|
uid: authUid,
|
||||||
|
firstName: user.firstName,
|
||||||
|
lastName: user.lastName,
|
||||||
|
email: user.email,
|
||||||
|
phoneNumber: user.phoneNumber,
|
||||||
|
role: user.role,
|
||||||
|
profilePhotoUrl: user.profilePhotoUrl,
|
||||||
|
);
|
||||||
|
_users.add(newUser);
|
||||||
|
notifyListeners();
|
||||||
|
} catch (e) {
|
||||||
|
// En cas d'erreur, supprimer l'utilisateur Auth si créé
|
||||||
|
if (authUid != null) {
|
||||||
|
try {
|
||||||
|
await _auth.currentUser?.delete();
|
||||||
|
} catch (deleteError) {
|
||||||
|
print('Warning: Could not delete Auth user: $deleteError');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error creating user: $e');
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,23 +27,34 @@ class LoginViewModel extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await localAuthProvider.signInWithEmailAndPassword(
|
final userCredential = await localAuthProvider.signInWithEmailAndPassword(
|
||||||
emailController.text.trim(), passwordController.text);
|
emailController.text.trim(), passwordController.text);
|
||||||
print('User signed in');
|
print('User signed in');
|
||||||
|
|
||||||
|
// Attendre que les données utilisateur soient chargées
|
||||||
|
await localAuthProvider.loadUserData();
|
||||||
|
|
||||||
// Vérifier si le contexte est toujours valide
|
// Vérifier si le contexte est toujours valide
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
|
// Vérifier si l'utilisateur a bien été chargé
|
||||||
|
if (localAuthProvider.currentUser != null) {
|
||||||
// Utiliser pushReplacementNamed pour une transition propre
|
// Utiliser pushReplacementNamed pour une transition propre
|
||||||
Navigator.of(context, rootNavigator: true)
|
Navigator.of(context, rootNavigator: true)
|
||||||
.pushReplacementNamed('/calendar');
|
.pushReplacementNamed('/calendar');
|
||||||
|
} else {
|
||||||
|
errorMessage = 'Erreur lors du chargement des données utilisateur';
|
||||||
|
isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} on FirebaseAuthException catch (e) {
|
} on FirebaseAuthException catch (e) {
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
// Gérer les erreurs...
|
errorMessage =
|
||||||
|
e.message ?? 'Une erreur est survenue lors de la connexion';
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
} finally {
|
} catch (e) {
|
||||||
// S'assurer que isLoading est remis à false même en cas d'erreur inattendue
|
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
|
errorMessage = 'Une erreur inattendue est survenue';
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
import 'package:em2rp/providers/local_user_provider.dart';
|
import 'package:em2rp/providers/local_user_provider.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:em2rp/widgets/custom_app_bar.dart';
|
||||||
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
import 'package:em2rp/views/widgets/nav/main_drawer.dart';
|
||||||
import 'package:provider/provider.dart'; // Import Provider
|
import 'package:provider/provider.dart'; // Import Provider
|
||||||
import 'package:em2rp/utils/colors.dart';
|
import 'package:em2rp/utils/colors.dart';
|
||||||
|
|
||||||
class CalendarPage extends StatelessWidget {
|
class CalendarPage extends StatelessWidget {
|
||||||
const CalendarPage({super.key});
|
const CalendarPage({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final localAuthProvider = Provider.of<LocalUserProvider>(context);
|
final localAuthProvider = Provider.of<LocalUserProvider>(context);
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Calendrier')),
|
appBar: const CustomAppBar(
|
||||||
drawer: MainDrawer(
|
title: 'Calendrier',
|
||||||
currentPage: '/calendar'), // Pass UserProvider to MainDrawer
|
),
|
||||||
|
drawer: const MainDrawer(currentPage: '/calendar'),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
@ -6,8 +6,9 @@ import 'package:em2rp/models/user_model.dart';
|
|||||||
import 'package:em2rp/views/widgets/user_management/user_card.dart';
|
import 'package:em2rp/views/widgets/user_management/user_card.dart';
|
||||||
import 'package:em2rp/views/widgets/user_management/edit_user_dialog.dart';
|
import 'package:em2rp/views/widgets/user_management/edit_user_dialog.dart';
|
||||||
import 'package:em2rp/utils/colors.dart';
|
import 'package:em2rp/utils/colors.dart';
|
||||||
import 'package:em2rp/widgets/permission_gate.dart';
|
import 'package:em2rp/utils/permission_gate.dart';
|
||||||
import 'package:em2rp/models/role_model.dart';
|
import 'package:em2rp/models/role_model.dart';
|
||||||
|
import 'package:em2rp/widgets/custom_app_bar.dart';
|
||||||
|
|
||||||
class UserManagementPage extends StatefulWidget {
|
class UserManagementPage extends StatefulWidget {
|
||||||
const UserManagementPage({Key? key}) : super(key: key);
|
const UserManagementPage({Key? key}) : super(key: key);
|
||||||
@ -29,9 +30,8 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
|||||||
return PermissionGate(
|
return PermissionGate(
|
||||||
requiredPermissions: [Permission.viewUsers],
|
requiredPermissions: [Permission.viewUsers],
|
||||||
fallback: Scaffold(
|
fallback: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: const CustomAppBar(
|
||||||
title: const Text('Accès refusé'),
|
title: 'Accès refusé',
|
||||||
backgroundColor: AppColors.rouge,
|
|
||||||
),
|
),
|
||||||
body: const Center(
|
body: const Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
@ -42,9 +42,8 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
child: Scaffold(
|
child: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: const CustomAppBar(
|
||||||
title: const Text('Gestion des utilisateurs'),
|
title: 'Gestion des utilisateurs',
|
||||||
backgroundColor: AppColors.rouge,
|
|
||||||
),
|
),
|
||||||
drawer: const MainDrawer(currentPage: '/account_management'),
|
drawer: const MainDrawer(currentPage: '/account_management'),
|
||||||
body: Consumer<UsersProvider>(
|
body: Consumer<UsersProvider>(
|
||||||
@ -108,54 +107,180 @@ class _UserManagementPageState extends State<UserManagementPage> {
|
|||||||
final lastNameController = TextEditingController();
|
final lastNameController = TextEditingController();
|
||||||
final emailController = TextEditingController();
|
final emailController = TextEditingController();
|
||||||
final phoneController = TextEditingController();
|
final phoneController = TextEditingController();
|
||||||
final roleController = TextEditingController();
|
String selectedRole = Roles.values.first.name;
|
||||||
|
|
||||||
|
InputDecoration _buildInputDecoration(String label, IconData icon) {
|
||||||
|
return InputDecoration(
|
||||||
|
labelText: label,
|
||||||
|
prefixIcon: Icon(icon, color: AppColors.rouge),
|
||||||
|
border: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
),
|
||||||
|
focusedBorder: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
borderSide: const BorderSide(color: AppColors.rouge, width: 2),
|
||||||
|
),
|
||||||
|
contentPadding:
|
||||||
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
builder: (context) => Dialog(
|
||||||
title: const Text('Créer un utilisateur'),
|
shape: RoundedRectangleBorder(
|
||||||
content: Column(
|
borderRadius: BorderRadius.circular(16),
|
||||||
|
),
|
||||||
|
child: Container(
|
||||||
|
width: 400,
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
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,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
SingleChildScrollView(
|
||||||
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
TextField(
|
TextField(
|
||||||
controller: firstNameController,
|
controller: firstNameController,
|
||||||
decoration: const InputDecoration(labelText: 'Prénom')),
|
decoration:
|
||||||
|
_buildInputDecoration('Prénom', Icons.person_outline),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
TextField(
|
TextField(
|
||||||
controller: lastNameController,
|
controller: lastNameController,
|
||||||
decoration: const InputDecoration(labelText: 'Nom')),
|
decoration: _buildInputDecoration('Nom', Icons.person),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
TextField(
|
TextField(
|
||||||
controller: emailController,
|
controller: emailController,
|
||||||
decoration: const InputDecoration(labelText: 'Email')),
|
decoration:
|
||||||
|
_buildInputDecoration('Email', Icons.email_outlined),
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
TextField(
|
TextField(
|
||||||
controller: phoneController,
|
controller: phoneController,
|
||||||
decoration: const InputDecoration(labelText: 'Téléphone')),
|
decoration: _buildInputDecoration(
|
||||||
TextField(
|
'Téléphone', Icons.phone_outlined),
|
||||||
controller: roleController,
|
keyboardType: TextInputType.phone,
|
||||||
decoration: const InputDecoration(labelText: 'Rôle')),
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
DropdownButtonFormField<String>(
|
||||||
|
value: selectedRole,
|
||||||
|
decoration: _buildInputDecoration(
|
||||||
|
'Rôle', Icons.admin_panel_settings_outlined),
|
||||||
|
items: Roles.values.map((Role role) {
|
||||||
|
return DropdownMenuItem<String>(
|
||||||
|
value: role.name,
|
||||||
|
child: Text(role.name),
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
onChanged: (String? newValue) {
|
||||||
|
if (newValue != null) {
|
||||||
|
selectedRole = newValue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
actions: [
|
),
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
|
children: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: () => Navigator.pop(context),
|
||||||
child: const Text('Annuler'),
|
style: TextButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(
|
||||||
|
horizontal: 16, vertical: 12),
|
||||||
),
|
),
|
||||||
TextButton(
|
child: const Text(
|
||||||
onPressed: () {
|
'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(
|
final newUser = UserModel(
|
||||||
uid: '',
|
uid: '', // Sera généré par Firebase
|
||||||
firstName: firstNameController.text,
|
firstName: firstNameController.text,
|
||||||
lastName: lastNameController.text,
|
lastName: lastNameController.text,
|
||||||
email: emailController.text,
|
email: emailController.text,
|
||||||
phoneNumber: phoneController.text,
|
phoneNumber: phoneController.text,
|
||||||
role: roleController.text,
|
role: selectedRole,
|
||||||
profilePhotoUrl: '',
|
profilePhotoUrl: '',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await Provider.of<UsersProvider>(context, listen: false)
|
||||||
|
.createUserWithEmailInvite(newUser);
|
||||||
|
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text('Invitation envoyée avec succès'),
|
||||||
|
backgroundColor: Colors.green,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
'Erreur lors de la création: ${e.toString()}'),
|
||||||
|
backgroundColor: Colors.red,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
child: const Text('Créer'),
|
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),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import 'package:em2rp/views/user_management_page.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:em2rp/views/widgets/image/profile_picture.dart';
|
import 'package:em2rp/views/widgets/image/profile_picture.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:em2rp/widgets/permission_gate.dart';
|
import 'package:em2rp/utils/permission_gate.dart';
|
||||||
import 'package:em2rp/models/role_model.dart';
|
import 'package:em2rp/models/role_model.dart';
|
||||||
|
|
||||||
class MainDrawer extends StatelessWidget {
|
class MainDrawer extends StatelessWidget {
|
||||||
|
65
em2rp/lib/widgets/custom_app_bar.dart
Normal file
65
em2rp/lib/widgets/custom_app_bar.dart
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:em2rp/providers/local_user_provider.dart';
|
||||||
|
import 'package:em2rp/utils/colors.dart';
|
||||||
|
|
||||||
|
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||||
|
final String title;
|
||||||
|
final List<Widget>? actions;
|
||||||
|
final bool showLogoutButton;
|
||||||
|
|
||||||
|
const CustomAppBar({
|
||||||
|
Key? key,
|
||||||
|
required this.title,
|
||||||
|
this.actions,
|
||||||
|
this.showLogoutButton = true,
|
||||||
|
}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return AppBar(
|
||||||
|
title: Text(title),
|
||||||
|
backgroundColor: AppColors.rouge,
|
||||||
|
actions: [
|
||||||
|
if (showLogoutButton)
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.logout, color: AppColors.blanc),
|
||||||
|
onPressed: () async {
|
||||||
|
// Afficher une boîte de dialogue de confirmation
|
||||||
|
final shouldLogout = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: const Text('Déconnexion'),
|
||||||
|
content:
|
||||||
|
const Text('Voulez-vous vraiment vous déconnecter ?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, false),
|
||||||
|
child: const Text('Annuler'),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context, true),
|
||||||
|
child: const Text('Déconnexion'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (shouldLogout == true) {
|
||||||
|
// Déconnexion
|
||||||
|
await Provider.of<LocalUserProvider>(context, listen: false)
|
||||||
|
.signOut();
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context).pushReplacementNamed('/login');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
if (actions != null) ...actions!,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
|
||||||
|
}
|
Reference in New Issue
Block a user