2021-05-04 17:58:18 +00:00
|
|
|
import type { IFormatter, IFormatterValueType } from "@patternfly/react-table";
|
2020-10-02 13:56:46 +00:00
|
|
|
import FileSaver from "file-saver";
|
2020-12-07 19:23:18 +00:00
|
|
|
import _ from "lodash";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
|
|
|
import type { ProviderRepresentation } from "@keycloak/keycloak-admin-client/lib/defs/serverInfoRepesentation";
|
|
|
|
import type KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
2021-08-09 19:28:24 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2020-08-31 18:26:25 +00:00
|
|
|
|
2020-11-02 20:15:09 +00:00
|
|
|
export const sortProviders = (providers: {
|
|
|
|
[index: string]: ProviderRepresentation;
|
|
|
|
}) => {
|
|
|
|
return [...new Map(Object.entries(providers).sort(sortProvider)).keys()];
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortProvider = (
|
2020-09-01 14:51:59 +00:00
|
|
|
a: [string, ProviderRepresentation],
|
|
|
|
b: [string, ProviderRepresentation]
|
|
|
|
) => {
|
2020-08-31 18:26:25 +00:00
|
|
|
let s1, s2;
|
2020-10-27 11:25:54 +00:00
|
|
|
if (a[1].order !== b[1].order) {
|
2020-08-31 18:26:25 +00:00
|
|
|
s1 = b[1].order;
|
|
|
|
s2 = a[1].order;
|
2020-09-01 14:51:59 +00:00
|
|
|
} else {
|
2020-08-31 18:26:25 +00:00
|
|
|
s1 = a[0];
|
|
|
|
s2 = b[0];
|
2020-09-01 14:51:59 +00:00
|
|
|
}
|
2020-08-31 18:26:25 +00:00
|
|
|
if (s1 < s2) {
|
|
|
|
return -1;
|
|
|
|
} else if (s1 > s2) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2020-10-02 13:56:46 +00:00
|
|
|
|
|
|
|
export const exportClient = (client: ClientRepresentation): void => {
|
2020-12-07 19:23:18 +00:00
|
|
|
const clientCopy = _.cloneDeep(client);
|
2020-10-02 13:56:46 +00:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
};
|
2020-10-20 20:47:23 +00:00
|
|
|
|
2021-01-15 01:44:16 +00:00
|
|
|
export const toUpperCase = (name: string) =>
|
|
|
|
name.charAt(0).toUpperCase() + name.slice(1);
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2020-10-20 20:47:23 +00:00
|
|
|
export const convertToFormValues = (
|
|
|
|
obj: any,
|
|
|
|
prefix: string,
|
|
|
|
setValue: (name: string, value: any) => void
|
|
|
|
) => {
|
|
|
|
return Object.keys(obj).map((key) => {
|
2021-02-28 20:02:31 +00:00
|
|
|
const newKey = key.replace(/\./g, "-");
|
2020-10-20 20:47:23 +00:00
|
|
|
setValue(prefix + "." + newKey, obj[key]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-08-09 19:28:24 +00:00
|
|
|
export const flatten = (
|
|
|
|
obj: Record<string, any> | undefined,
|
|
|
|
path = ""
|
|
|
|
): {} => {
|
|
|
|
if (!(obj instanceof Object)) return { [path.replace(/\.$/g, "")]: obj };
|
|
|
|
|
|
|
|
return Object.keys(obj).reduce((output, key) => {
|
|
|
|
return obj instanceof Array
|
|
|
|
? {
|
|
|
|
...output,
|
|
|
|
...flatten(obj[key as unknown as number], path + "[" + key + "]."),
|
|
|
|
}
|
|
|
|
: { ...output, ...flatten(obj[key], path + key + ".") };
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const convertFormValuesToObject = (
|
|
|
|
obj: any,
|
|
|
|
firstInstanceOnly?: boolean
|
|
|
|
) => {
|
2020-10-20 20:47:23 +00:00
|
|
|
const keyValues = Object.keys(obj).map((key) => {
|
2021-08-09 19:28:24 +00:00
|
|
|
const newKey = firstInstanceOnly
|
|
|
|
? key.replace(/-/, ".")
|
|
|
|
: key.replace(/-/g, ".");
|
2020-10-20 20:47:23 +00:00
|
|
|
return { [newKey]: obj[key] };
|
|
|
|
});
|
|
|
|
return Object.assign({}, ...keyValues);
|
|
|
|
};
|
2020-12-07 18:37:36 +00:00
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
export const emptyFormatter =
|
|
|
|
(): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
return data ? data : "—";
|
|
|
|
};
|
2021-01-13 19:59:45 +00:00
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
export const upperCaseFormatter =
|
|
|
|
(): IFormatter => (data?: IFormatterValueType) => {
|
|
|
|
const value = data?.toString();
|
2021-01-20 21:10:25 +00:00
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
return (value ? toUpperCase(value) : undefined) as string;
|
|
|
|
};
|
2021-01-20 21:10:25 +00:00
|
|
|
|
2021-01-13 19:59:45 +00:00
|
|
|
export const getBaseUrl = (adminClient: KeycloakAdminClient) => {
|
2021-05-19 07:56:59 +00:00
|
|
|
return (
|
|
|
|
(adminClient.keycloak
|
|
|
|
? adminClient.keycloak.authServerUrl!
|
|
|
|
: adminClient.baseUrl) + "/"
|
|
|
|
);
|
2021-01-13 19:59:45 +00:00
|
|
|
};
|
2021-05-03 20:00:12 +00:00
|
|
|
|
2021-07-05 11:24:10 +00:00
|
|
|
export const emailRegexPattern =
|
|
|
|
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
2021-08-09 19:28:24 +00:00
|
|
|
|
|
|
|
export const forHumans = (seconds: number) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
const levels: [
|
|
|
|
[number, string],
|
|
|
|
[number, string],
|
|
|
|
[number, string],
|
|
|
|
[number, string],
|
|
|
|
[number, string]
|
|
|
|
] = [
|
|
|
|
[Math.floor(seconds / 31536000), t("common:times.years")],
|
|
|
|
[Math.floor((seconds % 31536000) / 86400), t("common:times.days")],
|
|
|
|
[
|
|
|
|
Math.floor(((seconds % 31536000) % 86400) / 3600),
|
|
|
|
t("common:times.hours"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
Math.floor((((seconds % 31536000) % 86400) % 3600) / 60),
|
|
|
|
t("common:times.minutes"),
|
|
|
|
],
|
|
|
|
[(((seconds % 31536000) % 86400) % 3600) % 60, t("common:times.seconds")],
|
|
|
|
];
|
|
|
|
let returntext = "";
|
|
|
|
|
|
|
|
for (let i = 0, max = levels.length; i < max; i++) {
|
|
|
|
if (levels[i][0] === 0) continue;
|
|
|
|
returntext +=
|
|
|
|
" " +
|
|
|
|
levels[i][0] +
|
|
|
|
" " +
|
|
|
|
(levels[i][0] === 1
|
|
|
|
? levels[i][1].substr(0, levels[i][1].length - 1)
|
|
|
|
: levels[i][1]);
|
|
|
|
}
|
|
|
|
return returntext.trim();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const interpolateTimespan = (forHumans: string) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const timespan = forHumans.split(" ");
|
|
|
|
|
|
|
|
if (timespan[1] === "Years") {
|
|
|
|
return t(`realm-settings:convertedToYearsValue`, {
|
|
|
|
convertedToYears: forHumans,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timespan[1] === "Days") {
|
|
|
|
return t(`realm-settings:convertedToDaysValue`, {
|
|
|
|
convertedToYears: forHumans,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timespan[1] === "Hours") {
|
|
|
|
return t(`realm-settings:convertedToHoursValue`, {
|
|
|
|
convertedToHours: forHumans,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timespan[1] === "Minutes") {
|
|
|
|
return t(`realm-settings:convertedToMinutesValue`, {
|
|
|
|
convertedToMinutes: forHumans,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timespan[1] === "Seconds") {
|
|
|
|
return t(`realm-settings:convertedToSecondsValue`, {
|
|
|
|
convertedToSeconds: forHumans,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2021-08-20 16:15:12 +00:00
|
|
|
|
|
|
|
export const KEY_PROVIDER_TYPE = "org.keycloak.keys.KeyProvider";
|