2021-03-23 19:02:27 +00:00
|
|
|
import React, { useEffect, 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-03-03 19:12:16 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type UserRepresentation from "keycloak-admin/lib/defs/userRepresentation";
|
2021-03-03 13:53:42 +00:00
|
|
|
import { UserForm } from "./UserForm";
|
2021-03-03 21:17:01 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useAdminClient } 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-05-04 17:58:18 +00:00
|
|
|
import type GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation";
|
2021-06-07 12:58:47 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
export const UsersTabs = () => {
|
|
|
|
const { t } = useTranslation("roles");
|
2021-03-03 21:17:01 +00:00
|
|
|
const { addAlert } = 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-03-23 19:02:27 +00:00
|
|
|
const [user, setUser] = useState("");
|
2021-04-20 12:22:36 +00:00
|
|
|
const [addedGroups, setAddedGroups] = useState<GroupRepresentation[]>([]);
|
2021-03-23 19:02:27 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const update = async () => {
|
|
|
|
if (id) {
|
|
|
|
const fetchedUser = await adminClient.users.findOne({ id });
|
|
|
|
setUser(fetchedUser.username!);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(update, 100);
|
|
|
|
}, []);
|
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-06-07 12:58:47 +00:00
|
|
|
history.push(`/${realm}/users/${createdUser.id}/settings`);
|
2021-03-11 20:23:08 +00:00
|
|
|
}
|
2021-03-03 21:17:01 +00:00
|
|
|
} catch (error) {
|
|
|
|
addAlert(
|
|
|
|
t("users:userCreateError", {
|
2021-03-29 11:37:47 +00:00
|
|
|
error: error.response?.data?.errorMessage || error,
|
2021-03-03 21:17:01 +00:00
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2021-03-03 13:53:42 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-06-07 12:58:47 +00:00
|
|
|
<ViewHeader titleKey={user! || t("users:createUser")} divider={!id} />
|
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2021-03-11 20:23:08 +00:00
|
|
|
{id && (
|
|
|
|
<KeycloakTabs isBox>
|
|
|
|
<Tab
|
2021-04-15 10:23:36 +00:00
|
|
|
eventKey="settings"
|
2021-03-11 20:23:08 +00:00
|
|
|
data-testid="user-details-tab"
|
|
|
|
title={<TabTitleText>{t("details")}</TabTitleText>}
|
|
|
|
>
|
2021-06-07 12:58:47 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<UserForm
|
|
|
|
onGroupsUpdate={updateGroups}
|
|
|
|
form={userForm}
|
|
|
|
save={save}
|
|
|
|
editMode={true}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
2021-03-11 20:23:08 +00:00
|
|
|
</Tab>
|
2021-03-23 19:02:27 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="groups"
|
|
|
|
data-testid="user-groups-tab"
|
|
|
|
title={<TabTitleText>{t("groups")}</TabTitleText>}
|
|
|
|
>
|
2021-06-23 10:42:14 +00:00
|
|
|
<UserGroups />
|
2021-03-23 19:02:27 +00:00
|
|
|
</Tab>
|
2021-04-14 18:39:21 +00:00
|
|
|
<Tab
|
|
|
|
eventKey="consents"
|
|
|
|
data-testid="user-consents-tab"
|
|
|
|
title={<TabTitleText>{t("users:consents")}</TabTitleText>}
|
|
|
|
>
|
2021-06-08 13:23:32 +00:00
|
|
|
<UserConsents />
|
2021-04-14 18:39:21 +00:00
|
|
|
</Tab>
|
2021-03-11 20:23:08 +00:00
|
|
|
</KeycloakTabs>
|
|
|
|
)}
|
2021-04-20 12:22:36 +00:00
|
|
|
{!id && (
|
2021-06-07 12:58:47 +00:00
|
|
|
<PageSection variant="light">
|
|
|
|
<UserForm
|
|
|
|
onGroupsUpdate={updateGroups}
|
|
|
|
form={userForm}
|
|
|
|
save={save}
|
|
|
|
editMode={false}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
2021-04-20 12:22:36 +00:00
|
|
|
)}
|
2021-03-03 13:53:42 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|