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

[Cases Plugin] Migrate authc.getCurrentUser usage to coreStart.security #187190

Closed
Closed
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
1 change: 1 addition & 0 deletions x-pack/plugins/cases/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const App: React.FC<{ deps: RenderAppProps }> = ({ deps }) => {
kibanaVersion,
...coreStart,
...pluginsStart,
securityService: coreStart.security,
Copy link
Member Author

Choose a reason for hiding this comment

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

Note, KibanaContextProvider isn't well typed for custom service objects like this. See the FIXME in x-pack/plugins/cases/public/common/lib/kibana/kibana_react.ts

storage,
}}
>
Expand Down
25 changes: 5 additions & 20 deletions x-pack/plugins/cases/public/common/lib/kibana/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,15 @@ export const useCurrentUser = (): AuthenticatedElasticUser | null => {

const toasts = useToasts();

const { security } = useKibana().services;
const { securityService } = useKibana().services;

const fetchUser = useCallback(() => {
let didCancel = false;
const fetchData = async () => {
try {
if (security != null) {
const response = await security.authc.getCurrentUser();
if (!didCancel) {
setUser(convertToCamelCase<AuthenticatedUser, AuthenticatedElasticUser>(response));
}
} else {
setUser({
username: i18n.translate('xpack.cases.getCurrentUser.unknownUser', {
defaultMessage: 'Unknown',
}),
email: '',
fullName: '',
roles: [],
enabled: false,
authenticationRealm: { name: '', type: '' },
lookupRealm: { name: '', type: '' },
authenticationProvider: '',
});
const response = await securityService.authc.getCurrentUser();
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the else as it would have been dead code. The new security service comes from Core and is always available.

if (!didCancel) {
setUser(convertToCamelCase<AuthenticatedUser, AuthenticatedElasticUser>(response));
}
} catch (error) {
if (!didCancel) {
Expand All @@ -102,7 +87,7 @@ export const useCurrentUser = (): AuthenticatedElasticUser | null => {
return () => {
didCancel = true;
};
}, [security, toasts]);
}, [securityService.authc, toasts]);

useEffect(() => {
fetchUser();
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/public/common/lib/kibana/kibana_react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ import type { StartServices } from '../../../types';

const useTypedKibana = () => useKibana<StartServices>();

// FIXME use a typed KibanaContextProvider that requires StartServices

export { KibanaContextProvider, useTypedKibana as useKibana, useUiSetting, useUiSetting$ };
6 changes: 5 additions & 1 deletion x-pack/plugins/cases/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type { CoreStart } from '@kbn/core/public';
import type { CoreStart, SecurityServiceStart } from '@kbn/core/public';
import type { IHttpFetchError, ResponseErrorBody } from '@kbn/core-http-browser';
import type { ReactElement, PropsWithChildren } from 'react';
import type React from 'react';
Expand Down Expand Up @@ -86,6 +86,10 @@ export interface CasesPublicStartDependencies {
licensing?: LicensingPluginStart;
contentManagement: ContentManagementPublicStart;
security: SecurityPluginStart;
/**
* Use the securityService field to ensure that APIs come from Core
*/
securityService: SecurityServiceStart;
serverless?: ServerlessPluginStart;
spaces?: SpacesPluginStart;
storage: Storage;
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -11952,7 +11952,6 @@
"xpack.cases.features.deleteSubFeatureDetails": "Supprimer les cas et les commentaires",
"xpack.cases.features.deleteSubFeatureName": "Supprimer",
"xpack.cases.getCurrentUser.Error": "Erreur lors de l'obtention de l'utilisateur",
"xpack.cases.getCurrentUser.unknownUser": "Inconnu",
"xpack.cases.header.badge.betaDesc": "Cette fonctionnalité est actuellement en version bêta. Si vous rencontrez des bugs ou si vous souhaitez apporter des commentaires, ouvrez un ticket de problème ou visitez notre forum de discussion.",
"xpack.cases.header.badge.betaLabel": "Bêta",
"xpack.cases.header.editableTitle.cancel": "Annuler",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -11932,7 +11932,6 @@
"xpack.cases.features.deleteSubFeatureDetails": "ケースとコメントを削除",
"xpack.cases.features.deleteSubFeatureName": "削除",
"xpack.cases.getCurrentUser.Error": "ユーザーの取得エラー",
"xpack.cases.getCurrentUser.unknownUser": "不明",
"xpack.cases.header.badge.betaDesc": "現在、この機能はベータです。不具合を見つけた場合やご意見がある場合、サポートに問い合わせるか、またはディスカッションフォーラムにご報告ください。",
"xpack.cases.header.badge.betaLabel": "ベータ",
"xpack.cases.header.editableTitle.cancel": "キャンセル",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -11956,7 +11956,6 @@
"xpack.cases.features.deleteSubFeatureDetails": "删除案例和注释",
"xpack.cases.features.deleteSubFeatureName": "删除",
"xpack.cases.getCurrentUser.Error": "获取用户时出错",
"xpack.cases.getCurrentUser.unknownUser": "未知",
"xpack.cases.header.badge.betaDesc": "此功能当前为公测版。如果遇到任何错误或有任何反馈,请报告问题或访问我们的论坛。",
"xpack.cases.header.badge.betaLabel": "公测版",
"xpack.cases.header.editableTitle.cancel": "取消",
Expand Down