Sha256: 444093a50ebb4eda036c9a8f7a84b2c2abc93cba7f012cfef20a1702668fc265

Contents?: true

Size: 1.57 KB

Versions: 34

Compression:

Stored size: 1.57 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 ||
    user.usergroups.nodes.find(usergroup => usergroup.admin === true)
  ) {
    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

34 entries across 34 versions & 1 rubygems

Version Path
foreman_ansible-15.0.2 webpack/permissionsHelper.js
foreman_ansible-15.0.1 webpack/permissionsHelper.js
foreman_ansible-15.0.0 webpack/permissionsHelper.js
foreman_ansible-14.2.2 webpack/permissionsHelper.js
foreman_ansible-14.2.1 webpack/permissionsHelper.js
foreman_ansible-13.0.7 webpack/permissionsHelper.js
foreman_ansible-14.2.0 webpack/permissionsHelper.js
foreman_ansible-14.1.1 webpack/permissionsHelper.js
foreman_ansible-14.1.0 webpack/permissionsHelper.js
foreman_ansible-13.0.5 webpack/permissionsHelper.js
foreman_ansible-14.0.0 webpack/permissionsHelper.js
foreman_ansible-13.0.4 webpack/permissionsHelper.js
foreman_ansible-13.0.3 webpack/permissionsHelper.js
foreman_ansible-13.0.2 webpack/permissionsHelper.js
foreman_ansible-13.0.1 webpack/permissionsHelper.js
foreman_ansible-13.0.0 webpack/permissionsHelper.js
foreman_ansible-12.0.7 webpack/permissionsHelper.js
foreman_ansible-10.4.4 webpack/permissionsHelper.js
foreman_ansible-12.0.6 webpack/permissionsHelper.js
foreman_ansible-12.0.5 webpack/permissionsHelper.js