Sha256: 4ec5cb793afe8b8b1e69d0786f64c0f4ec5a7545f1cec3bfcb6e06b35ecb0f1e

Contents?: true

Size: 1.49 KB

Versions: 24

Compression:

Stored size: 1.49 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;
};

export const allowPrimaryAction = (
  emptyStateProps,
  currentUser,
  permissionsRequired
) => {
  if (!permissionCheck(currentUser, permissionsRequired).allowed) {
    return Object.keys(emptyStateProps)
      .filter(key => key !== 'action')
      .reduce((memo, key) => {
        memo[key] = emptyStateProps[key];
        return memo;
      }, {});
  }
  return emptyStateProps;
};

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
foreman_ansible-7.0.3 webpack/permissionsHelper.js
foreman_ansible-7.0.2 webpack/permissionsHelper.js
foreman_ansible-7.0.1 webpack/permissionsHelper.js
foreman_ansible-7.0.0 webpack/permissionsHelper.js