2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2022-08-22 15:51:19 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { Link, useNavigate } from "react-router-dom-v5-compat";
|
2021-12-01 07:58:25 +00:00
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
Alert,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Switch,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
|
|
|
import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation";
|
|
|
|
import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation";
|
|
|
|
import { ResourceDetailsParams, toResourceDetails } from "../routes/Resource";
|
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { ViewHeader } from "../../components/view-header/ViewHeader";
|
|
|
|
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
|
|
|
import { FormAccess } from "../../components/form-access/FormAccess";
|
2022-04-20 17:11:46 +00:00
|
|
|
import type { KeyValueType } from "../../components/key-value-form/key-value-convert";
|
2021-12-08 15:08:42 +00:00
|
|
|
import { convertFormValuesToObject, convertToFormValues } from "../../util";
|
|
|
|
import { MultiLineInput } from "../../components/multi-line-input/MultiLineInput";
|
2022-01-31 07:20:35 +00:00
|
|
|
import { toAuthorizationTab } from "../routes/AuthenticationTab";
|
2021-12-01 07:58:25 +00:00
|
|
|
import { ScopePicker } from "./ScopePicker";
|
2022-04-20 17:11:46 +00:00
|
|
|
import { KeyValueInput } from "../../components/key-value-form/KeyValueInput";
|
2022-04-21 15:03:26 +00:00
|
|
|
import { KeycloakTextInput } from "../../components/keycloak-text-input/KeycloakTextInput";
|
2021-12-01 07:58:25 +00:00
|
|
|
|
|
|
|
import "./resource-details.css";
|
|
|
|
|
2022-03-09 16:42:23 +00:00
|
|
|
type SubmittedResource = Omit<ResourceRepresentation, "attributes"> & {
|
2021-12-01 07:58:25 +00:00
|
|
|
attributes: KeyValueType[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function ResourceDetails() {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const [client, setClient] = useState<ClientRepresentation>();
|
|
|
|
const [resource, setResource] = useState<ResourceRepresentation>();
|
|
|
|
|
|
|
|
const [permissions, setPermission] =
|
|
|
|
useState<ResourceServerRepresentation[]>();
|
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-12-01 07:58:25 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const form = useForm<SubmittedResource>({
|
|
|
|
shouldUnregister: false,
|
|
|
|
mode: "onChange",
|
|
|
|
});
|
|
|
|
const { register, errors, control, setValue, handleSubmit } = form;
|
|
|
|
|
|
|
|
const { id, resourceId, realm } = useParams<ResourceDetailsParams>();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-12-01 07:58:25 +00:00
|
|
|
|
|
|
|
const setupForm = (resource: ResourceRepresentation = {}) => {
|
2022-03-09 16:42:23 +00:00
|
|
|
convertToFormValues(resource, setValue);
|
2021-12-01 07:58:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useFetch(
|
2022-01-04 09:17:43 +00:00
|
|
|
() =>
|
|
|
|
Promise.all([
|
2021-12-01 07:58:25 +00:00
|
|
|
adminClient.clients.findOne({ id }),
|
|
|
|
resourceId
|
|
|
|
? adminClient.clients.getResource({ id, resourceId })
|
|
|
|
: Promise.resolve(undefined),
|
|
|
|
resourceId
|
|
|
|
? adminClient.clients.listPermissionsByResource({ id, resourceId })
|
|
|
|
: Promise.resolve(undefined),
|
2022-01-04 09:17:43 +00:00
|
|
|
]),
|
|
|
|
([client, resource, permissions]) => {
|
2021-12-01 07:58:25 +00:00
|
|
|
if (!client) {
|
|
|
|
throw new Error(t("common:notFound"));
|
|
|
|
}
|
|
|
|
setClient(client);
|
|
|
|
setPermission(permissions);
|
|
|
|
setResource(resource);
|
|
|
|
setupForm(resource);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const save = async (submitted: SubmittedResource) => {
|
2022-02-21 15:43:23 +00:00
|
|
|
const resource = convertFormValuesToObject<
|
|
|
|
SubmittedResource,
|
|
|
|
ResourceRepresentation
|
2022-03-09 16:42:23 +00:00
|
|
|
>(submitted);
|
2021-12-01 07:58:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (resourceId) {
|
|
|
|
await adminClient.clients.updateResource({ id, resourceId }, resource);
|
|
|
|
} else {
|
|
|
|
const result = await adminClient.clients.createResource(
|
|
|
|
{ id },
|
|
|
|
resource
|
|
|
|
);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toResourceDetails({ realm, id, resourceId: result._id! }));
|
2021-12-01 07:58:25 +00:00
|
|
|
}
|
|
|
|
addAlert(
|
|
|
|
t((resourceId ? "update" : "create") + "ResourceSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2021-12-16 16:31:17 +00:00
|
|
|
addError("clients:resourceSaveError", error);
|
2021-12-01 07:58:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "clients:deleteResource",
|
|
|
|
children: (
|
|
|
|
<>
|
|
|
|
{t("deleteResourceConfirm")}
|
|
|
|
{permissions?.length !== 0 && (
|
|
|
|
<Alert
|
|
|
|
variant="warning"
|
|
|
|
isInline
|
|
|
|
isPlain
|
|
|
|
title={t("deleteResourceWarning")}
|
|
|
|
className="pf-u-pt-lg"
|
|
|
|
>
|
|
|
|
<p className="pf-u-pt-xs">
|
|
|
|
{permissions?.map((permission) => (
|
|
|
|
<strong key={permission.id} className="pf-u-pr-md">
|
|
|
|
{permission.name}
|
|
|
|
</strong>
|
|
|
|
))}
|
|
|
|
</p>
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
continueButtonLabel: "clients:confirm",
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.delResource({
|
|
|
|
id,
|
|
|
|
resourceId: resourceId!,
|
|
|
|
});
|
|
|
|
addAlert(t("resourceDeletedSuccess"), AlertVariant.success);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toAuthorizationTab({ realm, clientId: id, tab: "resources" }));
|
2021-12-01 07:58:25 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError("clients:resourceDeletedError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
return <KeycloakSpinner />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={resourceId ? resource?.name! : "clients:createResource"}
|
|
|
|
dropdownItems={
|
|
|
|
resourceId
|
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
data-testid="delete-resource"
|
|
|
|
onClick={() => toggleDeleteDialog()}
|
|
|
|
>
|
|
|
|
{t("common:delete")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
|
|
|
<FormProvider {...form}>
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
2022-05-30 09:23:24 +00:00
|
|
|
role="view-clients"
|
2021-12-01 07:58:25 +00:00
|
|
|
className="keycloak__resource-details__form"
|
|
|
|
onSubmit={handleSubmit(save)}
|
|
|
|
>
|
|
|
|
<FormGroup
|
|
|
|
label={t("owner")}
|
|
|
|
fieldId="owner"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:owner"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:owner"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
|
|
|
id="owner"
|
|
|
|
value={client.clientId}
|
|
|
|
isReadOnly
|
|
|
|
/>
|
2021-12-01 07:58:25 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("common:name")}
|
|
|
|
fieldId="name"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:resourceName"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="name"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput
|
2021-12-01 07:58:25 +00:00
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
validated={
|
|
|
|
errors.name
|
|
|
|
? ValidatedOptions.error
|
|
|
|
: ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("displayName")}
|
|
|
|
fieldId="displayName"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:displayName"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="name"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput id="displayName" name="name" ref={register} />
|
2021-12-01 07:58:25 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("type")}
|
|
|
|
fieldId="type"
|
|
|
|
labelIcon={
|
2021-12-14 14:56:36 +00:00
|
|
|
<HelpItem helpText="clients-help:type" fieldLabelId="type" />
|
2021-12-01 07:58:25 +00:00
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput id="type" name="type" ref={register} />
|
2021-12-01 07:58:25 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
label={t("uris")}
|
|
|
|
fieldId="uris"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:uris"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:uris"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<MultiLineInput
|
|
|
|
name="uris"
|
2022-03-16 13:57:50 +00:00
|
|
|
aria-label={t("uris")}
|
2021-12-01 07:58:25 +00:00
|
|
|
addButtonLabel="clients:addUri"
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<ScopePicker clientId={id} />
|
|
|
|
<FormGroup
|
|
|
|
label={t("iconUri")}
|
|
|
|
fieldId="iconUri"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:iconUri"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:iconUri"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2022-04-21 15:03:26 +00:00
|
|
|
<KeycloakTextInput id="iconUri" name="icon_uri" ref={register} />
|
2021-12-01 07:58:25 +00:00
|
|
|
</FormGroup>
|
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t("ownerManagedAccess")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:ownerManagedAccess"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:ownerManagedAccess"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="ownerManagedAccess"
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="ownerManagedAccess"
|
|
|
|
control={control}
|
|
|
|
defaultValue={false}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="ownerManagedAccess"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={value}
|
|
|
|
onChange={onChange}
|
2022-08-30 13:07:51 +00:00
|
|
|
aria-label={t("ownerManagedAccess")}
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t("resourceAttribute")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:resourceAttribute"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:resourceAttribute"
|
2021-12-01 07:58:25 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="resourceAttribute"
|
|
|
|
>
|
2022-04-20 17:11:46 +00:00
|
|
|
<KeyValueInput name="attributes" />
|
2021-12-01 07:58:25 +00:00
|
|
|
</FormGroup>
|
|
|
|
<ActionGroup>
|
|
|
|
<div className="pf-u-mt-md">
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.primary}
|
|
|
|
type="submit"
|
|
|
|
data-testid="save"
|
|
|
|
>
|
|
|
|
{t("common:save")}
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
data-testid="cancel"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
2022-01-31 07:20:35 +00:00
|
|
|
to={toAuthorizationTab({
|
2021-12-01 07:58:25 +00:00
|
|
|
realm,
|
|
|
|
clientId: id,
|
2022-01-31 07:20:35 +00:00
|
|
|
tab: "resources",
|
2021-12-01 07:58:25 +00:00
|
|
|
})}
|
|
|
|
></Link>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormProvider>
|
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|