keycloak-scim/src/util.ts
Erik Jan de Wit bfa0c6e1ea
Advanced tab (#373)
* initial version of the advanced tab

* added registered nodes

* added fine grain open id connect configuration

* added open id connect compatibility section

* added advanced section

* added backend

* fixed type

* renamed 'advanced' to advancedtab

to prevent strange add of '/index.js' by snowpack

* fixed storybook stories

* change '_' to '-' because '_' is also used

* fix spacing buttons

* stop passing the form

* cypress test for advanced tab

* more tests

* saml section

* changed to use NumberInput

* added authetnication flow override

* fixed merge error

* updated text and added link to settings tab

* fixed test

* added filter on flows and better reset

* added now mandetory error handler

* added sorting

* Revert "changed to use NumberInput"

This reverts commit 7829f2656ae8fc8ed4a4a6b1c4b1961241a09d8e.

* allow users to put empty string as value

* already on detail page after save

* fixed merge error
2021-02-28 15:02:31 -05:00

93 lines
2.4 KiB
TypeScript

import { IFormatter, IFormatterValueType } from "@patternfly/react-table";
import FileSaver from "file-saver";
import _ from "lodash";
import ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
import { ProviderRepresentation } from "keycloak-admin/lib/defs/serverInfoRepesentation";
import KeycloakAdminClient from "keycloak-admin";
export const sortProviders = (providers: {
[index: string]: ProviderRepresentation;
}) => {
return [...new Map(Object.entries(providers).sort(sortProvider)).keys()];
};
const sortProvider = (
a: [string, ProviderRepresentation],
b: [string, ProviderRepresentation]
) => {
let s1, s2;
if (a[1].order !== b[1].order) {
s1 = b[1].order;
s2 = a[1].order;
} else {
s1 = a[0];
s2 = b[0];
}
if (s1 < s2) {
return -1;
} else if (s1 > s2) {
return 1;
} else {
return 0;
}
};
export const exportClient = (client: ClientRepresentation): void => {
const clientCopy = _.cloneDeep(client);
delete clientCopy.id;
if (clientCopy.protocolMappers) {
for (let i = 0; i < clientCopy.protocolMappers.length; i++) {
delete clientCopy.protocolMappers[i].id;
}
}
FileSaver.saveAs(
new Blob([JSON.stringify(clientCopy, null, 2)], {
type: "application/json",
}),
clientCopy.clientId + ".json"
);
};
export const toUpperCase = (name: string) =>
name.charAt(0).toUpperCase() + name.slice(1);
export const convertToFormValues = (
obj: any,
prefix: string,
setValue: (name: string, value: any) => void
) => {
return Object.keys(obj).map((key) => {
const newKey = key.replace(/\./g, "-");
setValue(prefix + "." + newKey, obj[key]);
});
};
export const convertFormValuesToObject = (obj: any) => {
const keyValues = Object.keys(obj).map((key) => {
const newKey = key.replace(/-/g, ".");
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
};
export const emptyFormatter = (): IFormatter => (
data?: IFormatterValueType
) => {
return data ? data : "—";
};
export const boolFormatter = (): IFormatter => (data?: IFormatterValueType) => {
const boolVal = data?.toString();
return (boolVal
? boolVal.charAt(0).toUpperCase() + boolVal.slice(1)
: undefined) as string;
};
export const getBaseUrl = (adminClient: KeycloakAdminClient) => {
return adminClient.keycloak
? adminClient.keycloak.authServerUrl!
: adminClient.baseUrl + "/";
};