2022-12-07 09:03:51 +00:00
|
|
|
import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation";
|
2022-01-04 09:17:43 +00:00
|
|
|
import {
|
|
|
|
ActionGroup,
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
PageSection,
|
|
|
|
} from "@patternfly/react-core";
|
2022-12-07 09:03:51 +00:00
|
|
|
import { useState } from "react";
|
2024-03-19 13:22:10 +00:00
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2022-12-07 09:03:51 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-01-18 12:09:49 +00:00
|
|
|
import { Link, useNavigate } from "react-router-dom";
|
2024-03-19 13:22:10 +00:00
|
|
|
import { TextControl } from "ui-shared";
|
2023-05-03 13:51:02 +00:00
|
|
|
import { adminClient } from "../../admin-client";
|
2022-12-07 09:03:51 +00:00
|
|
|
import { useAlerts } from "../../components/alert/Alerts";
|
2023-05-24 12:11:06 +00:00
|
|
|
import { FormAccess } from "../../components/form/FormAccess";
|
2022-01-04 09:17:43 +00:00
|
|
|
import { ViewHeader } from "../../components/view-header/ViewHeader";
|
2023-05-03 15:40:27 +00:00
|
|
|
import { useFetch } from "../../utils/useFetch";
|
2022-12-07 14:23:12 +00:00
|
|
|
import { useParams } from "../../utils/useParams";
|
2022-01-04 09:17:43 +00:00
|
|
|
import useToggle from "../../utils/useToggle";
|
2022-12-07 09:03:51 +00:00
|
|
|
import { toAuthorizationTab } from "../routes/AuthenticationTab";
|
|
|
|
import type { ScopeDetailsParams } from "../routes/Scope";
|
2022-01-04 09:17:43 +00:00
|
|
|
import { DeleteScopeDialog } from "./DeleteScopeDialog";
|
|
|
|
|
2022-12-07 09:03:51 +00:00
|
|
|
type FormFields = Omit<ScopeRepresentation, "resources">;
|
|
|
|
|
2022-01-04 09:17:43 +00:00
|
|
|
export default function ScopeDetails() {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2022-01-04 09:17:43 +00:00
|
|
|
const { id, scopeId, realm } = useParams<ScopeDetailsParams>();
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2022-01-04 09:17:43 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
const [deleteDialog, toggleDeleteDialog] = useToggle();
|
|
|
|
const [scope, setScope] = useState<ScopeRepresentation>();
|
2024-03-19 13:22:10 +00:00
|
|
|
const form = useForm<FormFields>({
|
2022-04-08 12:37:31 +00:00
|
|
|
mode: "onChange",
|
|
|
|
});
|
2024-03-19 13:22:10 +00:00
|
|
|
const { reset, handleSubmit } = form;
|
2022-01-04 09:17:43 +00:00
|
|
|
|
|
|
|
useFetch(
|
|
|
|
async () => {
|
|
|
|
if (scopeId) {
|
|
|
|
const scope = await adminClient.clients.getAuthorizationScope({
|
|
|
|
id,
|
|
|
|
scopeId,
|
|
|
|
});
|
|
|
|
if (!scope) {
|
2023-09-14 09:01:15 +00:00
|
|
|
throw new Error(t("notFound"));
|
2022-01-04 09:17:43 +00:00
|
|
|
}
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(scope) => {
|
|
|
|
setScope(scope);
|
|
|
|
reset({ ...scope });
|
|
|
|
},
|
2023-07-11 14:03:21 +00:00
|
|
|
[],
|
2022-01-04 09:17:43 +00:00
|
|
|
);
|
|
|
|
|
2022-12-07 09:03:51 +00:00
|
|
|
const onSubmit = async (scope: ScopeRepresentation) => {
|
2022-01-04 09:17:43 +00:00
|
|
|
try {
|
|
|
|
if (scopeId) {
|
|
|
|
await adminClient.clients.updateAuthorizationScope(
|
|
|
|
{ id, scopeId },
|
2023-07-11 14:03:21 +00:00
|
|
|
scope,
|
2022-01-04 09:17:43 +00:00
|
|
|
);
|
|
|
|
setScope(scope);
|
|
|
|
} else {
|
|
|
|
await adminClient.clients.createAuthorizationScope(
|
|
|
|
{ id },
|
|
|
|
{
|
|
|
|
name: scope.name!,
|
|
|
|
displayName: scope.displayName,
|
|
|
|
iconUri: scope.iconUri,
|
2023-07-11 14:03:21 +00:00
|
|
|
},
|
2022-01-04 09:17:43 +00:00
|
|
|
);
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toAuthorizationTab({ realm, clientId: id, tab: "scopes" }));
|
2022-01-04 09:17:43 +00:00
|
|
|
}
|
|
|
|
addAlert(
|
|
|
|
t((scopeId ? "update" : "create") + "ScopeSuccess"),
|
2023-07-11 14:03:21 +00:00
|
|
|
AlertVariant.success,
|
2022-01-04 09:17:43 +00:00
|
|
|
);
|
|
|
|
} catch (error) {
|
2023-09-13 14:05:17 +00:00
|
|
|
addError("scopeSaveError", error);
|
2022-01-04 09:17:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DeleteScopeDialog
|
|
|
|
clientId={id}
|
|
|
|
open={deleteDialog}
|
|
|
|
toggleDialog={toggleDeleteDialog}
|
|
|
|
selectedScope={scope}
|
|
|
|
refresh={() =>
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toAuthorizationTab({ realm, clientId: id, tab: "scopes" }))
|
2022-01-04 09:17:43 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ViewHeader
|
2023-09-13 14:05:17 +00:00
|
|
|
titleKey={scopeId ? scope?.name! : t("createAuthorizationScope")}
|
2022-01-04 09:17:43 +00:00
|
|
|
dropdownItems={
|
|
|
|
scopeId
|
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="delete"
|
|
|
|
data-testid="delete-resource"
|
|
|
|
onClick={() => toggleDeleteDialog()}
|
|
|
|
>
|
2023-09-14 09:01:15 +00:00
|
|
|
{t("delete")}
|
2022-01-04 09:17:43 +00:00
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
2024-03-19 13:22:10 +00:00
|
|
|
<FormProvider {...form}>
|
|
|
|
<FormAccess
|
|
|
|
isHorizontal
|
|
|
|
role="manage-authorization"
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
2022-01-04 09:17:43 +00:00
|
|
|
>
|
2024-03-19 13:22:10 +00:00
|
|
|
<TextControl
|
|
|
|
name="name"
|
|
|
|
label={t("name")}
|
|
|
|
labelIcon={t("scopeNameHelp")}
|
|
|
|
rules={{ required: t("required") }}
|
2022-01-04 09:17:43 +00:00
|
|
|
/>
|
2024-03-19 13:22:10 +00:00
|
|
|
<TextControl
|
|
|
|
name="displayName"
|
|
|
|
label={t("displayName")}
|
|
|
|
labelIcon={t("scopeDisplayNameHelp")}
|
|
|
|
/>
|
|
|
|
<TextControl
|
|
|
|
name="iconUri"
|
|
|
|
label={t("iconUri")}
|
|
|
|
labelIcon={t("iconUriHelp")}
|
|
|
|
/>
|
|
|
|
<ActionGroup>
|
|
|
|
<div className="pf-u-mt-md">
|
2022-01-04 09:17:43 +00:00
|
|
|
<Button
|
2024-03-19 13:22:10 +00:00
|
|
|
variant={ButtonVariant.primary}
|
|
|
|
type="submit"
|
|
|
|
data-testid="save"
|
2022-01-04 09:17:43 +00:00
|
|
|
>
|
2024-03-19 13:22:10 +00:00
|
|
|
{t("save")}
|
2022-01-04 09:17:43 +00:00
|
|
|
</Button>
|
2024-03-19 13:22:10 +00:00
|
|
|
|
|
|
|
{!scope ? (
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
data-testid="cancel"
|
|
|
|
component={(props) => (
|
|
|
|
<Link
|
|
|
|
{...props}
|
|
|
|
to={toAuthorizationTab({
|
|
|
|
realm,
|
|
|
|
clientId: id,
|
|
|
|
tab: "scopes",
|
|
|
|
})}
|
|
|
|
></Link>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
variant="link"
|
|
|
|
data-testid="revert"
|
|
|
|
onClick={() => reset({ ...scope })}
|
|
|
|
>
|
|
|
|
{t("revert")}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormAccess>
|
|
|
|
</FormProvider>
|
2022-01-04 09:17:43 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|