Skip to content

Commit a108eb9

Browse files
committed
Remove explicit dependency on Spaces plugin from edit role management UI.
1 parent baa580c commit a108eb9

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

x-pack/legacy/plugins/security/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ export const security = (kibana: Record<string, any>) =>
2929
.default();
3030
},
3131

32-
uiExports: {
33-
hacks: ['plugins/security/hacks/legacy'],
34-
injectDefaultVars: (server: Server) => {
35-
return { enableSpaceAwarePrivileges: server.config().get('xpack.spaces.enabled') };
36-
},
37-
},
32+
uiExports: { hacks: ['plugins/security/hacks/legacy'] },
3833

3934
async init(server: Server) {
4035
const securityPlugin = server.newPlatform.setup.plugins.security as SecurityPluginSetup;

x-pack/plugins/security/public/management/roles/edit_role/edit_role_page.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ function getProps({
163163
const { http, docLinks, notifications } = coreMock.createStart();
164164
http.get.mockImplementation(async (path: any) => {
165165
if (path === '/api/spaces/space') {
166-
return buildSpaces();
166+
if (spacesEnabled) {
167+
return buildSpaces();
168+
}
169+
170+
const notFoundError = { response: { status: 404 } };
171+
throw notFoundError;
167172
}
168173
});
169174

@@ -181,7 +186,6 @@ function getProps({
181186
notifications,
182187
docLinks: new DocumentationLinksService(docLinks),
183188
fatalErrors,
184-
spacesEnabled,
185189
uiCapabilities: buildUICapabilities(canManageSpaces),
186190
};
187191
}

x-pack/plugins/security/public/management/roles/edit_role/edit_role_page.tsx

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ interface Props {
7878
docLinks: DocumentationLinksService;
7979
http: HttpStart;
8080
license: SecurityLicense;
81-
spacesEnabled: boolean;
8281
uiCapabilities: Capabilities;
8382
notifications: NotificationsStart;
8483
fatalErrors: FatalErrorsSetup;
@@ -221,14 +220,21 @@ function useRole(
221220
return [role, setRole] as [Role | null, typeof setRole];
222221
}
223222

224-
function useSpaces(http: HttpStart, fatalErrors: FatalErrorsSetup, spacesEnabled: boolean) {
225-
const [spaces, setSpaces] = useState<Space[] | null>(null);
223+
function useSpaces(http: HttpStart, fatalErrors: FatalErrorsSetup) {
224+
const [spaces, setSpaces] = useState<{ enabled: boolean; list: Space[] } | null>(null);
226225
useEffect(() => {
227-
(spacesEnabled ? http.get('/api/spaces/space') : Promise.resolve([])).then(
228-
fetchedSpaces => setSpaces(fetchedSpaces),
229-
err => fatalErrors.add(err)
226+
http.get('/api/spaces/space').then(
227+
fetchedSpaces => setSpaces({ enabled: true, list: fetchedSpaces }),
228+
(err: IHttpFetchError) => {
229+
// Spaces plugin can be disabled and hence this endpoint can be unavailable.
230+
if (err.response?.status === 404) {
231+
setSpaces({ enabled: false, list: [] });
232+
} else {
233+
fatalErrors.add(err);
234+
}
235+
}
230236
);
231-
}, [http, fatalErrors, spacesEnabled]);
237+
}, [http, fatalErrors]);
232238

233239
return spaces;
234240
}
@@ -278,7 +284,6 @@ export const EditRolePage: FunctionComponent<Props> = ({
278284
roleName,
279285
action,
280286
fatalErrors,
281-
spacesEnabled,
282287
license,
283288
docLinks,
284289
uiCapabilities,
@@ -292,7 +297,7 @@ export const EditRolePage: FunctionComponent<Props> = ({
292297
const runAsUsers = useRunAsUsers(userAPIClient, fatalErrors);
293298
const indexPatternsTitles = useIndexPatternsTitles(indexPatterns, fatalErrors, notifications);
294299
const privileges = usePrivileges(privilegesAPIClient, fatalErrors);
295-
const spaces = useSpaces(http, fatalErrors, spacesEnabled);
300+
const spaces = useSpaces(http, fatalErrors);
296301
const features = useFeatures(getFeatures, fatalErrors);
297302
const [role, setRole] = useRole(
298303
rolesAPIClient,
@@ -432,8 +437,8 @@ export const EditRolePage: FunctionComponent<Props> = ({
432437
<EuiSpacer />
433438
<KibanaPrivilegesRegion
434439
kibanaPrivileges={new KibanaPrivileges(kibanaPrivileges, features)}
435-
spaces={spaces}
436-
spacesEnabled={spacesEnabled}
440+
spaces={spaces.list}
441+
spacesEnabled={spaces.enabled}
437442
uiCapabilities={uiCapabilities}
438443
canCustomizeSubFeaturePrivileges={license.getFeatures().allowSubFeaturePrivileges}
439444
editable={!isRoleReadOnly}
@@ -517,7 +522,7 @@ export const EditRolePage: FunctionComponent<Props> = ({
517522
setFormError(null);
518523

519524
try {
520-
await rolesAPIClient.saveRole({ role, spacesEnabled });
525+
await rolesAPIClient.saveRole({ role, spacesEnabled: spaces.enabled });
521526
} catch (error) {
522527
notifications.toasts.addDanger(get(error, 'data.message'));
523528
return;
@@ -552,7 +557,7 @@ export const EditRolePage: FunctionComponent<Props> = ({
552557
backToRoleList();
553558
};
554559

555-
const description = spacesEnabled ? (
560+
const description = spaces.enabled ? (
556561
<FormattedMessage
557562
id="xpack.security.management.editRole.setPrivilegesToKibanaSpacesDescription"
558563
defaultMessage="Set privileges on your Elasticsearch data and control access to your Kibana spaces."

x-pack/plugins/security/public/management/roles/roles_management_app.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ export const rolesManagementApp = Object.freeze({
3636
];
3737

3838
const [
39-
[
40-
{ application, docLinks, http, i18n: i18nStart, injectedMetadata, notifications },
41-
{ data, features },
42-
],
39+
[{ application, docLinks, http, i18n: i18nStart, notifications }, { data, features }],
4340
{ RolesGridPage },
4441
{ EditRolePage },
4542
{ RolesAPIClient },
@@ -80,9 +77,6 @@ export const rolesManagementApp = Object.freeze({
8077
<EditRolePage
8178
action={action}
8279
roleName={roleName}
83-
spacesEnabled={
84-
injectedMetadata.getInjectedVar('enableSpaceAwarePrivileges') as boolean
85-
}
8680
rolesAPIClient={rolesAPIClient}
8781
userAPIClient={new UserAPIClient(http)}
8882
indicesAPIClient={new IndicesAPIClient(http)}

0 commit comments

Comments
 (0)