-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rbac): watch users and permission-policies (#1102)
- Loading branch information
Showing
6 changed files
with
125 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,80 @@ | ||
import React from 'react'; | ||
import { useAsync, useAsyncRetry } from 'react-use'; | ||
import { useAsyncRetry, useInterval } from 'react-use'; | ||
|
||
import { useApi } from '@backstage/core-plugin-api'; | ||
|
||
import { rbacApiRef } from '../api/RBACBackendClient'; | ||
import { getPermissionsData } from '../utils/rbac-utils'; | ||
|
||
export const usePermissionPolicies = (entityReference: string) => { | ||
const getErrorText = ( | ||
policies: any, | ||
permissionPolicies: any, | ||
): { name: number; message: string } | undefined => { | ||
if (!Array.isArray(policies) && (policies as Response)?.statusText) { | ||
return { | ||
name: (policies as Response).status, | ||
message: `Error fetching policies. ${(policies as Response).statusText}`, | ||
}; | ||
} else if ( | ||
!Array.isArray(permissionPolicies) && | ||
(permissionPolicies as Response)?.statusText | ||
) { | ||
return { | ||
name: (permissionPolicies as Response).status, | ||
message: `Error fetching the plugins. ${ | ||
(permissionPolicies as Response).statusText | ||
}`, | ||
}; | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const usePermissionPolicies = ( | ||
entityReference: string, | ||
pollInterval?: number, | ||
) => { | ||
const rbacApi = useApi(rbacApiRef); | ||
const { | ||
loading: policiesLoading, | ||
value: policies, | ||
retry, | ||
error, | ||
retry: policiesRetry, | ||
error: policiesError, | ||
} = useAsyncRetry(async () => { | ||
return await rbacApi.getAssociatedPolicies(entityReference); | ||
}); | ||
|
||
const { | ||
value: permissionPolicies, | ||
loading: permissionPoliciesLoading, | ||
error: permissionPoliciesError, | ||
} = useAsync(async () => { | ||
retry: permissionPoliciesRetry, | ||
} = useAsyncRetry(async () => { | ||
return await rbacApi.listPermissions(); | ||
}); | ||
|
||
const loading = policiesLoading || permissionPoliciesLoading; | ||
const loading = | ||
!permissionPoliciesError && | ||
!policiesError && | ||
!policies && | ||
!permissionPolicies; | ||
|
||
const data = React.useMemo(() => { | ||
const pp = Array.isArray(permissionPolicies) ? permissionPolicies : []; | ||
return Array.isArray(policies) ? getPermissionsData(policies, pp) : []; | ||
}, [policies, permissionPolicies]); | ||
|
||
useInterval( | ||
() => { | ||
policiesRetry(); | ||
permissionPoliciesRetry(); | ||
}, | ||
loading ? null : pollInterval || 10000, | ||
); | ||
return { | ||
loading, | ||
data, | ||
retry, | ||
error: (error as Error) || | ||
(permissionPoliciesError as Error) || { | ||
name: (policies as Response)?.status, | ||
message: `Error fetching policies. ${(policies as Response) | ||
?.statusText}`, | ||
} || { | ||
name: (permissionPolicies as Response)?.status, | ||
message: `Error fetching permission policies. ${( | ||
permissionPolicies as Response | ||
)?.statusText}`, | ||
}, | ||
retry: { policiesRetry, permissionPoliciesRetry }, | ||
error: | ||
policiesError || | ||
permissionPoliciesError || | ||
getErrorText(policies, permissionPolicies), | ||
}; | ||
}; |