Skip to content

Commit

Permalink
[APM Plugin - Browser] Migrate authc.getCurrentUser usage to coreStar…
Browse files Browse the repository at this point in the history
…t.security (#187192)

Part of #186574

## Summary

This PR migrates the method to access an APM Plugin view model field,
which consumes `authc.getCurrentUser`, to use `coreStart.security`.

Background: This PR serves as an example of a plugin migrating away from
depending on the Security plugin, which is a high priority effort for
the last release before 9.0.

### Checklist

Delete any items that are not applicable to this PR.

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
tsullivan authored Jul 3, 2024
1 parent 69c1533 commit 78fa3c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
import { useState, useEffect } from 'react';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { AuthenticatedUser } from '@kbn/security-plugin/common';
import { ApmPluginStartDeps } from '../plugin';
import { ApmServices } from '../plugin';

export function useCurrentUser() {
const {
services: { security },
} = useKibana<ApmPluginStartDeps>();
services: { securityService },
} = useKibana<ApmServices>();

const [user, setUser] = useState<AuthenticatedUser>();

useEffect(() => {
const getCurrentUser = async () => {
try {
const authenticatedUser = await security?.authc.getCurrentUser();
const authenticatedUser = await securityService.authc.getCurrentUser();
setUser(authenticatedUser);
} catch {
setUser(undefined);
}
};
getCurrentUser();
}, [security?.authc]);
}, [securityService.authc]);

return user;
}
3 changes: 3 additions & 0 deletions x-pack/plugins/observability_solution/apm/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
DEFAULT_APP_CATEGORIES,
Plugin,
PluginInitializerContext,
SecurityServiceStart,
} from '@kbn/core/public';
import type { DataPublicPluginSetup, DataPublicPluginStart } from '@kbn/data-plugin/public';
import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
Expand Down Expand Up @@ -107,6 +108,7 @@ export interface ApmPluginSetupDeps {
}

export interface ApmServices {
securityService: SecurityServiceStart;
telemetry: ITelemetryClient;
}

Expand Down Expand Up @@ -390,6 +392,7 @@ export class ApmPlugin implements Plugin<ApmPluginSetup, ApmPluginStart> {
pluginsStart: pluginsStart as ApmPluginStartDeps,
observabilityRuleTypeRegistry,
apmServices: {
securityService: coreStart.security,
telemetry,
},
});
Expand Down

0 comments on commit 78fa3c3

Please sign in to comment.