import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation"; import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { FormGroup, Select, SelectVariant, SelectOption, PageSection, ActionGroup, Button, Switch, ExpandableSection, TextInput, } from "@patternfly/react-core"; import { Controller, useFormContext } from "react-hook-form"; import { FormAccess } from "../../components/form-access/FormAccess"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { FormPanel } from "../../components/scroll-form/FormPanel"; import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation"; import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation"; import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; import type ResourceEvaluation from "@keycloak/keycloak-admin-client/lib/defs/resourceEvaluation"; import { useRealm } from "../../context/realm-context/RealmContext"; import { AttributeInput } from "../../components/attribute-input/AttributeInput"; import { defaultContextAttributes } from "../utils"; import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation"; import { useParams } from "react-router-dom"; import type ScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/scopeRepresentation"; export type AttributeType = { key: string; name: string; custom?: boolean; values?: { [key: string]: string; }[]; }; type ClientSettingsProps = { clients: ClientRepresentation[]; clientName?: string; save: () => void; reset: () => void; users: UserRepresentation[]; clientRoles: RoleRepresentation[]; }; export const AuthorizationEvaluate = ({ clients, clientRoles, clientName, users, reset, }: ClientSettingsProps) => { const form = useFormContext(); const { control } = form; const { t } = useTranslation("clients"); const adminClient = useAdminClient(); const realm = useRealm(); const { clientId } = useParams<{ clientId: string }>(); const [clientsDropdownOpen, setClientsDropdownOpen] = useState(false); const [scopesDropdownOpen, setScopesDropdownOpen] = useState(false); const [userDropdownOpen, setUserDropdownOpen] = useState(false); const [roleDropdownOpen, setRoleDropdownOpen] = useState(false); const [isExpanded, setIsExpanded] = useState(false); const [applyToResourceType, setApplyToResourceType] = useState(false); const [resources, setResources] = useState([]); const [scopes, setScopes] = useState([]); const [selectedClient, setSelectedClient] = useState(); const [selectedUser, setSelectedUser] = useState(); useFetch( async () => Promise.all([ adminClient.clients.listResources({ id: clientId, }), adminClient.clients.listAllScopes({ id: clientId, }), ]), ([resources, scopes]) => { setResources(resources); setScopes(scopes); }, [] ); const evaluate = (formValues: ResourceEvaluation) => { const resEval: ResourceEvaluation = { roleIds: formValues.roleIds ?? [], userId: selectedUser?.id!, entitlements: false, context: formValues.context, resources: formValues.resources, clientId: selectedClient?.id!, }; return adminClient.clients.evaluateResource( { id: clientId!, realm: realm.realm }, resEval ); }; return ( } fieldId="client" > ( )} /> } fieldId="loginTheme" > ( )} /> } fieldId="realmRole" > ( )} /> } > ( { onChange(value.toString()); setApplyToResourceType(value); }} /> )} /> {!applyToResourceType && ( } helperTextInvalid={t("common:required")} fieldId={name!} > item.name!)} resources={resources} isKeySelectable name="resources" /> )} {applyToResourceType && ( <> } fieldId="client" > } fieldId="authScopes" > ( )} /> )} setIsExpanded(!isExpanded)} isExpanded={isExpanded} > } helperTextInvalid={t("common:required")} fieldId={name!} > item.name )} isKeySelectable name="context" /> ); };