2023-07-28 02:21:42 +00:00
|
|
|
import 'dart:math';
|
|
|
|
import 'package:app_popup_menu/app_popup_menu.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
|
|
|
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
|
|
|
|
import 'package:flutter_spinkit/flutter_spinkit.dart';
|
|
|
|
import 'package:form_builder_validators/form_builder_validators.dart';
|
|
|
|
import 'package:multi_dropdown/multiselect_dropdown.dart';
|
2023-10-03 00:33:10 +00:00
|
|
|
import 'package:search_page/search_page.dart';
|
2023-07-28 02:21:42 +00:00
|
|
|
import 'package:unit2/bloc/rbac/rbac_operations/permission/permission_bloc.dart';
|
2023-10-03 00:33:10 +00:00
|
|
|
import 'package:unit2/model/rbac/permission.dart';
|
2023-07-28 02:21:42 +00:00
|
|
|
import 'package:unit2/model/rbac/rbac.dart';
|
|
|
|
import 'package:unit2/screens/superadmin/role/shared_pop_up_menu.dart';
|
|
|
|
import 'package:unit2/theme-data.dart/btn-style.dart';
|
|
|
|
import 'package:unit2/theme-data.dart/form-style.dart';
|
|
|
|
import 'package:unit2/utils/global_context.dart';
|
|
|
|
import 'package:unit2/widgets/Leadings/add_leading.dart';
|
|
|
|
import 'package:unit2/widgets/error_state.dart';
|
|
|
|
import '../../../theme-data.dart/box_shadow.dart';
|
|
|
|
import '../../../theme-data.dart/colors.dart';
|
|
|
|
import '../../../utils/alerts.dart';
|
|
|
|
import '../../../utils/global.dart';
|
|
|
|
import '../../../widgets/empty_data.dart';
|
|
|
|
|
|
|
|
class RbacPermissionScreen extends StatelessWidget {
|
|
|
|
final int id;
|
|
|
|
const RbacPermissionScreen({super.key, required this.id});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
List<RBAC> objects = [];
|
|
|
|
RBAC? selectedObject;
|
|
|
|
List<RBAC> operations = [];
|
|
|
|
List<ValueItem> valueItemOperations = [];
|
|
|
|
List<ValueItem> selectedValueItemOperations = [];
|
2023-10-03 00:33:10 +00:00
|
|
|
List<RBACPermission> permissions = [];
|
|
|
|
final bloc = BlocProvider.of<PermissionBloc>(context);
|
2023-07-28 02:21:42 +00:00
|
|
|
final formKey = GlobalKey<FormBuilderState>();
|
|
|
|
BuildContext? parent;
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-08-22 06:22:08 +00:00
|
|
|
centerTitle: true,
|
2023-07-28 02:21:42 +00:00
|
|
|
backgroundColor: primary,
|
|
|
|
title: const Text("Permissions Screen"),
|
2023-10-03 00:33:10 +00:00
|
|
|
actions: context.watch<PermissionBloc>().state is PermissonLoadingState ||
|
|
|
|
context.watch<PermissionBloc>().state is PermissionErrorState ||
|
|
|
|
context.watch<PermissionBloc>().state is PermissionDeletedState ||
|
|
|
|
context.watch<PermissionBloc>().state is PermissionAddedState
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
showSearch(
|
|
|
|
context: context,
|
|
|
|
delegate: SearchPage(
|
|
|
|
barTheme: ThemeData(cardColor: Colors.white),
|
|
|
|
builder: (RBACPermission rbac) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: screenWidth,
|
|
|
|
decoration: box1(),
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 12, vertical: 8),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
"${rbac.object?.name} - ${rbac.operation?.name}",
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.titleMedium!
|
|
|
|
.copyWith(
|
|
|
|
fontWeight:
|
|
|
|
FontWeight.w500,
|
|
|
|
color: primary)),
|
|
|
|
),
|
|
|
|
AppPopupMenu<int>(
|
|
|
|
offset: const Offset(-10, -10),
|
|
|
|
elevation: 3,
|
|
|
|
onSelected: (value) {
|
|
|
|
if (value == 1) {
|
|
|
|
confirmAlert(context, () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
bloc.add(DeleteRbacPermission(
|
|
|
|
permissionId: rbac.id!));
|
|
|
|
}, "Delete?", "Confirm Delete?");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
menuItems: [
|
|
|
|
popMenuItem(
|
|
|
|
text: "Remove",
|
|
|
|
value: 1,
|
|
|
|
icon: Icons.delete),
|
|
|
|
],
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.more_vert,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
tooltip: "Options",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 5,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
filter: (RBACPermission rbac) {
|
|
|
|
return [
|
|
|
|
rbac.object!.name! + rbac.operation!.name!
|
|
|
|
];
|
|
|
|
},
|
|
|
|
failure: const Center(
|
|
|
|
child: Text("No permission found :("),
|
2023-07-28 02:21:42 +00:00
|
|
|
),
|
2023-10-03 00:33:10 +00:00
|
|
|
items: permissions,
|
|
|
|
searchLabel: "Search Permission",
|
|
|
|
suggestion: const Center(
|
|
|
|
child: Text("Search permission by name"),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.search)),
|
|
|
|
AddLeading(onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context:
|
|
|
|
NavigationService.navigatorKey.currentState!.context,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
valueItemOperations = operations.map((e) {
|
|
|
|
return ValueItem(label: e.name!, value: e.name);
|
|
|
|
}).toList();
|
|
|
|
return AlertDialog(
|
|
|
|
title: const Text("Add Permission"),
|
|
|
|
content: FormBuilder(
|
|
|
|
key: formKey,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
FormBuilderDropdown<RBAC>(
|
|
|
|
validator: FormBuilderValidators.required(
|
|
|
|
errorText: "This field is required"),
|
|
|
|
name: "object",
|
|
|
|
decoration: normalTextFieldStyle(
|
|
|
|
"Permission", "Permission"),
|
|
|
|
items: objects.isEmpty
|
|
|
|
? []
|
|
|
|
: objects.map((e) {
|
|
|
|
return DropdownMenuItem(
|
|
|
|
value: e, child: Text(e.name!));
|
|
|
|
}).toList(),
|
|
|
|
onChanged: (RBAC? object) {
|
|
|
|
selectedObject = object;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 12,
|
|
|
|
),
|
|
|
|
MultiSelectDropDown(
|
|
|
|
onOptionSelected:
|
|
|
|
(List<ValueItem> selectedOptions) {
|
|
|
|
selectedValueItemOperations =
|
|
|
|
selectedOptions;
|
|
|
|
},
|
|
|
|
borderColor: Colors.grey,
|
|
|
|
borderWidth: 1,
|
|
|
|
borderRadius: 5,
|
|
|
|
hint: "Operations",
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
options: valueItemOperations,
|
|
|
|
selectionType: SelectionType.multi,
|
|
|
|
chipConfig: const ChipConfig(
|
|
|
|
wrapType: WrapType.wrap),
|
|
|
|
dropdownHeight: 300,
|
|
|
|
optionTextStyle:
|
|
|
|
const TextStyle(fontSize: 16),
|
|
|
|
selectedOptionIcon:
|
|
|
|
const Icon(Icons.check_circle),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 12,
|
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 50,
|
|
|
|
width: double.maxFinite,
|
|
|
|
child: ElevatedButton(
|
|
|
|
style: mainBtnStyle(primary,
|
|
|
|
Colors.transparent, second),
|
|
|
|
onPressed: () {
|
|
|
|
if (formKey.currentState!
|
|
|
|
.saveAndValidate() &&
|
|
|
|
selectedValueItemOperations
|
|
|
|
.isNotEmpty) {
|
|
|
|
int assignerId = id;
|
|
|
|
int objectId = selectedObject!.id!;
|
|
|
|
|
|
|
|
List<int> opIds = [];
|
|
|
|
for (var operation in operations) {
|
|
|
|
selectedValueItemOperations
|
|
|
|
.forEach((element) {
|
|
|
|
if (element.label
|
|
|
|
.toLowerCase() ==
|
|
|
|
operation.name
|
|
|
|
?.toLowerCase()) {
|
|
|
|
opIds.add(operation.id!);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-07-28 02:21:42 +00:00
|
|
|
|
2023-10-03 00:33:10 +00:00
|
|
|
Navigator.pop(context);
|
|
|
|
parent!.read<PermissionBloc>().add(
|
|
|
|
AddRbacPermission(
|
|
|
|
assignerId: assignerId,
|
|
|
|
objectId: objectId,
|
|
|
|
operationIds: opIds));
|
2023-07-28 02:21:42 +00:00
|
|
|
}
|
2023-10-03 00:33:10 +00:00
|
|
|
},
|
|
|
|
child: const Text("Submit")),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
2023-07-28 02:21:42 +00:00
|
|
|
),
|
|
|
|
body: ProgressHUD(
|
|
|
|
padding: const EdgeInsets.all(24),
|
|
|
|
backgroundColor: Colors.black87,
|
|
|
|
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
|
|
|
|
child: BlocConsumer<PermissionBloc, PermissionState>(
|
|
|
|
listener: (context, state) {
|
|
|
|
if (state is PermissonLoadingState) {
|
|
|
|
final progress = ProgressHUD.of(context);
|
|
|
|
progress!.showWithText("Please wait...");
|
|
|
|
}
|
|
|
|
if (state is PermissionLoaded ||
|
|
|
|
state is PermissionErrorState ||
|
|
|
|
state is PermissionAddedState) {
|
|
|
|
final progress = ProgressHUD.of(context);
|
|
|
|
progress!.dismiss();
|
|
|
|
}
|
|
|
|
////Added State
|
|
|
|
if (state is PermissionAddedState) {
|
|
|
|
if (state.response['success']) {
|
|
|
|
successAlert(
|
|
|
|
context, "Adding Successfull!", state.response['message'],
|
|
|
|
() {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
context.read<PermissionBloc>().add(GetPermissions());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
errorAlert(context, "Adding Failed", state.response['message'],
|
|
|
|
() {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
context.read<PermissionBloc>().add(GetPermissions());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
////Deleted State
|
|
|
|
if (state is PermissionDeletedState) {
|
|
|
|
if (state.success) {
|
|
|
|
successAlert(context, "Delete Successfull!",
|
|
|
|
"Permission Deleted Successfully", () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
context.read<PermissionBloc>().add(GetPermissions());
|
|
|
|
});
|
|
|
|
} else {
|
2023-10-03 00:33:10 +00:00
|
|
|
errorAlert(
|
|
|
|
context, "Delete Failed", "Permission Deletion Failed", () {
|
2023-07-28 02:21:42 +00:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
context.read<PermissionBloc>().add(GetPermissions());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
builder: (context, state) {
|
|
|
|
parent = context;
|
|
|
|
if (state is PermissionLoaded) {
|
|
|
|
objects = state.objects;
|
|
|
|
operations = state.operations;
|
|
|
|
if (state.permissions.isNotEmpty) {
|
2023-10-03 00:33:10 +00:00
|
|
|
permissions = state.permissions;
|
2023-07-28 02:21:42 +00:00
|
|
|
return ListView.builder(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(vertical: 8, horizontal: 10),
|
|
|
|
itemCount: state.permissions.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: screenWidth,
|
|
|
|
decoration: box1(),
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 12, vertical: 8),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Row(
|
2023-10-03 00:33:10 +00:00
|
|
|
children: [
|
|
|
|
CircleAvatar(
|
2023-07-28 02:21:42 +00:00
|
|
|
child: Text('${index + 1}'),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 12,
|
|
|
|
),
|
2023-10-03 00:33:10 +00:00
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
"${state.permissions[index].object?.name} - ${state.permissions[index].operation?.name}",
|
|
|
|
style: Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.titleMedium!
|
|
|
|
.copyWith(
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
color: primary)),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)),
|
2023-07-28 02:21:42 +00:00
|
|
|
AppPopupMenu<int>(
|
|
|
|
offset: const Offset(-10, -10),
|
|
|
|
elevation: 3,
|
|
|
|
onSelected: (value) {
|
2023-10-03 00:33:10 +00:00
|
|
|
if (value == 1) {
|
2023-07-28 02:21:42 +00:00
|
|
|
confirmAlert(context, () {
|
2023-10-03 00:33:10 +00:00
|
|
|
context.read<PermissionBloc>().add(
|
|
|
|
DeleteRbacPermission(
|
|
|
|
permissionId: state
|
|
|
|
.permissions[index].id!));
|
2023-07-28 02:21:42 +00:00
|
|
|
}, "Delete?", "Confirm Delete?");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
menuItems: [
|
|
|
|
popMenuItem(
|
|
|
|
text: "Remove",
|
|
|
|
value: 1,
|
|
|
|
icon: Icons.delete),
|
|
|
|
],
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.more_vert,
|
|
|
|
color: Colors.grey,
|
|
|
|
),
|
|
|
|
tooltip: "Options",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 5,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return const EmptyData(
|
|
|
|
message: "No Permission available. Please click + to add.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (state is PermissionErrorState) {
|
|
|
|
return SomethingWentWrong(
|
2023-10-03 00:33:10 +00:00
|
|
|
message: state.message,
|
|
|
|
onpressed: () {
|
|
|
|
parent!.read<PermissionBloc>().add(GetPermissions());
|
2023-08-22 06:22:08 +00:00
|
|
|
});
|
2023-07-28 02:21:42 +00:00
|
|
|
}
|
|
|
|
return Container();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|