group_permission_settings: Refactor get_assigned_permission_object.

- Function now returns early if can_edit_settings is false since
we do not want details if the group has direct permission or not.
- Added function to get the tooltip text if group has permission
due to being subgroup of a group that has permission.
- Added comments for some explaination around different blocks.
This commit is contained in:
Sahil Batra
2025-01-28 15:14:34 +05:30
committed by Tim Abbott
parent 143b4d30d3
commit bae14944ad

View File

@@ -137,71 +137,94 @@ export type AssignedGroupPermission = {
tooltip_message?: string; tooltip_message?: string;
}; };
export function get_tooltip_for_group_without_direct_permission(supergroup_id: number): string {
const supergroup = user_groups.get_user_group_from_id(supergroup_id);
return $t(
{
defaultMessage:
"This group has this permission because it's a subgroup of {supergroup_name}.",
},
{
supergroup_name: user_groups.get_display_group_name(supergroup.name),
},
);
}
export function get_assigned_permission_object( export function get_assigned_permission_object(
setting_value: GroupSettingValue, setting_value: GroupSettingValue,
setting_name: RealmGroupSettingName | StreamGroupSettingName | GroupGroupSettingName, setting_name: RealmGroupSettingName | StreamGroupSettingName | GroupGroupSettingName,
group_id: number, group_id: number,
can_edit_settings: boolean, can_edit_settings: boolean,
): AssignedGroupPermission | undefined { ): AssignedGroupPermission | undefined {
// This function returns an object of type AssignedGroupPermission
// containing details about whether the user can edit the setting,
// if the group has the permission, and returns undefined otherwise.
const assigned_permission_object: AssignedGroupPermission = { const assigned_permission_object: AssignedGroupPermission = {
setting_name, setting_name,
can_edit: can_edit_settings, can_edit: can_edit_settings,
}; };
if (!can_edit_settings) { if (!can_edit_settings) {
if (!user_groups.group_has_permission(setting_value, group_id)) {
// The group does not have permission.
return undefined;
}
// Since user cannot change this setting, the tooltip is
// the same whether the group has direct permission or has
// permission due to being subgroup of a group with permission.
assigned_permission_object.tooltip_message = $t({ assigned_permission_object.tooltip_message = $t({
defaultMessage: "You are not allowed to remove this permission.", defaultMessage: "You are not allowed to remove this permission.",
}); });
return assigned_permission_object;
} }
// The user has permission to change the setting, but whether the user
// will be able to remove the permission for this particular group
// depends on whether the group has the permission directly or not.
if (typeof setting_value === "number") { if (typeof setting_value === "number") {
if (setting_value === group_id) { if (setting_value === group_id) {
// The group has permission directly, so the user can remove
// the permission for this particular group, and there is no
// need to show a tooltip.
return assigned_permission_object; return assigned_permission_object;
} }
if (user_groups.is_subgroup_of_target_group(setting_value, group_id)) { if (user_groups.is_subgroup_of_target_group(setting_value, group_id)) {
if (can_edit_settings) { // The group has permission because it is one of the subgroups of
// the group that has permission. Therefore, the user cannot remove
// the permission for this group, and the UI should show a disabled
// checkbox with an appropriate tooltip.
assigned_permission_object.can_edit = false; assigned_permission_object.can_edit = false;
const supergroup = user_groups.get_user_group_from_id(setting_value); assigned_permission_object.tooltip_message =
assigned_permission_object.tooltip_message = $t( get_tooltip_for_group_without_direct_permission(setting_value);
{
defaultMessage:
"This group has this permission because it's a subgroup of {supergroup_name}.",
},
{
supergroup_name: user_groups.get_display_group_name(supergroup.name),
},
);
}
return assigned_permission_object; return assigned_permission_object;
} }
// The group does not have permission.
return undefined; return undefined;
} }
// Setting is set to an anonymous group.
const direct_subgroup_ids = setting_value.direct_subgroups; const direct_subgroup_ids = setting_value.direct_subgroups;
if (direct_subgroup_ids.includes(group_id)) { if (direct_subgroup_ids.includes(group_id)) {
// The group is one of the groups that has permission and can be
// changed to not have permission.
return assigned_permission_object; return assigned_permission_object;
} }
for (const direct_subgroup_id of direct_subgroup_ids) { for (const direct_subgroup_id of direct_subgroup_ids) {
if (user_groups.is_subgroup_of_target_group(direct_subgroup_id, group_id)) { if (user_groups.is_subgroup_of_target_group(direct_subgroup_id, group_id)) {
if (can_edit_settings) { // The group has permission because it is a subgroup of one of the
// groups that has permission. Therefore, the user cannot remove the
// permission for this group.
assigned_permission_object.can_edit = false; assigned_permission_object.can_edit = false;
const supergroup = user_groups.get_user_group_from_id(direct_subgroup_id); assigned_permission_object.tooltip_message =
assigned_permission_object.tooltip_message = $t( get_tooltip_for_group_without_direct_permission(direct_subgroup_id);
{
defaultMessage:
"This group has this permission because it's a subgroup of {supergroup_name}.",
},
{
supergroup_name: user_groups.get_display_group_name(supergroup.name),
},
);
}
return assigned_permission_object; return assigned_permission_object;
} }
} }
// The group does not have permission.
return undefined; return undefined;
} }