Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/sessions/contrib/policyBlocked/browser/policyBlocked.contribution.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable, MutableDisposable } from '../../../../base/common/lifecycle.js';
7
import { IWorkbenchContribution, registerWorkbenchContribution2, WorkbenchPhase } from '../../../../workbench/common/contributions.js';
8
import { IWorkbenchLayoutService } from '../../../../workbench/services/layout/browser/layoutService.js';
9
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
10
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
11
import { IDefaultAccountService } from '../../../../platform/defaultAccount/common/defaultAccount.js';
12
import { ChatConfiguration } from '../../../../workbench/contrib/chat/common/constants.js';
13
import { ISessionsBlockedOverlayOptions, SessionsBlockedReason, SessionsPolicyBlockedOverlay } from './sessionsPolicyBlocked.js';
14
import { AccountPolicyGateState, AccountPolicyGateUnsatisfiedReason, IAccountPolicyGateService } from '../../../../workbench/services/policies/common/accountPolicyService.js';
15
16
export class SessionsPolicyBlockedContribution extends Disposable implements IWorkbenchContribution {
17
18
static readonly ID = 'workbench.contrib.sessionsPolicyBlocked';
19
20
private readonly overlayRef = this._register(new MutableDisposable());
21
private currentReason: SessionsBlockedReason | undefined;
22
23
constructor(
24
@IConfigurationService private readonly configurationService: IConfigurationService,
25
@IWorkbenchLayoutService private readonly layoutService: IWorkbenchLayoutService,
26
@IInstantiationService private readonly instantiationService: IInstantiationService,
27
@IAccountPolicyGateService private readonly gateService: IAccountPolicyGateService,
28
@IDefaultAccountService private readonly defaultAccountService: IDefaultAccountService,
29
) {
30
super();
31
32
this.update();
33
34
this._register(this.configurationService.onDidChangeConfiguration(e => {
35
if (e.affectsConfiguration(ChatConfiguration.AgentEnabled)) {
36
this.update();
37
}
38
}));
39
40
this._register(this.gateService.onDidChangeGateInfo(() => this.update()));
41
}
42
43
private update(): void {
44
const gateInfo = this.gateService.gateInfo;
45
46
// The gate forces chat.agent.enabled = false via restrictedValue when stably
47
// Restricted. Suppress AgentDisabled in that case so users see the gate-specific
48
// overlay (or the welcome screen for noAccount/wrongProvider) instead.
49
const gateForcesAgentDisabled = gateInfo.state === AccountPolicyGateState.Restricted
50
&& gateInfo.reason !== AccountPolicyGateUnsatisfiedReason.PolicyNotResolved;
51
52
const agentEnabled = this.configurationService.getValue<boolean>(ChatConfiguration.AgentEnabled);
53
if (agentEnabled === false && !gateForcesAgentDisabled) {
54
this.showOverlay({ reason: SessionsBlockedReason.AgentDisabled });
55
return;
56
}
57
58
if (gateInfo.state === AccountPolicyGateState.Restricted) {
59
// Defer to the sessions welcome/walkthrough so the user signs in via the standard flow.
60
if (gateInfo.reason === AccountPolicyGateUnsatisfiedReason.NoAccount
61
|| gateInfo.reason === AccountPolicyGateUnsatisfiedReason.WrongProvider) {
62
this.overlayRef.clear();
63
this.currentReason = undefined;
64
return;
65
}
66
67
if (gateInfo.reason === AccountPolicyGateUnsatisfiedReason.PolicyNotResolved) {
68
this.showOverlay({ reason: SessionsBlockedReason.Loading });
69
} else {
70
const accountName = this.defaultAccountService.currentDefaultAccount?.accountName;
71
this.showOverlay({
72
reason: SessionsBlockedReason.AccountPolicyGate,
73
approvedOrganizations: gateInfo.approvedOrganizations,
74
accountName,
75
});
76
}
77
return;
78
}
79
80
this.overlayRef.clear();
81
this.currentReason = undefined;
82
}
83
84
private showOverlay(options: ISessionsBlockedOverlayOptions): void {
85
// AccountPolicyGate may need re-render when the account name changes.
86
if (this.currentReason === options.reason && options.reason !== SessionsBlockedReason.AccountPolicyGate) {
87
return;
88
}
89
this.overlayRef.clear();
90
this.currentReason = options.reason;
91
92
this.overlayRef.value = this.instantiationService.createInstance(
93
SessionsPolicyBlockedOverlay,
94
this.layoutService.mainContainer,
95
options,
96
);
97
}
98
}
99
100
registerWorkbenchContribution2(SessionsPolicyBlockedContribution.ID, SessionsPolicyBlockedContribution, WorkbenchPhase.BlockRestore);
101
102