-
Notifications
You must be signed in to change notification settings - Fork 13
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
Allow widget visibility functions to be used #69
Allow widget visibility functions to be used #69
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how you cleaned the previous isOrg attempt! Two adjustments are needed to fully utilize all of the visibility functions.
src/Routes/Default/Default.tsx
Outdated
for (const key of Object.keys(mapping)) { | ||
const widgetConfig = mapping[key].config; | ||
if (widgetConfig && widgetConfig.permissions && !(await checkPermissions(widgetConfig.permissions as WidgetPermission[]))) { | ||
delete mapping[key]; // remove widget from mapping if user does not have permissions for it | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use for with nested for. It's a bit more elegant with reduce
function, moreover the checkPermissions
function returns Promise and Boolean(Promise) == true
. We'd not really filter out the values.
Object.entries(mapping).reduce(async (acc, [key, value]) => {
// calculate visibility
if (await isVisible) {
acc[key] = value;
}
return acc;
}, {});
src/Routes/Default/Default.tsx
Outdated
console.log('Checking permissions: ', permissions); | ||
for (const permission of permissions) { | ||
if (permission.method === 'permissions' && permission.args && !(await visibilityFunctions.hasPermissions(permission.args))) { | ||
return false; | ||
} | ||
|
||
if (permission.method === 'loosePermissions' && permission.args && !(await visibilityFunctions.loosePermissions(permission.args))) { | ||
return false; | ||
} | ||
|
||
if (permission.method === 'isOrgAdmin' && !(await visibilityFunctions.isOrgAdmin())) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This for loop only allows 3 methods. We want all of those available in chrome. This PR exports the type RedHatInsights/frontend-components#1999 Also, it might be better to use every
, without it we'd only check for one permission.
const resolved = Promise.all(permissions.map(async ({ method, args }) => (method && await (visibilityFunctions[method] as any))(...(args || [])))).every(Boolean)
@karelhala This is working for
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit
src/Routes/Default/Default.tsx
Outdated
const checkPermissions = async (permissions: WidgetPermission[]): Promise<boolean> => { | ||
const permissionResults = await Promise.all( | ||
permissions.map(async (permission) => { | ||
const { method, args } = permission; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to wrap the inner map function with try ... catch
and return false from the catch branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! Added this in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking really good! As pointed out by @Hyperkid123 we might want to wrap the inner part of the map function with try ... catch
Description
Check the permissions defined in the backend widget configuration using chrome's
visibilityFunctions
. Remove widgets from the layout and widget drawer that the user does not have permissions to use.RHCLOUD-32312
Checklist ☑️