Skip to content

Commit

Permalink
Fix: convert checkbox to stateful widget to fix permissions for servi…
Browse files Browse the repository at this point in the history
…ce accounts failing occasionally (#1170)

* convert checkbox to stateful widget

* convert checkbox to stateful widget also on the groups permission page
  • Loading branch information
IrinaSouth authored Jul 22, 2024
1 parent 674f5e2 commit a125472
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,22 @@ class _GroupPermissionDetailState extends State<_GroupPermissionDetailWidget> {
padding: const EdgeInsets.all(8.0),
child: SelectableText(env.name),
),
getPermissionCheckbox(env.id, RoleType.READ),
getPermissionCheckbox(env.id, RoleType.LOCK),
getPermissionCheckbox(env.id, RoleType.UNLOCK),
getPermissionCheckbox(env.id, RoleType.CHANGE_VALUE),
PermissionsCheckbox(
envId: env.id,
newEnvironmentRoles: newEnvironmentRoles,
roleType: RoleType.READ),
PermissionsCheckbox(
envId: env.id,
newEnvironmentRoles: newEnvironmentRoles,
roleType: RoleType.LOCK),
PermissionsCheckbox(
envId: env.id,
newEnvironmentRoles: newEnvironmentRoles,
roleType: RoleType.UNLOCK),
PermissionsCheckbox(
envId: env.id,
newEnvironmentRoles: newEnvironmentRoles,
roleType: RoleType.CHANGE_VALUE),
]));
}

Expand Down Expand Up @@ -399,25 +411,6 @@ class _GroupPermissionDetailState extends State<_GroupPermissionDetailWidget> {
]);
}

Widget getPermissionCheckbox(String envId, RoleType roleType) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Checkbox(
value: newEnvironmentRoles.containsKey(envId) &&
newEnvironmentRoles[envId]!.roles.contains(roleType),
onChanged: (value) {
setState(() {
if (value == true) {
newEnvironmentRoles[envId]!.roles.add(roleType);
} else {
newEnvironmentRoles[envId]!.roles.remove(roleType);
}
});
},
),
);
}

bool hasEditPermission(Group group, String aid) {
final agr = group.applicationRoles.firstWhereOrNull(
(item) => item.applicationId == aid && item.groupId == group.id);
Expand Down Expand Up @@ -494,3 +487,43 @@ class _GroupPermissionDetailState extends State<_GroupPermissionDetailWidget> {
}
}
}

class PermissionsCheckbox extends StatefulWidget {
final Map<String, EnvironmentGroupRole> newEnvironmentRoles;
final String envId;
final RoleType roleType;
const PermissionsCheckbox(
{Key? key,
required this.envId,
required this.newEnvironmentRoles,
required this.roleType})
: super(key: key);

@override
State<PermissionsCheckbox> createState() => _PermissionsCheckboxState();
}

class _PermissionsCheckboxState extends State<PermissionsCheckbox> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Checkbox(
value: widget.newEnvironmentRoles.containsKey(widget.envId) &&
widget.newEnvironmentRoles[widget.envId]!.roles
.contains(widget.roleType),
onChanged: (value) {
setState(() {
if (value == true) {
widget.newEnvironmentRoles[widget.envId]!.roles
.add(widget.roleType);
} else {
widget.newEnvironmentRoles[widget.envId]!.roles
.remove(widget.roleType);
}
});
},
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,33 @@ class _ServiceAccountPermissionDetailState
padding: const EdgeInsets.all(8.0),
child: SelectableText(env.name),
),
getPermissionCheckbox(env.id, RoleType.READ),
getPermissionCheckbox(env.id, RoleType.LOCK),
getPermissionCheckbox(env.id, RoleType.UNLOCK),
getPermissionCheckbox(env.id, RoleType.CHANGE_VALUE),
PermissionsCheckbox(
newServiceAccountPermission:
newServiceAccountPermission,
envId: env.id,
permissionType: RoleType.READ),
PermissionsCheckbox(
newServiceAccountPermission:
newServiceAccountPermission,
envId: env.id,
permissionType: RoleType.LOCK),
PermissionsCheckbox(
newServiceAccountPermission:
newServiceAccountPermission,
envId: env.id,
permissionType: RoleType.UNLOCK),
PermissionsCheckbox(
newServiceAccountPermission:
newServiceAccountPermission,
envId: env.id,
permissionType: RoleType.CHANGE_VALUE),
if (widget.bloc.mrClient.identityProviders
.featurePropertyExtendedDataEnabled)
getPermissionCheckbox(env.id, RoleType.EXTENDED_DATA),
PermissionsCheckbox(
newServiceAccountPermission:
newServiceAccountPermission,
envId: env.id,
permissionType: RoleType.EXTENDED_DATA),
]));
}

Expand Down Expand Up @@ -274,8 +294,11 @@ class _ServiceAccountPermissionDetailState
newSa.permissions = newList;
widget.bloc
.updateServiceAccountPermissions(
newSa.id, saSnapshot.data!,
(envSnapshot.data?.isNotEmpty == true) ? envSnapshot.data?.first.applicationId : null)
newSa.id,
saSnapshot.data!,
(envSnapshot.data?.isNotEmpty == true)
? envSnapshot.data?.first.applicationId
: null)
.then((serviceAccount) => widget.bloc.mrClient
.addSnackbar(Text(
"Service account '${serviceAccount?.name ?? '<unknown>'}' updated!")))
Expand Down Expand Up @@ -345,30 +368,6 @@ class _ServiceAccountPermissionDetailState
]);
}

Widget getPermissionCheckbox(String envId, RoleType permissionType) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Checkbox(
value: newServiceAccountPermission[envId]!
.permissions
.contains(permissionType),
onChanged: (bool? value) {
setState(() {
if (value == true) {
newServiceAccountPermission[envId]!
.permissions
.add(permissionType);
} else {
newServiceAccountPermission[envId]!
.permissions
.remove(permissionType);
}
});
},
),
);
}

Map<String, ServiceAccountPermission> createMap(
List<Environment> environments, ServiceAccount serviceAccount) {
final retMap = <String, ServiceAccountPermission>{};
Expand All @@ -387,3 +386,42 @@ class _ServiceAccountPermissionDetailState
return retMap;
}
}

class PermissionsCheckbox extends StatefulWidget {
final Map<String, ServiceAccountPermission> newServiceAccountPermission;
final String envId;
final RoleType permissionType;
const PermissionsCheckbox(
{Key? key,
required this.newServiceAccountPermission,
required this.envId,
required this.permissionType})
: super(key: key);

@override
State<PermissionsCheckbox> createState() => _PermissionsCheckboxState();
}

class _PermissionsCheckboxState extends State<PermissionsCheckbox> {
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Checkbox(
value: widget.newServiceAccountPermission[widget.envId]!.permissions
.contains(widget.permissionType),
onChanged: (bool? value) {
setState(() {
if (value == true) {
widget.newServiceAccountPermission[widget.envId]!.permissions
.add(widget.permissionType);
} else {
widget.newServiceAccountPermission[widget.envId]!.permissions
.remove(widget.permissionType);
}
});
},
),
);
}
}

0 comments on commit a125472

Please sign in to comment.