database
This commit is contained in:
parent
14ce881a3c
commit
6b83a1ab5e
11
lib/Components/colors.dart
Normal file
11
lib/Components/colors.dart
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
class AppColors {
|
||||||
|
static const primaryBlue =
|
||||||
|
Color(0xFF04365F); // Replace with your exact logo tone
|
||||||
|
static const secondaryBlue =
|
||||||
|
Color(0xFF1976D2); // Replace if your logo has a secondary tone
|
||||||
|
static const neutralGrey = Color(0xFF8A8A8A);
|
||||||
|
static const accent =
|
||||||
|
Color(0xFFF7941D); // Example: if your logo has an orange or accent
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,18 +1,22 @@
|
|||||||
// Config/database_config.dart - Version améliorée
|
// Config/database_config.dart
|
||||||
class DatabaseConfig {
|
|
||||||
// static const String host = '10.0.2.2';
|
|
||||||
//static const String host = '172.20.10.5';
|
|
||||||
static const String host = 'localhost';
|
|
||||||
static const int port = 3306;
|
|
||||||
static const String username = 'root';
|
|
||||||
static const String? password = null;
|
|
||||||
static const String database = 'gico';
|
|
||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
class DatabaseConfig {
|
||||||
|
// Local MySQL settings
|
||||||
|
static const String localHost = '192.168.88.73';
|
||||||
|
static const String localUsername = 'guycom';
|
||||||
|
static const String? localPassword = '3iV59wjRdbuXAPR';
|
||||||
|
static const String localDatabase = 'guycom';
|
||||||
|
|
||||||
|
// Production (public) MySQL settings
|
||||||
static const String prodHost = '185.70.105.157';
|
static const String prodHost = '185.70.105.157';
|
||||||
static const String prodUsername = 'guycom';
|
static const String prodUsername = 'guycom';
|
||||||
static const String prodPassword = '3iV59wjRdbuXAPR';
|
static const String prodPassword = '3iV59wjRdbuXAPR';
|
||||||
static const String prodDatabase = 'guycom';
|
static const String prodDatabase = 'guycom';
|
||||||
|
|
||||||
|
static const int port = 3306;
|
||||||
static const Duration connectionTimeout = Duration(seconds: 30);
|
static const Duration connectionTimeout = Duration(seconds: 30);
|
||||||
static const Duration queryTimeout = Duration(seconds: 15);
|
static const Duration queryTimeout = Duration(seconds: 15);
|
||||||
|
|
||||||
@ -21,46 +25,72 @@ class DatabaseConfig {
|
|||||||
|
|
||||||
static bool get isDevelopment => false;
|
static bool get isDevelopment => false;
|
||||||
|
|
||||||
static Map<String, dynamic> getConfig() {
|
/// Build config map for connection
|
||||||
if (isDevelopment) {
|
static Map<String, dynamic> _buildConfig({
|
||||||
return {
|
required String host,
|
||||||
'host': host,
|
required String user,
|
||||||
'port': port,
|
required String? password,
|
||||||
'user': username,
|
required String database,
|
||||||
'password': password,
|
}) {
|
||||||
'database': database,
|
return {
|
||||||
'timeout': connectionTimeout.inSeconds,
|
'host': host,
|
||||||
};
|
'port': port,
|
||||||
} else {
|
'user': user,
|
||||||
return {
|
'password': password,
|
||||||
'host': prodHost,
|
'database': database,
|
||||||
'port': port,
|
'timeout': connectionTimeout.inSeconds,
|
||||||
'user': prodUsername,
|
};
|
||||||
'password': prodPassword,
|
}
|
||||||
'database': prodDatabase,
|
|
||||||
'timeout': connectionTimeout.inSeconds,
|
/// TCP check if MySQL server is reachable
|
||||||
};
|
static Future<bool> isServerReachable(String host, {int port = 3306}) async {
|
||||||
|
try {
|
||||||
|
final socket =
|
||||||
|
await Socket.connect(host, port, timeout: Duration(seconds: 2));
|
||||||
|
socket.destroy();
|
||||||
|
return true;
|
||||||
|
} catch (_) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validation de la configuration
|
/// Get smart config (local if reachable, otherwise public)
|
||||||
static bool validateConfig() {
|
static Future<Map<String, dynamic>> getSmartConfig() async {
|
||||||
|
if (await isServerReachable(localHost, port: port)) {
|
||||||
|
return _buildConfig(
|
||||||
|
host: localHost,
|
||||||
|
user: localUsername,
|
||||||
|
password: localPassword,
|
||||||
|
database: localDatabase,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return _buildConfig(
|
||||||
|
host: prodHost,
|
||||||
|
user: prodUsername,
|
||||||
|
password: prodPassword,
|
||||||
|
database: prodDatabase,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate any config
|
||||||
|
static bool validateConfig(Map<String, dynamic> config) {
|
||||||
try {
|
try {
|
||||||
final config = getConfig();
|
return config['host']?.toString().isNotEmpty == true &&
|
||||||
return config['host']?.toString().isNotEmpty == true &&
|
config['database']?.toString().isNotEmpty == true &&
|
||||||
config['database']?.toString().isNotEmpty == true &&
|
config['user'] != null;
|
||||||
config['user'] != null;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Erreur de validation de la configuration: $e");
|
print("Erreur de validation de la configuration: $e");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration avec retry automatique
|
/// Add retry config
|
||||||
static Map<String, dynamic> getConfigWithRetry() {
|
static Map<String, dynamic> addRetry(Map<String, dynamic> config) {
|
||||||
final config = getConfig();
|
return {
|
||||||
config['retryCount'] = 3;
|
...config,
|
||||||
config['retryDelay'] = 5000; // ms
|
'retryCount': 3,
|
||||||
return config;
|
'retryDelay': 5000, // ms
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,7 +14,7 @@ class UserController extends GetxController {
|
|||||||
final _userId = 0.obs;
|
final _userId = 0.obs;
|
||||||
final _pointDeVenteId = 0.obs;
|
final _pointDeVenteId = 0.obs;
|
||||||
final _pointDeVenteDesignation = ''.obs;
|
final _pointDeVenteDesignation = ''.obs;
|
||||||
|
|
||||||
// Cache service
|
// Cache service
|
||||||
final PermissionCacheService _cacheService = PermissionCacheService.instance;
|
final PermissionCacheService _cacheService = PermissionCacheService.instance;
|
||||||
|
|
||||||
@ -39,17 +39,18 @@ class UserController extends GetxController {
|
|||||||
Future<void> loadUserData() async {
|
Future<void> loadUserData() async {
|
||||||
try {
|
try {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
final storedUsername = prefs.getString('username') ?? '';
|
final storedUsername = prefs.getString('username') ?? '';
|
||||||
final storedRole = prefs.getString('role') ?? '';
|
final storedRole = prefs.getString('role') ?? '';
|
||||||
final storedUserId = prefs.getInt('user_id') ?? 0;
|
final storedUserId = prefs.getInt('user_id') ?? 0;
|
||||||
final storedPointDeVenteId = prefs.getInt('point_de_vente_id') ?? 0;
|
final storedPointDeVenteId = prefs.getInt('point_de_vente_id') ?? 0;
|
||||||
final storedPointDeVenteDesignation = prefs.getString('point_de_vente_designation') ?? '';
|
final storedPointDeVenteDesignation =
|
||||||
|
prefs.getString('point_de_vente_designation') ?? '';
|
||||||
|
|
||||||
if (storedUsername.isNotEmpty) {
|
if (storedUsername.isNotEmpty) {
|
||||||
try {
|
try {
|
||||||
Users user = await AppDatabase.instance.getUser(storedUsername);
|
Users user = await AppDatabase.instance.getUser(storedUsername);
|
||||||
|
|
||||||
_username.value = user.username;
|
_username.value = user.username;
|
||||||
_email.value = user.email;
|
_email.value = user.email;
|
||||||
_name.value = user.name;
|
_name.value = user.name;
|
||||||
@ -59,14 +60,14 @@ class UserController extends GetxController {
|
|||||||
_userId.value = storedUserId;
|
_userId.value = storedUserId;
|
||||||
_pointDeVenteId.value = storedPointDeVenteId;
|
_pointDeVenteId.value = storedPointDeVenteId;
|
||||||
_pointDeVenteDesignation.value = storedPointDeVenteDesignation;
|
_pointDeVenteDesignation.value = storedPointDeVenteDesignation;
|
||||||
|
|
||||||
if (_pointDeVenteDesignation.value.isEmpty && _pointDeVenteId.value > 0) {
|
if (_pointDeVenteDesignation.value.isEmpty &&
|
||||||
|
_pointDeVenteId.value > 0) {
|
||||||
await loadPointDeVenteDesignation();
|
await loadPointDeVenteDesignation();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ✅ Précharger les permissions en arrière-plan (non bloquant)
|
// ✅ Précharger les permissions en arrière-plan (non bloquant)
|
||||||
_preloadPermissionsInBackground();
|
_preloadPermissionsInBackground();
|
||||||
|
|
||||||
} catch (dbError) {
|
} catch (dbError) {
|
||||||
print("❌ Erreur BDD, utilisation du fallback: $dbError");
|
print("❌ Erreur BDD, utilisation du fallback: $dbError");
|
||||||
_username.value = storedUsername;
|
_username.value = storedUsername;
|
||||||
@ -77,7 +78,7 @@ class UserController extends GetxController {
|
|||||||
_userId.value = storedUserId;
|
_userId.value = storedUserId;
|
||||||
_pointDeVenteId.value = storedPointDeVenteId;
|
_pointDeVenteId.value = storedPointDeVenteId;
|
||||||
_pointDeVenteDesignation.value = storedPointDeVenteDesignation;
|
_pointDeVenteDesignation.value = storedPointDeVenteDesignation;
|
||||||
|
|
||||||
// Précharger quand même
|
// Précharger quand même
|
||||||
_preloadPermissionsInBackground();
|
_preloadPermissionsInBackground();
|
||||||
}
|
}
|
||||||
@ -103,15 +104,17 @@ class UserController extends GetxController {
|
|||||||
|
|
||||||
Future<void> loadPointDeVenteDesignation() async {
|
Future<void> loadPointDeVenteDesignation() async {
|
||||||
if (_pointDeVenteId.value <= 0) return;
|
if (_pointDeVenteId.value <= 0) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final pointDeVente = await AppDatabase.instance.getPointDeVenteById(_pointDeVenteId.value);
|
final pointDeVente =
|
||||||
|
await AppDatabase.instance.getPointDeVenteById(_pointDeVenteId.value);
|
||||||
if (pointDeVente != null) {
|
if (pointDeVente != null) {
|
||||||
_pointDeVenteDesignation.value = pointDeVente['nom'] as String;
|
_pointDeVenteDesignation.value = pointDeVente['nom'] as String;
|
||||||
await saveUserData();
|
await saveUserData();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur lors du chargement de la désignation du point de vente: $e');
|
print(
|
||||||
|
'❌ Erreur lors du chargement de la désignation du point de vente: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,14 +128,14 @@ class UserController extends GetxController {
|
|||||||
_password.value = user.password;
|
_password.value = user.password;
|
||||||
_userId.value = userId;
|
_userId.value = userId;
|
||||||
_pointDeVenteId.value = user.pointDeVenteId ?? 0;
|
_pointDeVenteId.value = user.pointDeVenteId ?? 0;
|
||||||
|
|
||||||
print("✅ Utilisateur mis à jour avec credentials:");
|
print("✅ Utilisateur mis à jour avec credentials:");
|
||||||
print(" Username: ${_username.value}");
|
print(" Username: ${_username.value}");
|
||||||
print(" Role: ${_role.value}");
|
print(" Role: ${_role.value}");
|
||||||
print(" UserID: ${_userId.value}");
|
print(" UserID: ${_userId.value}");
|
||||||
|
|
||||||
saveUserData();
|
saveUserData();
|
||||||
|
|
||||||
// ✅ Précharger immédiatement les permissions après connexion
|
// ✅ Précharger immédiatement les permissions après connexion
|
||||||
_preloadPermissionsInBackground();
|
_preloadPermissionsInBackground();
|
||||||
}
|
}
|
||||||
@ -144,7 +147,7 @@ class UserController extends GetxController {
|
|||||||
_name.value = user.name;
|
_name.value = user.name;
|
||||||
_lastname.value = user.lastName;
|
_lastname.value = user.lastName;
|
||||||
_password.value = user.password;
|
_password.value = user.password;
|
||||||
|
|
||||||
saveUserData();
|
saveUserData();
|
||||||
_preloadPermissionsInBackground();
|
_preloadPermissionsInBackground();
|
||||||
}
|
}
|
||||||
@ -152,7 +155,7 @@ class UserController extends GetxController {
|
|||||||
Future<void> saveUserData() async {
|
Future<void> saveUserData() async {
|
||||||
try {
|
try {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
await prefs.setString('username', _username.value);
|
await prefs.setString('username', _username.value);
|
||||||
await prefs.setString('email', _email.value);
|
await prefs.setString('email', _email.value);
|
||||||
await prefs.setString('role', _role.value);
|
await prefs.setString('role', _role.value);
|
||||||
@ -160,8 +163,9 @@ class UserController extends GetxController {
|
|||||||
await prefs.setString('lastname', _lastname.value);
|
await prefs.setString('lastname', _lastname.value);
|
||||||
await prefs.setInt('user_id', _userId.value);
|
await prefs.setInt('user_id', _userId.value);
|
||||||
await prefs.setInt('point_de_vente_id', _pointDeVenteId.value);
|
await prefs.setInt('point_de_vente_id', _pointDeVenteId.value);
|
||||||
await prefs.setString('point_de_vente_designation', _pointDeVenteDesignation.value);
|
await prefs.setString(
|
||||||
|
'point_de_vente_designation', _pointDeVenteDesignation.value);
|
||||||
|
|
||||||
print("✅ Données sauvegardées avec succès");
|
print("✅ Données sauvegardées avec succès");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur lors de la sauvegarde: $e');
|
print('❌ Erreur lors de la sauvegarde: $e');
|
||||||
@ -172,10 +176,10 @@ class UserController extends GetxController {
|
|||||||
Future<void> clearUserData() async {
|
Future<void> clearUserData() async {
|
||||||
try {
|
try {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
// ✅ IMPORTANT: Vider le cache de session
|
// ✅ IMPORTANT: Vider le cache de session
|
||||||
_cacheService.clearAllCache();
|
_cacheService.clearAllCache();
|
||||||
|
|
||||||
// Effacer SharedPreferences
|
// Effacer SharedPreferences
|
||||||
await prefs.remove('username');
|
await prefs.remove('username');
|
||||||
await prefs.remove('email');
|
await prefs.remove('email');
|
||||||
@ -185,7 +189,7 @@ class UserController extends GetxController {
|
|||||||
await prefs.remove('user_id');
|
await prefs.remove('user_id');
|
||||||
await prefs.remove('point_de_vente_id');
|
await prefs.remove('point_de_vente_id');
|
||||||
await prefs.remove('point_de_vente_designation');
|
await prefs.remove('point_de_vente_designation');
|
||||||
|
|
||||||
// Effacer les observables
|
// Effacer les observables
|
||||||
_username.value = '';
|
_username.value = '';
|
||||||
_email.value = '';
|
_email.value = '';
|
||||||
@ -196,9 +200,8 @@ class UserController extends GetxController {
|
|||||||
_userId.value = 0;
|
_userId.value = 0;
|
||||||
_pointDeVenteId.value = 0;
|
_pointDeVenteId.value = 0;
|
||||||
_pointDeVenteDesignation.value = '';
|
_pointDeVenteDesignation.value = '';
|
||||||
|
|
||||||
print("✅ Données utilisateur et cache de session vidés");
|
print("✅ Données utilisateur et cache de session vidés");
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur lors de l\'effacement: $e');
|
print('❌ Erreur lors de l\'effacement: $e');
|
||||||
}
|
}
|
||||||
@ -215,28 +218,28 @@ class UserController extends GetxController {
|
|||||||
print('⚠️ Username vide, rechargement...');
|
print('⚠️ Username vide, rechargement...');
|
||||||
await loadUserData();
|
await loadUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_username.value.isEmpty) {
|
if (_username.value.isEmpty) {
|
||||||
print('❌ Utilisateur non connecté');
|
print('❌ Utilisateur non connecté');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Essayer d'abord le cache
|
// Essayer d'abord le cache
|
||||||
if (_cacheService.isLoaded) {
|
if (_cacheService.isLoaded) {
|
||||||
return _cacheService.hasPermission(_username.value, permission, route);
|
return _cacheService.hasPermission(_username.value, permission, route);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si pas encore chargé, charger et essayer de nouveau
|
// Si pas encore chargé, charger et essayer de nouveau
|
||||||
print("🔄 Cache non chargé, chargement des permissions...");
|
print("🔄 Cache non chargé, chargement des permissions...");
|
||||||
await _cacheService.loadUserPermissions(_username.value);
|
await _cacheService.loadUserPermissions(_username.value);
|
||||||
|
|
||||||
return _cacheService.hasPermission(_username.value, permission, route);
|
return _cacheService.hasPermission(_username.value, permission, route);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('❌ Erreur vérification permission: $e');
|
print('❌ Erreur vérification permission: $e');
|
||||||
// Fallback vers la méthode originale en cas d'erreur
|
// Fallback vers la méthode originale en cas d'erreur
|
||||||
try {
|
try {
|
||||||
return await AppDatabase.instance.hasPermission(_username.value, permission, route);
|
return await AppDatabase.instance
|
||||||
|
.hasPermission(_username.value, permission, route);
|
||||||
} catch (fallbackError) {
|
} catch (fallbackError) {
|
||||||
print('❌ Erreur fallback permission: $fallbackError');
|
print('❌ Erreur fallback permission: $fallbackError');
|
||||||
return false;
|
return false;
|
||||||
@ -245,7 +248,8 @@ class UserController extends GetxController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// ✅ Vérification de permissions multiples
|
/// ✅ Vérification de permissions multiples
|
||||||
Future<bool> hasAnyPermission(List<String> permissionNames, String menuRoute) async {
|
Future<bool> hasAnyPermission(
|
||||||
|
List<String> permissionNames, String menuRoute) async {
|
||||||
for (String permissionName in permissionNames) {
|
for (String permissionName in permissionNames) {
|
||||||
if (await hasPermission(permissionName, menuRoute)) {
|
if (await hasPermission(permissionName, menuRoute)) {
|
||||||
return true;
|
return true;
|
||||||
@ -286,8 +290,8 @@ class UserController extends GetxController {
|
|||||||
print("IsLoggedIn: $isLoggedIn");
|
print("IsLoggedIn: $isLoggedIn");
|
||||||
print("Cache Ready: $isCacheReady");
|
print("Cache Ready: $isCacheReady");
|
||||||
print("========================");
|
print("========================");
|
||||||
|
|
||||||
// Debug du cache
|
// Debug du cache
|
||||||
_cacheService.debugPrintCache();
|
_cacheService.debugPrintCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user