From bbce1e0e0fe5bc2abfd19319c2da9fcfc427a466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Kiss?= Date: Wed, 3 Jan 2018 21:06:08 +0100 Subject: [PATCH] fix(user-config): Validate device, keymap, and macro names (#543) * fix(user-config): Validate device, keymap, and macro names * fix device name renaming --- package-lock.json | 5 +++-- .../keymap/header/keymap-header.component.ts | 2 +- .../macro/header/macro-header.component.ts | 2 +- .../side-menu/side-menu.component.ts | 6 +++++- packages/uhk-web/src/app/store/index.ts | 3 ++- .../app/store/reducers/user-configuration.ts | 21 +++++++++++++++---- packages/uhk-web/src/app/util/index.ts | 1 + packages/uhk-web/src/app/util/validators.ts | 3 +++ 8 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 packages/uhk-web/src/app/util/validators.ts diff --git a/package-lock.json b/package-lock.json index 44cc709b..b677397d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "uhk-agent", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -4085,7 +4085,8 @@ "jsbn": { "version": "0.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "json-schema": { "version": "0.2.3", diff --git a/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts b/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts index 30f35578..425bff27 100644 --- a/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts +++ b/packages/uhk-web/src/app/components/keymap/header/keymap-header.component.ts @@ -74,7 +74,7 @@ export class KeymapHeaderComponent implements OnChanges { } editKeymapName(name: string) { - if (name.length === 0) { + if (!util.isValidName(name)) { this.setName(); return; } diff --git a/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts b/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts index d481aee7..72c37b10 100644 --- a/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts +++ b/packages/uhk-web/src/app/components/macro/header/macro-header.component.ts @@ -59,7 +59,7 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges { } editMacroName(name: string) { - if (name.length === 0) { + if (!util.isValidName(name)) { this.setName(); return; } diff --git a/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts b/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts index 4c2fe86e..4b0e66c8 100644 --- a/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts +++ b/packages/uhk-web/src/app/components/side-menu/side-menu.component.ts @@ -118,7 +118,11 @@ export class SideMenuComponent implements AfterContentInit, OnDestroy { this.store.dispatch(MacroActions.addMacro()); } - editDeviceName(name): void { + editDeviceName(name: string): void { + if (!util.isValidName(name) || name.trim() === this.deviceNameValue) { + this.setDeviceName(); + return; + } this.store.dispatch(new RenameUserConfigurationAction(name)); } diff --git a/packages/uhk-web/src/app/store/index.ts b/packages/uhk-web/src/app/store/index.ts index 07500fc1..7fcdcec3 100644 --- a/packages/uhk-web/src/app/store/index.ts +++ b/packages/uhk-web/src/app/store/index.ts @@ -39,9 +39,10 @@ export const metaReducers: MetaReducer[] = environment.production : [storeFreeze]; export const getUserConfiguration = (state: AppState) => state.userConfiguration; -export const getDeviceName = (state: AppState) => state.userConfiguration.deviceName; +export const getDeviceName = createSelector(getUserConfiguration, fromUserConfig.getDeviceName); export const appState = (state: AppState) => state.app; + export const showAddonMenu = createSelector(appState, fromApp.showAddonMenu); export const autoWriteUserConfiguration = createSelector(appState, fromApp.autoWriteUserConfiguration); export const getUndoableNotification = createSelector(appState, fromApp.getUndoableNotification); diff --git a/packages/uhk-web/src/app/store/reducers/user-configuration.ts b/packages/uhk-web/src/app/store/reducers/user-configuration.ts index 9c21391e..204debd4 100644 --- a/packages/uhk-web/src/app/store/reducers/user-configuration.ts +++ b/packages/uhk-web/src/app/store/reducers/user-configuration.ts @@ -4,10 +4,11 @@ import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/map'; -import { KeyAction, Keymap, KeyActionHelper, Layer, Macro, Module, SwitchLayerAction, UserConfiguration } from 'uhk-common'; +import { KeyAction, KeyActionHelper, Keymap, Layer, Macro, Module, SwitchLayerAction, UserConfiguration } from 'uhk-common'; import { KeymapActions, MacroActions } from '../actions'; import { AppState } from '../index'; import { ActionTypes } from '../actions/user-config'; +import { isValidName } from '../../util'; export const initialState: UserConfiguration = new UserConfiguration(); @@ -31,7 +32,11 @@ export function reducer(state = initialState, action: Action & { payload?: any } break; } case KeymapActions.EDIT_NAME: { - const name: string = action.payload.name; + if (!isValidName(action.payload.name)) { + break; + } + + const name: string = action.payload.name.trim(); const duplicate = state.keymaps.some((keymap: Keymap) => { return keymap.name === name && keymap.abbreviation !== action.payload.abbr; @@ -169,7 +174,11 @@ export function reducer(state = initialState, action: Action & { payload?: any } break; } case MacroActions.EDIT_NAME: { - const name: string = action.payload.name; + if (!isValidName(action.payload.name)) { + break; + } + + const name: string = action.payload.name.trim(); const duplicate = state.macros.some((macro: Macro) => { return macro.id !== action.payload.id && macro.name === name; @@ -242,7 +251,9 @@ export function reducer(state = initialState, action: Action & { payload?: any } break; case ActionTypes.RENAME_USER_CONFIGURATION: { - changedUserConfiguration.deviceName = action.payload; + if (isValidName(action.payload)) { + changedUserConfiguration.deviceName = action.payload.trim(); + } break; } @@ -399,3 +410,5 @@ function setKeyActionToLayer(newLayer: Layer, moduleIndex: number, keyIndex: num newModule.keyActions = newModule.keyActions.slice(); newModule.keyActions[keyIndex] = newKeyAction; } + +export const getDeviceName = (state: UserConfiguration) => state.deviceName; diff --git a/packages/uhk-web/src/app/util/index.ts b/packages/uhk-web/src/app/util/index.ts index 0a1e1d6e..ffa5f8cf 100644 --- a/packages/uhk-web/src/app/util/index.ts +++ b/packages/uhk-web/src/app/util/index.ts @@ -1 +1,2 @@ export * from './html-helper'; +export * from './validators'; diff --git a/packages/uhk-web/src/app/util/validators.ts b/packages/uhk-web/src/app/util/validators.ts new file mode 100644 index 00000000..897acf84 --- /dev/null +++ b/packages/uhk-web/src/app/util/validators.ts @@ -0,0 +1,3 @@ +export function isValidName(name: string): boolean { + return name && name.trim().length > 0; +}