-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
adapt Core userSettings
service to no longer depends on the security
plugin
#181538
Merged
pgayvallet
merged 7 commits into
elastic:main
from
pgayvallet:kbn-174578-user-settings-service-in-core
Apr 29, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a5d1dfd
[security-in-core] change `userSettings` service to no longer depends…
pgayvallet b634935
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine d9f5b94
Merge remote-tracking branch 'upstream/main' into kbn-174578-user-set…
pgayvallet 087595b
fix / adapt mocks
pgayvallet c56b9b6
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 0ca397c
fix tests
pgayvallet 780dff1
Merge remote-tracking branch 'upstream/main' into kbn-174578-user-set…
pgayvallet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...s/core/user-settings/core-user-settings-server-internal/src/user_settings_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { mockCoreContext } from '@kbn/core-base-server-mocks'; | ||
import { httpServerMock } from '@kbn/core-http-server-mocks'; | ||
import { userProfileServiceMock } from '@kbn/core-user-profile-server-mocks'; | ||
import type { UserProfileWithSecurity } from '@kbn/core-user-profile-common'; | ||
import { UserSettingsService } from './user_settings_service'; | ||
|
||
describe('#setup', () => { | ||
let coreContext: ReturnType<typeof mockCoreContext.create>; | ||
let service: UserSettingsService; | ||
let startDeps: { userProfile: ReturnType<typeof userProfileServiceMock.createInternalStart> }; | ||
|
||
beforeEach(() => { | ||
coreContext = mockCoreContext.create(); | ||
service = new UserSettingsService(coreContext); | ||
startDeps = { | ||
userProfile: userProfileServiceMock.createInternalStart(), | ||
}; | ||
}); | ||
|
||
const createUserProfile = (darkMode: string | undefined): UserProfileWithSecurity => { | ||
return { | ||
data: { | ||
userSettings: { | ||
darkMode, | ||
}, | ||
}, | ||
} as unknown as UserProfileWithSecurity; | ||
}; | ||
|
||
it('fetches userSettings when client is set and returns `true` when `darkMode` is set to `dark`', async () => { | ||
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile('dark')); | ||
|
||
const { getUserSettingDarkMode } = service.setup(); | ||
service.start(startDeps); | ||
|
||
const kibanaRequest = httpServerMock.createKibanaRequest(); | ||
const darkMode = await getUserSettingDarkMode(kibanaRequest); | ||
|
||
expect(darkMode).toEqual(true); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledTimes(1); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledWith({ | ||
request: kibanaRequest, | ||
dataPath: 'userSettings', | ||
}); | ||
}); | ||
|
||
it('fetches userSettings when client is set and returns `false` when `darkMode` is set to `light`', async () => { | ||
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile('light')); | ||
|
||
const { getUserSettingDarkMode } = service.setup(); | ||
service.start(startDeps); | ||
|
||
const kibanaRequest = httpServerMock.createKibanaRequest(); | ||
const darkMode = await getUserSettingDarkMode(kibanaRequest); | ||
|
||
expect(darkMode).toEqual(false); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledTimes(1); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledWith({ | ||
request: kibanaRequest, | ||
dataPath: 'userSettings', | ||
}); | ||
}); | ||
|
||
it('fetches userSettings when client is set and returns `undefined` when `darkMode` is set to `` (the default value)', async () => { | ||
startDeps.userProfile.getCurrent.mockResolvedValue(createUserProfile('')); | ||
|
||
const { getUserSettingDarkMode } = service.setup(); | ||
service.start(startDeps); | ||
|
||
const kibanaRequest = httpServerMock.createKibanaRequest(); | ||
const darkMode = await getUserSettingDarkMode(kibanaRequest); | ||
|
||
expect(darkMode).toEqual(undefined); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledTimes(1); | ||
expect(startDeps.userProfile.getCurrent).toHaveBeenCalledWith({ | ||
request: kibanaRequest, | ||
dataPath: 'userSettings', | ||
}); | ||
}); | ||
|
||
it('does not fetch userSettings when client is not set, returns `undefined`, and logs a debug statement', async () => { | ||
const { getUserSettingDarkMode } = service.setup(); | ||
|
||
const kibanaRequest = httpServerMock.createKibanaRequest(); | ||
const darkMode = await getUserSettingDarkMode(kibanaRequest); | ||
|
||
expect(darkMode).toEqual(undefined); | ||
expect(coreContext.logger.get().debug).toHaveBeenCalledWith('userProfile not set'); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
packages/core/user-settings/core-user-settings-server-internal/src/user_settings_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { CoreContext } from '@kbn/core-base-server-internal'; | ||
import type { Logger } from '@kbn/logging'; | ||
import type { KibanaRequest } from '@kbn/core-http-server'; | ||
import type { DarkModeValue } from '@kbn/core-ui-settings-common'; | ||
import type { InternalUserProfileServiceStart } from '@kbn/core-user-profile-server-internal'; | ||
|
||
export interface UserSettingsServiceStartDeps { | ||
userProfile: InternalUserProfileServiceStart; | ||
} | ||
|
||
const userSettingsDataPath = 'userSettings'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface InternalUserSettingsServiceSetup { | ||
getUserSettingDarkMode: (request: KibanaRequest) => Promise<DarkModeValue | undefined>; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class UserSettingsService { | ||
private logger: Logger; | ||
private userProfile?: InternalUserProfileServiceStart; | ||
|
||
constructor(coreContext: CoreContext) { | ||
this.logger = coreContext.logger.get('user-settings-service'); | ||
} | ||
|
||
public setup(): InternalUserSettingsServiceSetup { | ||
return { | ||
getUserSettingDarkMode: async (request: KibanaRequest) => { | ||
const userSettings = await this.getSettings(request); | ||
return getUserSettingDarkMode(userSettings); | ||
}, | ||
}; | ||
} | ||
|
||
public start(deps: UserSettingsServiceStartDeps) { | ||
this.userProfile = deps.userProfile; | ||
} | ||
|
||
private async getSettings(request: KibanaRequest): Promise<Record<string, string>> { | ||
if (this.userProfile) { | ||
const userProfile = await this.userProfile.getCurrent({ | ||
request, | ||
dataPath: userSettingsDataPath, | ||
}); | ||
return (userProfile?.data?.[userSettingsDataPath] ?? {}) as Record<string, string>; | ||
} else { | ||
this.logger.debug('userProfile not set'); | ||
return {}; | ||
} | ||
} | ||
} | ||
|
||
const getUserSettingDarkMode = ( | ||
userSettings: Record<string, string> | ||
): DarkModeValue | undefined => { | ||
if (userSettings?.darkMode) { | ||
return userSettings.darkMode.toUpperCase() === 'DARK'; | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 0 additions & 82 deletions
82
packages/core/user-settings/core-user-settings-server-internal/user_settings_service.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this is not a new file, it was moved from
packages/core/user-settings/core-user-settings-server-internal/user_settings_service.ts
, but I guess the file changed too much so it lost the info