Sha256: fa7869ce76b6a7122081839e976b525791af44167164cb90522cdc290a1399e4

Contents?: true

Size: 1.11 KB

Versions: 22

Compression:

Stored size: 1.11 KB

Contents

import { translate as __, sprintf } from 'foremanReact/common/I18n';

export const permissionCheck = (user, permissionsRequired) => {
  if (permissionsRequired.length === 0) {
    return { allowed: true };
  }

  if (!user) {
    throw new Error(
      'No user data when loading the page - cannot determine if current user is allowed to view the page.'
    );
  }

  if (user.admin) {
    return { allowed: true };
  }

  const permList = permissionsRequired.reduce((memo, item) => {
    const found = user.permissions.nodes.find(
      permission => permission.name === item
    );
    memo.push({ name: item, present: !!found });
    return memo;
  }, []);

  if (permList.reduce((memo, item) => memo && item.present, true)) {
    return { allowed: true, permissions: permList };
  }

  return { allowed: false, permissions: permList };
};

export const permissionDeniedMsg = permissions => {
  let msg = __('You are not authorized to view the page. ');
  if (permissions?.length > 0) {
    msg += sprintf(
      __('Request the following permissions from administrator: %s.'),
      permissions.join(', ')
    );
  }
  return msg;
};

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
foreman_openscap-10.1.0 webpack/helpers/permissionsHelper.js
foreman_openscap-10.0.3 webpack/helpers/permissionsHelper.js
foreman_openscap-10.0.2 webpack/helpers/permissionsHelper.js
foreman_openscap-10.0.1 webpack/helpers/permissionsHelper.js
foreman_openscap-10.0.0 webpack/helpers/permissionsHelper.js
foreman_openscap-9.0.4 webpack/helpers/permissionsHelper.js
foreman_openscap-9.0.3 webpack/helpers/permissionsHelper.js
foreman_openscap-9.0.2 webpack/helpers/permissionsHelper.js
foreman_openscap-9.0.1 webpack/helpers/permissionsHelper.js
foreman_openscap-9.0.0 webpack/helpers/permissionsHelper.js
foreman_openscap-8.0.0 webpack/helpers/permissionsHelper.js
foreman_openscap-7.1.1 webpack/helpers/permissionsHelper.js
foreman_openscap-7.1.0 webpack/helpers/permissionsHelper.js
foreman_openscap-7.0.0 webpack/helpers/permissionsHelper.js
foreman_openscap-6.0.0 webpack/helpers/permissionsHelper.js
foreman_openscap-5.2.3 webpack/helpers/permissionsHelper.js
foreman_openscap-5.2.2 webpack/helpers/permissionsHelper.js
foreman_openscap-5.1.1 webpack/helpers/permissionsHelper.js
foreman_openscap-5.2.1 webpack/helpers/permissionsHelper.js
foreman_openscap-5.2.0 webpack/helpers/permissionsHelper.js