diff --git a/src/clients/authorization/PermissionDetails.tsx b/src/clients/authorization/PermissionDetails.tsx index c175b2d363..3e67d10c77 100644 --- a/src/clients/authorization/PermissionDetails.tsx +++ b/src/clients/authorization/PermissionDetails.tsx @@ -46,7 +46,7 @@ export default function PermissionDetails() { const { register, control, reset, errors, handleSubmit } = form; const history = useHistory(); - const { id, realm, permissionType, permissionId } = useParams< + const { id, realm, permissionType, permissionId, selectedId } = useParams< NewPermissionParams & PermissionDetailsParams >(); @@ -287,6 +287,9 @@ export default function PermissionDetails() { name="resources" searchFunction="listResources" clientId={id} + preSelected={ + permissionType === "scope" ? undefined : selectedId + } variant={ permissionType === "scope" ? SelectVariant.typeahead @@ -309,7 +312,11 @@ export default function PermissionDetails() { validated={errors.scopes ? "error" : "default"} isRequired > - + )} { realm, id: clientId, permissionType: "resource", + selectedId: resource._id, })} /> )} diff --git a/src/clients/authorization/ResourcesPolicySelect.tsx b/src/clients/authorization/ResourcesPolicySelect.tsx index 885b1d6192..52bfb3ae4d 100644 --- a/src/clients/authorization/ResourcesPolicySelect.tsx +++ b/src/clients/authorization/ResourcesPolicySelect.tsx @@ -12,6 +12,12 @@ type ResourcesPolicySelectProps = { clientId: string; searchFunction: keyof Pick; variant?: SelectVariant; + preSelected?: string; +}; + +type Policies = { + id?: string; + name?: string; }; export const ResourcesPolicySelect = ({ @@ -19,12 +25,13 @@ export const ResourcesPolicySelect = ({ searchFunction, clientId, variant = SelectVariant.typeaheadMulti, + preSelected, }: ResourcesPolicySelectProps) => { const { t } = useTranslation("clients"); const adminClient = useAdminClient(); const { control } = useFormContext(); - const [items, setItems] = useState([]); + const [items, setItems] = useState([]); const [search, setSearch] = useState(""); const [open, setOpen] = useState(false); @@ -41,21 +48,21 @@ export const ResourcesPolicySelect = ({ id: "_id" in p ? p._id : "id" in p ? p.id : undefined, name: p.name, })), - (policies) => - setItems( - policies.map((p) => ( - - {p.name} - - )) - ), + (policies) => setItems(policies), [search] ); + const toSelectOptions = () => + items.map((p) => ( + + {p.name} + + )); + return ( ( )} /> diff --git a/src/clients/authorization/ScopeSelect.tsx b/src/clients/authorization/ScopeSelect.tsx index d14e9ccf22..beeeb5be58 100644 --- a/src/clients/authorization/ScopeSelect.tsx +++ b/src/clients/authorization/ScopeSelect.tsx @@ -9,9 +9,14 @@ import { useAdminClient, useFetch } from "../../context/auth/AdminClient"; type ScopeSelectProps = { clientId: string; resourceId?: string; + preSelected?: string; }; -export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => { +export const ScopeSelect = ({ + clientId, + resourceId, + preSelected, +}: ScopeSelectProps) => { const { t } = useTranslation("clients"); const adminClient = useAdminClient(); @@ -57,7 +62,7 @@ export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => { return ( value.length > 0 }} render={({ onChange, value }) => ( @@ -85,6 +90,7 @@ export const ScopeSelect = ({ clientId, resourceId }: ScopeSelectProps) => { isOpen={open} aria-labelledby={t("scopes")} validated={errors.scopes ? "error" : "default"} + isDisabled={!!preSelected} > {toSelectOptions(scopes)} diff --git a/src/clients/authorization/Scopes.tsx b/src/clients/authorization/Scopes.tsx index 2fde08f96f..70e225072b 100644 --- a/src/clients/authorization/Scopes.tsx +++ b/src/clients/authorization/Scopes.tsx @@ -208,6 +208,7 @@ export const AuthorizationScopes = ({ clientId }: ScopesProps) => { realm, id: clientId, permissionType: "scope", + selectedId: scope.id, })} /> )} diff --git a/src/clients/routes/NewPermission.ts b/src/clients/routes/NewPermission.ts index 456a9855d2..b4c0508f4c 100644 --- a/src/clients/routes/NewPermission.ts +++ b/src/clients/routes/NewPermission.ts @@ -9,10 +9,11 @@ export type NewPermissionParams = { realm: string; id: string; permissionType: PermissionType; + selectedId?: string; }; export const NewPermissionRoute: RouteDef = { - path: "/:realm/clients/:id/authorization/permission/new/:permissionType", + path: "/:realm/clients/:id/authorization/permission/new/:permissionType/:selectedId?", component: lazy(() => import("../authorization/PermissionDetails")), breadcrumb: (t) => t("clients:createPermission"), access: "manage-clients",