2021-11-17 08:27:56 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2022-05-04 17:51:17 +00:00
|
|
|
import { Controller, FormProvider, useForm } from "react-hook-form";
|
2021-11-17 08:27:56 +00:00
|
|
|
import {
|
2021-12-16 16:31:17 +00:00
|
|
|
AlertVariant,
|
2021-11-17 08:27:56 +00:00
|
|
|
Button,
|
|
|
|
Divider,
|
|
|
|
FormGroup,
|
|
|
|
PageSection,
|
|
|
|
Radio,
|
|
|
|
Switch,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
|
|
|
|
import type ResourceServerRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceServerRepresentation";
|
2021-12-01 07:58:25 +00:00
|
|
|
import { KeycloakSpinner } from "../../components/keycloak-spinner/KeycloakSpinner";
|
2021-11-17 08:27:56 +00:00
|
|
|
import { useAdminClient, useFetch } from "../../context/auth/AdminClient";
|
|
|
|
import { FormAccess } from "../../components/form-access/FormAccess";
|
|
|
|
import { HelpItem } from "../../components/help-enabler/HelpItem";
|
|
|
|
import { SaveReset } from "../advanced/SaveReset";
|
2021-12-01 11:05:24 +00:00
|
|
|
import { ImportDialog } from "./ImportDialog";
|
|
|
|
import useToggle from "../../utils/useToggle";
|
2021-12-16 16:31:17 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2022-01-21 14:10:36 +00:00
|
|
|
import { DecisionStrategySelect } from "./DecisionStragegySelect";
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
const POLICY_ENFORCEMENT_MODES = [
|
|
|
|
"ENFORCING",
|
|
|
|
"PERMISSIVE",
|
|
|
|
"DISABLED",
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
export const AuthorizationSettings = ({ clientId }: { clientId: string }) => {
|
|
|
|
const { t } = useTranslation("clients");
|
|
|
|
const [resource, setResource] = useState<ResourceServerRepresentation>();
|
2021-12-01 11:05:24 +00:00
|
|
|
const [importDialog, toggleImportDialog] = useToggle();
|
|
|
|
|
2022-05-04 17:51:17 +00:00
|
|
|
const form = useForm<ResourceServerRepresentation>({});
|
|
|
|
const { control, reset, handleSubmit } = form;
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
2021-12-16 16:31:17 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-11-17 08:27:56 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
() => adminClient.clients.getResourceServer({ id: clientId }),
|
|
|
|
(resource) => {
|
|
|
|
setResource(resource);
|
|
|
|
reset(resource);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2021-12-16 16:31:17 +00:00
|
|
|
const importResource = async (value: ResourceServerRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.importResource({ id: clientId }, value);
|
|
|
|
addAlert(t("importResourceSuccess"), AlertVariant.success);
|
|
|
|
reset({ ...value });
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:importResourceError", error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const save = async (resource: ResourceServerRepresentation) => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.updateResourceServer(
|
|
|
|
{ id: clientId },
|
|
|
|
resource
|
|
|
|
);
|
|
|
|
addAlert(t("updateResourceSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError("clients:resourceSaveError", error);
|
|
|
|
}
|
2021-12-01 11:05:24 +00:00
|
|
|
};
|
|
|
|
|
2021-11-17 08:27:56 +00:00
|
|
|
if (!resource) {
|
2021-12-01 07:58:25 +00:00
|
|
|
return <KeycloakSpinner />;
|
2021-11-17 08:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageSection variant="light">
|
2021-12-01 11:05:24 +00:00
|
|
|
{importDialog && (
|
|
|
|
<ImportDialog
|
|
|
|
onConfirm={importResource}
|
|
|
|
closeDialog={toggleImportDialog}
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-17 08:27:56 +00:00
|
|
|
<FormAccess role="manage-clients" isHorizontal>
|
|
|
|
<FormGroup
|
|
|
|
label={t("import")}
|
|
|
|
fieldId="import"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:import"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:import"
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
2021-12-01 11:05:24 +00:00
|
|
|
<Button variant="secondary" onClick={toggleImportDialog}>
|
|
|
|
{t("import")}
|
|
|
|
</Button>
|
2021-11-17 08:27:56 +00:00
|
|
|
</FormGroup>
|
|
|
|
<Divider />
|
|
|
|
<FormGroup
|
|
|
|
label={t("policyEnforcementMode")}
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
|
|
|
helpText="clients-help:policyEnforcementMode"
|
2021-12-14 14:56:36 +00:00
|
|
|
fieldLabelId="clients:policyEnforcementMode"
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
fieldId="policyEnforcementMode"
|
|
|
|
hasNoPaddingTop
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="policyEnforcementMode"
|
|
|
|
data-testid="policyEnforcementMode"
|
2022-01-12 16:01:54 +00:00
|
|
|
defaultValue={POLICY_ENFORCEMENT_MODES[0]}
|
2021-11-17 08:27:56 +00:00
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<>
|
|
|
|
{POLICY_ENFORCEMENT_MODES.map((mode) => (
|
|
|
|
<Radio
|
|
|
|
id={mode}
|
|
|
|
key={mode}
|
|
|
|
data-testid={mode}
|
|
|
|
isChecked={value === mode}
|
|
|
|
name="policyEnforcementMode"
|
|
|
|
onChange={() => onChange(mode)}
|
|
|
|
label={t(`policyEnforcementModes.${mode}`)}
|
|
|
|
className="pf-u-mb-md"
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
2022-05-04 17:51:17 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<DecisionStrategySelect isLimited />
|
|
|
|
</FormProvider>
|
2021-11-17 08:27:56 +00:00
|
|
|
<FormGroup
|
|
|
|
hasNoPaddingTop
|
|
|
|
label={t("allowRemoteResourceManagement")}
|
|
|
|
fieldId="allowRemoteResourceManagement"
|
|
|
|
labelIcon={
|
|
|
|
<HelpItem
|
2021-12-14 14:56:36 +00:00
|
|
|
helpText="clients-help:allowRemoteResourceManagement"
|
|
|
|
fieldLabelId="clients:allowRemoteResourceManagement"
|
2021-11-17 08:27:56 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Controller
|
|
|
|
name="allowRemoteResourceManagement"
|
|
|
|
data-testid="allowRemoteResourceManagement"
|
|
|
|
defaultValue={false}
|
|
|
|
control={control}
|
|
|
|
render={({ onChange, value }) => (
|
|
|
|
<Switch
|
|
|
|
id="allowRemoteResourceManagement"
|
|
|
|
label={t("common:on")}
|
|
|
|
labelOff={t("common:off")}
|
|
|
|
isChecked={value}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<SaveReset
|
2021-12-16 16:31:17 +00:00
|
|
|
name="authenticationSettings"
|
|
|
|
save={() => handleSubmit(save)()}
|
2021-11-17 08:27:56 +00:00
|
|
|
reset={() => reset(resource)}
|
|
|
|
/>
|
|
|
|
</FormAccess>
|
|
|
|
</PageSection>
|
|
|
|
);
|
|
|
|
};
|