fix(user-config): Validate device, keymap, and macro names (#543)

* fix(user-config): Validate device, keymap, and macro names

* fix device name renaming
This commit is contained in:
Róbert Kiss
2018-01-03 21:06:08 +01:00
committed by László Monda
parent 13f064229f
commit bbce1e0e0f
8 changed files with 33 additions and 10 deletions

5
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "uhk-agent", "name": "uhk-agent",
"version": "1.0.3", "version": "1.0.4",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@@ -4085,7 +4085,8 @@
"jsbn": { "jsbn": {
"version": "0.1.1", "version": "0.1.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"json-schema": { "json-schema": {
"version": "0.2.3", "version": "0.2.3",

View File

@@ -74,7 +74,7 @@ export class KeymapHeaderComponent implements OnChanges {
} }
editKeymapName(name: string) { editKeymapName(name: string) {
if (name.length === 0) { if (!util.isValidName(name)) {
this.setName(); this.setName();
return; return;
} }

View File

@@ -59,7 +59,7 @@ export class MacroHeaderComponent implements AfterViewInit, OnChanges {
} }
editMacroName(name: string) { editMacroName(name: string) {
if (name.length === 0) { if (!util.isValidName(name)) {
this.setName(); this.setName();
return; return;
} }

View File

@@ -118,7 +118,11 @@ export class SideMenuComponent implements AfterContentInit, OnDestroy {
this.store.dispatch(MacroActions.addMacro()); 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)); this.store.dispatch(new RenameUserConfigurationAction(name));
} }

View File

@@ -39,9 +39,10 @@ export const metaReducers: MetaReducer<AppState>[] = environment.production
: [storeFreeze]; : [storeFreeze];
export const getUserConfiguration = (state: AppState) => state.userConfiguration; 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 appState = (state: AppState) => state.app;
export const showAddonMenu = createSelector(appState, fromApp.showAddonMenu); export const showAddonMenu = createSelector(appState, fromApp.showAddonMenu);
export const autoWriteUserConfiguration = createSelector(appState, fromApp.autoWriteUserConfiguration); export const autoWriteUserConfiguration = createSelector(appState, fromApp.autoWriteUserConfiguration);
export const getUndoableNotification = createSelector(appState, fromApp.getUndoableNotification); export const getUndoableNotification = createSelector(appState, fromApp.getUndoableNotification);

View File

@@ -4,10 +4,11 @@ import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of'; import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map'; 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 { KeymapActions, MacroActions } from '../actions';
import { AppState } from '../index'; import { AppState } from '../index';
import { ActionTypes } from '../actions/user-config'; import { ActionTypes } from '../actions/user-config';
import { isValidName } from '../../util';
export const initialState: UserConfiguration = new UserConfiguration(); export const initialState: UserConfiguration = new UserConfiguration();
@@ -31,7 +32,11 @@ export function reducer(state = initialState, action: Action & { payload?: any }
break; break;
} }
case KeymapActions.EDIT_NAME: { 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) => { const duplicate = state.keymaps.some((keymap: Keymap) => {
return keymap.name === name && keymap.abbreviation !== action.payload.abbr; return keymap.name === name && keymap.abbreviation !== action.payload.abbr;
@@ -169,7 +174,11 @@ export function reducer(state = initialState, action: Action & { payload?: any }
break; break;
} }
case MacroActions.EDIT_NAME: { 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) => { const duplicate = state.macros.some((macro: Macro) => {
return macro.id !== action.payload.id && macro.name === name; return macro.id !== action.payload.id && macro.name === name;
@@ -242,7 +251,9 @@ export function reducer(state = initialState, action: Action & { payload?: any }
break; break;
case ActionTypes.RENAME_USER_CONFIGURATION: { case ActionTypes.RENAME_USER_CONFIGURATION: {
changedUserConfiguration.deviceName = action.payload; if (isValidName(action.payload)) {
changedUserConfiguration.deviceName = action.payload.trim();
}
break; break;
} }
@@ -399,3 +410,5 @@ function setKeyActionToLayer(newLayer: Layer, moduleIndex: number, keyIndex: num
newModule.keyActions = newModule.keyActions.slice(); newModule.keyActions = newModule.keyActions.slice();
newModule.keyActions[keyIndex] = newKeyAction; newModule.keyActions[keyIndex] = newKeyAction;
} }
export const getDeviceName = (state: UserConfiguration) => state.deviceName;

View File

@@ -1 +1,2 @@
export * from './html-helper'; export * from './html-helper';
export * from './validators';

View File

@@ -0,0 +1,3 @@
export function isValidName(name: string): boolean {
return name && name.trim().length > 0;
}