Skip to content

Commit

Permalink
[Spaces] - Update Roles screen to use public API (#21031)
Browse files Browse the repository at this point in the history
This PR updates the new Role Management screen to use the public role API introduced as part of #20732.

Additionally, this updates the breadcrumb nav for Spaces and Role management screens to be consistent with the nav introduced in #20739.

There are a couple of visual glitches in this PR which are a result of current defects on master, so they can be safely ignored for the time being.
  • Loading branch information
legrego authored Jul 20, 2018
1 parent 6fa01f1 commit b6d9ec2
Show file tree
Hide file tree
Showing 28 changed files with 213 additions and 339 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/security/public/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import chrome from 'ui/chrome';

const usersUrl = chrome.addBasePath('/api/security/v1/users');
const rolesUrl = chrome.addBasePath('/api/security/v1/roles');
const rolesUrl = chrome.addBasePath('/api/security/role');

export const createApiClient = (httpClient) => {
return {
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/security/public/objects/lib/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/
import chrome from 'ui/chrome';
import { omit } from 'lodash';

const apiBase = chrome.addBasePath(`/api/security/v1/roles`);
const apiBase = chrome.addBasePath(`/api/security/role`);

export async function saveRole($http, role) {
return await $http.post(`${apiBase}/${role.name}`, role);
const data = omit(role, 'name', 'transient_metadata', '_unrecognized_applications');
return await $http.put(`${apiBase}/${role.name}`, data);
}

export async function deleteRole($http, name) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
EuiFlexItem,
EuiButton,
} from '@elastic/eui';
import { PageHeader } from './page_header';
import { saveRole, deleteRole } from '../../../../objects';
import { isReservedRole } from '../../../../lib/role';
import { RoleValidator } from '../lib/validate_role';
Expand All @@ -36,7 +35,6 @@ export class EditRolePage extends Component {
indexPatterns: PropTypes.array.isRequired,
httpClient: PropTypes.func.isRequired,
rbacEnabled: PropTypes.bool.isRequired,
rbacApplication: PropTypes.string,
allowDocumentLevelSecurity: PropTypes.bool.isRequired,
allowFieldLevelSecurity: PropTypes.bool.isRequired,
kibanaAppPrivileges: PropTypes.array.isRequired,
Expand All @@ -55,7 +53,6 @@ export class EditRolePage extends Component {
render() {
return (
<EuiPage className="editRolePage">
<PageHeader breadcrumbs={this.props.breadcrumbs} />
<EuiForm {...this.state.formError}>
{this.getFormTitle()}

Expand Down Expand Up @@ -172,7 +169,6 @@ export class EditRolePage extends Component {
<EuiSpacer />
<KibanaPrivileges
kibanaAppPrivileges={this.props.kibanaAppPrivileges}
rbacApplication={this.props.rbacApplication}
role={this.state.role}
onChange={this.onRoleChange}
/>
Expand Down Expand Up @@ -237,8 +233,8 @@ export class EditRolePage extends Component {
...this.state.role
};

role.indices = role.indices.filter(i => !this.isPlaceholderPrivilege(i));
role.indices.forEach((index) => index.query || delete index.query);
role.elasticsearch.indices = role.elasticsearch.indices.filter(i => !this.isPlaceholderPrivilege(i));
role.elasticsearch.indices.forEach((index) => index.query || delete index.query);

saveRole(httpClient, role)
.then(() => {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ exports[`it renders without crashing 1`] = `
onChange={[Function]}
role={
Object {
"cluster": Array [],
"indices": Array [],
"run_as": Array [],
"elasticsearch": Object {
"cluster": Array [],
"indices": Array [],
"run_as": Array [],
},
}
}
/>
Expand Down Expand Up @@ -129,9 +131,11 @@ exports[`it renders without crashing 1`] = `
onChange={[MockFunction]}
role={
Object {
"cluster": Array [],
"indices": Array [],
"run_as": Array [],
"elasticsearch": Object {
"cluster": Array [],
"indices": Array [],
"run_as": Array [],
},
}
}
validator={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ClusterPrivileges extends Component {
label: i
}));

const selectionMap = (role.cluster || [])
const selectionMap = (role.elasticsearch.cluster || [])
.map(k => ({ [k]: true }))
.reduce((acc, o) => ({ ...acc, ...o }), {});

Expand All @@ -58,7 +58,7 @@ export class ClusterPrivileges extends Component {
};

onClusterPrivilegesChange = (privilege) => {
const { cluster } = this.props.role;
const { cluster } = this.props.role.elasticsearch;
const indexOfExistingPrivilege = cluster.indexOf(privilege);

const shouldRemove = indexOfExistingPrivilege >= 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { ClusterPrivileges } from './cluster_privileges';
import { EuiCheckboxGroup } from '@elastic/eui';

test('it renders without crashing', () => {
const wrapper = shallow(<ClusterPrivileges role={{}} onChange={jest.fn()} />);
const wrapper = shallow(<ClusterPrivileges role={{ elasticsearch: {} }} onChange={jest.fn()} />);
expect(wrapper).toMatchSnapshot();
});

test('it renders 2 checkbox groups of privileges', () => {
const wrapper = mount(<ClusterPrivileges role={{}} onChange={jest.fn()} />);
const wrapper = mount(<ClusterPrivileges role={{ elasticsearch: {} }} onChange={jest.fn()} />);
expect(wrapper.find(EuiCheckboxGroup)).toHaveLength(2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class ElasticsearchPrivileges extends Component {
<EuiComboBox
placeholder={this.props.editable ? 'Add a user...' : null}
options={this.props.runAsUsers.map(username => ({ id: username, label: username }))}
selectedOptions={this.props.role.run_as.map(u => ({ label: u }))}
selectedOptions={this.props.role.elasticsearch.run_as.map(u => ({ label: u }))}
onChange={this.onRunAsUserChange}
isDisabled={!this.props.editable}
/>
Expand Down Expand Up @@ -129,7 +129,7 @@ export class ElasticsearchPrivileges extends Component {
addIndexPrivilege = () => {
const { role } = this.props;

const newIndices = [...role.indices, {
const newIndices = [...role.elasticsearch.indices, {
names: [],
privileges: [],
field_security: {
Expand All @@ -139,14 +139,20 @@ export class ElasticsearchPrivileges extends Component {

this.props.onChange({
...this.props.role,
indices: newIndices
elasticsearch: {
...this.props.role.elasticsearch,
indices: newIndices
}
});
};

onClusterPrivilegesChange = (cluster) => {
const role = {
...this.props.role,
cluster
elasticsearch: {
...this.props.role.elasticsearch,
cluster,
},
};

this.props.onChange(role);
Expand All @@ -155,7 +161,10 @@ export class ElasticsearchPrivileges extends Component {
onRunAsUserChange = (users) => {
const role = {
...this.props.role,
run_as: users.map(u => u.label)
elasticsearch: {
...this.props.role.elasticsearch,
run_as: users.map(u => u.label),
},
};

this.props.onChange(role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { RoleValidator } from '../../lib/validate_role';
test('it renders without crashing', () => {
const props = {
role: {
cluster: [],
indices: [],
run_as: []
elasticsearch: {
cluster: [],
indices: [],
run_as: [],
},
},
editable: true,
httpClient: jest.fn(),
Expand All @@ -34,9 +36,11 @@ test('it renders without crashing', () => {
test('it renders ClusterPrivileges', () => {
const props = {
role: {
cluster: [],
indices: [],
run_as: []
elasticsearch: {
cluster: [],
indices: [],
run_as: [],
},
},
editable: true,
httpClient: jest.fn(),
Expand All @@ -54,9 +58,11 @@ test('it renders ClusterPrivileges', () => {
test('it renders IndexPrivileges', () => {
const props = {
role: {
cluster: [],
indices: [],
run_as: []
elasticsearch: {
cluster: [],
indices: [],
run_as: [],
},
},
editable: true,
httpClient: jest.fn(),
Expand All @@ -69,4 +75,4 @@ test('it renders IndexPrivileges', () => {
};
const wrapper = mount(<ElasticsearchPrivileges {...props} />);
expect(wrapper.find(IndexPrivileges)).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export class IndexPrivileges extends Component {
}

componentDidMount() {
this.loadAvailableFields(this.props.role.indices);
this.loadAvailableFields(this.props.role.elasticsearch.indices);
}

render() {
const { indices = [] } = this.props.role;
const { indices = [] } = this.props.role.elasticsearch;

const {
indexPatterns,
Expand Down Expand Up @@ -67,7 +67,7 @@ export class IndexPrivileges extends Component {
addIndexPrivilege = () => {
const { role } = this.props;

const newIndices = [...role.indices, {
const newIndices = [...role.elasticsearch.indices, {
names: [],
privileges: [],
field_security: {
Expand All @@ -77,21 +77,27 @@ export class IndexPrivileges extends Component {

this.props.onChange({
...this.props.role,
indices: newIndices
elasticsearch: {
...this.props.role.elasticsearch,
indices: newIndices,
},
});
};

onIndexPrivilegeChange = (privilegeIndex) => {
return (updatedPrivilege) => {
const { role } = this.props;
const { indices } = role;
const { indices } = role.elasticsearch;

const newIndices = [...indices];
newIndices[privilegeIndex] = updatedPrivilege;

this.props.onChange({
...this.props.role,
indices: newIndices
elasticsearch: {
...this.props.role.elasticsearch,
indices: newIndices,
},
});

this.loadAvailableFields(newIndices);
Expand All @@ -102,12 +108,15 @@ export class IndexPrivileges extends Component {
return () => {
const { role } = this.props;

const newIndices = [...role.indices];
const newIndices = [...role.elasticsearch.indices];
newIndices.splice(privilegeIndex, 1);

this.props.onChange({
...this.props.role,
indices: newIndices
elasticsearch: {
...this.props.role.elasticsearch,
indices: newIndices
},
});
};
}
Expand Down
Loading

0 comments on commit b6d9ec2

Please sign in to comment.