Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix always loading RBACProvider on empty appName #1900

Merged
merged 1 commit into from
Aug 29, 2023
Merged
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
14 changes: 10 additions & 4 deletions packages/components/src/RBACProvider/RBACProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const hasAccessWithUserPermissions = (userPermissions: (Access | string)[], chec
};

export interface RBACProviderProps {
appName: string;
checkResourceDefinitions: boolean;
appName?: string | null;
checkResourceDefinitions?: boolean;
}

export const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appName, checkResourceDefinitions = false, children }) => {
const [permissionState, setPermissionState] = useState(initialPermissions);

const fetchPermissions = async () => {
const { isOrgAdmin, permissions: userPermissions } = await getRBAC(appName, true);
const { isOrgAdmin, permissions: userPermissions } = await getRBAC(appName === null ? '' : appName, true);

setPermissionState((currentPerms) => ({
...currentPerms,
Expand All @@ -38,8 +38,14 @@ export const RBACProvider: React.FunctionComponent<RBACProviderProps> = ({ appNa
};

useEffect(() => {
if (appName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would trigger the call to fetch permissions twice. It could be better to allow null value to be passed as appName and if such value request empty application.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this makes sense. Added null support

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would flip it arround, if null or string fire a fetchPermissions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karelhala, implemented

// if null or string - then fetch the permissions
if (appName !== undefined) {
fetchPermissions();
} else {
setPermissionState((currentPerms) => ({
...currentPerms,
isLoading: false,
}));
}
}, [appName]);

Expand Down