enhance: improve command filter (#2492)

This commit is contained in:
Baptiste Augrain
2025-09-03 16:20:23 +02:00
committed by GitHub
parent e648323548
commit f574039dc4

View File

@@ -1,5 +1,5 @@
diff --git a/src/vs/workbench/contrib/commands/common/commands.contribution.ts b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
index 3fd6b59..04bb34f 100644
index 3fd6b59..97a0e04 100644
--- a/src/vs/workbench/contrib/commands/common/commands.contribution.ts
+++ b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
@@ -9,2 +9,3 @@ import { Action2, registerAction2 } from '../../../../platform/actions/common/ac
@@ -10,7 +10,7 @@ index 3fd6b59..04bb34f 100644
import { INotificationService } from '../../../../platform/notification/common/notification.js';
+import { Registry } from '../../../../platform/registry/common/platform.js';
@@ -156,2 +158,31 @@ class RunCommands extends Action2 {
@@ -156,2 +158,30 @@ class RunCommands extends Action2 {
+Registry.as<IConfigurationRegistry>('base.contributions.configuration')
+ .registerConfiguration({
@@ -20,17 +20,16 @@ index 3fd6b59..04bb34f 100644
+ type: 'object',
+ properties: {
+ 'commands.filters': {
+ enum: ['off', 'on',],
+ enumItemLabels: [
+ // nls.localize('ask', "Ask"),
+ nls.localize('off', "Never authorized"),
+ nls.localize('on', "Always authorized"),
+ ],
+ additionalProperties: {
+ type: 'string',
+ enum: ['ask', 'off', 'on'],
+ enumDescriptions: [
+ // nls.localize('commands.filters.ask', 'Ask the user before executing the command.'),
+ nls.localize('commands.filters.ask', 'Ask the user before executing the command.'),
+ nls.localize('commands.filters.off', 'The command is never authorized.'),
+ nls.localize('commands.filters.on', 'The command is always authorized.'),
+ ],
+ description: nls.localize('commands.filters.value', "Authorization for the command."),
+ },
+ description: nls.localize('commands.filters', "Controls which commands are authorized to be executed."),
+ default: {
+ 'workbench.action.terminal.newLocal': 'off'
@@ -43,52 +42,78 @@ index 3fd6b59..04bb34f 100644
+
registerAction2(RunCommands);
diff --git a/src/vs/workbench/services/commands/common/commandService.ts b/src/vs/workbench/services/commands/common/commandService.ts
index 93d1631..f21ca94 100644
index 93d1631..0533cf0 100644
--- a/src/vs/workbench/services/commands/common/commandService.ts
+++ b/src/vs/workbench/services/commands/common/commandService.ts
@@ -9,2 +9,3 @@ import { Disposable } from '../../../../base/common/lifecycle.js';
@@ -8,3 +8,6 @@ import { Emitter, Event } from '../../../../base/common/event.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
+import Severity from '../../../../base/common/severity.js';
import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../../platform/commands/common/commands.js';
+import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
+import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
@@ -30,3 +31,4 @@ export class CommandService extends Disposable implements ICommandService {
@@ -20,2 +23,3 @@ export class CommandService extends Disposable implements ICommandService {
private _starActivation: CancelablePromise<void> | null;
+ private _commandFilters: Record<string, "ask" | "off" | "on">
@@ -30,3 +34,5 @@ export class CommandService extends Disposable implements ICommandService {
@IExtensionService private readonly _extensionService: IExtensionService,
- @ILogService private readonly _logService: ILogService
+ @ILogService private readonly _logService: ILogService,
+ @IConfigurationService private readonly configurationService: IConfigurationService
+ @IConfigurationService private readonly _configurationService: IConfigurationService,
+ @IDialogService private readonly _dialogService: IDialogService
) {
@@ -56,2 +58,20 @@ export class CommandService extends Disposable implements ICommandService {
const commandIsRegistered = !!CommandsRegistry.getCommand(id);
+ const commandFilters = this.configurationService.getValue<Record<string, 'ask' | 'off' | 'on'>>('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
@@ -35,2 +41,9 @@ export class CommandService extends Disposable implements ICommandService {
this._starActivation = null;
+ this._commandFilters = this._configurationService.getValue('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
+
+ const filter = commandFilters[id];
+ this._configurationService.onDidChangeConfiguration(async (event) => {
+ if (event.affectsConfiguration('commands.filters')) {
+ this._commandFilters = this._configurationService.getValue('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
+ }
+ })
}
@@ -57,2 +70,27 @@ export class CommandService extends Disposable implements ICommandService {
+ const filter = this._commandFilters[id];
+ if (filter === 'off') {
+ return Promise.reject(new Error(`command '${id}' not authorized`));
+ }
+ // if (filter === 'ask') {
+ // const result = await showWarningMessage(
+ // `Are you sure you want to execute the command "${id}"?`,
+ // { modal: true }, // this makes the dialog modal (blocks other input)
+ // 'Yes',
+ // 'No'
+ // );
+ else if (filter === 'ask') {
+ const { result } = await this._dialogService.prompt({
+ type: Severity.Error,
+ message: `Are you sure you want to execute the command "${id}"?`,
+ buttons: [
+ {
+ label: 'Yes',
+ run: () => true
+ },
+ {
+ label: 'No',
+ run: () => false
+ }
+ ],
+ });
+
+ // if (result === 'No') {
+ // return Promise.reject(new Error(`command '${id}' not authorized`));
+ // }
+ // }
+ if (!result) {
+ return Promise.reject(new Error(`command '${id}' not authorized`));
+ }
+ }
+
if (commandIsRegistered) {
diff --git a/src/vs/workbench/services/commands/test/common/commandService.test.ts b/src/vs/workbench/services/commands/test/common/commandService.test.ts
index ca3be11..38b0474 100644
index ca3be11..fb456a3 100644
--- a/src/vs/workbench/services/commands/test/common/commandService.test.ts
+++ b/src/vs/workbench/services/commands/test/common/commandService.test.ts
@@ -12,2 +12,6 @@ import { NullExtensionService } from '../../../extensions/common/extensions.js';
@@ -12,2 +12,7 @@ import { NullExtensionService } from '../../../extensions/common/extensions.js';
import { CommandService } from '../../common/commandService.js';
+import { NullPolicyService } from '../../../../../platform/policy/common/policy.js';
+import { FileService } from '../../../../../platform/files/common/fileService.js';
+import { ConfigurationService } from '../../../../../platform/configuration/common/configurationService.js';
+import { URI } from '../../../../../base/common/uri.js';
+import { ConfigurationService } from '../../../../../platform/configuration/common/configurationService.js';
+import { TestDialogService } from '../../../../../platform/dialogs/test/common/testDialogService.js';
@@ -16,4 +20,16 @@ suite('CommandService', function () {
@@ -16,4 +21,16 @@ suite('CommandService', function () {
const store = ensureNoDisposablesAreLeakedInTestSuite();
+ const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();
+ let nullConfigService: ConfigurationService
@@ -105,38 +130,38 @@ index ca3be11..38b0474 100644
+ ));
+
store.add(CommandsRegistry.registerCommand('foo', function () { }));
@@ -30,3 +46,3 @@ suite('CommandService', function () {
@@ -30,3 +47,3 @@ suite('CommandService', function () {
}
- }, new NullLogService()));
+ }, new NullLogService(), nullConfigService));
+ }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -50,3 +66,3 @@ suite('CommandService', function () {
@@ -50,3 +67,3 @@ suite('CommandService', function () {
- const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -68,3 +84,3 @@ suite('CommandService', function () {
@@ -68,3 +85,3 @@ suite('CommandService', function () {
}
- }, new NullLogService()));
+ }, new NullLogService(), nullConfigService));
+ }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -85,3 +101,3 @@ suite('CommandService', function () {
@@ -85,3 +102,3 @@ suite('CommandService', function () {
}
- }, new NullLogService()));
+ }, new NullLogService(), nullConfigService));
+ }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -125,3 +141,3 @@ suite('CommandService', function () {
@@ -125,3 +142,3 @@ suite('CommandService', function () {
- }, new NullLogService()));
+ }, new NullLogService(), nullConfigService));
+ }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -166,3 +182,3 @@ suite('CommandService', function () {
@@ -166,3 +183,3 @@ suite('CommandService', function () {
- }, new NullLogService()));
+ }, new NullLogService(), nullConfigService));
+ }, new NullLogService(), nullConfigService, new TestDialogService()));
@@ -187,3 +203,3 @@ suite('CommandService', function () {
@@ -187,3 +204,3 @@ suite('CommandService', function () {
};
- const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService));
+ const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService, new TestDialogService()));