Skip to content

Commit

Permalink
DRY out logic interfaces
Browse files Browse the repository at this point in the history
Should have done this long ago
  • Loading branch information
scottybollinger committed Jun 24, 2021
1 parent 37832fe commit f75af53
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

import { kea, MakeLogicType } from 'kea';

import { EuiComboBoxOptionOption } from '@elastic/eui';

import {
clearFlashMessages,
flashAPIErrors,
setSuccessMessage,
} from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import {
RoleMappingsBaseServerDetails,
RoleMappingsBaseActions,
RoleMappingsBaseValues,
} from '../../../shared/role_mapping';
import { ANY_AUTH_PROVIDER } from '../../../shared/role_mapping/constants';
import { AttributeName, SingleUserRoleMapping, ElasticsearchUser } from '../../../shared/types';
import { ASRoleMapping, RoleTypes } from '../../types';
Expand All @@ -29,16 +32,11 @@ import {

type UserMapping = SingleUserRoleMapping<ASRoleMapping>;

interface RoleMappingsServerDetails {
interface RoleMappingsServerDetails extends RoleMappingsBaseServerDetails {
roleMappings: ASRoleMapping[];
attributes: string[];
authProviders: string[];
availableEngines: Engine[];
elasticsearchRoles: string[];
elasticsearchUsers: ElasticsearchUser[];
hasAdvancedRoles: boolean;
multipleAuthProvidersConfig: boolean;
singleUserRoleMappings: UserMapping[];
hasAdvancedRoles: boolean;
}

const getFirstAttributeName = (roleMapping: ASRoleMapping) =>
Expand All @@ -47,24 +45,7 @@ const getFirstAttributeValue = (roleMapping: ASRoleMapping) =>
Object.entries(roleMapping.rules)[0][1] as AttributeName;
const emptyUser = { username: '', email: '' } as ElasticsearchUser;

interface RoleMappingsActions {
handleAccessAllEnginesChange(selected: boolean): { selected: boolean };
handleAuthProviderChange(value: string[]): { value: string[] };
handleAttributeSelectorChange(
value: AttributeName,
firstElasticsearchRole: string
): { value: AttributeName; firstElasticsearchRole: string };
handleAttributeValueChange(value: string): { value: string };
handleDeleteMapping(roleMappingId: string): { roleMappingId: string };
handleEngineSelectionChange(engineNames: string[]): { engineNames: string[] };
handleRoleChange(roleType: RoleTypes): { roleType: RoleTypes };
handleUsernameSelectChange(username: string): { username: string };
handleSaveMapping(): void;
handleSaveUser(): void;
initializeRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeSingleUserRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeRoleMappings(): void;
resetState(): void;
interface RoleMappingsActions extends RoleMappingsBaseActions {
setRoleMapping(roleMapping: ASRoleMapping): { roleMapping: ASRoleMapping };
setSingleUserRoleMapping(data?: UserMapping): { singleUserRoleMapping: UserMapping };
setRoleMappings({
Expand All @@ -73,48 +54,22 @@ interface RoleMappingsActions {
roleMappings: ASRoleMapping[];
}): { roleMappings: ASRoleMapping[] };
setRoleMappingsData(data: RoleMappingsServerDetails): RoleMappingsServerDetails;
setElasticsearchUser(
elasticsearchUser?: ElasticsearchUser
): { elasticsearchUser: ElasticsearchUser };
openRoleMappingFlyout(): void;
openSingleUserRoleMappingFlyout(): void;
closeUsersAndRolesFlyout(): void;
setRoleMappingErrors(errors: string[]): { errors: string[] };
enableRoleBasedAccess(): void;
setUserExistingRadioValue(userFormUserIsExisting: boolean): { userFormUserIsExisting: boolean };
setElasticsearchUsernameValue(username: string): { username: string };
setElasticsearchEmailValue(email: string): { email: string };
setUserCreated(): void;
setUserFormIsNewUser(userFormIsNewUser: boolean): { userFormIsNewUser: boolean };
handleAccessAllEnginesChange(selected: boolean): { selected: boolean };
handleEngineSelectionChange(engineNames: string[]): { engineNames: string[] };
handleRoleChange(roleType: RoleTypes): { roleType: RoleTypes };
}

interface RoleMappingsValues {
interface RoleMappingsValues extends RoleMappingsBaseValues {
accessAllEngines: boolean;
attributeName: AttributeName;
attributeValue: string;
attributes: string[];
availableAuthProviders: string[];
availableEngines: Engine[];
dataLoading: boolean;
elasticsearchRoles: string[];
elasticsearchUsers: ElasticsearchUser[];
elasticsearchUser: ElasticsearchUser;
hasAdvancedRoles: boolean;
multipleAuthProvidersConfig: boolean;
roleMapping: ASRoleMapping | null;
roleMappings: ASRoleMapping[];
singleUserRoleMapping: UserMapping | null;
singleUserRoleMappings: UserMapping[];
roleType: RoleTypes;
selectedAuthProviders: string[];
selectedEngines: Set<string>;
roleMappingFlyoutOpen: boolean;
singleUserRoleMappingFlyoutOpen: boolean;
selectedOptions: EuiComboBoxOptionOption[];
roleMappingErrors: string[];
userFormUserIsExisting: boolean;
userCreated: boolean;
userFormIsNewUser: boolean;
hasAdvancedRoles: boolean;
}

export const RoleMappingsLogic = kea<MakeLogicType<RoleMappingsValues, RoleMappingsActions>>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

export * from './types';
export { AttributeSelector } from './attribute_selector';
export { DeactivatedUserCallout } from './deactivated_user_callout';
export { RolesEmptyPrompt } from './roles_empty_prompt';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EuiComboBoxOptionOption } from '@elastic/eui';

import { AttributeName, ElasticsearchUser } from '../../shared/types';

export interface RoleMappingsBaseServerDetails {
attributes: string[];
authProviders: string[];
elasticsearchRoles: string[];
elasticsearchUsers: ElasticsearchUser[];
multipleAuthProvidersConfig: boolean;
}

export interface RoleMappingsBaseActions {
handleAuthProviderChange(value: string[]): { value: string[] };
handleAttributeSelectorChange(
value: AttributeName,
firstElasticsearchRole: string
): { value: AttributeName; firstElasticsearchRole: string };
handleAttributeValueChange(value: string): { value: string };
handleDeleteMapping(roleMappingId: string): { roleMappingId: string };
handleUsernameSelectChange(username: string): { username: string };
handleSaveMapping(): void;
handleSaveUser(): void;
initializeRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeSingleUserRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeRoleMappings(): void;
resetState(): void;
setElasticsearchUser(
elasticsearchUser?: ElasticsearchUser
): { elasticsearchUser: ElasticsearchUser };
openRoleMappingFlyout(): void;
openSingleUserRoleMappingFlyout(): void;
closeUsersAndRolesFlyout(): void;
setRoleMappingErrors(errors: string[]): { errors: string[] };
enableRoleBasedAccess(): void;
setUserExistingRadioValue(userFormUserIsExisting: boolean): { userFormUserIsExisting: boolean };
setElasticsearchUsernameValue(username: string): { username: string };
setElasticsearchEmailValue(email: string): { email: string };
setUserCreated(): void;
setUserFormIsNewUser(userFormIsNewUser: boolean): { userFormIsNewUser: boolean };
}

export interface RoleMappingsBaseValues extends RoleMappingsBaseServerDetails {
attributeName: AttributeName;
attributeValue: string;
availableAuthProviders: string[];
dataLoading: boolean;
elasticsearchUser: ElasticsearchUser;
roleMappingFlyoutOpen: boolean;
singleUserRoleMappingFlyoutOpen: boolean;
selectedOptions: EuiComboBoxOptionOption[];
roleMappingErrors: string[];
userFormUserIsExisting: boolean;
userCreated: boolean;
userFormIsNewUser: boolean;
accessAllEngines: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

import { kea, MakeLogicType } from 'kea';

import { EuiComboBoxOptionOption } from '@elastic/eui';

import {
clearFlashMessages,
flashAPIErrors,
setSuccessMessage,
} from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import {
RoleMappingsBaseServerDetails,
RoleMappingsBaseActions,
RoleMappingsBaseValues,
} from '../../../shared/role_mapping';
import { ANY_AUTH_PROVIDER } from '../../../shared/role_mapping/constants';
import { AttributeName, SingleUserRoleMapping, ElasticsearchUser } from '../../../shared/types';
import { RoleGroup, WSRoleMapping, Role } from '../../types';
Expand All @@ -28,14 +31,9 @@ import {

type UserMapping = SingleUserRoleMapping<WSRoleMapping>;

interface RoleMappingsServerDetails {
interface RoleMappingsServerDetails extends RoleMappingsBaseServerDetails {
roleMappings: WSRoleMapping[];
attributes: string[];
authProviders: string[];
availableGroups: RoleGroup[];
elasticsearchUsers: ElasticsearchUser[];
elasticsearchRoles: string[];
multipleAuthProvidersConfig: boolean;
singleUserRoleMappings: UserMapping[];
}

Expand All @@ -45,24 +43,8 @@ const getFirstAttributeValue = (roleMapping: WSRoleMapping): string =>
Object.entries(roleMapping.rules)[0][1] as string;
const emptyUser = { username: '', email: '' } as ElasticsearchUser;

interface RoleMappingsActions {
handleAllGroupsSelectionChange(selected: boolean): { selected: boolean };
handleAuthProviderChange(value: string[]): { value: string[] };
handleAttributeSelectorChange(
value: AttributeName,
firstElasticsearchRole: string
): { value: AttributeName; firstElasticsearchRole: string };
handleAttributeValueChange(value: string): { value: string };
handleDeleteMapping(roleMappingId: string): { roleMappingId: string };
handleGroupSelectionChange(groupIds: string[]): { groupIds: string[] };
handleRoleChange(roleType: Role): { roleType: Role };
handleUsernameSelectChange(username: string): { username: string };
handleSaveMapping(): void;
handleSaveUser(): void;
initializeRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeSingleUserRoleMapping(roleMappingId?: string): { roleMappingId?: string };
initializeRoleMappings(): void;
resetState(): void;
interface RoleMappingsActions extends RoleMappingsBaseActions {
setDefaultGroup(availableGroups: RoleGroup[]): { availableGroups: RoleGroup[] };
setRoleMapping(roleMapping: WSRoleMapping): { roleMapping: WSRoleMapping };
setSingleUserRoleMapping(data?: UserMapping): { singleUserRoleMapping: UserMapping };
setRoleMappings({
Expand All @@ -71,48 +53,21 @@ interface RoleMappingsActions {
roleMappings: WSRoleMapping[];
}): { roleMappings: WSRoleMapping[] };
setRoleMappingsData(data: RoleMappingsServerDetails): RoleMappingsServerDetails;
setElasticsearchUser(
elasticsearchUser?: ElasticsearchUser
): { elasticsearchUser: ElasticsearchUser };
setDefaultGroup(availableGroups: RoleGroup[]): { availableGroups: RoleGroup[] };
openRoleMappingFlyout(): void;
openSingleUserRoleMappingFlyout(): void;
closeUsersAndRolesFlyout(): void;
setRoleMappingErrors(errors: string[]): { errors: string[] };
enableRoleBasedAccess(): void;
setUserExistingRadioValue(userFormUserIsExisting: boolean): { userFormUserIsExisting: boolean };
setElasticsearchUsernameValue(username: string): { username: string };
setElasticsearchEmailValue(email: string): { email: string };
setUserCreated(): void;
setUserFormIsNewUser(userFormIsNewUser: boolean): { userFormIsNewUser: boolean };
handleAllGroupsSelectionChange(selected: boolean): { selected: boolean };
handleGroupSelectionChange(groupIds: string[]): { groupIds: string[] };
handleRoleChange(roleType: Role): { roleType: Role };
}

interface RoleMappingsValues {
interface RoleMappingsValues extends RoleMappingsBaseValues {
includeInAllGroups: boolean;
attributeName: AttributeName;
attributeValue: string;
attributes: string[];
availableAuthProviders: string[];
availableGroups: RoleGroup[];
dataLoading: boolean;
elasticsearchRoles: string[];
elasticsearchUsers: ElasticsearchUser[];
elasticsearchUser: ElasticsearchUser;
multipleAuthProvidersConfig: boolean;
roleMapping: WSRoleMapping | null;
roleMappings: WSRoleMapping[];
singleUserRoleMapping: UserMapping | null;
singleUserRoleMappings: UserMapping[];
roleType: Role;
selectedAuthProviders: string[];
selectedGroups: Set<string>;
roleMappingFlyoutOpen: boolean;
singleUserRoleMappingFlyoutOpen: boolean;
selectedOptions: EuiComboBoxOptionOption[];
roleMappingErrors: string[];
userFormUserIsExisting: boolean;
userCreated: boolean;
userFormIsNewUser: boolean;
}

export const RoleMappingsLogic = kea<MakeLogicType<RoleMappingsValues, RoleMappingsActions>>({
Expand Down

0 comments on commit f75af53

Please sign in to comment.