8000 17728 - Added User Attribute Search by andreas-blaettlinger-bosch · Pull Request #19574 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/apps/admin-ui/public/locales/de/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"save": "Speichern",
"cancel": "Abbrechen",
"delete": "Löschen",
"reset": "Zurücksetzen",
"remove": "Entfernen",
"search": "Suche",
"key": "Schlüssel",
Expand Down
8 changes: 8 additions & 0 deletions js/apps/admin-ui/public/locales/de/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"usersExplain": "Benutzer in diesem Realm.",
"userList": "Benutzerliste",
"searchForUser": "Benutzer suchen",
"searchType.default": "Standartsuche",
"searchType.attribute": "Attributsuche",
"selectAttribute": "Wähle Attribut",
"selectAttributes": "Wähle Attribute",
"searchUserByAttributeMissingKeyError": "Attributschlüssel angeben",
"searchUserByAttributeKeyAlreadyInUseError": "Attributschlüssel bereits in Verwendung",
"searchUserByAttributeMissingValueError": "Attributwert angeben",
"searchUserByAttributeDescription": "Es unterstützt die Einstellung mehrerer Attribute als Suchfilter, indem verschiedene Schlüssel oder Werte festgelegt werden. Für einen Schlüssel kann nur ein Wert eingegeben werden.",
"join": "Beitreten",
"leave": "Verlassen",
"groupMembership": "Gruppen-Mitglied",
Expand Down
1 change: 1 addition & 0 deletions js/apps/admin-ui/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"continue": "Continue",
"close": "Close",
"delete": "Delete",
"reset": "Reset",
"remove": "Remove",
"revoke": "Revoke",
"search": "Search",
Expand Down
8 changes: 8 additions & 0 deletions js/apps/admin-ui/public/locales/en/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"usersExplain": "Users are the users in the current realm.",
"userList": "User list",
"searchForUser": "Search user",
"searchType.default": "Default search",
"searchType.attribute": "Attribute search",
"selectAttribute": "Select attribute",
"selectAttributes": "Select attributes",
"searchUserByAttributeMissingKeyError": "Specify a attribute key",
"searchUserByAttributeKeyAlreadyInUseError": "Attribute key already in use",
"searchUserByAttributeMissingValueError": "Specify a attribute value",
"searchUserByAttributeDescription": "It supports setting multiple attributes as the search filter by setting different keys or values. Only one value can be typed for a key.",
"startBySearchingAUser": "Start by searching for users",
"searchForUserDescription": "This realm may have a federated provider. Viewing all users may cause the system to slow down, but it can be done by searching for \"*\". Please search for a user above.",
"createUser": "Create user",
Expand Down
185 changes: 132 additions & 53 deletions js/apps/admin-ui/src/components/users/UserDataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
import {
AlertVariant,
Button,
ButtonVariant,
Chip,
ChipGroup,
EmptyState,
InputGroup,
FlexItem,
Label,
Text,
TextContent,
TextInput,
Toolbar,
ToolbarContent,
ToolbarItem,
Expand All @@ -19,7 +21,6 @@ import {
import {
ExclamationCircleIcon,
InfoCircleIcon,
SearchIcon,
WarningTriangleIcon,
} from "@patternfly/react-icons";
import type { IRowData } from "@patternfly/react-table";
Expand All @@ -40,16 +41,27 @@ import { useFetch } from "../../utils/useFetch";
import { toAddUser } from "../../user/routes/AddUser";
import { toUser } from "../../user/routes/User";
import { UserDataTableToolbarItems } from "./UserDataTableToolbarItems";
import { SearchType } from "../../user/details/SearchFilter";

export type UserAttribute = {
name: string;
displayName: string;
value: string;
};

export function UserDataTable() {
const { t } = useTranslation("users");
const { addAlert, addError } = useAlerts();
const { realm: realmName } = useRealm();
const navigate = useNavigate();
const [userStorage, setUserStorage] = useState<ComponentRepresentation[]>();
const [searchUser, setSearchUser] = useState<string>();
const [searchUser, setSearchUser] = useState("");
const [realm, setRealm] = useState<RealmRepresentation | undefined>();
const [selectedRows, setSelectedRows] = useState<UserRepresentation[]>([]);
const [searchType, setSearchType] = useState<SearchType>("default");
const [activeFilters, setActiveFilters] = useState<UserAttribute[]>([]);
const [profile, setProfile] = useState<UserProfileConfig>({});
const [query, setQuery] = useState("");

const [key, setKey] = useState(0);
const refresh = () => setKey(key + 1);
Expand All @@ -64,19 +76,22 @@ export function UserDataTable() {
return await Promise.all([
adminClient.components.find(testParams),
adminClient.realms.findOne({ realm: realmName }),
adminClient.users.getProfile(),
]);
} catch {
return [[], {}] as [
return [[], {}, {}] as [
ComponentRepresentation[],
RealmRepresentation | undefined
RealmRepresentation | undefined,
UserProfileConfig
];
}
},
([storageProviders, realm]) => {
([storageProviders, realm, profile]) => {
setUserStorage(
storageProviders.filter((p) => p.config?.enabled[0] === "true")
);
setRealm(realm);
setProfile(profile);
},
[]
);
Expand All @@ -94,6 +109,7 @@ export function UserDataTable() {
const params: { [name: string]: string | number } = {
first: first!,
max: max!,
q: query!,
};

const searchParam = search || searchUser || "";
Expand Down Expand Up @@ -146,7 +162,7 @@ export function UserDataTable() {
await adminClient.users.del({ id: user.id! });
}
setSelectedRows([]);
refresh();
clearAllFilters();
addAlert(t("userDeletedSuccess"), AlertVariant.success);
} catch (error) {
addError("users:userDeletedError", error);
Expand Down Expand Up @@ -197,56 +213,126 @@ export function UserDataTable() {
//should *only* list users when no user federation is configured
const listUsers = !(userStorage.length > 0);

const clearAllFilters = () => {
const filtered = [...activeFilters].filter(
(chip) => chip.name !== chip.name
);
setActiveFilters(filtered);
setSearchUser("");
setQuery("");
refresh();
};

const createQueryString = (filters: UserAttribute[]) => {
return filters.map((filter) => `${filter.name}:${filter.value}`).join(" ");
};

const searchUserWithAttributes = () => {
const attributes = createQueryString(activeFilters);
setQuery(attributes);
refresh();
};

const createAttributeSearchChips = () => {
return (
<FlexItem>
{activeFilters.length > 0 && (
<>
{Object.values(activeFilters).map((entry) => {
return (
<ChipGroup
className="pf-u-mt-md pf-u-mr-md"
key={entry.name}
categoryName={
entry.displayName.length ? entry.displayName : entry.name
}
isClosable
=> {
event.stopPropagation();

const filtered = [...activeFilters].filter(
(chip) => chip.name !== entry.name
);
const attributes = createQueryString(filtered);

setActiveFilters(filtered);
setQuery(attributes);
refresh();
}}
>
<Chip key={entry.name} isReadOnly>
{entry.value}
</Chip>
</ChipGroup>
);
})}
</>
)}
</FlexItem>
);
};

const toolbar = () => {
return (
<UserDataTableToolbarItems
realm={realm}
hasSelectedRows={selectedRows.length === 0}
toggleDeleteDialog={toggleDeleteDialog}
toggleUnlockUsersDialog={toggleUnlockUsersDialog}
goToCreate={goToCreate}
searchType={searchType}
setSearchType={setSearchType}
searchUser={searchUser}
setSearchUser={setSearchUser}
activeFilters={activeFilters}
setActiveFilters={setActiveFilters}
refresh={refresh}
profile={profile}
clearAllFilters={clearAllFilters}
createAttributeSearchChips={createAttributeSearchChips}
searchUserWithAttributes={searchUserWithAttributes}
/>
);
};

const subtoolbar = () => {
if (!activeFilters.length) {
return;
}
return (
<div className="user-attribute-search-form-subtoolbar">
<ToolbarItem>{createAttributeSearchChips()}</ToolbarItem>
<ToolbarItem>
<Button
variant="link"
=> {
clearAllFilters();
}}
>
{t("common:clearAllFilters")}
</Button>
</ToolbarItem>
</div>
);
};

return (
<>
<DeleteConfirm />
<UnlockUsersConfirm />
<KeycloakDataTable
isSearching
key={key}
loader={loader}
isPaginated
ariaLabelKey="users:title"
searchPlaceholderKey="users:searchForUser"
canSelectAll
any[]) => setSelectedRows([...rows])}
UserRepresentation[]) => setSelectedRows([...rows])}
emptyState={
!listUsers ? (
<>
<Toolbar>
<ToolbarContent>
<ToolbarItem>
<InputGroup>
<TextInput
name="search-input"
type="search"
aria-label={t("search")}
placeholder={t("users:searchForUser")}
=> {
setSearchUser(value);
}}
=> {
if (e.key === "Enter") {
refresh();
}
}}
/>
<Button
variant={ButtonVariant.control}
aria-label={t("common:search")}
>
>
<SearchIcon />
</Button>
</InputGroup>
</ToolbarItem>
<UserDataTableToolbarItems
realm={realm}
hasSelectedRows={selectedRows.length === 0}
toggleDeleteDialog={toggleDeleteDialog}
toggleUnlockUsersDialog={toggleUnlockUsersDialog}
goToCreate={goToCreate}
/>
</ToolbarContent>
<ToolbarContent>{toolbar()}</ToolbarContent>
</Toolbar>
<EmptyState data-testid="empty-state" variant="large">
<TextContent className="kc-search-users-text">
Expand All @@ -263,15 +349,8 @@ export function UserDataTable() {
/>
)
}
toolbarItem={
<UserDataTableToolbarItems
realm={realm}
hasSelectedRows={selectedRows.length === 0}
toggleDeleteDialog={toggleDeleteDialog}
toggleUnlockUsersDialog={toggleUnlockUsersDialog}
goToCreate={goToCreate}
/>
}
toolbarItem={toolbar()}
subToolbar={subtoolbar()}
actionResolver={(rowData: IRowData) => {
const user: UserRepresentation = rowData.data;
if (!user.access?.manage) return [];
Expand Down
Loading
0