Skip to content

Commit

Permalink
Merge pull request #7050 from pavinduLakshan/fix_userstore_logic_role…
Browse files Browse the repository at this point in the history
…_assign
  • Loading branch information
pavinduLakshan authored Oct 27, 2024
2 parents 8dab4bf + a9df7da commit d47ae5c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-queens-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wso2is/admin.roles.v2": patch
---

Reuse the business logic for determining available userstores user assign view in individual roles
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const AdministratorsList: FunctionComponent<AdministratorsListProps> = (
useEffect(() => {
setSelectedUserStore(
isPrivilegedUsersInConsoleSettingsEnabled && selectedAdministratorGroup === "administrators"
? undefined
? PRIMARY_USERSTORE
: userstoresConfig?.primaryUserstoreName
);
},[ isPrivilegedUsersInConsoleSettingsEnabled, selectedAdministratorGroup ]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ const ConsoleRolesEdit: FunctionComponent<ConsoleRolesEditPropsInterface> = (
isReadOnly={ !hasRolesUpdatePermissions }
role={ roleObject }
onRoleUpdate={ onRoleUpdate }
activeUserStore={ isPrivilegedUsersInConsoleSettingsEnabled ? activeUserStore : null }
activeUserStore={
isPrivilegedUsersInConsoleSettingsEnabled && isFirstLevelOrganization()
? activeUserStore
: null
}
tabIndex={ 3 }
/>
</ResourceTab.Pane>
Expand Down
98 changes: 52 additions & 46 deletions features/admin.roles.v2/components/edit-role/edit-role-users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import Select from "@oxygen-ui/react/Select";
import TextField from "@oxygen-ui/react/TextField";
import { updateResources } from "@wso2is/admin.core.v1/api/bulk-operations";
import { getEmptyPlaceholderIllustrations } from "@wso2is/admin.core.v1/configs/ui";
import { AppState } from "@wso2is/admin.core.v1/store";
import { userstoresConfig } from "@wso2is/admin.extensions.v1/configs/userstores";
import { GroupsInterface } from "@wso2is/admin.groups.v1/models/groups";
import { RemoteUserStoreConstants } from "@wso2is/admin.remote-userstores.v1/constants/remote-user-stores";
import { useUsersList } from "@wso2is/admin.users.v1/api";
import {
PatchBulkUserDataInterface,
Expand All @@ -41,8 +39,10 @@ import {
PatchUserRemoveOpInterface,
UserBasicInterface
} from "@wso2is/admin.users.v1/models";
import { useUserStores } from "@wso2is/admin.userstores.v1/api";
import { UserStoreListItem } from "@wso2is/admin.userstores.v1/models/user-stores";
import { getAUserStore, useUserStores } from "@wso2is/admin.userstores.v1/api";
import {
UserStoreDropdownItem, UserStoreListItem, UserStorePostData, UserStoreProperty
} from "@wso2is/admin.userstores.v1/models/user-stores";
import { AlertLevels, IdentifiableComponentInterface, RolesMemberInterface } from "@wso2is/core/models";
import { addAlert } from "@wso2is/core/store";
import { EmphasizedSegment, EmptyPlaceholder, Heading, PrimaryButton } from "@wso2is/react-components";
Expand All @@ -59,7 +59,7 @@ import React, {
useState
} from "react";
import { useTranslation } from "react-i18next";
import { useDispatch, useSelector } from "react-redux";
import { useDispatch } from "react-redux";
import { Dispatch } from "redux";
import { Icon } from "semantic-ui-react";
import { AutoCompleteRenderOption } from "./edit-role-common/auto-complete-render-option";
Expand All @@ -69,8 +69,6 @@ import { RoleEditSectionsInterface } from "../../models/roles";
import { RoleManagementUtils } from "../../utils/role-management-utils";
import "./edit-role.scss";

type UserstoreDisplayItem = Omit<UserStoreListItem,"description" | "self" | "enabled">

type RoleUsersPropsInterface = IdentifiableComponentInterface & RoleEditSectionsInterface;

export const RoleUsersList: FunctionComponent<RoleUsersPropsInterface> = (
Expand All @@ -89,15 +87,12 @@ export const RoleUsersList: FunctionComponent<RoleUsersPropsInterface> = (
const { t } = useTranslation();
const dispatch: Dispatch = useDispatch();

const disabledUserstores: string[] = useSelector(
(state: AppState) => state.config.ui.features.userStores.disabledFeatures);

const [ userSearchValue, setUserSearchValue ] = useState<string>(undefined);
const [ isUserSearchLoading, setUserSearchLoading ] = useState<boolean>(false);
const [ users, setUsers ] = useState<UserBasicInterface[]>([]);
const [ isSubmitting, setIsSubmitting ] = useState<boolean>(false);
const [ activeOption, setActiveOption ] = useState<GroupsInterface|UserBasicInterface>(undefined);
const [ availableUserStores, setAvailableUserStores ] = useState<UserstoreDisplayItem[]>([]);
const [ availableUserStores, setAvailableUserStores ] = useState<UserStoreDropdownItem[]>([]);
const [ selectedUserStoreDomainName, setSelectedUserStoreDomainName ] = useState<string>();
const [ isPlaceholderVisible, setIsPlaceholderVisible ] = useState<boolean>(true);
const [ selectedUsersFromUserStore, setSelectedUsersFromUserStore ] = useState<UserBasicInterface[]>([]);
Expand All @@ -108,6 +103,44 @@ export const RoleUsersList: FunctionComponent<RoleUsersPropsInterface> = (
isLoading: isUserStoresLoading
} = useUserStores(null);

useEffect(() => {
if (userStores && !isUserStoresLoading) {
const storeOptions: UserStoreDropdownItem[] = [
{
key: -1,
text: userstoresConfig?.primaryUserstoreName,
value: userstoresConfig?.primaryUserstoreName
}
];

let storeOption: UserStoreDropdownItem = {
key: null,
text: "",
value: ""
};

userStores?.forEach((store: UserStoreListItem, index: number) => {
if (store?.name?.toUpperCase() !== userstoresConfig?.primaryUserstoreName) {
getAUserStore(store.id).then((response: UserStorePostData) => {
const isDisabled: boolean = response.properties.find(
(property: UserStoreProperty) => property.name === "Disabled")?.value === "true";

if (!isDisabled) {
storeOption = {
key: index,
text: store.name,
value: store.name
};
storeOptions.push(storeOption);
}
});
}
});

setAvailableUserStores(storeOptions);
}
}, [ userStores, isUserStoresLoading ]);

const {
data: userResponse,
isLoading: isUserListFetchRequestLoading,
Expand Down Expand Up @@ -162,34 +195,6 @@ export const RoleUsersList: FunctionComponent<RoleUsersPropsInterface> = (
setSelectedUsersFromUserStore(usersFromSelectedStore);
},[ role, selectedUserStoreDomainName ]);

useEffect(() => {
if (!userStores) {
return;
}

if (userStores) {
const availableUserStoreList: UserstoreDisplayItem[] = disabledUserstores?.includes(
RemoteUserStoreConstants.PRIMARY_USER_STORE_NAME)
? []
: [
{
id: RemoteUserStoreConstants.PRIMARY_USER_STORE_NAME,
name: t("users:userstores.userstoreOptions.primary")
}
];

setAvailableUserStores(
[
...availableUserStoreList,
...userStores.map((userStore: UserStoreListItem) => ({
id: userStore.id,
name: userStore.name
}))
]
);
}
}, [ userStores ]);

/**
* Set available to select users.
*/
Expand Down Expand Up @@ -425,13 +430,14 @@ export const RoleUsersList: FunctionComponent<RoleUsersPropsInterface> = (
>
{ isUserStoresLoading
? <p>{ t("common:loading") }</p>
: availableUserStores?.map((userstore: UserStoreListItem) =>
(<MenuItem
key={ userstore.name }
value={ userstore.id }
>
{ userstore.name }
</MenuItem>)
: availableUserStores?.map(
(userstore: UserStoreDropdownItem) =>
(<MenuItem
key={ userstore.key }
value={ userstore.value }
>
{ userstore.text }
</MenuItem>)
)
}
</Select>
Expand Down

0 comments on commit d47ae5c

Please sign in to comment.