2021-01-20 21:10:25 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useParams } from "react-router-dom";
|
2021-02-23 13:36:37 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Dropdown,
|
|
|
|
DropdownItem,
|
|
|
|
DropdownToggle,
|
|
|
|
Label,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2021-01-20 21:10:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2021-02-23 13:36:37 +00:00
|
|
|
import { CaretDownIcon, FilterIcon } from "@patternfly/react-icons";
|
|
|
|
import KeycloakAdminClient from "keycloak-admin";
|
|
|
|
|
|
|
|
type AliasRendererComponentProps = {
|
|
|
|
name?: string;
|
|
|
|
containerId?: string;
|
|
|
|
filterType: string;
|
|
|
|
adminClient: KeycloakAdminClient;
|
|
|
|
id: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AliasRendererComponent = ({
|
|
|
|
name,
|
|
|
|
containerId,
|
|
|
|
filterType,
|
|
|
|
adminClient,
|
|
|
|
id,
|
|
|
|
}: AliasRendererComponentProps) => {
|
|
|
|
const [containerName, setContainerName] = useState<string>("");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
adminClient.clients
|
|
|
|
.findOne({ id: containerId! })
|
|
|
|
.then((client) => setContainerName(client.clientId as string));
|
|
|
|
}, [containerId]);
|
|
|
|
|
|
|
|
if (filterType === "roles") {
|
|
|
|
return <>{name}</>;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filterType === "clients") {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{containerId && (
|
|
|
|
<Label color="blue" key={`label-${id}`}>
|
|
|
|
{containerName}
|
|
|
|
</Label>
|
|
|
|
)}{" "}
|
|
|
|
{name}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
2021-01-20 21:10:25 +00:00
|
|
|
|
|
|
|
export type AssociatedRolesModalProps = {
|
|
|
|
open: boolean;
|
|
|
|
toggleDialog: () => void;
|
2021-02-04 20:50:13 +00:00
|
|
|
onConfirm: (newReps: RoleRepresentation[]) => void;
|
|
|
|
existingCompositeRoles: RoleRepresentation[];
|
2021-01-20 21:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const attributesToArray = (attributes: { [key: string]: string }): any => {
|
|
|
|
if (!attributes || Object.keys(attributes).length == 0) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
key: "",
|
|
|
|
value: "",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return Object.keys(attributes).map((key) => ({
|
|
|
|
key: key,
|
|
|
|
value: attributes[key],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const AssociatedRolesModal = (props: AssociatedRolesModalProps) => {
|
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const form = useForm<RoleRepresentation>({ mode: "onChange" });
|
|
|
|
const [name, setName] = useState("");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const [selectedRows, setSelectedRows] = useState<RoleRepresentation[]>([]);
|
2021-02-23 13:36:37 +00:00
|
|
|
const [allClientRoles, setAllClientRoles] = useState<RoleRepresentation[]>(
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const [isFilterDropdownOpen, setIsFilterDropdownOpen] = useState(false);
|
|
|
|
const [filterType, setFilterType] = useState("roles");
|
|
|
|
const tableRefresher = React.useRef<() => void>();
|
2021-01-20 21:10:25 +00:00
|
|
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
|
|
|
|
2021-02-23 13:36:37 +00:00
|
|
|
const alphabetize = (rolesList: RoleRepresentation[]) => {
|
|
|
|
return rolesList.sort((r1, r2) => {
|
|
|
|
const r1Name = r1.name?.toUpperCase();
|
|
|
|
const r2Name = r2.name?.toUpperCase();
|
|
|
|
if (r1Name! < r2Name!) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r1Name! > r2Name!) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-20 21:10:25 +00:00
|
|
|
const loader = async () => {
|
|
|
|
const allRoles = await adminClient.roles.find();
|
2021-02-04 20:50:13 +00:00
|
|
|
const existingAdditionalRoles = await adminClient.roles.getCompositeRoles({
|
|
|
|
id,
|
|
|
|
});
|
2021-01-25 15:52:53 +00:00
|
|
|
|
2021-02-23 13:36:37 +00:00
|
|
|
return alphabetize(allRoles).filter((role: RoleRepresentation) => {
|
2021-02-04 20:50:13 +00:00
|
|
|
return (
|
|
|
|
existingAdditionalRoles.find(
|
|
|
|
(existing: RoleRepresentation) => existing.name === role.name
|
|
|
|
) === undefined && role.name !== name
|
|
|
|
);
|
|
|
|
});
|
2021-01-25 15:52:53 +00:00
|
|
|
};
|
2021-01-20 21:10:25 +00:00
|
|
|
|
2021-02-23 13:36:37 +00:00
|
|
|
const AliasRenderer = (role: RoleRepresentation) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AliasRendererComponent
|
|
|
|
id={id}
|
|
|
|
name={role.name}
|
|
|
|
adminClient={adminClient}
|
|
|
|
filterType={filterType}
|
|
|
|
containerId={role.containerId}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const clientRolesLoader = async () => {
|
|
|
|
const clients = await adminClient.clients.find();
|
|
|
|
|
|
|
|
const clientIdArray = clients.map((client) => client.id);
|
|
|
|
|
|
|
|
let rolesList: RoleRepresentation[] = [];
|
|
|
|
for (const id of clientIdArray) {
|
|
|
|
const clientRolesList = await adminClient.clients.listRoles({
|
|
|
|
id: id as string,
|
|
|
|
});
|
|
|
|
rolesList = [...rolesList, ...clientRolesList];
|
|
|
|
}
|
|
|
|
const existingAdditionalRoles = await adminClient.roles.getCompositeRoles({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
|
|
|
setAllClientRoles(rolesList);
|
|
|
|
console.log(allClientRoles);
|
|
|
|
|
|
|
|
return alphabetize(rolesList).filter((role: RoleRepresentation) => {
|
|
|
|
return (
|
|
|
|
existingAdditionalRoles.find(
|
|
|
|
(existing: RoleRepresentation) => existing.name === role.name
|
|
|
|
) === undefined && role.name !== name
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
tableRefresher.current && tableRefresher.current();
|
|
|
|
}, [filterType]);
|
|
|
|
|
2021-01-20 21:10:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
if (id) {
|
|
|
|
const fetchedRole = await adminClient.roles.findOneById({ id });
|
|
|
|
setName(fetchedRole.name!);
|
|
|
|
setupForm(fetchedRole);
|
|
|
|
} else {
|
|
|
|
setName(t("createRole"));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setupForm = (role: RoleRepresentation) => {
|
|
|
|
Object.entries(role).map((entry) => {
|
|
|
|
if (entry[0] === "attributes") {
|
|
|
|
form.setValue(entry[0], attributesToArray(entry[1]));
|
|
|
|
} else {
|
|
|
|
form.setValue(entry[0], entry[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-23 13:36:37 +00:00
|
|
|
const onFilterDropdownToggle = () => {
|
|
|
|
setIsFilterDropdownOpen(!isFilterDropdownOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onFilterDropdownSelect = (filterType: string) => {
|
|
|
|
if (filterType == "roles") {
|
|
|
|
setFilterType("clients");
|
|
|
|
}
|
|
|
|
if (filterType == "clients") {
|
|
|
|
setFilterType("roles");
|
|
|
|
}
|
|
|
|
setIsFilterDropdownOpen(!isFilterDropdownOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setRefresher = (refresher: () => void) => {
|
|
|
|
tableRefresher.current = refresher;
|
|
|
|
};
|
|
|
|
|
2021-01-20 21:10:25 +00:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title={t("roles:associatedRolesModalTitle", { name })}
|
|
|
|
isOpen={props.open}
|
|
|
|
onClose={props.toggleDialog}
|
|
|
|
variant={ModalVariant.large}
|
|
|
|
actions={[
|
|
|
|
<Button
|
2021-01-27 20:22:26 +00:00
|
|
|
key="add"
|
2021-02-04 20:50:13 +00:00
|
|
|
id="add-associated-roles-button"
|
2021-01-20 21:10:25 +00:00
|
|
|
variant="primary"
|
2021-02-04 20:50:13 +00:00
|
|
|
isDisabled={!selectedRows?.length}
|
2021-01-20 21:10:25 +00:00
|
|
|
onClick={() => {
|
|
|
|
props.toggleDialog();
|
2021-02-04 20:50:13 +00:00
|
|
|
props.onConfirm(selectedRows);
|
2021-01-20 21:10:25 +00:00
|
|
|
}}
|
|
|
|
>
|
2021-01-27 20:22:26 +00:00
|
|
|
{t("common:add")}
|
2021-01-20 21:10:25 +00:00
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
key="cancel"
|
|
|
|
variant="link"
|
|
|
|
onClick={() => {
|
|
|
|
props.toggleDialog();
|
|
|
|
}}
|
|
|
|
>
|
2021-01-27 20:22:26 +00:00
|
|
|
{t("common:cancel")}
|
2021-01-20 21:10:25 +00:00
|
|
|
</Button>,
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<KeycloakDataTable
|
|
|
|
key="role-list-modal"
|
2021-02-23 13:36:37 +00:00
|
|
|
loader={filterType == "roles" ? loader : clientRolesLoader}
|
2021-01-20 21:10:25 +00:00
|
|
|
ariaLabelKey="roles:roleList"
|
|
|
|
searchPlaceholderKey="roles:searchFor"
|
2021-02-23 13:36:37 +00:00
|
|
|
setRefresher={setRefresher}
|
|
|
|
searchTypeComponent={
|
|
|
|
<Dropdown
|
|
|
|
onSelect={() => onFilterDropdownSelect(filterType)}
|
|
|
|
data-cy="filter-type-dropdown"
|
|
|
|
toggle={
|
|
|
|
<DropdownToggle
|
|
|
|
id="toggle-id-9"
|
|
|
|
onToggle={onFilterDropdownToggle}
|
|
|
|
toggleIndicator={CaretDownIcon}
|
|
|
|
icon={<FilterIcon />}
|
|
|
|
>
|
|
|
|
Filter by {filterType}
|
|
|
|
</DropdownToggle>
|
|
|
|
}
|
|
|
|
isOpen={isFilterDropdownOpen}
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
data-cy="filter-type-dropdown-item"
|
|
|
|
key="filter-type"
|
|
|
|
>
|
|
|
|
{filterType == "roles"
|
|
|
|
? t("filterByClients")
|
|
|
|
: t("filterByRoles")}{" "}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
2021-01-20 21:10:25 +00:00
|
|
|
canSelectAll
|
|
|
|
// isPaginated
|
|
|
|
onSelect={(rows) => {
|
|
|
|
setSelectedRows([...rows]);
|
|
|
|
}}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
displayKey: "roles:roleName",
|
2021-02-23 13:36:37 +00:00
|
|
|
cellRenderer: AliasRenderer,
|
2021-01-20 21:10:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2021-02-23 13:36:37 +00:00
|
|
|
displayKey: "common:description",
|
2021-01-20 21:10:25 +00:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noRolesInThisRealm")}
|
|
|
|
instructions={t("noRolesInThisRealmInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
|
|
|
// onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|