feat-command-filter.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. diff --git a/src/vs/workbench/contrib/commands/common/commands.contribution.ts b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
  2. index 3fd6b59..04bb34f 100644
  3. --- a/src/vs/workbench/contrib/commands/common/commands.contribution.ts
  4. +++ b/src/vs/workbench/contrib/commands/common/commands.contribution.ts
  5. @@ -9,2 +9,3 @@ import { Action2, registerAction2 } from '../../../../platform/actions/common/ac
  6. import { ICommandService } from '../../../../platform/commands/common/commands.js';
  7. +import { ConfigurationScope, IConfigurationRegistry } from '../../../../platform/configuration/common/configurationRegistry.js';
  8. import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
  9. @@ -12,2 +13,3 @@ import { ILogService } from '../../../../platform/log/common/log.js';
  10. import { INotificationService } from '../../../../platform/notification/common/notification.js';
  11. +import { Registry } from '../../../../platform/registry/common/platform.js';
  12. @@ -156,2 +158,31 @@ class RunCommands extends Action2 {
  13. +Registry.as<IConfigurationRegistry>('base.contributions.configuration')
  14. + .registerConfiguration({
  15. + id: 'commands',
  16. + order: 30,
  17. + title: nls.localize('commandsConfigurationTitle', "Commands"),
  18. + type: 'object',
  19. + properties: {
  20. + 'commands.filters': {
  21. + enum: ['off', 'on',],
  22. + enumItemLabels: [
  23. + // nls.localize('ask', "Ask"),
  24. + nls.localize('off', "Never authorized"),
  25. + nls.localize('on', "Always authorized"),
  26. + ],
  27. + enumDescriptions: [
  28. + // nls.localize('commands.filters.ask', 'Ask the user before executing the command.'),
  29. + nls.localize('commands.filters.off', 'The command is never authorized.'),
  30. + nls.localize('commands.filters.on', 'The command is always authorized.'),
  31. + ],
  32. + description: nls.localize('commands.filters', "Controls which commands are authorized to be executed."),
  33. + default: {
  34. + 'workbench.action.terminal.newLocal': 'off'
  35. + },
  36. + scope: ConfigurationScope.APPLICATION,
  37. + tags: []
  38. + },
  39. + }
  40. + });
  41. +
  42. registerAction2(RunCommands);
  43. diff --git a/src/vs/workbench/services/commands/common/commandService.ts b/src/vs/workbench/services/commands/common/commandService.ts
  44. index 93d1631..f21ca94 100644
  45. --- a/src/vs/workbench/services/commands/common/commandService.ts
  46. +++ b/src/vs/workbench/services/commands/common/commandService.ts
  47. @@ -9,2 +9,3 @@ import { Disposable } from '../../../../base/common/lifecycle.js';
  48. import { CommandsRegistry, ICommandEvent, ICommandService } from '../../../../platform/commands/common/commands.js';
  49. +import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
  50. import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
  51. @@ -30,3 +31,4 @@ export class CommandService extends Disposable implements ICommandService {
  52. @IExtensionService private readonly _extensionService: IExtensionService,
  53. - @ILogService private readonly _logService: ILogService
  54. + @ILogService private readonly _logService: ILogService,
  55. + @IConfigurationService private readonly configurationService: IConfigurationService
  56. ) {
  57. @@ -56,2 +58,20 @@ export class CommandService extends Disposable implements ICommandService {
  58. const commandIsRegistered = !!CommandsRegistry.getCommand(id);
  59. + const commandFilters = this.configurationService.getValue<Record<string, 'ask' | 'off' | 'on'>>('commands.filters') ?? { 'workbench.action.terminal.newLocal': 'off' };
  60. +
  61. + const filter = commandFilters[id];
  62. + if (filter === 'off') {
  63. + return Promise.reject(new Error(`command '${id}' not authorized`));
  64. + }
  65. + // if (filter === 'ask') {
  66. + // const result = await showWarningMessage(
  67. + // `Are you sure you want to execute the command "${id}"?`,
  68. + // { modal: true }, // this makes the dialog modal (blocks other input)
  69. + // 'Yes',
  70. + // 'No'
  71. + // );
  72. +
  73. + // if (result === 'No') {
  74. + // return Promise.reject(new Error(`command '${id}' not authorized`));
  75. + // }
  76. + // }
  77. diff --git a/src/vs/workbench/services/commands/test/common/commandService.test.ts b/src/vs/workbench/services/commands/test/common/commandService.test.ts
  78. index ca3be11..38b0474 100644
  79. --- a/src/vs/workbench/services/commands/test/common/commandService.test.ts
  80. +++ b/src/vs/workbench/services/commands/test/common/commandService.test.ts
  81. @@ -12,2 +12,6 @@ import { NullExtensionService } from '../../../extensions/common/extensions.js';
  82. import { CommandService } from '../../common/commandService.js';
  83. +import { NullPolicyService } from '../../../../../platform/policy/common/policy.js';
  84. +import { FileService } from '../../../../../platform/files/common/fileService.js';
  85. +import { ConfigurationService } from '../../../../../platform/configuration/common/configurationService.js';
  86. +import { URI } from '../../../../../base/common/uri.js';
  87. @@ -16,4 +20,16 @@ suite('CommandService', function () {
  88. const store = ensureNoDisposablesAreLeakedInTestSuite();
  89. + const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();
  90. + let nullConfigService: ConfigurationService
  91. setup(function () {
  92. + const nullPolicyService = new NullPolicyService();
  93. + const nullLogService = testDisposables.add(new NullLogService());
  94. + const nullFileService = testDisposables.add(new FileService(nullLogService));
  95. + nullConfigService = testDisposables.add(new ConfigurationService(
  96. + URI.file('/config.json'),
  97. + nullFileService,
  98. + nullPolicyService,
  99. + nullLogService,
  100. + ));
  101. +
  102. store.add(CommandsRegistry.registerCommand('foo', function () { }));
  103. @@ -30,3 +46,3 @@ suite('CommandService', function () {
  104. }
  105. - }, new NullLogService()));
  106. + }, new NullLogService(), nullConfigService));
  107. @@ -50,3 +66,3 @@ suite('CommandService', function () {
  108. - const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
  109. + const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService));
  110. @@ -68,3 +84,3 @@ suite('CommandService', function () {
  111. }
  112. - }, new NullLogService()));
  113. + }, new NullLogService(), nullConfigService));
  114. @@ -85,3 +101,3 @@ suite('CommandService', function () {
  115. }
  116. - }, new NullLogService()));
  117. + }, new NullLogService(), nullConfigService));
  118. @@ -125,3 +141,3 @@ suite('CommandService', function () {
  119. - }, new NullLogService()));
  120. + }, new NullLogService(), nullConfigService));
  121. @@ -166,3 +182,3 @@ suite('CommandService', function () {
  122. - }, new NullLogService()));
  123. + }, new NullLogService(), nullConfigService));
  124. @@ -187,3 +203,3 @@ suite('CommandService', function () {
  125. };
  126. - const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService()));
  127. + const service = store.add(new CommandService(new InstantiationService(), extensionService, new NullLogService(), nullConfigService));