2021-09-02 13:44:31 +00:00
|
|
|
import React, { useState } from "react";
|
2021-03-11 20:23:08 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
PageSection,
|
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
} from "@patternfly/react-core";
|
2021-03-03 13:53:42 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-09-02 13:44:31 +00:00
|
|
|
import { FormProvider, useForm } from "react-hook-form";
|
2021-03-03 13:53:42 +00:00
|
|
|
|
2021-08-26 08:39:35 +00:00
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2021-09-02 13:44:31 +00:00
|
|
|
import type GroupRepresentation from "@keycloak/keycloak-admin-client/lib/defs/groupRepresentation";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { BruteForced, UserForm } from "./UserForm";
|
2021-03-03 21:17:01 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-09-02 13:44:31 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-06-07 12:58:47 +00:00
|
|
|
import { useHistory, useParams } from "react-router-dom";
|
2021-03-11 20:23:08 +00:00
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
2021-03-23 19:02:27 +00:00
|
|
|
import { UserGroups } from "./UserGroups";
|
2021-04-14 18:39:21 +00:00
|
|
|
import { UserConsents } from "./UserConsents";
|
2021-06-07 12:58:47 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-08-31 08:13:25 +00:00
|
|
|
import { UserIdentityProviderLinks } from "./UserIdentityProviderLinks";
|
2021-09-02 13:44:31 +00:00
|
|
|
import { toUser } from "./routes/User";
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
export const UsersTabs = () => {
|
|
|
|
const { t } = useTranslation("roles");
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-03-04 18:49:05 +00:00
|
|
|
const history = useHistory();
|
2021-06-07 12:58:47 +00:00
|
|
|
const { realm } = useRealm();
|
2021-03-04 18:49:05 +00:00
|
|
|
|
2021-03-03 21:17:01 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-04-14 18:39:21 +00:00
|
|
|
const userForm = useForm<UserRepresentation>({ mode: "onChange" });
|
2021-03-11 20:23:08 +00:00
|
|
|
const { id } = useParams<{ id: string }>();
|
2021-09-02 13:44:31 +00:00
|
|
|
const [user, setUser] = useState<UserRepresentation>();
|
|
|
|
const [bruteForced, setBruteForced] = useState<BruteForced>();
|
2021-04-20 12:22:36 +00:00
|
|
|
const [addedGroups, setAddedGroups] = useState<GroupRepresentation[]>([]);
|
2021-03-23 19:02:27 +00:00
|
|
|
|
2021-09-02 13:44:31 +00:00
|
|
|
useFetch(
|
|
|
|
async () => {
|
2021-03-23 19:02:27 +00:00
|
|
|
if (id) {
|
2021-09-02 13:44:31 +00:00
|
|
|
const user = await adminClient.users.findOne({ id });
|
|
|
|
const isBruteForceProtected = (
|
|
|
|
await adminClient.realms.findOne({ realm })
|
|
|
|
).bruteForceProtected;
|
|
|
|
const isLocked: boolean =
|
|
|
|
isBruteForceProtected &&
|
|
|
|
(await adminClient.attackDetection.findOne({ id: user.id! }))
|
|
|
|
?.disabled;
|
|
|
|
return { user, bruteForced: { isBruteForceProtected, isLocked } };
|
2021-03-23 19:02:27 +00:00
|
|
|
}
|
2021-09-02 13:44:31 +00:00
|
|
|
return { user: undefined };
|
|
|
|
},
|
|
|
|
({ user, bruteForced }) => {
|
|
|
|
setUser(user);
|
|
|
|
setBruteForced(bruteForced);
|
|
|
|
user && setupForm(user);
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const setupForm = (user: UserRepresentation) => {
|
|
|
|
userForm.reset(user);
|
|
|
|
};
|
2021-03-03 21:17:01 +00:00
|
|
|
|
2021-04-20 12:22:36 +00:00
|
|
|
const updateGroups = (groups: GroupRepresentation[]) => {
|
|
|
|
setAddedGroups(groups);
|
|
|
|
};
|
|
|
|
|
2021-03-03 21:17:01 +00:00
|
|
|
const save = async (user: UserRepresentation) => {
|
|
|
|
try {
|
2021-03-11 20:23:08 +00:00
|
|
|
if (id) {
|
2021-06-16 11:35:03 +00:00
|
|
|
await adminClient.users.update({ id }, user);
|
2021-03-11 20:23:08 +00:00
|
|
|
addAlert(t("users:userSaved"), AlertVariant.success);
|
|
|
|
} else {
|
2021-06-07 12:58:47 +00:00
|
|
|
const createdUser = await adminClient.users.create(user);
|
2021-04-20 12:22:36 +00:00
|
|
|
|
|
|
|
addedGroups.forEach(async (group) => {
|
|
|
|
await adminClient.users.addToGroup({
|
2021-06-07 12:58:47 +00:00
|
|
|
id: createdUser.id!,
|
2021-04-20 12:22:36 +00:00
|
|
|
groupId: group.id!,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-03-11 20:23:08 +00:00
|
|
|
addAlert(t("users:userCreated"), AlertVariant.success);
|
2021-09-02 13:44:31 +00:00
|
|
|
history.push(toUser({ id: createdUser.id, realm, tab: "settings" }));
|
2021-03-11 20:23:08 +00:00
|
|
|
}
|
2021-03-03 21:17:01 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("users:userCreateError", error);
|
2021-03-03 21:17:01 +00:00
|
|
|
}
|
|
|
|
};
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-09-02 13:44:31 +00:00
|
|
|
<ViewHeader
|
|
|
|
titleKey={user?.username || t("users:createUser")}
|
|
|
|
divider={!id}
|
|
|
|
/>
|
2021-06-07 12:58:47 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2021-09-02 13:44:31 +00:00
|
|
|
<FormProvider {...userForm}>
|
|
|
|
{id && user && (
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
|
|
|
eventKey="settings"
|
|
|
|
data-testid="user-details-tab"
|
|
|
|
title={<TabTitleText>{t("details")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<PageSection variant="light">
|
|
|
|
{bruteForced && (
|
|
|
|
<UserForm
|
|
|
|
onGroupsUpdate={updateGroups}
|
|
|
|
save={save}
|
|
|
|
user={user}
|
|
|
|
bruteForce={bruteForced}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</PageSection>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="groups"
|
|
|
|
data-testid="user-groups-tab"
|
|
|
|
title={<TabTitleText>{t("groups")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<UserGroups user={user} />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="consents"
|
|
|
|
data-testid="user-consents-tab"
|
|
|
|
title={<TabTitleText>{t("users:consents")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<UserConsents />
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
eventKey="identity-provider-links"
|
|
|
|
data-testid="identity-provider-links-tab"
|
|
|
|
title={
|
|
|
|
<TabTitleText>
|
|
|
|
{t("users:identityProviderLinks")}
|
|
|
|
</TabTitleText>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<UserIdentityProviderLinks />
|
|
|
|
</Tab>
|
|
|
|
</KeycloakTabs>
|
|
|
|
)}
|
|
|
|
{!id && (
|
|
|
|
<PageSection variant="light">
|
|
|
|
<UserForm onGroupsUpdate={updateGroups} save={save} />
|
|
|
|
</PageSection>
|
|
|
|
)}
|
|
|
|
</FormProvider>
|
2021-03-03 13:53:42 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|