Skip to content

Commit

Permalink
[REV] figure,chart: Prevent destructive creation
Browse files Browse the repository at this point in the history
Since we did not verify the coherence of the commands payload in the
past, we cannot trust them and cannot rely on it now.

This reverts commit 8746ac1.

Part-of: #2150
  • Loading branch information
rrahir committed Mar 2, 2023
1 parent 80ce86a commit 313e965
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 400 deletions.
18 changes: 1 addition & 17 deletions src/plugins/core/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,8 @@ export class ChartPlugin extends CorePlugin<ChartState> implements ChartState {
allowDispatch(cmd: Command) {
const success: CommandResult = CommandResult.Success;
switch (cmd.type) {
case "CREATE_CHART":
return this.checkValidations(
cmd,
this.chainValidations(
this.checkChartDuplicate,
this.checkEmptyDataset,
this.checkDataset
),
this.checkLabelRange
);
case "UPDATE_CHART":
case "CREATE_CHART":
return this.checkValidations(
cmd,
this.chainValidations(this.checkEmptyDataset, this.checkDataset),
Expand Down Expand Up @@ -540,11 +531,4 @@ export class ChartPlugin extends CorePlugin<ChartState> implements ChartState {
const invalidLabels = !rangeReference.test(cmd.definition.labelRange || "");
return invalidLabels ? CommandResult.InvalidLabelRange : CommandResult.Success;
}

private checkChartDuplicate(cmd: CreateChartCommand): CommandResult {
if (this.chartFigures[cmd.sheetId]?.[cmd.id]) {
return CommandResult.DuplicatedChartId;
}
return CommandResult.Success;
}
}
6 changes: 3 additions & 3 deletions src/plugins/core/figures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class FigurePlugin extends CorePlugin<FigureState> implements FigureState
allowDispatch(cmd: CoreCommand) {
switch (cmd.type) {
case "CREATE_FIGURE":
return this.checkFigureDuplicate(cmd.sheetId, cmd.figure.id);
return this.checkFigureDuplicate(cmd.figure.id);
case "UPDATE_FIGURE":
case "DELETE_FIGURE":
return this.checkFigureExists(cmd.sheetId, cmd.id);
Expand Down Expand Up @@ -95,8 +95,8 @@ export class FigurePlugin extends CorePlugin<FigureState> implements FigureState
return CommandResult.Success;
}

private checkFigureDuplicate(sheetId: UID, figureId: UID): CommandResult {
if (this.figures[sheetId]?.[figureId]) {
private checkFigureDuplicate(figureId: UID): CommandResult {
if (Object.values(this.figures).find((sheet) => sheet?.[figureId])) {
return CommandResult.DuplicatedFigureId;
}
return CommandResult.Success;
Expand Down
1 change: 0 additions & 1 deletion src/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,6 @@ export const enum CommandResult {
InvalidViewportSize,
FigureDoesNotExist,
DuplicatedFigureId,
DuplicatedChartId,
}

export interface CommandHandler<T> {
Expand Down
Loading

0 comments on commit 313e965

Please sign in to comment.