2021-02-24 07:58:50 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-02-17 07:17:04 +00:00
|
|
|
import { useHistory, useParams, useRouteMatch } from "react-router-dom";
|
2021-02-04 20:50:13 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
2021-02-17 07:17:04 +00:00
|
|
|
Checkbox,
|
2021-02-04 20:50:13 +00:00
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { formattedLinkTableCell } from "../components/external-link/FormattedLink";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-02-25 09:10:28 +00:00
|
|
|
import { emptyFormatter } from "../util";
|
2021-02-17 07:17:04 +00:00
|
|
|
import { AssociatedRolesModal } from "./AssociatedRolesModal";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { RoleFormType } from "./RealmRoleTabs";
|
2021-02-25 09:10:28 +00:00
|
|
|
import ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
import { AliasRendererComponent } from "./AliasRendererComponent";
|
2021-02-04 20:50:13 +00:00
|
|
|
|
|
|
|
type AssociatedRolesTabProps = {
|
|
|
|
additionalRoles: RoleRepresentation[];
|
2021-02-17 07:17:04 +00:00
|
|
|
addComposites: (newReps: RoleRepresentation[]) => void;
|
|
|
|
parentRole: RoleFormType;
|
|
|
|
onRemove: (newReps: RoleRepresentation[]) => void;
|
2021-02-25 09:10:28 +00:00
|
|
|
client?: ClientRepresentation;
|
2021-02-04 20:50:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const AssociatedRolesTab = ({
|
|
|
|
additionalRoles,
|
2021-02-17 07:17:04 +00:00
|
|
|
addComposites,
|
|
|
|
parentRole,
|
|
|
|
onRemove,
|
2021-02-04 20:50:13 +00:00
|
|
|
}: AssociatedRolesTabProps) => {
|
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const history = useHistory();
|
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
const { url } = useRouteMatch();
|
2021-02-24 07:58:50 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
2021-02-04 20:50:13 +00:00
|
|
|
|
2021-02-17 07:17:04 +00:00
|
|
|
const [selectedRows, setSelectedRows] = useState<RoleRepresentation[]>([]);
|
2021-02-25 09:10:28 +00:00
|
|
|
const [isInheritedHidden, setIsInheritedHidden] = useState(false);
|
|
|
|
|
2021-02-17 07:17:04 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
2021-02-25 09:10:28 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
|
|
|
const inheritanceMap = React.useRef<{ [key: string]: string }>({});
|
|
|
|
|
|
|
|
const getSubRoles = async (
|
|
|
|
role: RoleRepresentation,
|
|
|
|
allRoles: RoleRepresentation[]
|
|
|
|
): Promise<RoleRepresentation[]> => {
|
|
|
|
// Fetch all composite roles
|
|
|
|
const allCompositeRoles = await adminClient.roles.getCompositeRoles({
|
|
|
|
id: role.id!,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Need to ensure we don't get into an infinite loop, do not add any role that is already there or the starting role
|
|
|
|
const newRoles: Promise<RoleRepresentation[]> = allCompositeRoles.reduce(
|
|
|
|
async (acc: Promise<RoleRepresentation[]>, newRole) => {
|
|
|
|
const resolvedRoles = await acc;
|
|
|
|
if (!allRoles.find((ar) => ar.id === newRole.id)) {
|
|
|
|
inheritanceMap.current[newRole.id!] = role.name!;
|
|
|
|
resolvedRoles.push(newRole);
|
|
|
|
const subRoles = await getSubRoles(newRole, [
|
|
|
|
...allRoles,
|
|
|
|
...resolvedRoles,
|
|
|
|
]);
|
|
|
|
resolvedRoles.push(...subRoles);
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
Promise.resolve([] as RoleRepresentation[])
|
|
|
|
);
|
|
|
|
|
|
|
|
return newRoles;
|
|
|
|
};
|
2021-02-04 20:50:13 +00:00
|
|
|
|
|
|
|
const loader = async () => {
|
2021-02-25 09:10:28 +00:00
|
|
|
if (isInheritedHidden) {
|
|
|
|
return additionalRoles;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allRoles: Promise<RoleRepresentation[]> = additionalRoles.reduce(
|
|
|
|
async (acc: Promise<RoleRepresentation[]>, role) => {
|
|
|
|
const resolvedRoles = await acc;
|
|
|
|
resolvedRoles.push(role);
|
|
|
|
const subRoles = await getSubRoles(role, resolvedRoles);
|
|
|
|
resolvedRoles.push(...subRoles);
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
Promise.resolve([] as RoleRepresentation[])
|
|
|
|
);
|
|
|
|
|
|
|
|
return allRoles;
|
2021-02-04 20:50:13 +00:00
|
|
|
};
|
|
|
|
|
2021-02-24 07:58:50 +00:00
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
2021-02-25 09:10:28 +00:00
|
|
|
}, [additionalRoles, isInheritedHidden]);
|
2021-02-04 20:50:13 +00:00
|
|
|
|
2021-02-25 09:10:28 +00:00
|
|
|
const InheritedRoleName = (role: RoleRepresentation) => {
|
|
|
|
return <>{inheritanceMap.current[role.id!]}</>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AliasRenderer = (role: RoleRepresentation) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AliasRendererComponent
|
|
|
|
id={id}
|
|
|
|
name={role.name}
|
|
|
|
adminClient={adminClient}
|
|
|
|
containerId={role.containerId}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log(inheritanceMap);
|
2021-02-04 20:50:13 +00:00
|
|
|
|
2021-02-17 07:17:04 +00:00
|
|
|
const toggleModal = () => setOpen(!open);
|
2021-02-04 20:50:13 +00:00
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "roles:roleRemoveAssociatedRoleConfirm",
|
2021-02-17 07:17:04 +00:00
|
|
|
messageKey: t("roles:roleRemoveAssociatedText"),
|
2021-02-04 20:50:13 +00:00
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2021-02-17 07:17:04 +00:00
|
|
|
await adminClient.roles.delCompositeRoles({ id }, selectedRows);
|
|
|
|
setSelectedRows([]);
|
2021-02-04 20:50:13 +00:00
|
|
|
|
2021-02-17 07:17:04 +00:00
|
|
|
addAlert(t("associatedRolesRemoved"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("roleDeleteError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [
|
|
|
|
toggleDeleteAssociatedRolesDialog,
|
|
|
|
DeleteAssociatedRolesConfirm,
|
|
|
|
] = useConfirmDialog({
|
|
|
|
titleKey: t("roles:removeAssociatedRoles") + "?",
|
|
|
|
messageKey: t("roles:removeAllAssociatedRolesConfirmDialog", {
|
|
|
|
name: parentRole?.name || t("createRole"),
|
|
|
|
}),
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
if (selectedRows.length === additionalRoles.length) {
|
|
|
|
onRemove(selectedRows);
|
|
|
|
const loc = url.replace(/\/AssociatedRoles/g, "/details");
|
|
|
|
history.push(loc);
|
|
|
|
}
|
|
|
|
onRemove(selectedRows);
|
|
|
|
await adminClient.roles.delCompositeRoles({ id }, selectedRows);
|
|
|
|
addAlert(t("associatedRolesRemoved"), AlertVariant.success);
|
2021-02-04 20:50:13 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(`${t("roleDeleteError")} ${error}`, AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const goToCreate = () => history.push(`${url}/add-role`);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<DeleteConfirm />
|
2021-02-17 07:17:04 +00:00
|
|
|
<DeleteAssociatedRolesConfirm />
|
|
|
|
<AssociatedRolesModal
|
|
|
|
onConfirm={addComposites}
|
|
|
|
existingCompositeRoles={additionalRoles}
|
|
|
|
open={open}
|
|
|
|
toggleDialog={() => setOpen(!open)}
|
|
|
|
/>
|
2021-02-04 20:50:13 +00:00
|
|
|
<KeycloakDataTable
|
2021-02-24 07:58:50 +00:00
|
|
|
key={key}
|
2021-02-04 20:50:13 +00:00
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="roles:roleList"
|
|
|
|
searchPlaceholderKey="roles:searchFor"
|
2021-02-17 07:17:04 +00:00
|
|
|
canSelectAll
|
|
|
|
onSelect={(rows) => {
|
|
|
|
setSelectedRows([...rows]);
|
|
|
|
}}
|
2021-02-04 20:50:13 +00:00
|
|
|
toolbarItem={
|
|
|
|
<>
|
2021-02-17 07:17:04 +00:00
|
|
|
<Checkbox
|
|
|
|
label="Hide inherited roles"
|
|
|
|
key="associated-roles-check"
|
|
|
|
id="kc-hide-inherited-roles-checkbox"
|
2021-02-25 09:10:28 +00:00
|
|
|
onChange={() => setIsInheritedHidden(!isInheritedHidden)}
|
|
|
|
isChecked={isInheritedHidden}
|
2021-02-17 07:17:04 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
className="kc-add-role-button"
|
|
|
|
key="add-role-button"
|
|
|
|
onClick={() => toggleModal()}
|
2021-03-01 15:22:25 +00:00
|
|
|
data-testid="add-role-button"
|
2021-02-17 07:17:04 +00:00
|
|
|
>
|
|
|
|
{t("addRole")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
isDisabled={selectedRows.length == 0}
|
|
|
|
key="remove-role-button"
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteAssociatedRolesDialog();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("removeRoles")}
|
|
|
|
</Button>
|
2021-02-04 20:50:13 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:remove"),
|
|
|
|
onRowClick: (role) => {
|
2021-02-17 07:17:04 +00:00
|
|
|
setSelectedRows([role]);
|
2021-02-04 20:50:13 +00:00
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
displayKey: "roles:roleName",
|
2021-02-25 09:10:28 +00:00
|
|
|
cellRenderer: AliasRenderer,
|
2021-02-04 20:50:13 +00:00
|
|
|
cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
2021-02-25 09:10:28 +00:00
|
|
|
name: "containerId",
|
2021-02-17 07:17:04 +00:00
|
|
|
displayKey: "roles:inheritedFrom",
|
2021-02-25 09:10:28 +00:00
|
|
|
cellRenderer: InheritedRoleName,
|
|
|
|
cellFormatters: [emptyFormatter()],
|
2021-02-04 20:50:13 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: "common:description",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noRolesInThisRealm")}
|
|
|
|
instructions={t("noRolesInThisRealmInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|