Permissions

Learn about applying user permissions to different parts of your customizations.

As explained in OAuth Scopes and user permissions, each customization has a unique pair of default user permissions: "view" and "manage." Additionally, you can use more granular permission groups to cover strict business requirements.

When developing a customization, you might want to enforce these user permissions in some parts of your customization. For example, performing certain actions like creating, updating, or deleting a resource should only be possible if the user has the "manage" permission.

Defining constants

To avoid defining permissions manually, we recommend using one of the following helper functions inside your constants.js file:

  • For Custom Applications: entryPointUriPathToPermissionKeys
  • For Custom Views: resolveCustomViewResourceAccesses

For Custom Applications

For Custom Views


The following example shows the use of the entryPointUriPathToPermissionKeys function:


constants.jsJavaScript
import { entryPointUriPathToPermissionKeys } from '@commercetools-frontend/application-shell/ssr';
export const entryPointUriPath = 'channels';
export const PERMISSIONS = entryPointUriPathToPermissionKeys(entryPointUriPath);

The `PERMISSIONS` variable contains a `View` and `Manage` properties, with the values being the computed values based on the `entryPointUriPath`:
  • PERMISSIONS.View: maps to ViewChannels.
  • PERMISSIONS.Manage: maps to ManageChannels.

You can then use the PERMISSIONS variable to reference the permission in the application code.


When using additional permission groups (available from version 21.21.0), you must also provide the unique group to the entryPointUriPathToPermissionKeys function to generate the correct permission keys.


The group names can be exported and referenced also in the Custom Application config file.
constants.jsJavaScript
import { entryPointUriPathToPermissionKeys } from '@commercetools-frontend/application-shell/ssr';
export const entryPointUriPath = 'channels';
export const groupNames = {
delivery: 'delivery',
promotion: 'promotion',
};
export const PERMISSIONS = entryPointUriPathToPermissionKeys(
entryPointUriPath,
['delivery', 'promotion']
);

In this scenario, the PERMISSIONS variable contains a View, Manage, ViewDelivery, ManageDelivery, ViewPromotion and ManagePromotion properties, with the values being the computed values based on the entryPointUriPath and the provided permission group names:


  • PERMISSIONS.View: maps to ViewChannels.
  • PERMISSIONS.Manage: maps to ManageChannels.
  • PERMISSIONS.ViewDelivery: maps to ViewChannelsDelivery.
  • PERMISSIONS.ManageDelivery: maps to ManageChannelsDelivery.
  • PERMISSIONS.ViewPromotion: maps to ViewChannelsPromotion.
  • PERMISSIONS.ManagePromotion: maps to ManageChannelsPromotion.

The following example shows the use of the resolveCustomViewResourceAccesses function:


constants.jsJavaScript
import { resolveCustomViewResourceAccesses } from '@commercetools-frontend/application-config';
export const PERMISSIONS = resolveCustomViewResourceAccesses();

The PERMISSIONS variable contains a View and Manage properties:

  • PERMISSIONS.View: maps to View.
  • PERMISSIONS.Manage: maps to Manage.

You can then use the PERMISSIONS variable to reference the permission in the application code.


When using additional permission groups, you must also provide the unique group to the resolveCustomViewResourceAccesses function to generate the correct permission keys.


The group names can be exported and referenced also in the Custom View config file.


constants.jsJavaScript
import { resolveCustomViewResourceAccesses } from '@commercetools-frontend/application-config';
export const PERMISSIONS = resolveCustomViewResourceAccesses([
'delivery',
'promotion',
]);

In this scenario, the PERMISSIONS variable contains View, Manage, ViewDelivery, ManageDelivery, ViewPromotion and ManagePromotion properties, with the values being the computed values based on the provided permission group names.

Applying user permissions

Customizations let you check and evaluate if certain user permissions are assigned or not, making it possible to determine whether to render something or not, or to turn off some UI functionalities.

In routes

If certain routes should not be accessible without proper user permissions, you can render the route conditionally based on the evaluated permission.

To do so, you can use the useIsAuthorized hook:

routes.jsJavaScript
import { Switch, Route, useRouteMatch } from 'react-router-dom';
import { useIsAuthorized } from '@commercetools-frontend/permissions';
import { PageUnauthorized } from '@commercetools-frontend/application-components';
import { PERMISSIONS } from './constants';
import ChannelsCreate from './components/channels-create';
import ChannelsList from './components/channels-list';
const CustomizationRoutes = () => {
const match = useRouteMatch();
const canManage = useIsAuthorized({
demandedPermissions: [PERMISSIONS.Manage],
});
return (
<Switch>
<Route path={`${match.path}/new`}>
{canManage ? (
<ChannelsCreate />
) : (
<PageUnauthorized />
)}
</Route>
<Route>
<ChannelsList>
</Route>
</Switch>
);
};

In components

You can evaluate user permissions in your React components, for example to deactivate a button.

import { useIsAuthorized } from '@commercetools-frontend/permissions';
import PrimaryButton from '@commercetools-uikit/primary-button';
import { PERMISSIONS } from '../constants';
const MyComponent = () => {
const canManage = useIsAuthorized({
demandedPermissions: [PERMISSIONS.ManagePromotion],
});
return (
<div>
<PrimaryButton label="Create promotion" isDisabled={!canManage} />
</div>
);
};