User Management - Support new object types: Server Role, Application Role and Database Role (#22889)

* server role dialogs

* dialogs for other types

* refactor

* find object dialog

* script button

* refactoring

* fix issues

* fix title

* vbump sts

* remove language from links
This commit is contained in:
Alan Ren
2023-04-28 12:05:20 -07:00
committed by GitHub
parent ba09248483
commit 4f53d76eb5
18 changed files with 1411 additions and 480 deletions

View File

@@ -3,36 +3,19 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// TODO:
// 1. include server properties and other properties in the telemetry.
import * as azdata from 'azdata';
import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as vscode from 'vscode';
import { EOL } from 'os';
import { generateUuid } from 'vscode-languageclient/lib/utils/uuid';
import { getErrorMessage } from '../../utils';
import { TelemetryActions, ObjectManagementViewName } from '../constants';
import {
CreateObjectOperationDisplayName, HelpText, LoadingDialogText,
NameText,
NewObjectDialogTitle, NoActionScriptedMessage, ObjectPropertiesDialogTitle, OkText, ScriptError, ScriptGeneratedText, ScriptText, SelectedText, UpdateObjectOperationDisplayName
} from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils';
import * as localizedConstants from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode, refreshParentNode } from '../utils';
import { DialogBase } from './dialogBase';
import { ObjectManagementViewName, TelemetryActions } from '../constants';
import { TelemetryReporter } from '../../telemetry';
import { getErrorMessage } from '../../utils';
import { providerId } from '../../constants';
import { equals } from '../../util/objects';
export const DefaultLabelWidth = 150;
export const DefaultInputWidth = 300;
export const DefaultTableWidth = DefaultInputWidth + DefaultLabelWidth;
export const DefaultTableMaxHeight = 400;
export const DefaultTableMinRowCount = 2;
export const TableRowHeight = 25;
export const TableColumnHeaderHeight = 30;
export function getTableHeight(rowCount: number, minRowCount: number = DefaultTableMinRowCount, maxHeight: number = DefaultTableMaxHeight): number {
return Math.min(Math.max(rowCount, minRowCount) * TableRowHeight + TableColumnHeaderHeight, maxHeight);
}
function getDialogName(type: ObjectManagement.NodeType, isNewObject: boolean): string {
return isNewObject ? `New${type}` : `${type}Properties`
@@ -50,56 +33,90 @@ export interface ObjectManagementDialogOptions {
objectName?: string;
}
export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectManagement.SqlObject, ViewInfoType extends ObjectManagement.ObjectViewInfo<ObjectInfoType>> {
protected readonly disposables: vscode.Disposable[] = [];
protected readonly dialogObject: azdata.window.Dialog;
export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectManagement.SqlObject, ViewInfoType extends ObjectManagement.ObjectViewInfo<ObjectInfoType>> extends DialogBase<void> {
private _contextId: string;
private _viewInfo: ViewInfoType;
private _originalObjectInfo: ObjectInfoType;
private _modelView: azdata.ModelView;
private _loadingComponent: azdata.LoadingComponent;
private _formContainer: azdata.DivContainer;
private _helpButton: azdata.window.Button;
private _scriptButton: azdata.window.Button;
constructor(protected readonly objectManagementService: IObjectManagementService, protected readonly options: ObjectManagementDialogOptions) {
this.options.width = this.options.width || 'narrow';
const objectTypeDisplayName = getNodeTypeDisplayName(options.objectType, true);
const dialogTitle = options.isNewObject ? NewObjectDialogTitle(objectTypeDisplayName) : ObjectPropertiesDialogTitle(objectTypeDisplayName, options.objectName);
this.dialogObject = azdata.window.createModelViewDialog(dialogTitle, getDialogName(options.objectType, options.isNewObject), options.width);
this.dialogObject.okButton.label = OkText;
this.disposables.push(this.dialogObject.onClosed(async (reason: azdata.window.CloseReason) => { await this.dispose(reason); }));
this._helpButton = azdata.window.createButton(HelpText, 'left');
super(options.isNewObject ? localizedConstants.NewObjectDialogTitle(getNodeTypeDisplayName(options.objectType, true)) :
localizedConstants.ObjectPropertiesDialogTitle(getNodeTypeDisplayName(options.objectType, true), options.objectName),
getDialogName(options.objectType, options.isNewObject),
options.width || 'narrow', 'flyout'
);
this._helpButton = azdata.window.createButton(localizedConstants.HelpText, 'left');
this.disposables.push(this._helpButton.onClick(async () => {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(this.docUrl));
}));
this._scriptButton = azdata.window.createButton(ScriptText, 'left');
this._scriptButton = azdata.window.createButton(localizedConstants.ScriptText, 'left');
this.disposables.push(this._scriptButton.onClick(async () => { await this.onScriptButtonClick(); }));
this.dialogObject.customButtons = [this._helpButton, this._scriptButton];
this.updateLoadingStatus(true);
this._contextId = generateUuid();
this.dialogObject.registerCloseValidator(async (): Promise<boolean> => {
const confirmed = await this.onConfirmation();
if (!confirmed) {
return false;
}
return await this.runValidation();
});
}
protected abstract initializeUI(): Promise<void>;
protected abstract validateInput(): Promise<string[]>;
protected abstract get docUrl(): string;
protected postInitializeData(): void {
protected postInitializeData(): void { }
}
protected onObjectValueChange(): void {
protected override onFormFieldChange(): void {
this._scriptButton.enabled = this.isDirty;
this.dialogObject.okButton.enabled = this.isDirty;
}
protected async onConfirmation(): Promise<boolean> {
return true;
protected override async validateInput(): Promise<string[]> {
const errors: string[] = [];
if (!this.objectInfo.name) {
errors.push(localizedConstants.NameCannotBeEmptyError);
}
return errors;
}
protected override async initialize(): Promise<void> {
await this.initializeData();
await this.initializeUI();
const typeDisplayName = getNodeTypeDisplayName(this.options.objectType);
this.dialogObject.registerOperation({
displayName: this.options.isNewObject ? localizedConstants.CreateObjectOperationDisplayName(typeDisplayName)
: localizedConstants.UpdateObjectOperationDisplayName(typeDisplayName, this.options.objectName),
description: '',
isCancelable: false,
operation: async (operation: azdata.BackgroundOperation): Promise<void> => {
const actionName = this.options.isNewObject ? TelemetryActions.CreateObject : TelemetryActions.UpdateObject;
try {
if (this.isDirty) {
const startTime = Date.now();
await this.objectManagementService.save(this._contextId, this.objectInfo);
if (this.options.objectExplorerContext) {
if (this.options.isNewObject) {
await refreshNode(this.options.objectExplorerContext);
} else {
// For edit mode, the node context is the object itself, we need to refresh the parent node to reflect the changes.
await refreshParentNode(this.options.objectExplorerContext);
}
}
TelemetryReporter.sendTelemetryEvent(actionName, {
objectType: this.options.objectType
}, {
elapsedTimeMs: Date.now() - startTime
});
operation.updateStatus(azdata.TaskStatus.Succeeded);
}
}
catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, getErrorMessage(err));
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, actionName, err).withAdditionalProperties({
objectType: this.options.objectType
}).send();
} finally {
await this.disposeView();
}
}
});
}
protected get viewInfo(): ViewInfoType {
@@ -114,85 +131,12 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
return this._originalObjectInfo;
}
protected get formContainer(): azdata.DivContainer {
return this._formContainer;
protected get contextId(): string {
return this._contextId;
}
protected get modelView(): azdata.ModelView {
return this._modelView;
}
public async open(): Promise<void> {
try {
const initializeViewPromise = new Promise<void>((async resolve => {
await this.dialogObject.registerContent(async view => {
this._modelView = view;
resolve();
this._formContainer = this.createFormContainer([]);
this._loadingComponent = view.modelBuilder.loadingComponent().withItem(this._formContainer).withProps({
loading: true,
loadingText: LoadingDialogText,
showText: true,
CSSStyles: {
width: "100%",
height: "100%"
}
}).component();
await view.initializeModel(this._loadingComponent);
});
}));
azdata.window.openDialog(this.dialogObject);
await this.initializeData();
await initializeViewPromise;
await this.initializeUI();
this._originalObjectInfo = deepClone(this.objectInfo);
const typeDisplayName = getNodeTypeDisplayName(this.options.objectType);
this.dialogObject.registerOperation({
displayName: this.options.isNewObject ? CreateObjectOperationDisplayName(typeDisplayName)
: UpdateObjectOperationDisplayName(typeDisplayName, this.options.objectName),
description: '',
isCancelable: false,
operation: async (operation: azdata.BackgroundOperation): Promise<void> => {
const actionName = this.options.isNewObject ? TelemetryActions.CreateObject : TelemetryActions.UpdateObject;
try {
if (JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo)) {
const startTime = Date.now();
await this.objectManagementService.save(this._contextId, this.objectInfo);
if (this.options.isNewObject && this.options.objectExplorerContext) {
await refreshNode(this.options.objectExplorerContext);
}
TelemetryReporter.sendTelemetryEvent(actionName, {
objectType: this.options.objectType
}, {
elapsedTimeMs: Date.now() - startTime
});
operation.updateStatus(azdata.TaskStatus.Succeeded);
}
}
catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, getErrorMessage(err));
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, actionName, err).withAdditionalProperties({
objectType: this.options.objectType
}).send();
} finally {
await this.disposeView();
}
}
});
this.updateLoadingStatus(false);
} catch (err) {
const actionName = this.options.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog;
TelemetryReporter.createErrorEvent2(ObjectManagementViewName, actionName, err).withAdditionalProperties({
objectType: this.options.objectType
}).send();
void vscode.window.showErrorMessage(getErrorMessage(err));
azdata.window.closeDialog(this.dialogObject);
}
}
private async dispose(reason: azdata.window.CloseReason): Promise<void> {
this.disposables.forEach(disposable => disposable.dispose());
protected override async dispose(reason: azdata.window.CloseReason): Promise<void> {
await super.dispose(reason);
if (reason !== 'ok') {
await this.disposeView();
}
@@ -206,140 +150,17 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
const viewInfo = await this.objectManagementService.initializeView(this._contextId, this.options.objectType, this.options.connectionUri, this.options.database, this.options.isNewObject, this.options.parentUrn, this.options.objectUrn);
this._viewInfo = viewInfo as ViewInfoType;
this.postInitializeData();
this._originalObjectInfo = deepClone(this.objectInfo);
}
protected async runValidation(showErrorMessage: boolean = true): Promise<boolean> {
const errors = await this.validateInput();
if (errors.length > 0 && (this.dialogObject.message?.text || showErrorMessage)) {
this.dialogObject.message = {
text: errors.join(EOL),
level: azdata.window.MessageLevel.Error
};
} else {
this.dialogObject.message = undefined;
}
return errors.length === 0;
}
protected createLabelInputContainer(label: string, input: azdata.InputBoxComponent | azdata.DropDownComponent): azdata.FlexContainer {
const labelComponent = this.modelView.modelBuilder.text().withProps({ width: DefaultLabelWidth, value: label, requiredIndicator: input.required }).component();
const row = this.modelView.modelBuilder.flexContainer().withLayout({ flexFlow: 'horizontal', flexWrap: 'nowrap', alignItems: 'center' }).withItems([labelComponent, input]).component();
return row;
}
protected createCheckbox(label: string, checked: boolean = false, enabled: boolean = true): azdata.CheckBoxComponent {
return this.modelView.modelBuilder.checkBox().withProps({
label: label,
checked: checked,
enabled: enabled
}).component();
}
protected createPasswordInputBox(ariaLabel: string, value: string = '', enabled: boolean = true, width: number = DefaultInputWidth): azdata.InputBoxComponent {
return this.createInputBox(ariaLabel, value, enabled, 'password', width);
}
protected createInputBox(ariaLabel: string, value: string = '', enabled: boolean = true, type: azdata.InputBoxInputType = 'text', width: number = DefaultInputWidth): azdata.InputBoxComponent {
return this.modelView.modelBuilder.inputBox().withProps({ inputType: type, enabled: enabled, ariaLabel: ariaLabel, value: value, width: width }).component();
}
protected createGroup(header: string, items: azdata.Component[], collapsible: boolean = true, collapsed: boolean = false): azdata.GroupContainer {
return this.modelView.modelBuilder.groupContainer().withLayout({
header: header,
collapsible: collapsible,
collapsed: collapsed
}).withItems(items).component();
}
protected createFormContainer(items: azdata.Component[]): azdata.DivContainer {
return this.modelView.modelBuilder.divContainer().withLayout({ width: 'calc(100% - 20px)', height: 'calc(100% - 20px)' }).withProps({
CSSStyles: { 'padding': '10px' }
}).withItems(items, { CSSStyles: { 'margin-block-end': '10px' } }).component();
}
protected createTableList(ariaLabel: string, listValues: string[], selectedValues: string[], data?: any[][]): azdata.TableComponent {
let tableData = data;
if (tableData === undefined) {
tableData = listValues.map(name => {
const isSelected = selectedValues.indexOf(name) !== -1;
return [isSelected, name];
});
}
const table = this.modelView.modelBuilder.table().withProps(
{
ariaLabel: ariaLabel,
data: tableData,
columns: [
{
value: SelectedText,
type: azdata.ColumnType.checkBox,
options: { actionOnCheckbox: azdata.ActionOnCellCheckboxCheck.customAction }
}, {
value: NameText,
}
],
width: DefaultTableWidth,
height: getTableHeight(tableData.length)
}
).component();
this.disposables.push(table.onCellAction!((arg: azdata.ICheckboxCellActionEventArgs) => {
const name = listValues[arg.row];
const idx = selectedValues.indexOf(name);
if (arg.checked && idx === -1) {
selectedValues.push(name);
} else if (!arg.checked && idx !== -1) {
selectedValues.splice(idx, 1)
}
this.onObjectValueChange();
}));
return table;
}
protected createDropdown(ariaLabel: string, values: string[], value: string | undefined, enabled: boolean = true, width: number = DefaultInputWidth): azdata.DropDownComponent {
// Automatically add an empty item to the beginning of the list if the current value is not specified.
// This is needed when no meaningful default value can be provided.
// Create a new array so that the original array isn't modified.
const dropdownValues = [];
dropdownValues.push(...values);
if (!value) {
dropdownValues.unshift('');
}
return this.modelView.modelBuilder.dropDown().withProps({
ariaLabel: ariaLabel,
values: dropdownValues,
value: value,
width: width,
enabled: enabled
}).component();
}
protected removeItem(container: azdata.DivContainer | azdata.FlexContainer, item: azdata.Component): void {
if (container.items.indexOf(item) !== -1) {
container.removeItem(item);
}
}
protected addItem(container: azdata.DivContainer | azdata.FlexContainer, item: azdata.Component, index?: number): void {
if (container.items.indexOf(item) === -1) {
if (index === undefined) {
container.addItem(item);
} else {
container.insertItem(item, index);
}
}
}
private updateLoadingStatus(isLoading: boolean): void {
this._scriptButton.enabled = !isLoading;
protected override onLoadingStatusChanged(isLoading: boolean): void {
super.onLoadingStatusChanged(isLoading);
this._helpButton.enabled = !isLoading;
this.dialogObject.okButton.enabled = isLoading ? false : this.isDirty;
if (this._loadingComponent) {
this._loadingComponent.loading = isLoading;
}
this.dialogObject.okButton.enabled = this._scriptButton.enabled = isLoading ? false : this.isDirty;
}
private async onScriptButtonClick(): Promise<void> {
this.updateLoadingStatus(true);
this.onLoadingStatusChanged(true);
try {
const isValid = await this.runValidation();
if (!isValid) {
@@ -348,10 +169,10 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
let message: string;
const script = await this.objectManagementService.script(this._contextId, this.objectInfo);
if (script) {
message = ScriptGeneratedText;
message = localizedConstants.ScriptGeneratedText;
await azdata.queryeditor.openQueryDocument({ content: script }, providerId);
} else {
message = NoActionScriptedMessage;
message = localizedConstants.NoActionScriptedMessage;
}
this.dialogObject.message = {
text: message,
@@ -359,15 +180,15 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
};
} catch (err) {
this.dialogObject.message = {
text: ScriptError(getErrorMessage(err)),
text: localizedConstants.ScriptError(getErrorMessage(err)),
level: azdata.window.MessageLevel.Error
};
} finally {
this.updateLoadingStatus(false);
this.onLoadingStatusChanged(false);
}
}
private get isDirty(): boolean {
return JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo);
return !equals(this.objectInfo, this._originalObjectInfo, false);
}
}