Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App Search] Add roleHasScopedEngines helper + small roles/ cleanup #94038

Merged
merged 2 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Account } from '../../types';

import { RoleTypes, AbilityTypes, Role } from './types';

/**
* Transforms the `role` data we receive from the Enterprise Search
* server into a more convenient format for front-end use
*/
export const getRoleAbilities = (role: Account['role']): Role => {
Comment on lines +13 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fn is the exact same as before, I just moved the type definitions out into types.ts. The types are also the same as before

// Role ability function helpers
const myRole = {
can: (action: AbilityTypes, subject: string): boolean => {
return (
role?.ability?.manage?.includes(subject) ||
(Array.isArray(role.ability[action]) && role.ability[action].includes(subject))
);
},
};

// Clone top-level role props, and move some props out of `ability` and into the top-level for convenience
const topLevelProps = {
id: role.id,
roleType: role.roleType as RoleTypes,
availableRoleTypes: role.ability.availableRoleTypes as RoleTypes[],
credentialTypes: role.ability.credentialTypes,
};

// Ability shorthands (also in top level of role obj for convenience)
// Example usage: `const { myRole: { canViewSettings } } = useValues(AppLogic);`
const abilities = {
canAccessAllEngines: role.ability.accessAllEngines,
canViewMetaEngines: myRole.can('view', 'account_meta_engines'),
canViewAccountCredentials: myRole.can('view', 'account_credentials'),
canViewEngineAnalytics: myRole.can('view', 'engine_analytics'),
canViewEngineApiLogs: myRole.can('view', 'engine_api_logs'),
canViewEngineCrawler: myRole.can('view', 'engine_crawler'),
canViewEngineCredentials: myRole.can('view', 'engine_credentials'),
canViewEngineDocuments: myRole.can('view', 'engine_documents'),
canViewEngineSchema: myRole.can('view', 'engine_schema'),
canViewEngineQueryTester: myRole.can('view', 'engine_query_tester'),
canViewMetaEngineSourceEngines: myRole.can('view', 'meta_engine_source_engines'),
canViewSettings: myRole.can('view', 'account_settings'),
canViewRoleMappings: myRole.can('view', 'role_mappings'),
canManageEngines: myRole.can('manage', 'account_engines'),
canManageMetaEngines: myRole.can('manage', 'account_meta_engines'),
canManageLogSettings: myRole.can('manage', 'account_log_settings'),
canManageSettings: myRole.can('manage', 'account_settings'),
canManageEngineCrawler: myRole.can('manage', 'engine_crawler'),
canManageEngineDocuments: myRole.can('manage', 'engine_documents'),
canManageEngineSynonyms: myRole.can('manage', 'engine_synonyms'),
canManageEngineCredentials: myRole.can('manage', 'engine_credentials'),
canManageEngineCurations: myRole.can('manage', 'engine_curations'),
canManageEngineRelevanceTuning: myRole.can('manage', 'engine_relevance_tuning'),
canManageEngineResultSettings: myRole.can('manage', 'engine_result_settings'),
canManageEngineSchema: myRole.can('manage', 'engine_schema'),
canManageEngineSearchUi: myRole.can('manage', 'engine_reference_ui'),
canManageMetaEngineSourceEngines: myRole.can('manage', 'meta_engine_source_engines'),
};

return Object.assign(myRole, topLevelProps, abilities);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { roleHasScopedEngines } from './';

describe('roleHasScopedEngines()', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not make this update for this PR, it's not worth running CI. Just noting that we usually don't add parens to the end of function names in describes.

Suggested change
describe('roleHasScopedEngines()', () => {
describe('roleHasScopedEngines', () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha thanks Jason, I was totally about to smash commit suggestion before seeing the do not (italics totally worked). This is an accidental copypaste carry over from

  • I think I originally added the () because it's a function helper within a 'class'/obj that has static vars mixed in with fn's 🤔 not sure if the () is helpful in those scenarios - agreed they're not helpful for utility files where everything is a function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottybollinger if you're down, feel free to make Jason's suggested copy change in a future role mappings PR!

it('returns false for owner and admin roles', () => {
expect(roleHasScopedEngines('owner')).toEqual(false);
expect(roleHasScopedEngines('admin')).toEqual(false);
});

it('returns true for dev, editor, and analyst roles', () => {
expect(roleHasScopedEngines('dev')).toEqual(true);
expect(roleHasScopedEngines('editor')).toEqual(true);
expect(roleHasScopedEngines('analyst')).toEqual(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RoleTypes } from './types';

/**
* Small utility helper for determining if a given role can have scoped engines
*/
export const roleHasScopedEngines = (roleType: RoleTypes): boolean => {
const unscopedRoles = ['dev', 'editor', 'analyst'];
return unscopedRoles.includes(roleType);
};
Comment on lines +13 to +16
Copy link
Contributor Author

@cee-chen cee-chen Mar 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main new added utility/functionality (ported over from https://github.com/elastic/ent-search/blob/master/app/javascript/app_search/classes.ts#L10-L13)

Original file line number Diff line number Diff line change
Expand Up @@ -5,111 +5,6 @@
* 2.0.
*/

import { RoleMapping } from '../../../shared/types';
import { Engine } from '../../components/engine/types';
import { Account } from '../../types';

export type RoleTypes = 'owner' | 'admin' | 'dev' | 'editor' | 'analyst';
export type AbilityTypes = 'manage' | 'edit' | 'view';

export interface Role {
id: string;
roleType: RoleTypes;
availableRoleTypes: RoleTypes[];
credentialTypes: string[];
canAccessAllEngines: boolean;
can(action: AbilityTypes, subject: string): boolean;
canViewMetaEngines: boolean;
canViewAccountCredentials: boolean;
canViewEngineAnalytics: boolean;
canViewEngineApiLogs: boolean;
canViewEngineCrawler: boolean;
canViewEngineCredentials: boolean;
canViewEngineDocuments: boolean;
canViewEngineSchema: boolean;
canViewEngineQueryTester: boolean;
canViewMetaEngineSourceEngines: boolean;
canViewSettings: boolean;
canViewRoleMappings: boolean;
canManageEngines: boolean;
canManageMetaEngines: boolean;
canManageLogSettings: boolean;
canManageSettings: boolean;
canManageEngineCrawler: boolean;
canManageEngineDocuments: boolean;
canManageEngineSynonyms: boolean;
canManageEngineCredentials: boolean;
canManageEngineCurations: boolean;
canManageEngineRelevanceTuning: boolean;
canManageEngineResultSettings: boolean;
canManageEngineSchema: boolean;
canManageEngineSearchUi: boolean;
canManageMetaEngineSourceEngines: boolean;
}

/**
* Transforms the `role` data we receive from the Enterprise Search
* server into a more convenient format for front-end use
*/
export const getRoleAbilities = (role: Account['role']): Role => {
// Role ability function helpers
const myRole = {
can: (action: AbilityTypes, subject: string): boolean => {
return (
role?.ability?.manage?.includes(subject) ||
(Array.isArray(role.ability[action]) && role.ability[action].includes(subject))
);
},
// TODO: canHaveScopedEngines fn
};

// Clone top-level role props, and move some props out of `ability` and into the top-level for convenience
const topLevelProps = {
id: role.id,
roleType: role.roleType as RoleTypes,
availableRoleTypes: role.ability.availableRoleTypes as RoleTypes[],
credentialTypes: role.ability.credentialTypes,
};

// Ability shorthands (also in top level of role obj for convenience)
// Example usage: `const { myRole: { canViewSettings } } = useValues(AppLogic);`
const abilities = {
canAccessAllEngines: role.ability.accessAllEngines,
canViewMetaEngines: myRole.can('view', 'account_meta_engines'),
canViewAccountCredentials: myRole.can('view', 'account_credentials'),
canViewEngineAnalytics: myRole.can('view', 'engine_analytics'),
canViewEngineApiLogs: myRole.can('view', 'engine_api_logs'),
canViewEngineCrawler: myRole.can('view', 'engine_crawler'),
canViewEngineCredentials: myRole.can('view', 'engine_credentials'),
canViewEngineDocuments: myRole.can('view', 'engine_documents'),
canViewEngineSchema: myRole.can('view', 'engine_schema'),
canViewEngineQueryTester: myRole.can('view', 'engine_query_tester'),
canViewMetaEngineSourceEngines: myRole.can('view', 'meta_engine_source_engines'),
canViewSettings: myRole.can('view', 'account_settings'),
canViewRoleMappings: myRole.can('view', 'role_mappings'),
canManageEngines: myRole.can('manage', 'account_engines'),
canManageMetaEngines: myRole.can('manage', 'account_meta_engines'),
canManageLogSettings: myRole.can('manage', 'account_log_settings'),
canManageSettings: myRole.can('manage', 'account_settings'),
canManageEngineCrawler: myRole.can('manage', 'engine_crawler'),
canManageEngineDocuments: myRole.can('manage', 'engine_documents'),
canManageEngineSynonyms: myRole.can('manage', 'engine_synonyms'),
canManageEngineCredentials: myRole.can('manage', 'engine_credentials'),
canManageEngineCurations: myRole.can('manage', 'engine_curations'),
canManageEngineRelevanceTuning: myRole.can('manage', 'engine_relevance_tuning'),
canManageEngineResultSettings: myRole.can('manage', 'engine_result_settings'),
canManageEngineSchema: myRole.can('manage', 'engine_schema'),
canManageEngineSearchUi: myRole.can('manage', 'engine_reference_ui'),
canManageMetaEngineSourceEngines: myRole.can('manage', 'meta_engine_source_engines'),
};

return Object.assign(myRole, topLevelProps, abilities);
};

export interface ASRoleMapping extends RoleMapping {
accessAllEngines: boolean;
engines: Engine[];
toolTip?: {
content: string;
};
}
export * from './types';
export { getRoleAbilities } from './get_role_abilities';
export { roleHasScopedEngines } from './has_scoped_engines';
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RoleMapping } from '../../../shared/types';
import { Engine } from '../../components/engine/types';

export type RoleTypes = 'owner' | 'admin' | 'dev' | 'editor' | 'analyst';
export type AbilityTypes = 'manage' | 'edit' | 'view';

export interface Role {
id: string;
roleType: RoleTypes;
availableRoleTypes: RoleTypes[];
credentialTypes: string[];
canAccessAllEngines: boolean;
can(action: AbilityTypes, subject: string): boolean;
canViewMetaEngines: boolean;
canViewAccountCredentials: boolean;
canViewEngineAnalytics: boolean;
canViewEngineApiLogs: boolean;
canViewEngineCrawler: boolean;
canViewEngineCredentials: boolean;
canViewEngineDocuments: boolean;
canViewEngineSchema: boolean;
canViewEngineQueryTester: boolean;
canViewMetaEngineSourceEngines: boolean;
canViewSettings: boolean;
canViewRoleMappings: boolean;
canManageEngines: boolean;
canManageMetaEngines: boolean;
canManageLogSettings: boolean;
canManageSettings: boolean;
canManageEngineCrawler: boolean;
canManageEngineDocuments: boolean;
canManageEngineSynonyms: boolean;
canManageEngineCredentials: boolean;
canManageEngineCurations: boolean;
canManageEngineRelevanceTuning: boolean;
canManageEngineResultSettings: boolean;
canManageEngineSchema: boolean;
canManageEngineSearchUi: boolean;
canManageMetaEngineSourceEngines: boolean;
}

export interface ASRoleMapping extends RoleMapping {
accessAllEngines: boolean;
engines: Engine[];
toolTip?: {
content: string;
};
}