Skip to content

Commit

Permalink
[FIX] model: prevent UI plugins to refuse core commands
Browse files Browse the repository at this point in the history
Currently it's possible for UI plugins to handle and refuse core commands
in the allowDispatch. This is a problem because this could lead to
de-synchronized states in multi-user.

This commit fixes this by making sure that the core commands don't go
through the allowDispatch of the UI plugins.

Odoo task 3209463

closes #2120

Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
hokolomopo committed Mar 30, 2023
1 parent 21333f9 commit 96d5e44
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 27 deletions.
19 changes: 18 additions & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
GridRenderingContext,
isCoreCommand,
LAYERS,
LocalCommand,
UID,
} from "./types/index";
import { NotifyUIEvent } from "./types/ui";
Expand Down Expand Up @@ -267,7 +268,11 @@ export class Model extends EventBus<any> implements CommandDispatcher {
}

get handlers(): CommandHandler<Command>[] {
return [this.range, ...this.corePlugins, ...this.allUIPlugins, this.history];
return [...this.coreHandlers, ...this.allUIPlugins, this.history];
}

get coreHandlers(): CommandHandler<Command>[] {
return [this.range, ...this.corePlugins];
}

get allUIPlugins(): UIPlugin[] {
Expand Down Expand Up @@ -419,6 +424,18 @@ export class Model extends EventBus<any> implements CommandDispatcher {
* Check if the given command is allowed by all the plugins and the history.
*/
private checkDispatchAllowed(command: Command): DispatchResult {
if (isCoreCommand(command)) {
return this.checkDispatchAllowedCoreCommand(command);
}
return this.checkDispatchAllowedLocalCommand(command);
}

private checkDispatchAllowedCoreCommand(command: CoreCommand): DispatchResult {
const results = this.coreHandlers.map((handler) => handler.allowDispatch(command));
return new DispatchResult(results.flat());
}

private checkDispatchAllowedLocalCommand(command: LocalCommand): DispatchResult {
const results = this.handlers.map((handler) => handler.allowDispatch(command));
return new DispatchResult(results.flat());
}
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/ui_core_views/filter_evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
Border,
CellPosition,
CellValueType,
Command,
CommandResult,
ExcelFilterData,
ExcelWorkbookData,
Expand All @@ -24,7 +23,7 @@ import {
Zone,
} from "../../types";
import { UIPlugin } from "../ui_plugin";
import { UpdateFilterCommand } from "./../../types/commands";
import { Command, LocalCommand, UpdateFilterCommand } from "./../../types/commands";

export class FilterEvaluationPlugin extends UIPlugin {
static getters = [
Expand All @@ -41,7 +40,7 @@ export class FilterEvaluationPlugin extends UIPlugin {
hiddenRows: Set<number> = new Set();
isEvaluationDirty = false;

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "UPDATE_FILTER":
if (!this.getters.getFilterId(cmd)) {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_core_views/sheetview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EdgeScrollInfo,
Figure,
HeaderIndex,
LocalCommand,
Pixel,
Position,
Rect,
Expand Down Expand Up @@ -117,7 +118,7 @@ export class SheetViewPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult | CommandResult[] {
switch (cmd.type) {
case "SET_VIEWPORT_OFFSET":
return this.checkScrollingDirection(cmd);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_feature/autofill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GridRenderingContext,
HeaderIndex,
LAYERS,
LocalCommand,
Tooltip,
Zone,
} from "../../types/index";
Expand Down Expand Up @@ -94,7 +95,7 @@ export class AutofillPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "AUTOFILL_SELECT":
const sheetId = this.getters.getActiveSheetId();
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/cell_popovers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { positionToZone } from "../../helpers";
import { cellPopoverRegistry } from "../../registries/cell_popovers_registry";
import { CellPosition, Command, CommandResult, Position, Rect } from "../../types";
import { CellPosition, Command, CommandResult, LocalCommand, Position, Rect } from "../../types";
import {
CellPopoverType,
ClosedCellPopover,
Expand All @@ -20,7 +20,7 @@ export class CellPopoverPlugin extends UIPlugin {

private persistentPopover?: CellPosition & { type: CellPopoverType };

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "OPEN_CELL_POPOVER":
try {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/selection_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../../helpers/index";
import { StreamCallbacks } from "../../selection_stream/event_stream";
import { SelectionEvent } from "../../types/event_stream";
import { Command, CommandResult, Highlight, LAYERS, UID } from "../../types/index";
import { Command, CommandResult, Highlight, LAYERS, LocalCommand, UID } from "../../types/index";
import { UIPlugin, UIPluginConfig } from "../ui_plugin";

export interface RangeInputValue {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class SelectionInputPlugin extends UIPlugin implements StreamCallbacks<Se
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "ADD_EMPTY_RANGE":
if (this.inputHasSingleRange && this.ranges.length === 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/selection_inputs_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { positionToZone, rangeReference, splitReference } from "../../helpers/index";
import { Command, CommandResult, Highlight, LAYERS, UID } from "../../types/index";
import { Command, CommandResult, Highlight, LAYERS, LocalCommand, UID } from "../../types/index";
import { UIPlugin, UIPluginConfig } from "../ui_plugin";
import { RangeInputValue, SelectionInputPlugin } from "./selection_input";

Expand Down Expand Up @@ -33,7 +33,7 @@ export class SelectionInputsManagerPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "FOCUS_RANGE":
const index = this.currentInput?.getIndex(cmd.rangeId);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_feature/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommandResult,
EvaluatedCell,
HeaderIndex,
LocalCommand,
Position,
SortCommand,
SortDirection,
Expand All @@ -21,7 +22,7 @@ import { UIPlugin } from "../ui_plugin";
export class SortPlugin extends UIPlugin {
static getters = ["getContiguousZone"] as const;

allowDispatch(cmd: Command) {
allowDispatch(cmd: LocalCommand): CommandResult | CommandResult[] {
switch (cmd.type) {
case "SORT_CELLS":
if (!isInside(cmd.col, cmd.row, cmd.zone)) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_feature/ui_sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PADDING_AUTORESIZE_HORIZONTAL,
} from "../../constants";
import { computeIconWidth, computeTextWidth, positions } from "../../helpers/index";
import { Command, CommandResult, UID } from "../../types";
import { Command, CommandResult, LocalCommand, UID } from "../../types";
import {
CellPosition,
Dimension,
Expand Down Expand Up @@ -32,7 +32,7 @@ export class SheetUIPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "AUTORESIZE_ROWS":
case "AUTORESIZE_COLUMNS":
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_stateful/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
GridRenderingContext,
isCoreCommand,
LAYERS,
LocalCommand,
Zone,
} from "../../types/index";
import { UIPlugin } from "../ui_plugin";
Expand Down Expand Up @@ -47,7 +48,7 @@ export class ClipboardPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "CUT":
const zones = cmd.target || this.getters.getSelectedZones();
Expand Down
15 changes: 7 additions & 8 deletions src/plugins/ui_stateful/edition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ import {
import { loopThroughReferenceType } from "../../helpers/reference_type";
import { _lt } from "../../translation";
import {
AddColumnsRowsCommand,
CellPosition,
CellValueType,
Command,
CommandResult,
Format,
HeaderIndex,
Highlight,
LocalCommand,
Range,
RangePart,
RemoveColumnsRowsCommand,
UID,
Zone,
} from "../../types";
import { SelectionEvent } from "../../types/event_stream";
import {
AddColumnsRowsCommand,
Command,
CommandResult,
HeaderIndex,
RemoveColumnsRowsCommand,
} from "../../types/index";
import { UIPlugin } from "../ui_plugin";

type EditionMode =
Expand Down Expand Up @@ -80,7 +79,7 @@ export class EditionPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "CHANGE_COMPOSER_CURSOR_SELECTION":
return this.validateSelection(this.currentContent.length, cmd.start, cmd.end);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/ui_stateful/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
GridRenderingContext,
HeaderIndex,
LAYERS,
LocalCommand,
MoveColumnsRowsCommand,
RemoveColumnsRowsCommand,
Selection,
Expand Down Expand Up @@ -132,7 +133,7 @@ export class GridSelectionPlugin extends UIPlugin {
// Command Handling
// ---------------------------------------------------------------------------

allowDispatch(cmd: Command): CommandResult {
allowDispatch(cmd: LocalCommand): CommandResult {
switch (cmd.type) {
case "ACTIVATE_SHEET":
try {
Expand Down
24 changes: 22 additions & 2 deletions tests/collaborative/collaborative.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Model } from "../../src";
import { Model, UIPlugin } from "../../src";
import { DEFAULT_REVISION_ID, MESSAGE_VERSION } from "../../src/constants";
import { functionRegistry } from "../../src/functions";
import { getDefaultCellHeight, range, toCartesian, toZone } from "../../src/helpers";
import { CommandResult, CoreCommand } from "../../src/types";
import { featurePluginRegistry } from "../../src/plugins";
import { Command, CommandResult, CoreCommand } from "../../src/types";
import { CollaborationMessage } from "../../src/types/collaborative/transport_service";
import {
activateSheet,
Expand Down Expand Up @@ -1010,3 +1011,22 @@ describe("Multi users synchronisation", () => {
);
});
});

test("UI plugins cannot refuse core command and de-synchronize the users", () => {
class MyUIPlugin extends UIPlugin {
allowDispatch(cmd: Command) {
if (cmd.type === "UPDATE_CELL") {
return this.getters.getClient().name === "Alice"
? CommandResult.Success
: CommandResult.CancelledForUnknownReason;
}
return CommandResult.Success;
}
}
featurePluginRegistry.add("myUIPlugin", MyUIPlugin);
const { alice, bob } = setupCollaborativeEnv();

setCellContent(alice, "A1", "hello");
expect([alice, bob]).toHaveSynchronizedValue((user) => getCellContent(user, "A1"), "hello");
featurePluginRegistry.remove("myUIPlugin");
});
17 changes: 17 additions & 0 deletions tests/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ describe("Model", () => {
featurePluginRegistry.remove("myUIPlugin");
});

test("UI plugins cannot refuse core commands", () => {
class MyUIPlugin extends UIPlugin {
allowDispatch(cmd: Command) {
if (cmd.type === "UPDATE_CELL") {
return CommandResult.CancelledForUnknownReason;
}
return CommandResult.Success;
}
}
featurePluginRegistry.add("myUIPlugin", MyUIPlugin);
const model = new Model();

setCellContent(model, "A1", "hello");
expect(getCellContent(model, "A1")).toBe("hello");
featurePluginRegistry.remove("myUIPlugin");
});

test("Can open a model in readonly mode", () => {
const model = new Model({}, { mode: "readonly" });
expect(model.getters.isReadonly()).toBe(true);
Expand Down

0 comments on commit 96d5e44

Please sign in to comment.