implemented permision controller functionallity
This commit is contained in:
parent
27a1c6a3a7
commit
c9a3805819
100
lib/controller/permission_controller.dart
Normal file
100
lib/controller/permission_controller.dart
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:get/get.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:marco/helpers/services/permission_service.dart';
|
||||||
|
import 'package:marco/model/user_permission.dart';
|
||||||
|
|
||||||
|
class PermissionController extends GetxController {
|
||||||
|
var permissions = <UserPermission>[].obs;
|
||||||
|
Timer? _refreshTimer;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onInit() {
|
||||||
|
super.onInit();
|
||||||
|
_loadStoredPermissions(); // Try to load from local storage first
|
||||||
|
_startAutoRefresh(); // Schedule auto-refresh every 15 minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load permissions from SharedPreferences
|
||||||
|
Future<void> _loadStoredPermissions() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
final storedJson = prefs.getString('user_permissions');
|
||||||
|
|
||||||
|
if (storedJson != null) {
|
||||||
|
print("Loaded Permissions from SharedPreferences: $storedJson");
|
||||||
|
try {
|
||||||
|
final List<dynamic> parsedList = jsonDecode(storedJson);
|
||||||
|
|
||||||
|
permissions.assignAll(
|
||||||
|
parsedList
|
||||||
|
.map((e) => UserPermission.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
print("Error decoding stored permissions: $e");
|
||||||
|
await prefs.remove('user_permissions');
|
||||||
|
await _loadPermissionsFromAPI(); // fallback to API load
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If no permissions stored, fallback to API
|
||||||
|
await _loadPermissionsFromAPI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save permissions to SharedPreferences
|
||||||
|
Future<void> _storePermissions() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
final jsonList = permissions.map((e) => e.toJson()).toList();
|
||||||
|
print("Storing Permissions: $jsonList");
|
||||||
|
await prefs.setString('user_permissions', jsonEncode(jsonList));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Public method to load permissions (usually called from outside)
|
||||||
|
Future<void> loadPermissions(String token) async {
|
||||||
|
try {
|
||||||
|
final result = await PermissionService.fetchPermissions(token);
|
||||||
|
print("Fetched Permissions from API: $result");
|
||||||
|
|
||||||
|
permissions.assignAll(result); // Update observable list
|
||||||
|
await _storePermissions(); // Cache locally
|
||||||
|
} catch (e) {
|
||||||
|
print('Error loading permissions from API: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Internal helper to load token and fetch permissions from API
|
||||||
|
Future<void> _loadPermissionsFromAPI() async {
|
||||||
|
final token = await _getAuthToken();
|
||||||
|
if (token != null && token.isNotEmpty) {
|
||||||
|
await loadPermissions(token);
|
||||||
|
} else {
|
||||||
|
print("No token available for fetching permissions.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve token from SharedPreferences
|
||||||
|
Future<String?> _getAuthToken() async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
return prefs.getString(
|
||||||
|
'jwt_token'); // Or 'auth_token' if that’s the key you're using
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-refresh every 15 minutes
|
||||||
|
void _startAutoRefresh() {
|
||||||
|
_refreshTimer = Timer.periodic(Duration(minutes: 30), (timer) async {
|
||||||
|
await _loadPermissionsFromAPI();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for specific permission
|
||||||
|
bool hasPermission(String permissionId) {
|
||||||
|
return permissions.any((p) => p.id == permissionId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onClose() {
|
||||||
|
_refreshTimer?.cancel();
|
||||||
|
super.onClose();
|
||||||
|
}
|
||||||
|
}
|
44
lib/helpers/services/permission_service.dart
Normal file
44
lib/helpers/services/permission_service.dart
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:marco/model/user_permission.dart';
|
||||||
|
|
||||||
|
class PermissionService {
|
||||||
|
static Future<List<UserPermission>> fetchPermissions(String token) async {
|
||||||
|
try {
|
||||||
|
final response = await http.get(
|
||||||
|
Uri.parse('https://api.marcoaiot.com/api/user/profile'),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Print the full response for debugging
|
||||||
|
print('Status Code: ${response.statusCode}');
|
||||||
|
print('Response Body: ${response.body}');
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final decoded = json.decode(response.body);
|
||||||
|
|
||||||
|
// Debug the decoded data
|
||||||
|
print('Decoded Data: $decoded');
|
||||||
|
|
||||||
|
// Extract featurePermissions
|
||||||
|
final List<dynamic> featurePermissions =
|
||||||
|
decoded['data']['featurePermissions'];
|
||||||
|
|
||||||
|
// Check if the featurePermissions are indeed a List of Strings
|
||||||
|
print('FeaturePermissions Type: ${featurePermissions.runtimeType}');
|
||||||
|
|
||||||
|
// Map the featurePermissions to UserPermission objects
|
||||||
|
return featurePermissions
|
||||||
|
.map<UserPermission>(
|
||||||
|
(permissionId) => UserPermission.fromJson({'id': permissionId}))
|
||||||
|
.toList();
|
||||||
|
} else {
|
||||||
|
final errorData = json.decode(response.body);
|
||||||
|
throw Exception('Failed to load permissions: ${errorData['message']}');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Error fetching permissions: $e');
|
||||||
|
throw Exception('Error fetching permissions: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,8 @@ import 'package:marco/helpers/services/auth_service.dart';
|
|||||||
import 'package:marco/helpers/services/localizations/language.dart';
|
import 'package:marco/helpers/services/localizations/language.dart';
|
||||||
import 'package:marco/helpers/theme/theme_customizer.dart';
|
import 'package:marco/helpers/theme/theme_customizer.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:marco/model/user_permission.dart';
|
||||||
|
import 'dart:convert';
|
||||||
class LocalStorage {
|
class LocalStorage {
|
||||||
static const String _loggedInUserKey = "user";
|
static const String _loggedInUserKey = "user";
|
||||||
static const String _themeCustomizerKey = "theme_customizer";
|
static const String _themeCustomizerKey = "theme_customizer";
|
||||||
@ -18,6 +19,34 @@ class LocalStorage {
|
|||||||
}
|
}
|
||||||
return _preferencesInstance!;
|
return _preferencesInstance!;
|
||||||
}
|
}
|
||||||
|
// In LocalStorage class
|
||||||
|
static const String _userPermissionsKey = "user_permissions";
|
||||||
|
|
||||||
|
static Future<bool> setUserPermissions(
|
||||||
|
List<UserPermission> permissions) async {
|
||||||
|
// Convert the list of UserPermission objects to a List<Map<String, dynamic>>
|
||||||
|
final jsonList = permissions.map((e) => e.toJson()).toList();
|
||||||
|
|
||||||
|
// Save as a JSON string
|
||||||
|
return preferences.setString(_userPermissionsKey, jsonEncode(jsonList));
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<UserPermission> getUserPermissions() {
|
||||||
|
final storedJson = preferences.getString(_userPermissionsKey);
|
||||||
|
|
||||||
|
if (storedJson != null) {
|
||||||
|
final List<dynamic> parsedList = jsonDecode(storedJson);
|
||||||
|
return parsedList
|
||||||
|
.map((e) => UserPermission.fromJson(e as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return []; // Return empty list if no permissions are found
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<bool> removeUserPermissions() async {
|
||||||
|
return preferences.remove(_userPermissionsKey);
|
||||||
|
}
|
||||||
|
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
_preferencesInstance = await SharedPreferences.getInstance();
|
_preferencesInstance = await SharedPreferences.getInstance();
|
||||||
|
11
lib/helpers/utils/permission_constants.dart
Normal file
11
lib/helpers/utils/permission_constants.dart
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
class Permissions {
|
||||||
|
static const String manageMaster = "588a8824-f924-4955-82d8-fc51956cf323";
|
||||||
|
static const String manageProject = "172fc9b6-755b-4f62-ab26-55c34a330614";
|
||||||
|
static const String viewProjects = "6ea44136-987e-44ba-9e5d-1cf8f5837ebc";
|
||||||
|
static const String manageEmployees = "a97d366a-c2bb-448d-be93-402bd2324566";
|
||||||
|
static const String manageProjectInfra = "f2aee20a-b754-4537-8166-f9507b44585b";
|
||||||
|
static const String viewProjectInfra = "c7b68e33-72f0-474f-bd96-77636427ecc8";
|
||||||
|
static const String regularizeAttendance = "57802c4a-00aa-4a1f-a048-fd2f70dd44b6";
|
||||||
|
static const String assignToProject = "fbd213e0-0250-46f1-9f5f-4b2a1e6e76a3";
|
||||||
|
static const String infrastructure = "9666de86-d7c7-4d3d-acaa-fcd6d6b81f3c";
|
||||||
|
}
|
@ -11,7 +11,7 @@ import 'package:marco/helpers/theme/theme_customizer.dart';
|
|||||||
import 'package:marco/routes.dart';
|
import 'package:marco/routes.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:url_strategy/url_strategy.dart';
|
import 'package:url_strategy/url_strategy.dart';
|
||||||
|
import 'package:marco/controller/permission_controller.dart';
|
||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
setPathUrlStrategy();
|
setPathUrlStrategy();
|
||||||
@ -19,9 +19,11 @@ Future<void> main() async {
|
|||||||
await LocalStorage.init();
|
await LocalStorage.init();
|
||||||
AppStyle.init();
|
AppStyle.init();
|
||||||
await ThemeCustomizer.init();
|
await ThemeCustomizer.init();
|
||||||
|
Get.put(PermissionController());
|
||||||
runApp(ChangeNotifierProvider<AppNotifier>(
|
runApp(ChangeNotifierProvider<AppNotifier>(
|
||||||
create: (context) => AppNotifier(),
|
create: (context) => AppNotifier(),
|
||||||
child: MyApp(),
|
child: MyApp(),
|
||||||
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,7 +44,9 @@ class MyApp extends StatelessWidget {
|
|||||||
getPages: getPageRoute(),
|
getPages: getPageRoute(),
|
||||||
builder: (context, child) {
|
builder: (context, child) {
|
||||||
NavigationService.registerContext(context);
|
NavigationService.registerContext(context);
|
||||||
return Directionality(textDirection: AppTheme.textDirection, child: child ?? Container());
|
return Directionality(
|
||||||
|
textDirection: AppTheme.textDirection,
|
||||||
|
child: child ?? Container());
|
||||||
},
|
},
|
||||||
localizationsDelegates: [
|
localizationsDelegates: [
|
||||||
AppLocalizationsDelegate(context),
|
AppLocalizationsDelegate(context),
|
||||||
|
19
lib/model/user_permission.dart
Normal file
19
lib/model/user_permission.dart
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class UserPermission {
|
||||||
|
final String id;
|
||||||
|
|
||||||
|
UserPermission({required this.id});
|
||||||
|
|
||||||
|
// Deserialize from JSON
|
||||||
|
factory UserPermission.fromJson(Map<String, dynamic> json) {
|
||||||
|
return UserPermission(
|
||||||
|
id: json['id'], // Only the 'id' field is needed
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize to JSON
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'id': id, // Only include 'id' when converting to JSON
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user