2020-12-07 19:23:18 +00:00
|
|
|
import React, { useState } from "react";
|
2021-01-05 19:49:33 +00:00
|
|
|
import { Link, useHistory, useRouteMatch } from "react-router-dom";
|
2020-09-18 08:04:55 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-12-07 19:23:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { IFormatter, IFormatterValueType } from "@patternfly/react-table";
|
2020-09-18 08:04:55 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
|
2020-10-28 18:33:54 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2020-12-11 10:28:38 +00:00
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2021-01-11 19:29:52 +00:00
|
|
|
import { formattedLinkTableCell } from "../components/external-link/FormattedLink";
|
2020-12-07 19:23:18 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-01-15 01:44:16 +00:00
|
|
|
import { emptyFormatter, toUpperCase } from "../util";
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const RealmRolesSection = () => {
|
2020-09-18 08:04:55 +00:00
|
|
|
const { t } = useTranslation("roles");
|
|
|
|
const history = useHistory();
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-12-07 19:23:18 +00:00
|
|
|
const { addAlert } = useAlerts();
|
2021-01-05 19:49:33 +00:00
|
|
|
const { url } = useRouteMatch();
|
2020-10-14 20:50:10 +00:00
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const [selectedRole, setSelectedRole] = useState<RoleRepresentation>();
|
2020-09-18 08:04:55 +00:00
|
|
|
|
2021-01-15 01:44:16 +00:00
|
|
|
const loader = async (to?: number, max?: number, search?: string) => {
|
2020-12-07 19:23:18 +00:00
|
|
|
const params: { [name: string]: string | number } = {
|
2021-01-15 01:44:16 +00:00
|
|
|
to: to!,
|
2020-12-07 19:23:18 +00:00
|
|
|
max: max!,
|
|
|
|
search: search!,
|
|
|
|
};
|
|
|
|
return await adminClient.roles.find(params);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RoleDetailLink = (role: RoleRepresentation) => (
|
|
|
|
<>
|
2021-01-05 19:49:33 +00:00
|
|
|
<Link key={role.id} to={`${url}/${role.id}`}>
|
2020-12-07 19:23:18 +00:00
|
|
|
{role.name}
|
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
const boolFormatter = (): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
const boolVal = data?.toString();
|
|
|
|
|
2021-01-15 01:44:16 +00:00
|
|
|
return (boolVal ? toUpperCase(boolVal) : undefined) as string;
|
2020-12-07 19:23:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "roles:roleDeleteConfirm",
|
|
|
|
messageKey: t("roles:roleDeleteConfirmDialog", {
|
|
|
|
selectedRoleName: selectedRole ? selectedRole!.name : "",
|
|
|
|
}),
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.roles.delById({
|
|
|
|
id: selectedRole!.id!,
|
|
|
|
});
|
|
|
|
setSelectedRole(undefined);
|
|
|
|
addAlert(t("roleDeletedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(`${t("roleDeleteError")} ${error}`, AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-10-14 20:50:10 +00:00
|
|
|
|
2021-01-05 19:49:33 +00:00
|
|
|
const goToCreate = () => history.push(`${url}/add-role`);
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
2020-10-07 15:47:03 +00:00
|
|
|
<>
|
|
|
|
<ViewHeader titleKey="roles:title" subKey="roles:roleExplain" />
|
2020-10-14 20:50:10 +00:00
|
|
|
<PageSection variant="light">
|
2020-12-07 19:23:18 +00:00
|
|
|
<DeleteConfirm />
|
2020-12-11 10:18:29 +00:00
|
|
|
<KeycloakDataTable
|
2020-12-07 19:23:18 +00:00
|
|
|
key={selectedRole ? selectedRole.id : "roleList"}
|
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="roles:roleList"
|
|
|
|
searchPlaceholderKey="roles:searchFor"
|
|
|
|
isPaginated
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
|
|
|
<Button onClick={goToCreate}>{t("createRole")}</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
2021-01-08 18:04:58 +00:00
|
|
|
title: t("addAssociatedRolesText"),
|
2020-12-07 19:23:18 +00:00
|
|
|
onRowClick: (role) => {
|
|
|
|
setSelectedRole(role);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
2021-01-25 15:52:53 +00:00
|
|
|
},
|
2020-12-07 19:23:18 +00:00
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
displayKey: "roles:roleName",
|
|
|
|
cellRenderer: RoleDetailLink,
|
2021-01-11 19:29:52 +00:00
|
|
|
cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
|
2020-12-07 19:23:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "composite",
|
|
|
|
displayKey: "roles:composite",
|
|
|
|
cellFormatters: [boolFormatter(), emptyFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
2021-01-13 20:48:16 +00:00
|
|
|
displayKey: "common:description",
|
2020-12-07 19:23:18 +00:00
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
message={t("noRolesInThisRealm")}
|
|
|
|
instructions={t("noRolesInThisRealmInstructions")}
|
|
|
|
primaryActionText={t("createRole")}
|
|
|
|
onPrimaryAction={goToCreate}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2020-10-07 15:47:03 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-18 08:04:55 +00:00
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|