Skip to content

Commit

Permalink
Define variables and functions (#1145)
Browse files Browse the repository at this point in the history
Signed-off-by: Chang Liu <[email protected]>
  • Loading branch information
cliu123 authored Oct 17, 2022
1 parent 736a1d9 commit 4b02d09
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions server/saved_objects/saved_objects_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ export class SecuritySavedObjectsClientWrapper {
(this.httpStart!.auth.get(wrapperOptions.request).state as OpenSearchDashboardsAuthState) ||
{};

const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
const globalTenant = '';
const defaultTenant = 'default';
const privateTenant = '__user__';

let namespaceValue = selectedTenant;

const createWithNamespace = async <T = unknown>(
type: string,
attributes: T,
options?: SavedObjectsCreateOptions
) => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -65,10 +70,7 @@ export class SecuritySavedObjectsClientWrapper {
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -80,9 +82,9 @@ export class SecuritySavedObjectsClientWrapper {
): Promise<SavedObjectsFindResponse<T>> => {
const tenants = state.authInfo?.tenants;
const availableTenantNames = Object.keys(tenants!);
availableTenantNames.push('default');
availableTenantNames.push('');
availableTenantNames.push('__user__' + state.authInfo?.user_name);
availableTenantNames.push(defaultTenant);
availableTenantNames.push(globalTenant);
availableTenantNames.push(privateTenant + state.authInfo?.user_name);
_.assign(options, { namespaces: availableTenantNames });
return await wrapperOptions.client.find(options);
};
Expand All @@ -92,10 +94,7 @@ export class SecuritySavedObjectsClientWrapper {
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -108,10 +107,7 @@ export class SecuritySavedObjectsClientWrapper {
attributes: Partial<T>,
options: SavedObjectsUpdateOptions = {}
): Promise<SavedObjectsUpdateResponse<T>> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -122,10 +118,7 @@ export class SecuritySavedObjectsClientWrapper {
objects: Array<SavedObjectsBulkCreateObject<T>>,
options?: SavedObjectsCreateOptions
): Promise<SavedObjectsBulkResponse<T>> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -136,10 +129,7 @@ export class SecuritySavedObjectsClientWrapper {
objects: Array<SavedObjectsBulkUpdateObject<T>>,
options?: SavedObjectsBulkUpdateOptions
): Promise<SavedObjectsBulkUpdateResponse<T>> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -151,10 +141,7 @@ export class SecuritySavedObjectsClientWrapper {
id: string,
options: SavedObjectsDeleteOptions = {}
) => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -165,10 +152,7 @@ export class SecuritySavedObjectsClientWrapper {
objects: SavedObjectsCheckConflictsObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsCheckConflictsResponse> => {
const selectedTenant = state.selectedTenant;
const username = state.authInfo?.user_name;
let namespaceValue = selectedTenant;
if (selectedTenant === '__user__') {
if (selectedTenant !== undefined && isPrivateTenant(selectedTenant)) {
namespaceValue = selectedTenant + username;
}
_.assign(options, { namespace: [namespaceValue] });
Expand All @@ -192,3 +176,7 @@ export class SecuritySavedObjectsClientWrapper {
};
};
}

function isPrivateTenant(selectedTenant: string | undefined) {
return selectedTenant === '__user__';
}

0 comments on commit 4b02d09

Please sign in to comment.