Skip to content

Commit

Permalink
refactor: sw-2861 getUser, Permissions legacy chrome methods (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera authored Aug 28, 2024
1 parent 7ab85be commit 0402ec4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/components/authentication/authenticationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ const useGetAuthorization = ({
useSelectorsResponse: useAliasSelectorsResponse = storeHooks.reactRedux.useSelectorsResponse
} = {}) => {
const dispatch = useAliasDispatch();
const { updateDocumentTitle = helpers.noop, hideGlobalFilter = helpers.noop } = useAliasChrome();
const {
auth = { getUser: helpers.noop },
getUserPermissions = helpers.noop,
hideGlobalFilter = helpers.noop,
updateDocumentTitle = helpers.noop
} = useAliasChrome();
const { data, error, fulfilled, pending, responses } = useAliasSelectorsResponse([
{ id: 'auth', selector: ({ app }) => app?.auth },
{ id: 'locale', selector: ({ app }) => app?.locale },
Expand All @@ -56,7 +61,7 @@ const useGetAuthorization = ({
]);

useMount(async () => {
await dispatch(authorizeUser());
await dispatch(authorizeUser({ getUser: auth?.getUser, getUserPermissions }));
updateDocumentTitle(appName);
hideGlobalFilter();
});
Expand Down
12 changes: 9 additions & 3 deletions src/redux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Platform service wrappers for dispatch, state update.
* [~addNotification(data)](#Actions.module_PlatformActions..addNotification) ⇒ <code>\*</code>
* [~removeNotification(id)](#Actions.module_PlatformActions..removeNotification) ⇒ <code>\*</code>
* [~clearNotifications()](#Actions.module_PlatformActions..clearNotifications) ⇒ <code>\*</code>
* [~authorizeUser(appName)](#Actions.module_PlatformActions..authorizeUser) ⇒ <code>function</code>
* [~authorizeUser(params)](#Actions.module_PlatformActions..authorizeUser) ⇒ <code>function</code>
* [~getExistingExports(existingExports, notifications)](#Actions.module_PlatformActions..getExistingExports) ⇒ <code>function</code>
* [~deleteExistingExports(existingExports, notifications)](#Actions.module_PlatformActions..deleteExistingExports) ⇒ <code>function</code>
* [~getExistingExportsStatus(notifications)](#Actions.module_PlatformActions..getExistingExportsStatus) ⇒ <code>function</code>
Expand Down Expand Up @@ -115,7 +115,7 @@ Clear all platform plugin toast notifications.
**Kind**: inner method of [<code>PlatformActions</code>](#Actions.module_PlatformActions)
<a name="Actions.module_PlatformActions..authorizeUser"></a>

### PlatformActions~authorizeUser(appName) ⇒ <code>function</code>
### PlatformActions~authorizeUser(params) ⇒ <code>function</code>
Get an emulated and combined API response from the platforms "getUser" and "getUserPermissions" global methods.

**Kind**: inner method of [<code>PlatformActions</code>](#Actions.module_PlatformActions)
Expand All @@ -127,7 +127,13 @@ Get an emulated and combined API response from the platforms "getUser" and "getU
</thead>
<tbody>
<tr>
<td>appName</td><td><code>string</code> | <code>Array</code></td>
<td>params</td><td><code>object</code></td>
</tr><tr>
<td>params.appName</td><td><code>string</code> | <code>Array</code></td>
</tr><tr>
<td>params.getUser</td><td><code>function</code></td>
</tr><tr>
<td>params.getUserPermissions</td><td><code>function</code></td>
</tr> </tbody>
</table>

Expand Down
20 changes: 14 additions & 6 deletions src/redux/actions/platformActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ const clearNotifications = () => dispatch => dispatch(RcsClearNotifications());
/**
* Get an emulated and combined API response from the platforms "getUser" and "getUserPermissions" global methods.
*
* @param {string|Array} appName
* @param {object} params
* @param {string|Array} params.appName
* @param {Function} params.getUser
* @param {Function} params.getUserPermissions
* @returns {Function}
*/
const authorizeUser = appName => dispatch =>
dispatch({
type: platformTypes.PLATFORM_USER_AUTH,
payload: Promise.all([platformServices.getUser(), platformServices.getUserPermissions(appName)])
});
const authorizeUser =
({ appName, getUser, getUserPermissions } = {}) =>
dispatch =>
dispatch({
type: platformTypes.PLATFORM_USER_AUTH,
payload: Promise.all([
platformServices.getUser({ getUser }),
platformServices.getUserPermissions(appName, { getUserPermissions })
])
});

/**
* Get all existing exports, if pending poll, and when complete download. Includes toast notifications.
Expand Down
4 changes: 2 additions & 2 deletions src/services/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { authInterceptor } from '@redhat-cloud-services/frontend-components-utilities/interceptors';
import { axiosServiceCall } from './common/serviceConfig';
import { platformServices } from './platform/platformServices';

/**
* @namespace Services
Expand Down Expand Up @@ -34,7 +34,7 @@ const serviceConfig = (passedConfig = {}) => ({
* @returns {Promise<*>}
*/
const serviceCall = async config => {
await platformServices.getUser();
await authInterceptor();
return axiosServiceCall(serviceConfig(config));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports[`PlatformServices should export a specific properties, methods and clas
exports[`PlatformServices should return a failed getUser: failed authorized user 1`] = `
{
"data": undefined,
"message": "{ getUser } = insights.chrome.auth, insights.chrome.auth.getUser is not a function",
"message": "{ getUser } = insights.chrome.auth, aliasGetUser is not a function",
"status": 418,
"statusText": undefined,
}
Expand All @@ -24,7 +24,7 @@ exports[`PlatformServices should return a failed getUser: failed authorized user
exports[`PlatformServices should return a failed getUserPermissions: failed user permissions 1`] = `
{
"data": undefined,
"message": "{ getUserPermissions } = insights.chrome, insights.chrome.getUserPermissions is not a function",
"message": "{ getUserPermissions } = insights.chrome, aliasGetUserPermissions is not a function",
"status": 418,
"statusText": undefined,
}
Expand Down
18 changes: 12 additions & 6 deletions src/services/platform/platformServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import {
* @returns {Promise<*>}
*/
const getUser = async (options = {}) => {
const { schema = [platformSchemas.user], transform = [platformTransformers.user] } = options;
const { insights } = window;
const {
schema = [platformSchemas.user],
transform = [platformTransformers.user],
getUser: aliasGetUser = window?.insights?.chrome?.auth?.getUser
} = options;
return axiosServiceCall({
url: async () => {
try {
Expand All @@ -39,7 +42,7 @@ const getUser = async (options = {}) => {
],
process.env.REACT_APP_DEBUG_ORG_ADMIN === 'true'
)) ||
(await insights.chrome.auth.getUser())
(await aliasGetUser())
);
} catch (e) {
throw new Error(`{ getUser } = insights.chrome.auth, ${e.message}`);
Expand All @@ -58,9 +61,12 @@ const getUser = async (options = {}) => {
* @returns {Promise<*>}
*/
const getUserPermissions = (appName = Object.keys(rbacConfig), options = {}) => {
const { schema = [platformSchemas.permissions], transform = [platformTransformers.permissions] } = options;
const {
schema = [platformSchemas.permissions],
transform = [platformTransformers.permissions],
getUserPermissions: aliasGetUserPermissions = window?.insights?.chrome?.getUserPermissions
} = options;
const updatedAppName = (Array.isArray(appName) && appName) || [appName];
const { insights } = window;
const platformMethod = name =>
(helpers.DEV_MODE && [
{
Expand All @@ -70,7 +76,7 @@ const getUserPermissions = (appName = Object.keys(rbacConfig), options = {}) =>
[USER_PERMISSION_TYPES.PERMISSION]: process.env.REACT_APP_DEBUG_PERMISSION_APP_TWO
}
]) ||
insights.chrome.getUserPermissions(name);
aliasGetUserPermissions(name);

return axiosServiceCall({
url: async () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/dist.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,8 @@ exports[`Build distribution should match a specific file output 1`] = `
"./dist/js/2438*txt",
"./dist/js/2471*js",
"./dist/js/2531*js",
"./dist/js/254*js",
"./dist/js/254*txt",
"./dist/js/2745*js",
"./dist/js/2871*js",
"./dist/js/2901*js",
Expand Down Expand Up @@ -811,8 +813,6 @@ exports[`Build distribution should match a specific file output 1`] = `
"./dist/js/4869*js",
"./dist/js/4912*js",
"./dist/js/4927*js",
"./dist/js/496*js",
"./dist/js/496*txt",
"./dist/js/4990*js",
"./dist/js/5093*js",
"./dist/js/5125*js",
Expand Down Expand Up @@ -906,6 +906,7 @@ exports[`Build distribution should match a specific file output 1`] = `
"./dist/sourcemaps/2438*map",
"./dist/sourcemaps/2471*map",
"./dist/sourcemaps/2531*map",
"./dist/sourcemaps/254*map",
"./dist/sourcemaps/2745*map",
"./dist/sourcemaps/2871*map",
"./dist/sourcemaps/2901*map",
Expand Down Expand Up @@ -938,7 +939,6 @@ exports[`Build distribution should match a specific file output 1`] = `
"./dist/sourcemaps/4869*map",
"./dist/sourcemaps/4912*map",
"./dist/sourcemaps/4927*map",
"./dist/sourcemaps/496*map",
"./dist/sourcemaps/4990*map",
"./dist/sourcemaps/5093*map",
"./dist/sourcemaps/5125*map",
Expand Down

0 comments on commit 0402ec4

Please sign in to comment.