mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
restrict_spaceMemberUser_and_change_SignUpModel
This commit is contained in:
@ -7,10 +7,10 @@ import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:onesignal_flutter/onesignal_flutter.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/permission_model.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/app_layout/view/widgets/app_bar_home_dropdown.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
||||
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
|
||||
import 'package:syncrow_app/features/devices/view/widgets/devices_view_body.dart';
|
||||
@ -30,23 +30,30 @@ import 'package:syncrow_app/services/api/profile_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
|
||||
part 'home_state.dart';
|
||||
|
||||
class HomeCubit extends Cubit<HomeState> {
|
||||
HomeCubit._() : super(HomeInitial()) {
|
||||
// checkIfNotificationPermissionGranted();
|
||||
fetchUserInfo();
|
||||
if (selectedSpace == null) {
|
||||
fetchUnitsByUserId();
|
||||
// .then((value) {
|
||||
// if (selectedSpace != null) {
|
||||
// fetchRoomsByUnitId(selectedSpace!);
|
||||
// }
|
||||
// });
|
||||
}
|
||||
fetchUserInfo().then(
|
||||
(value) {
|
||||
if (selectedSpace == null) {
|
||||
fetchUnitsByUserId();
|
||||
fetchPermissions();
|
||||
|
||||
// .then((value) {
|
||||
// if (selectedSpace != null) {
|
||||
// fetchRoomsByUnitId(selectedSpace!);
|
||||
// }
|
||||
// });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
static UserModel? user;
|
||||
|
||||
List<PermissionModel>? permissionModel = [];
|
||||
|
||||
static HomeCubit? _instance;
|
||||
static HomeCubit getInstance() {
|
||||
// If an instance already exists, return it
|
||||
@ -56,15 +63,87 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
|
||||
Future fetchUserInfo() async {
|
||||
try {
|
||||
emit(HomeLoading());
|
||||
var uuid =
|
||||
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
||||
user = await ProfileApi().fetchUserInfo(uuid);
|
||||
emit(HomeUserInfoLoaded(user!)); // Emit state after fetching user info
|
||||
emit(HomeUserInfoLoaded(user!));
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static bool manageSupSpace = false;
|
||||
static bool manageScene = false;
|
||||
static bool manageDeviceLocation = false;
|
||||
static bool visitorPasswordManagement = false;
|
||||
|
||||
Future<void> fetchPermissions() async {
|
||||
try {
|
||||
emit(HomeLoading());
|
||||
final response = await ProfileApi().fetchPermissions(user!.role!.uuid);
|
||||
permissionModel = PermissionModel.fromJsonList(response);
|
||||
hasViewPermission();
|
||||
emit(PermissionsRoleLoaded(permissionModel!));
|
||||
} catch (e) {
|
||||
emit(HomeError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void hasViewPermission() {
|
||||
emit(HomeLoading());
|
||||
|
||||
manageSupSpace = hasPermission(
|
||||
permissionModel!,
|
||||
'SPACE_MANAGEMENT',
|
||||
'MANAGE_SPACE',
|
||||
'ASSIGN_USER_TO_SPACE',
|
||||
);
|
||||
manageScene = hasPermission(
|
||||
permissionModel!,
|
||||
'AUTOMATION_MANAGEMENT',
|
||||
'MANAGE_SCENES',
|
||||
'UPDATE',
|
||||
);
|
||||
manageDeviceLocation = hasPermission(
|
||||
permissionModel!,
|
||||
'DEVICE_MANAGEMENT',
|
||||
'MANAGE_DEVICE',
|
||||
'LOCATION_UPDATE',
|
||||
);
|
||||
visitorPasswordManagement = hasPermission(
|
||||
permissionModel!,
|
||||
'VISITOR_PASSWORD_MANAGEMENT',
|
||||
'MANAGE_VISITOR_PASSWORD',
|
||||
'VIEW',
|
||||
);
|
||||
emit(HomePermissionUpdated());
|
||||
}
|
||||
|
||||
bool hasPermission(List<PermissionModel> permissions, String mainTitle,
|
||||
String subTitle, String finalTitle) {
|
||||
try {
|
||||
final mainOption = permissions.firstWhere(
|
||||
(perm) => perm.title == mainTitle,
|
||||
);
|
||||
|
||||
final subOption = mainOption.subOptions.firstWhere(
|
||||
(sub) => sub.title == subTitle,
|
||||
);
|
||||
|
||||
if (subOption.subOptions == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final finalOption = subOption.subOptions!.firstWhere(
|
||||
(finalSub) => finalSub.title == finalTitle,
|
||||
);
|
||||
return finalOption.isChecked == true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void emitSafe(HomeState newState) {
|
||||
final cubit = this;
|
||||
if (!cubit.isClosed) {
|
||||
@ -89,7 +168,7 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
|
||||
static HomeCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
List<SpaceModel>? spaces;
|
||||
List<SpaceModel>? spaces = [];
|
||||
|
||||
SpaceModel? selectedSpace;
|
||||
|
||||
@ -261,6 +340,7 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
emitSafe(GetSpacesLoading());
|
||||
try {
|
||||
spaces = await SpacesAPI.getSpacesByUserId();
|
||||
emitSafe(GetSpacesSuccess(spaces!));
|
||||
} catch (failure) {
|
||||
emitSafe(GetSpacesError("No units found"));
|
||||
return;
|
||||
@ -298,11 +378,13 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
||||
var res = await SpacesAPI.activationCodeSpace(
|
||||
activationCode: activationCode, userUuid: uuid);
|
||||
|
||||
if (res['success'] == true) {
|
||||
fetchUserInfo();
|
||||
fetchUnitsByUserId();
|
||||
}
|
||||
emitSafe(GetSpacesSuccess(spaces!));
|
||||
|
||||
return res['success'];
|
||||
} on DioException catch (e) {
|
||||
final errorMessage = e.response?.data['error']['message'];
|
||||
@ -378,6 +460,8 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
// ),
|
||||
// onPressed: () {},
|
||||
// ),
|
||||
// HomeCubit.permissionModel!.scene!.visible == true
|
||||
// ?
|
||||
IconButton(
|
||||
icon: const Icon(
|
||||
Icons.add,
|
||||
@ -413,7 +497,8 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
.read<CreateSceneBloc>()
|
||||
.add(const ClearTabToRunSetting());
|
||||
},
|
||||
),
|
||||
)
|
||||
// : const SizedBox(),
|
||||
// IconButton(
|
||||
// icon: const Icon(
|
||||
// Icons.more_vert,
|
||||
@ -503,3 +588,28 @@ BottomNavigationBarItem defaultBottomNavBarItem(
|
||||
label: label,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// class PermissionUtils {
|
||||
// // Check if the "VIEW" permission exists in "MANAGE_SUBSPACE"
|
||||
// static bool hasViewPermission(List<dynamic> permissions) {
|
||||
// return _hasPermission(permissions, 'MANAGE_SUBSPACE', 'VIEW');
|
||||
// }
|
||||
|
||||
// // Generalized permission checker
|
||||
// static bool _hasPermission(
|
||||
// List<dynamic> permissions, String mainTitle, String subTitle) {
|
||||
// final mainOption = permissions.firstWhere(
|
||||
// (perm) => perm['title'] == mainTitle,
|
||||
// orElse: () => null,
|
||||
// );
|
||||
// if (mainOption != null) {
|
||||
// final subOption = mainOption['subOptions'].firstWhere(
|
||||
// (sub) => sub['title'] == subTitle,
|
||||
// orElse: () => null,
|
||||
// );
|
||||
// return subOption != null && subOption['isChecked'] == true;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
@ -14,9 +14,9 @@ class HomeError extends HomeState {
|
||||
}
|
||||
|
||||
class HomeSuccess extends HomeState {}
|
||||
class ActivationSuccess extends HomeState {}
|
||||
|
||||
|
||||
///specific states
|
||||
//get spaces
|
||||
class GetSpacesLoading extends HomeLoading {}
|
||||
|
||||
class GetSpacesSuccess extends HomeSuccess {
|
||||
@ -64,10 +64,16 @@ class RoomSelected extends HomeState {
|
||||
class RoomUnSelected extends HomeState {}
|
||||
|
||||
class NavChangePage extends HomeState {}
|
||||
class HomePermissionUpdated extends HomeState {}
|
||||
|
||||
|
||||
// Define new state classes
|
||||
class HomeUserInfoLoaded extends HomeState {
|
||||
final UserModel user;
|
||||
|
||||
HomeUserInfoLoaded(this.user);
|
||||
}
|
||||
|
||||
class PermissionsRoleLoaded extends HomeState {
|
||||
final List<PermissionModel> permissionModel;
|
||||
PermissionsRoleLoaded(this.permissionModel);
|
||||
}
|
||||
|
39
lib/features/app_layout/model/permission_model.dart
Normal file
39
lib/features/app_layout/model/permission_model.dart
Normal file
@ -0,0 +1,39 @@
|
||||
class PermissionModel {
|
||||
final String title;
|
||||
final List<PermissionAttributes> subOptions;
|
||||
|
||||
PermissionModel({required this.title, required this.subOptions});
|
||||
|
||||
factory PermissionModel.fromJson(Map<String, dynamic> json) {
|
||||
return PermissionModel(
|
||||
title: json['title'],
|
||||
subOptions: (json['subOptions'] as List)
|
||||
.map((e) => PermissionAttributes.fromJson(e))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
static List<PermissionModel> fromJsonList(List<dynamic> jsonList) {
|
||||
return jsonList.map((json) => PermissionModel.fromJson(json)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
class PermissionAttributes {
|
||||
final String title;
|
||||
final List<PermissionAttributes>? subOptions;
|
||||
final bool? isChecked;
|
||||
|
||||
PermissionAttributes({required this.title, this.subOptions, this.isChecked});
|
||||
|
||||
factory PermissionAttributes.fromJson(Map<String, dynamic> json) {
|
||||
return PermissionAttributes(
|
||||
title: json['title'],
|
||||
isChecked: json['isChecked'],
|
||||
subOptions: json['subOptions'] != null
|
||||
? (json['subOptions'] as List)
|
||||
.map((e) => PermissionAttributes.fromJson(e))
|
||||
.toList()
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
@ -17,7 +17,10 @@ class AppLayout extends StatelessWidget {
|
||||
child: BlocBuilder<HomeCubit, HomeState>(
|
||||
builder: (context, state) {
|
||||
return DefaultScaffold(
|
||||
appBar: HomeCubit.getInstance().spaces != null ? const DefaultAppBar() : null,
|
||||
appBar: HomeCubit.getInstance().spaces != null &&
|
||||
HomeCubit.getInstance().spaces!.isNotEmpty
|
||||
? const DefaultAppBar()
|
||||
: null,
|
||||
bottomNavBar: const DefaultNavBar(),
|
||||
child: const AppBody(),
|
||||
);
|
||||
|
@ -35,8 +35,10 @@ class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
)
|
||||
: null,
|
||||
),
|
||||
actions: HomeCubit.appBarActions[
|
||||
HomeCubit.bottomNavItems[HomeCubit.pageIndex].label],
|
||||
actions: HomeCubit.manageScene
|
||||
? HomeCubit.appBarActions[
|
||||
HomeCubit.bottomNavItems[HomeCubit.pageIndex].label]
|
||||
: null,
|
||||
));
|
||||
},
|
||||
);
|
||||
|
Reference in New Issue
Block a user