Skip to content

Commit

Permalink
BED-5051 fix: disable user when not assigned to SSO provider (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistahj67 committed Nov 20, 2024
1 parent e4456c6 commit d59c2b6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 190 deletions.
153 changes: 0 additions & 153 deletions packages/javascript/bh-shared-ui/src/hooks/useUsers.tsx

This file was deleted.

51 changes: 15 additions & 36 deletions packages/javascript/bh-shared-ui/src/views/Users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import {
CreateUserDialog,
} from '../../components';
import { apiClient, LuxonFormat } from '../../utils';
import { CreateUserRequest, PutUserAuthSecretRequest, UpdateUserRequest } from 'js-client-library';
import { CreateUserRequest, PutUserAuthSecretRequest, UpdateUserRequest, User } from 'js-client-library';
import find from 'lodash/find';
import { useToggle } from '../../hooks';
import { User } from '../../hooks/useUsers';
import UserActionsMenu from '../../components/UserActionsMenu';
import { useNotifications } from '../../providers';

Expand Down Expand Up @@ -93,51 +92,31 @@ const Users = () => {
}
);

const disableUserMutation = useMutation(
async (userId: string) => {
const user = listUsersQuery.data.find((user: User) => {
const disableEnableUserMutation = useMutation(
async ({ userId, disable }: { userId: string; disable: boolean }) => {
const user = listUsersQuery.data?.find((user: User) => {
return user.id === userId;
});

const updatedUser = {
emailAddress: user.email_address || '',
principal: user.principal_name || '',
firstName: user.first_name || '',
lastName: user.last_name || '',
SSOProviderId: user.sso_provider_id?.toString() || '',
roles: user.roles?.map((role: any) => role.id) || [],
is_disabled: true,
};
return apiClient.updateUser(selectedUserId!, updatedUser);
},
{
onSuccess: () => {
addNotification('User disabled successfully!', 'disableUserSuccess');
listUsersQuery.refetch();
},
}
);
if (!user) {
return;
}

const enableUserMutation = useMutation(
async (userId: string) => {
const user = listUsersQuery.data.find((user: User) => {
return user.id === userId;
});

const updatedUser = {
const updatedUser: UpdateUserRequest = {
emailAddress: user.email_address || '',
principal: user.principal_name || '',
firstName: user.first_name || '',
lastName: user.last_name || '',
SSOProviderId: user.sso_provider_id?.toString() || '',
...(user.sso_provider_id && { SSOProviderId: user.sso_provider_id }),
roles: user.roles?.map((role: any) => role.id) || [],
is_disabled: false,
is_disabled: disable,
};

return apiClient.updateUser(selectedUserId!, updatedUser);
},
{
onSuccess: () => {
addNotification('User enabled successfully!', 'enableUserSuccess');
onSuccess: (_, { disable }) => {
addNotification(`User ${disable ? 'disabled' : 'enabled'} successfully!`, 'disableEnableUserSuccess');
listUsersQuery.refetch();
},
}
Expand Down Expand Up @@ -301,7 +280,7 @@ const Users = () => {
title={'Enable User'}
onClose={(response) => {
if (response) {
enableUserMutation.mutate(selectedUserId!);
disableEnableUserMutation.mutate({ userId: selectedUserId!, disable: false });
}
toggleEnableUserDialog();
}}
Expand All @@ -312,7 +291,7 @@ const Users = () => {
title={'Disable User'}
onClose={(response) => {
if (response) {
disableUserMutation.mutate(selectedUserId!);
disableEnableUserMutation.mutate({ userId: selectedUserId!, disable: true });
}
toggleDisableUserDialog();
}}
Expand Down
3 changes: 2 additions & 1 deletion packages/javascript/js-client-library/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ class BHEAPIClient {
deleteUserToken = (tokenId: string, options?: types.RequestOptions) =>
this.baseClient.delete(`/api/v2/tokens/${tokenId}`, options);

listUsers = (options?: types.RequestOptions) => this.baseClient.get('/api/v2/bloodhound-users', options);
listUsers = (options?: types.RequestOptions) =>
this.baseClient.get<types.ListUsersResponse>('/api/v2/bloodhound-users', options);

getUser = (userId: string, options?: types.RequestOptions) =>
this.baseClient.get(`/api/v2/bloodhound-users/${userId}`, options);
Expand Down
30 changes: 30 additions & 0 deletions packages/javascript/js-client-library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,36 @@ export interface ListSSOProvidersResponse {
data: SSOProvider[];
}

export interface User {
id: string;
sso_provider_id: number | null;
AuthSecret: any;
roles: Role[];
first_name: string | null;
last_name: string | null;
email_address: string | null;
principal_name: string;
last_login: string;
}

interface Permission {
id: number;
name: string;
authority: string;
}

interface Role {
name: string;
description: string;
permissions: Permission[];
}

export interface ListUsersResponse {
data: {
users: User[];
};
}

export interface LoginRequest {
login_method: string;
secret: string;
Expand Down

0 comments on commit d59c2b6

Please sign in to comment.