Add support for Login and User management (#21981)

* initial commit

* leave only march release objects

* clean up

* login dialog

* localize and use background operation

* code cleanup

* remove tab

* support server role in login

* remove canEditName

* add user support

* comments and bug fixes

* remove hasDBAccess for now

* refactoring

* fix error

* user dialog UI

* telemetry, error handling and refactoring

* Fix references to dialogInfo (#21914)

* update telemetry

* Bump STS and use actual object management service

* add preview and handle no-change scenario

* fix merge issue

---------

Co-authored-by: Karl Burtram <karlb@microsoft.com>
This commit is contained in:
Alan Ren
2023-02-17 18:02:31 -08:00
committed by GitHub
parent 6231df85e0
commit b5ce7af090
16 changed files with 2169 additions and 13 deletions

View File

@@ -0,0 +1,299 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import { DefaultInputWidth, ObjectManagementDialogBase } from './objectManagementDialogBase';
import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as localizedConstants from '../localizedConstants';
import { AlterLoginDocUrl, AuthenticationType, CreateLoginDocUrl, NodeType, PublicServerRoleName } from '../constants';
import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, isValidSQLPassword } from '../utils';
export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Login, ObjectManagement.LoginViewInfo> {
private formContainer: azdata.DivContainer;
private generalSection: azdata.GroupContainer;
private sqlAuthSection: azdata.GroupContainer;
private serverRoleSection: azdata.GroupContainer;
private advancedSection: azdata.GroupContainer;
private nameInput: azdata.InputBoxComponent;
private authTypeDropdown: azdata.DropDownComponent;
private passwordInput: azdata.InputBoxComponent;
private confirmPasswordInput: azdata.InputBoxComponent;
private specifyOldPasswordCheckbox: azdata.CheckBoxComponent;
private oldPasswordInput: azdata.InputBoxComponent;
private enforcePasswordPolicyCheckbox: azdata.CheckBoxComponent;
private enforcePasswordExpirationCheckbox: azdata.CheckBoxComponent;
private mustChangePasswordCheckbox: azdata.CheckBoxComponent;
private defaultDatabaseDropdown: azdata.DropDownComponent;
private defaultLanguageDropdown: azdata.DropDownComponent;
private serverRoleTable: azdata.TableComponent;
private connectPermissionCheckbox: azdata.CheckBoxComponent;
private enabledCheckbox: azdata.CheckBoxComponent;
private lockedOutCheckbox: azdata.CheckBoxComponent;
constructor(objectManagementService: IObjectManagementService, connectionUri: string, isNewObject: boolean, name?: string, objectExplorerContext?: azdata.ObjectExplorerContext) {
super(NodeType.Login, isNewObject ? CreateLoginDocUrl : AlterLoginDocUrl, objectManagementService, connectionUri, isNewObject, name, objectExplorerContext);
}
protected override async onConfirmation(): Promise<boolean> {
// Empty password is only allowed when advanced password options are supported and the password policy check is off.
// To match the SSMS behavior, a warning is shown to the user.
if (this.viewInfo.supportAdvancedPasswordOptions
&& this.objectInfo.authenticationType === AuthenticationType.Sql
&& !this.objectInfo.password
&& !this.objectInfo.enforcePasswordPolicy) {
const result = await vscode.window.showWarningMessage(localizedConstants.BlankPasswordConfirmationText, { modal: true }, localizedConstants.YesText);
return result === localizedConstants.YesText;
}
return true;
}
protected async validateInput(): Promise<string[]> {
const errors: string[] = [];
if (!this.objectInfo.name) {
errors.push(localizedConstants.NameCannotBeEmptyError);
}
if (this.objectInfo.authenticationType === AuthenticationType.Sql) {
if (!this.objectInfo.password && !(this.viewInfo.supportAdvancedPasswordOptions && !this.objectInfo.enforcePasswordPolicy)) {
errors.push(localizedConstants.PasswordCannotBeEmptyError);
}
if (this.objectInfo.password && (this.objectInfo.enforcePasswordPolicy || !this.viewInfo.supportAdvancedPasswordOptions)
&& !isValidSQLPassword(this.objectInfo.password, this.objectInfo.name)
&& (this.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) {
errors.push(localizedConstants.InvalidPasswordError);
}
if (this.objectInfo.password !== this.confirmPasswordInput.value) {
errors.push(localizedConstants.PasswordsNotMatchError);
}
if (this.specifyOldPasswordCheckbox?.checked && !this.objectInfo.oldPassword) {
errors.push(localizedConstants.OldPasswordCannotBeEmptyError);
}
}
return errors;
}
protected async onComplete(): Promise<void> {
if (this.isNewObject) {
await this.objectManagementService.createLogin(this.contextId, this.objectInfo);
} else {
await this.objectManagementService.updateLogin(this.contextId, this.objectInfo);
}
}
protected async onDispose(): Promise<void> {
await this.objectManagementService.disposeLoginView(this.contextId);
}
protected async initializeData(): Promise<ObjectManagement.LoginViewInfo> {
const viewInfo = await this.objectManagementService.initializeLoginView(this.connectionUri, this.contextId, this.isNewObject, this.objectName);
viewInfo.objectInfo.password = viewInfo.objectInfo.password ?? '';
return viewInfo;
}
protected async initializeUI(): Promise<void> {
this.dialogObject.registerContent(async view => {
const sections: azdata.Component[] = [];
this.initializeGeneralSection(view);
sections.push(this.generalSection);
if (this.isNewObject || this.objectInfo.authenticationType === 'Sql') {
this.initializeSqlAuthSection(view);
sections.push(this.sqlAuthSection);
}
this.initializeServerRolesSection(view);
sections.push(this.serverRoleSection);
if (this.viewInfo.supportAdvancedOptions) {
this.initializeAdvancedSection(view);
sections.push(this.advancedSection);
}
this.formContainer = this.createFormContainer(view, sections);
return view.initializeModel(this.formContainer)
});
}
private initializeGeneralSection(view: azdata.ModelView): void {
this.nameInput = view.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText,
enabled: this.isNewObject,
value: this.objectInfo.name,
width: DefaultInputWidth
}).component();
this.nameInput.onTextChanged(async () => {
this.objectInfo.name = this.nameInput.value;
this.onObjectValueChange();
await this.runValidation(false);
});
const nameContainer = this.createLabelInputContainer(view, localizedConstants.NameText, this.nameInput);
const authTypes = [];
if (this.viewInfo.supportWindowsAuthentication) {
authTypes.push(localizedConstants.WindowsAuthenticationTypeDisplayText);
}
if (this.viewInfo.supportSQLAuthentication) {
authTypes.push(localizedConstants.SQLAuthenticationTypeDisplayText);
}
if (this.viewInfo.supportAADAuthentication) {
authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText);
}
this.authTypeDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.AuthTypeText,
values: authTypes,
value: getAuthenticationTypeDisplayName(this.objectInfo.authenticationType),
width: DefaultInputWidth,
enabled: this.isNewObject
}).component();
this.authTypeDropdown.onValueChanged(async () => {
this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value);
this.setViewByAuthenticationType();
this.onObjectValueChange();
await this.runValidation(false);
});
const authTypeContainer = this.createLabelInputContainer(view, localizedConstants.AuthTypeText, this.authTypeDropdown);
this.enabledCheckbox = this.createCheckbox(view, localizedConstants.EnabledText, this.objectInfo.isEnabled);
this.enabledCheckbox.onChanged(() => {
this.objectInfo.isEnabled = this.enabledCheckbox.checked;
this.onObjectValueChange();
});
this.generalSection = this.createGroup(view, localizedConstants.GeneralSectionHeader, [nameContainer, authTypeContainer, this.enabledCheckbox], false);
}
private initializeSqlAuthSection(view: azdata.ModelView): void {
const items: azdata.Component[] = [];
this.passwordInput = this.createPasswordInputBox(view, localizedConstants.PasswordText, this.objectInfo.password ?? '');
const passwordRow = this.createLabelInputContainer(view, localizedConstants.PasswordText, this.passwordInput);
this.confirmPasswordInput = this.createPasswordInputBox(view, localizedConstants.ConfirmPasswordText, this.objectInfo.password ?? '');
this.passwordInput.onTextChanged(async () => {
this.objectInfo.password = this.passwordInput.value;
this.onObjectValueChange();
await this.runValidation(false);
});
this.confirmPasswordInput.onTextChanged(async () => {
await this.runValidation(false);
});
const confirmPasswordRow = this.createLabelInputContainer(view, localizedConstants.ConfirmPasswordText, this.confirmPasswordInput);
items.push(passwordRow, confirmPasswordRow);
if (!this.isNewObject) {
this.specifyOldPasswordCheckbox = this.createCheckbox(view, localizedConstants.SpecifyOldPasswordText);
this.oldPasswordInput = this.createPasswordInputBox(view, localizedConstants.OldPasswordText, '', false);
const oldPasswordRow = this.createLabelInputContainer(view, localizedConstants.OldPasswordText, this.oldPasswordInput);
this.specifyOldPasswordCheckbox.onChanged(async () => {
this.oldPasswordInput.enabled = this.specifyOldPasswordCheckbox.checked;
this.objectInfo.oldPassword = '';
if (!this.specifyOldPasswordCheckbox.checked) {
this.oldPasswordInput.value = '';
}
this.onObjectValueChange();
await this.runValidation(false);
});
this.oldPasswordInput.onTextChanged(async () => {
this.objectInfo.oldPassword = this.oldPasswordInput.value;
this.onObjectValueChange();
await this.runValidation(false);
});
items.push(this.specifyOldPasswordCheckbox, oldPasswordRow);
}
if (this.viewInfo.supportAdvancedPasswordOptions) {
this.enforcePasswordPolicyCheckbox = this.createCheckbox(view, localizedConstants.EnforcePasswordPolicyText, this.objectInfo.enforcePasswordPolicy);
this.enforcePasswordExpirationCheckbox = this.createCheckbox(view, localizedConstants.EnforcePasswordExpirationText, this.objectInfo.enforcePasswordPolicy);
this.mustChangePasswordCheckbox = this.createCheckbox(view, localizedConstants.MustChangePasswordText, this.objectInfo.mustChangePassword);
this.enforcePasswordPolicyCheckbox.onChanged(async () => {
const enforcePolicy = this.enforcePasswordPolicyCheckbox.checked;
this.objectInfo.enforcePasswordPolicy = enforcePolicy;
this.enforcePasswordExpirationCheckbox.enabled = enforcePolicy;
this.mustChangePasswordCheckbox.enabled = enforcePolicy;
this.enforcePasswordExpirationCheckbox.checked = enforcePolicy;
this.mustChangePasswordCheckbox.checked = enforcePolicy;
this.onObjectValueChange();
await this.runValidation(false);
});
this.enforcePasswordExpirationCheckbox.onChanged(() => {
const enforceExpiration = this.enforcePasswordExpirationCheckbox.checked;
this.objectInfo.enforcePasswordExpiration = enforceExpiration;
this.mustChangePasswordCheckbox.enabled = enforceExpiration;
this.mustChangePasswordCheckbox.checked = enforceExpiration;
this.onObjectValueChange();
});
this.mustChangePasswordCheckbox.onChanged(() => {
this.objectInfo.mustChangePassword = this.mustChangePasswordCheckbox.checked;
this.onObjectValueChange();
});
items.push(this.enforcePasswordPolicyCheckbox, this.enforcePasswordExpirationCheckbox, this.mustChangePasswordCheckbox);
if (!this.isNewObject) {
this.lockedOutCheckbox = this.createCheckbox(view, localizedConstants.LoginLockedOutText, this.objectInfo.isLockedOut, this.viewInfo.canEditLockedOutState);
items.push(this.lockedOutCheckbox);
this.lockedOutCheckbox.onChanged(() => {
this.objectInfo.isLockedOut = this.lockedOutCheckbox.checked;
this.onObjectValueChange();
});
}
}
this.sqlAuthSection = this.createGroup(view, localizedConstants.SQLAuthenticationSectionHeader, items);
}
private initializeAdvancedSection(view: azdata.ModelView): void {
const items: azdata.Component[] = [];
if (this.viewInfo.supportAdvancedOptions) {
this.defaultDatabaseDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.DefaultDatabaseText,
values: this.viewInfo.databases,
value: this.objectInfo.defaultDatabase,
width: DefaultInputWidth
}).component();
const defaultDatabaseContainer = this.createLabelInputContainer(view, localizedConstants.DefaultDatabaseText, this.defaultDatabaseDropdown);
this.defaultDatabaseDropdown.onValueChanged(() => {
this.objectInfo.defaultDatabase = <string>this.defaultDatabaseDropdown.value;
this.onObjectValueChange();
});
this.defaultLanguageDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.DefaultLanguageText,
values: this.viewInfo.languages,
value: this.objectInfo.defaultLanguage,
width: DefaultInputWidth
}).component();
const defaultLanguageContainer = this.createLabelInputContainer(view, localizedConstants.DefaultLanguageText, this.defaultLanguageDropdown);
this.defaultLanguageDropdown.onValueChanged(() => {
this.objectInfo.defaultLanguage = <string>this.defaultLanguageDropdown.value;
this.onObjectValueChange();
});
this.connectPermissionCheckbox = this.createCheckbox(view, localizedConstants.PermissionToConnectText, this.objectInfo.connectPermission);
this.connectPermissionCheckbox.onChanged(() => {
this.objectInfo.connectPermission = this.connectPermissionCheckbox.checked;
this.onObjectValueChange();
});
items.push(defaultDatabaseContainer, defaultLanguageContainer, this.connectPermissionCheckbox);
}
this.advancedSection = this.createGroup(view, localizedConstants.AdvancedSectionHeader, items);
}
private initializeServerRolesSection(view: azdata.ModelView): void {
const serverRolesData = this.viewInfo.serverRoles.map(name => {
const isRoleSelected = this.objectInfo.serverRoles.indexOf(name) !== -1;
const isRoleSelectionEnabled = name !== PublicServerRoleName;
return [{ enabled: isRoleSelectionEnabled, checked: isRoleSelected }, name];
});
this.serverRoleTable = this.createTableList(view, localizedConstants.ServerRoleSectionHeader, this.viewInfo.serverRoles, this.objectInfo.serverRoles, serverRolesData);
this.serverRoleSection = this.createGroup(view, localizedConstants.ServerRoleSectionHeader, [this.serverRoleTable]);
}
private setViewByAuthenticationType(): void {
if (this.authTypeDropdown.value === localizedConstants.SQLAuthenticationTypeDisplayText) {
this.addItem(this.formContainer, this.sqlAuthSection, 1);
} else if (this.authTypeDropdown.value !== localizedConstants.SQLAuthenticationTypeDisplayText) {
this.removeItem(this.formContainer, this.sqlAuthSection);
}
}
}

View File

@@ -0,0 +1,260 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* 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 { NodeType, TelemetryActions, TelemetryViews } from '../constants';
import {
CreateObjectOperationDisplayName, HelpText, LoadingDialogText,
NameText,
NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, SelectedText, UpdateObjectOperationDisplayName
} from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils';
import { TelemetryReporter } from '../../telemetry';
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: NodeType, isNewObject: boolean): string {
return isNewObject ? `New${type}` : `${type}Properties`
}
export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectManagement.SqlObject, ViewInfoType extends ObjectManagement.ObjectViewInfo<ObjectInfoType>> {
protected readonly disposables: vscode.Disposable[] = [];
protected readonly dialogObject: azdata.window.Dialog;
protected readonly contextId: string;
private _viewInfo: ViewInfoType;
private _originalObjectInfo: ObjectInfoType;
constructor(private readonly objectType: NodeType,
docUrl: string,
protected readonly objectManagementService: IObjectManagementService,
protected readonly connectionUri: string,
protected isNewObject: boolean,
protected readonly objectName: string | undefined = undefined,
protected readonly objectExplorerContext?: azdata.ObjectExplorerContext,
dialogWidth: azdata.window.DialogWidth = 'narrow') {
const objectTypeDisplayName = getNodeTypeDisplayName(objectType, true);
const dialogTitle = isNewObject ? NewObjectDialogTitle(objectTypeDisplayName) : ObjectPropertiesDialogTitle(objectTypeDisplayName, objectName);
this.dialogObject = azdata.window.createModelViewDialog(dialogTitle, getDialogName(objectType, isNewObject), dialogWidth);
this.dialogObject.okButton.label = OkText;
this.disposables.push(this.dialogObject.onClosed(async () => { await this.dispose(); }));
const helpButton = azdata.window.createButton(HelpText, 'left');
this.disposables.push(helpButton.onClick(async () => {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(docUrl));
}));
this.dialogObject.customButtons = [helpButton];
this.contextId = generateUuid();
this.dialogObject.registerCloseValidator(async (): Promise<boolean> => {
const confirmed = await this.onConfirmation();
if (!confirmed) {
return false;
}
return await this.runValidation();
});
}
protected abstract initializeData(): Promise<ViewInfoType>;
protected abstract initializeUI(): Promise<void>;
protected abstract onComplete(): Promise<void>;
protected abstract onDispose(): Promise<void>;
protected abstract validateInput(): Promise<string[]>;
protected onObjectValueChange(): void {
this.dialogObject.okButton.enabled = JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo);
}
protected async onConfirmation(): Promise<boolean> {
return true;
}
protected get viewInfo(): ViewInfoType {
return this._viewInfo;
}
protected get objectInfo(): ObjectInfoType {
return this._viewInfo?.objectInfo;
}
protected get originalObjectInfo(): ObjectInfoType {
return this._originalObjectInfo;
}
public async open(): Promise<void> {
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: LoadingDialogText
}, async () => {
try {
this._viewInfo = await this.initializeData();
this._originalObjectInfo = deepClone(this.objectInfo);
await this.initializeUI();
const typeDisplayName = getNodeTypeDisplayName(this.objectType);
this.dialogObject.registerOperation({
displayName: this.isNewObject ? CreateObjectOperationDisplayName(typeDisplayName)
: UpdateObjectOperationDisplayName(typeDisplayName, this.objectName),
description: '',
isCancelable: false,
operation: async (operation: azdata.BackgroundOperation): Promise<void> => {
const actionName = this.isNewObject ? TelemetryActions.CreateObject : TelemetryActions.UpdateObject;
try {
if (JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo)) {
const startTime = Date.now();
await this.onComplete();
if (this.isNewObject && this.objectExplorerContext) {
await refreshNode(this.objectExplorerContext);
}
TelemetryReporter.sendTelemetryEvent(actionName, {
objectType: this.objectType
}, {
ellapsedTime: Date.now() - startTime
});
operation.updateStatus(azdata.TaskStatus.Succeeded);
}
}
catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, getErrorMessage(err));
TelemetryReporter.createErrorEvent(TelemetryViews.ObjectManagement, actionName).withAdditionalProperties({
objectType: this.objectType
}).send();
}
}
});
azdata.window.openDialog(this.dialogObject);
} catch (err) {
const actionName = this.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog;
TelemetryReporter.createErrorEvent(TelemetryViews.ObjectManagement, actionName).withAdditionalProperties({
objectType: this.objectType
}).send();
void vscode.window.showErrorMessage(getErrorMessage(err));
}
});
}
private async dispose(): Promise<void> {
await this.onDispose();
this.disposables.forEach(disposable => disposable.dispose());
}
protected async runValidation(showErrorMessage: boolean = true): Promise<boolean> {
const errors = await this.validateInput();
if (errors.length > 0 && (this.dialogObject.message || showErrorMessage)) {
this.dialogObject.message = {
text: errors.join(EOL),
level: azdata.window.MessageLevel.Error
};
} else {
this.dialogObject.message = undefined;
}
return errors.length === 0;
}
protected createLabelInputContainer(view: azdata.ModelView, label: string, input: azdata.InputBoxComponent | azdata.DropDownComponent): azdata.FlexContainer {
const labelComponent = view.modelBuilder.text().withProps({ width: DefaultLabelWidth, value: label, requiredIndicator: input.required }).component();
const row = view.modelBuilder.flexContainer().withLayout({ flexFlow: 'horizontal', flexWrap: 'nowrap', alignItems: 'center' }).withItems([labelComponent, input]).component();
return row;
}
protected createCheckbox(view: azdata.ModelView, label: string, checked: boolean = false, enabled: boolean = true): azdata.CheckBoxComponent {
return view.modelBuilder.checkBox().withProps({
label: label,
checked: checked,
enabled: enabled
}).component();
}
protected createPasswordInputBox(view: azdata.ModelView, ariaLabel: string, value: string = '', enabled: boolean = true, width: number = DefaultInputWidth): azdata.InputBoxComponent {
return this.createInputBox(view, ariaLabel, value, enabled, 'password', width);
}
protected createInputBox(view: azdata.ModelView, ariaLabel: string, value: string = '', enabled: boolean = true, type: azdata.InputBoxInputType = 'text', width: number = DefaultInputWidth): azdata.InputBoxComponent {
return view.modelBuilder.inputBox().withProps({ inputType: type, enabled: enabled, ariaLabel: ariaLabel, value: value, width: width }).component();
}
protected createGroup(view: azdata.ModelView, header: string, items: azdata.Component[], collapsible: boolean = true, collapsed: boolean = false): azdata.GroupContainer {
return view.modelBuilder.groupContainer().withLayout({
header: header,
collapsed: false,
collapsible: collapsible
}).withProps({ collapsed: collapsed }).withItems(items).component();
}
protected createFormContainer(view: azdata.ModelView, items: azdata.Component[]): azdata.DivContainer {
return view.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(view: azdata.ModelView, 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 = view.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();
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 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);
}
}
}
}

View File

@@ -0,0 +1,248 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata';
import { DefaultInputWidth, ObjectManagementDialogBase } from './objectManagementDialogBase';
import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as localizedConstants from '../localizedConstants';
import { AlterUserDocUrl, AuthenticationType, CreateUserDocUrl, NodeType, UserType } from '../constants';
import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, getUserTypeByDisplayName, getUserTypeDisplayName, isValidSQLPassword } from '../utils';
export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User, ObjectManagement.UserViewInfo> {
private formContainer: azdata.DivContainer;
private generalSection: azdata.GroupContainer;
private ownedSchemaSection: azdata.GroupContainer;
private membershipSection: azdata.GroupContainer;
private advancedSection: azdata.GroupContainer;
private nameInput: azdata.InputBoxComponent;
private typeDropdown: azdata.DropDownComponent;
private typeContainer: azdata.FlexContainer;
private authTypeDropdown: azdata.DropDownComponent;
private authTypeContainer: azdata.FlexContainer;
private loginDropdown: azdata.DropDownComponent;
private loginContainer: azdata.FlexContainer;
private passwordInput: azdata.InputBoxComponent;
private passwordContainer: azdata.FlexContainer;
private confirmPasswordInput: azdata.InputBoxComponent;
private confirmPasswordContainer: azdata.FlexContainer;
private defaultSchemaDropdown: azdata.DropDownComponent;
private defaultSchemaContainer: azdata.FlexContainer;
private defaultLanguageDropdown: azdata.DropDownComponent;
private ownedSchemaTable: azdata.TableComponent;
private membershipTable: azdata.TableComponent;
constructor(objectManagementService: IObjectManagementService, connectionUri: string, private readonly database: string, isNewObject: boolean, name?: string, objectExplorerContext?: azdata.ObjectExplorerContext) {
super(NodeType.User, isNewObject ? CreateUserDocUrl : AlterUserDocUrl, objectManagementService, connectionUri, isNewObject, name, objectExplorerContext);
}
protected async initializeData(): Promise<ObjectManagement.UserViewInfo> {
const viewInfo = await this.objectManagementService.initializeUserView(this.connectionUri, this.database, this.contextId, this.isNewObject, this.objectName);
viewInfo.objectInfo.password = viewInfo.objectInfo.password ?? '';
return viewInfo;
}
protected async validateInput(): Promise<string[]> {
const errors: string[] = [];
if (!this.objectInfo.name) {
errors.push(localizedConstants.NameCannotBeEmptyError);
}
if (this.objectInfo.type === UserType.Contained && this.objectInfo.authenticationType === AuthenticationType.Sql) {
if (!this.objectInfo.password) {
errors.push(localizedConstants.PasswordCannotBeEmptyError);
}
if (this.objectInfo.password !== this.confirmPasswordInput.value) {
errors.push(localizedConstants.PasswordsNotMatchError);
}
if (!isValidSQLPassword(this.objectInfo.password, this.objectInfo.name)
&& (this.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) {
errors.push(localizedConstants.InvalidPasswordError);
}
}
return errors;
}
protected async onComplete(): Promise<void> {
if (this.isNewObject) {
await this.objectManagementService.createUser(this.contextId, this.objectInfo);
} else {
await this.objectManagementService.updateUser(this.contextId, this.objectInfo);
}
}
protected async onDispose(): Promise<void> {
await this.objectManagementService.disposeUserView(this.contextId);
}
protected async initializeUI(): Promise<void> {
this.dialogObject.registerContent(async view => {
const sections: azdata.Component[] = [];
this.initializeGeneralSection(view);
this.initializeOwnedSchemaSection(view);
this.initializeMembershipSection(view);
this.initializeAdvancedSection(view);
sections.push(this.generalSection, this.ownedSchemaSection, this.membershipSection, this.advancedSection);
this.formContainer = this.createFormContainer(view, sections);
setTimeout(() => {
this.setViewByUserType();
}, 100);
return view.initializeModel(this.formContainer)
});
}
private initializeGeneralSection(view: azdata.ModelView): void {
this.nameInput = view.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText,
enabled: this.isNewObject,
value: this.objectInfo.name,
width: DefaultInputWidth
}).component();
this.nameInput.onTextChanged(async () => {
this.objectInfo.name = this.nameInput.value;
this.onObjectValueChange();
await this.runValidation(false);
});
const nameContainer = this.createLabelInputContainer(view, localizedConstants.NameText, this.nameInput);
this.defaultSchemaDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.DefaultSchemaText,
values: this.viewInfo.schemas,
value: this.objectInfo.defaultSchema,
width: DefaultInputWidth
}).component();
this.defaultSchemaContainer = this.createLabelInputContainer(view, localizedConstants.DefaultSchemaText, this.defaultSchemaDropdown);
this.defaultSchemaDropdown.onValueChanged(() => {
this.objectInfo.defaultSchema = <string>this.defaultSchemaDropdown.value;
this.onObjectValueChange();
});
this.typeDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.UserTypeText,
values: [localizedConstants.UserWithLoginText, localizedConstants.UserWithWindowsGroupLoginText, localizedConstants.ContainedUserText, localizedConstants.UserWithNoConnectAccess],
value: getUserTypeDisplayName(this.objectInfo.type),
width: DefaultInputWidth,
enabled: this.isNewObject
}).component();
this.typeDropdown.onValueChanged(async () => {
this.objectInfo.type = getUserTypeByDisplayName(<string>this.typeDropdown.value);
this.onObjectValueChange();
this.setViewByUserType();
await this.runValidation(false);
});
this.typeContainer = this.createLabelInputContainer(view, localizedConstants.UserTypeText, this.typeDropdown);
this.loginDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.LoginText,
values: this.viewInfo.logins,
value: this.objectInfo.loginName,
width: DefaultInputWidth,
enabled: this.isNewObject
}).component();
this.loginDropdown.onValueChanged(() => {
this.objectInfo.loginName = <string>this.loginDropdown.value;
this.onObjectValueChange();
});
this.loginContainer = this.createLabelInputContainer(view, localizedConstants.LoginText, this.loginDropdown);
const authTypes = [];
if (this.viewInfo.supportWindowsAuthentication) {
authTypes.push(localizedConstants.WindowsAuthenticationTypeDisplayText);
}
if (this.viewInfo.supportSQLAuthentication) {
authTypes.push(localizedConstants.SQLAuthenticationTypeDisplayText);
}
if (this.viewInfo.supportAADAuthentication) {
authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText);
}
this.authTypeDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.AuthTypeText,
values: authTypes,
value: getAuthenticationTypeDisplayName(this.objectInfo.authenticationType),
width: DefaultInputWidth,
enabled: this.isNewObject
}).component();
this.authTypeContainer = this.createLabelInputContainer(view, localizedConstants.AuthTypeText, this.authTypeDropdown);
this.authTypeDropdown.onValueChanged(async () => {
this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value);
this.onObjectValueChange();
this.setViewByAuthenticationType();
await this.runValidation(false);
});
this.passwordInput = this.createPasswordInputBox(view, localizedConstants.PasswordText, this.objectInfo.password ?? '');
this.passwordContainer = this.createLabelInputContainer(view, localizedConstants.PasswordText, this.passwordInput);
this.confirmPasswordInput = this.createPasswordInputBox(view, localizedConstants.ConfirmPasswordText, this.objectInfo.password ?? '');
this.confirmPasswordContainer = this.createLabelInputContainer(view, localizedConstants.ConfirmPasswordText, this.confirmPasswordInput);
this.passwordInput.onTextChanged(async () => {
this.objectInfo.password = this.passwordInput.value;
this.onObjectValueChange();
await this.runValidation(false);
});
this.confirmPasswordInput.onTextChanged(async () => {
await this.runValidation(false);
});
this.generalSection = this.createGroup(view, localizedConstants.GeneralSectionHeader, [
nameContainer,
this.defaultSchemaContainer,
this.typeContainer,
this.loginContainer,
this.authTypeContainer,
this.passwordContainer,
this.confirmPasswordContainer
], false);
}
private initializeOwnedSchemaSection(view: azdata.ModelView): void {
this.ownedSchemaTable = this.createTableList(view, localizedConstants.OwnedSchemaSectionHeader, this.viewInfo.schemas, this.objectInfo.ownedSchemas);
this.ownedSchemaSection = this.createGroup(view, localizedConstants.OwnedSchemaSectionHeader, [this.ownedSchemaTable]);
}
private initializeMembershipSection(view: azdata.ModelView): void {
this.membershipTable = this.createTableList(view, localizedConstants.MembershipSectionHeader, this.viewInfo.databaseRoles, this.objectInfo.databaseRoles);
this.membershipSection = this.createGroup(view, localizedConstants.MembershipSectionHeader, [this.membershipTable]);
}
private initializeAdvancedSection(view: azdata.ModelView): void {
this.defaultLanguageDropdown = view.modelBuilder.dropDown().withProps({
ariaLabel: localizedConstants.DefaultLanguageText,
values: this.viewInfo.languages,
value: this.objectInfo.defaultLanguage,
width: DefaultInputWidth
}).component();
this.defaultLanguageDropdown.onValueChanged(() => {
this.objectInfo.defaultLanguage = <string>this.defaultLanguageDropdown.value;
this.onObjectValueChange();
});
const container = this.createLabelInputContainer(view, localizedConstants.DefaultLanguageText, this.defaultLanguageDropdown);
this.advancedSection = this.createGroup(view, localizedConstants.AdvancedSectionHeader, [container]);
}
private setViewByUserType(): void {
if (this.typeDropdown.value === localizedConstants.UserWithLoginText) {
this.removeItem(this.generalSection, this.authTypeContainer);
this.removeItem(this.formContainer, this.advancedSection);
this.addItem(this.generalSection, this.loginContainer);
} else if (this.typeDropdown.value === localizedConstants.ContainedUserText) {
this.removeItem(this.generalSection, this.loginContainer);
this.addItem(this.generalSection, this.authTypeContainer);
this.addItem(this.formContainer, this.advancedSection);
} else {
this.removeItem(this.generalSection, this.loginContainer);
this.removeItem(this.generalSection, this.authTypeContainer);
this.removeItem(this.formContainer, this.advancedSection);
}
this.setViewByAuthenticationType();
}
private setViewByAuthenticationType(): void {
const showPassword = this.typeDropdown.value === localizedConstants.ContainedUserText && this.authTypeDropdown.value === localizedConstants.SQLAuthenticationTypeDisplayText;
if (showPassword) {
this.addItem(this.generalSection, this.passwordContainer);
this.addItem(this.generalSection, this.confirmPasswordContainer);
} else {
this.removeItem(this.generalSection, this.passwordContainer);
this.removeItem(this.generalSection, this.confirmPasswordContainer);
}
}
}