simplify object management feature APIs (#22781)

This commit is contained in:
Alan Ren
2023-04-19 19:26:29 -07:00
committed by GitHub
parent 34d092a7dd
commit decbe8dded
14 changed files with 413 additions and 578 deletions

View File

@@ -1,6 +1,6 @@
{ {
"downloadUrl": "https://github.com/Microsoft/sqltoolsservice/releases/download/{#version#}/microsoft.sqltools.servicelayer-{#fileName#}", "downloadUrl": "https://github.com/Microsoft/sqltoolsservice/releases/download/{#version#}/microsoft.sqltools.servicelayer-{#fileName#}",
"version": "4.7.0.12", "version": "4.7.0.14",
"downloadFileNames": { "downloadFileNames": {
"Windows_86": "win-x86-net7.0.zip", "Windows_86": "win-x86-net7.0.zip",
"Windows_64": "win-x64-net7.0.zip", "Windows_64": "win-x64-net7.0.zip",

View File

@@ -73,14 +73,9 @@
"title": "%title.changeNotebookConnection%" "title": "%title.changeNotebookConnection%"
}, },
{ {
"command": "mssql.newLogin", "command": "mssql.newObject",
"category": "MSSQL", "category": "MSSQL",
"title": "%title.newLogin%" "title": "%title.newObject%"
},
{
"command": "mssql.newUser",
"category": "MSSQL",
"title": "%title.newUser%"
}, },
{ {
"command": "mssql.objectProperties", "command": "mssql.objectProperties",
@@ -469,11 +464,7 @@
"when": "false" "when": "false"
}, },
{ {
"command": "mssql.newLogin", "command": "mssql.newObject",
"when": "false"
},
{
"command": "mssql.newUser",
"when": "false" "when": "false"
}, },
{ {
@@ -502,13 +493,8 @@
"group": "0_query@1" "group": "0_query@1"
}, },
{ {
"command": "mssql.newLogin", "command": "mssql.newObject",
"when": "connectionProvider == MSSQL && nodeType == Folder && objectType == ServerLevelLogins && config.workbench.enablePreviewFeatures", "when": "connectionProvider == MSSQL && nodeType == Folder && objectType =~ /^(ServerLevelLogins|Users)$/ && config.workbench.enablePreviewFeatures",
"group": "0_query@1"
},
{
"command": "mssql.newUser",
"when": "connectionProvider == MSSQL && nodeType == Folder && objectType == Users && config.workbench.enablePreviewFeatures",
"group": "0_query@1" "group": "0_query@1"
}, },
{ {
@@ -553,13 +539,8 @@
"group": "connection@1" "group": "connection@1"
}, },
{ {
"command": "mssql.newLogin", "command": "mssql.newObject",
"when": "connectionProvider == MSSQL && nodeType == Folder && objectType == ServerLevelLogins && config.workbench.enablePreviewFeatures", "when": "connectionProvider == MSSQL && nodeType == Folder && objectType =~ /^(ServerLevelLogins|Users)$/ && config.workbench.enablePreviewFeatures",
"group": "connection@1"
},
{
"command": "mssql.newUser",
"when": "connectionProvider == MSSQL && nodeType == Folder && objectType == Users && config.workbench.enablePreviewFeatures",
"group": "connection@1" "group": "connection@1"
}, },
{ {

View File

@@ -183,11 +183,7 @@
"mssql.objectExplorer.enableGroupBySchemaTitle": "SQL Server: Enable Group By Schema", "mssql.objectExplorer.enableGroupBySchemaTitle": "SQL Server: Enable Group By Schema",
"mssql.objectExplorer.disableGroupBySchemaTitle": "SQL Server: Disable Group By Schema", "mssql.objectExplorer.disableGroupBySchemaTitle": "SQL Server: Disable Group By Schema",
"mssql.objectExplorer.expandTimeout": "The timeout in seconds for expanding a node in Object Explorer. The default value is 45 seconds.", "mssql.objectExplorer.expandTimeout": "The timeout in seconds for expanding a node in Object Explorer. The default value is 45 seconds.",
"title.newServerRole": "New Server Role", "title.newObject": "New",
"title.newLogin": "New Login",
"title.newDatabaseRole": "New Database Role",
"title.newApplicationRole": "New Application Role",
"title.newUser": "New User",
"title.objectProperties": "Properties (Preview)", "title.objectProperties": "Properties (Preview)",
"title.deleteObject": "Delete", "title.deleteObject": "Delete",
"title.renameObject": "Rename" "title.renameObject": "Rename"

View File

@@ -1534,103 +1534,51 @@ export namespace ExecutionPlanComparisonRequest {
// ------------------------------- < Execution Plan > ------------------------------------ // ------------------------------- < Execution Plan > ------------------------------------
// ------------------------------- < Object Management > ------------------------------------ // ------------------------------- < Object Management > ------------------------------------
export interface InitializeLoginViewRequestParams { export interface InitializeViewRequestParams {
connectionUri: string; connectionUri: string;
contextId: string;
isNewObject: boolean;
name: string | undefined;
}
export namespace InitializeLoginViewRequest {
export const type = new RequestType<InitializeLoginViewRequestParams, mssql.ObjectManagement.LoginViewInfo, void, void>('objectManagement/initializeLoginView');
}
export interface CreateLoginRequestParams {
contextId: string;
login: mssql.ObjectManagement.Login;
}
export namespace CreateLoginRequest {
export const type = new RequestType<CreateLoginRequestParams, void, void, void>('objectManagement/createLogin');
}
export interface ScriptLoginRequestParams {
contextId: string;
login: mssql.ObjectManagement.Login;
}
export namespace ScriptLoginRequest {
export const type = new RequestType<ScriptLoginRequestParams, string, void, void>('objectManagement/scriptLogin');
}
export interface UpdateLoginRequestParams {
contextId: string;
login: mssql.ObjectManagement.Login;
}
export namespace UpdateLoginRequest {
export const type = new RequestType<UpdateLoginRequestParams, void, void, void>('objectManagement/updateLogin');
}
export interface DisposeLoginViewRequestParams {
contextId: string;
}
export namespace DisposeLoginViewRequest {
export const type = new RequestType<DisposeLoginViewRequestParams, void, void, void>('objectManagement/disposeLoginView');
}
export interface InitializeUserViewRequestParams {
connectionUri: string;
contextId: string;
isNewObject: boolean;
database: string; database: string;
name: string | undefined;
}
export namespace InitializeUserViewRequest {
export const type = new RequestType<InitializeUserViewRequestParams, mssql.ObjectManagement.UserViewInfo, void, void>('objectManagement/initializeUserView');
}
export interface CreateUserRequestParams {
contextId: string; contextId: string;
user: mssql.ObjectManagement.User; isNewObject: boolean;
objectType: string;
parentUrn: string;
objectUrn?: string;
} }
export namespace CreateUserRequest { export namespace InitializeViewRequest {
export const type = new RequestType<CreateUserRequestParams, void, void, void>('objectManagement/createUser'); export const type = new RequestType<InitializeViewRequestParams, mssql.ObjectManagement.ObjectViewInfo<mssql.ObjectManagement.SqlObject>, void, void>('objectManagement/initializeView');
} }
export interface ScriptUserRequestParams { export interface SaveObjectRequestParams {
contextId: string; contextId: string;
user: mssql.ObjectManagement.User; object: mssql.ObjectManagement.SqlObject;
} }
export namespace ScriptUserRequest { export namespace SaveObjectRequest {
export const type = new RequestType<ScriptUserRequestParams, string, void, void>('objectManagement/scriptUser'); export const type = new RequestType<SaveObjectRequestParams, void, void, void>('objectManagement/save');
} }
export interface UpdateUserRequestParams { export interface ScriptObjectRequestParams {
contextId: string; contextId: string;
user: mssql.ObjectManagement.User; object: mssql.ObjectManagement.SqlObject;
} }
export namespace UpdateUserRequest { export namespace ScriptObjectRequest {
export const type = new RequestType<UpdateUserRequestParams, void, void, void>('objectManagement/updateUser'); export const type = new RequestType<ScriptObjectRequestParams, string, void, void>('objectManagement/script');
} }
export interface DisposeUserViewRequestParams { export interface DisposeViewRequestParams {
contextId: string; contextId: string;
} }
export namespace DisposeUserViewRequest { export namespace DisposeViewRequest {
export const type = new RequestType<DisposeUserViewRequestParams, void, void, void>('objectManagement/disposeUserView'); export const type = new RequestType<DisposeViewRequestParams, void, void, void>('objectManagement/disposeView');
} }
export interface RenameObjectRequestParams { export interface RenameObjectRequestParams {
connectionUri: string; connectionUri: string;
newName: string; newName: string;
objectUrn: string; objectUrn: string;
objectType: mssql.ObjectManagement.NodeType;
} }
export namespace RenameObjectRequest { export namespace RenameObjectRequest {
@@ -1640,11 +1588,13 @@ export namespace RenameObjectRequest {
export interface DropObjectRequestParams { export interface DropObjectRequestParams {
connectionUri: string; connectionUri: string;
objectUrn: string; objectUrn: string;
objectType: mssql.ObjectManagement.NodeType;
} }
export namespace DropObjectRequest { export namespace DropObjectRequest {
export const type = new RequestType<DropObjectRequestParams, void, void, void>('objectManagement/drop'); export const type = new RequestType<DropObjectRequestParams, void, void, void>('objectManagement/drop');
} }
// ------------------------------- < Object Management > ------------------------------------ // ------------------------------- < Object Management > ------------------------------------
// ------------------------------- < Encryption IV/KEY updation Event > ------------------------------------ // ------------------------------- < Encryption IV/KEY updation Event > ------------------------------------

View File

@@ -875,6 +875,19 @@ declare module 'mssql' {
// Object Management - Begin. // Object Management - Begin.
export namespace ObjectManagement { export namespace ObjectManagement {
/**
* Object types.
*/
export const enum NodeType {
Column = "Column",
Database = "Database",
ServerLevelLogin = "ServerLevelLogin",
Table = "Table",
User = "User",
View = "View"
}
/** /**
* Base interface for all the objects. * Base interface for all the objects.
*/ */
@@ -963,7 +976,7 @@ declare module 'mssql' {
/** /**
* The authentication types. * The authentication types.
*/ */
export enum AuthenticationType { export const enum AuthenticationType {
Windows = 'Windows', Windows = 'Windows',
Sql = 'Sql', Sql = 'Sql',
AzureActiveDirectory = 'AAD' AzureActiveDirectory = 'AAD'
@@ -1086,7 +1099,7 @@ declare module 'mssql' {
/** /**
* User types. * User types.
*/ */
export enum UserType { export const enum UserType {
/** /**
* User with a server level login. * User with a server level login.
*/ */
@@ -1188,81 +1201,48 @@ declare module 'mssql' {
export interface IObjectManagementService { export interface IObjectManagementService {
/** /**
* Initialize the login view and return the information to render the view. * Initialize the object view and return the information to render the view.
* @param contextId The context id of the view, generated by the extension and will be used in subsequent save/script/dispose operations.
* @param objectType The object type.
* @param connectionUri The original connection's URI. * @param connectionUri The original connection's URI.
* @param contextId The context id of the view, generated by the extension and will be used in subsequent create/update/dispose operations. * @param database The target database.
* @param isNewObject Whether the view is for creating a new login object. * @param isNewObject Whether the view is for creating a new object.
* @param name Name of the login. Only applicable when isNewObject is false. * @param parentUrn The parent object's URN.
* @param objectUrn The object's URN.
*/ */
initializeLoginView(connectionUri: string, contextId: string, isNewObject: boolean, name: string | undefined): Thenable<ObjectManagement.LoginViewInfo>; initializeView(contextId: string, objectType: ObjectManagement.NodeType, connectionUri: string, database: string, isNewObject: boolean, parentUrn: string, objectUrn: string): Thenable<ObjectManagement.ObjectViewInfo<ObjectManagement.SqlObject>>;
/** /**
* Create a login. * Save an object.
* @param contextId The login view's context id. * @param contextId The object view's context id.
* @param login The login information. * @param object The object to be saved.
*/ */
createLogin(contextId: string, login: ObjectManagement.Login): Thenable<void>; save(contextId: string, object: ObjectManagement.SqlObject): Thenable<void>;
/** /**
* Update a login. * Script an object.
* @param contextId The login view's context id. * @param contextId The object view's context id.
* @param login The login information. * @param object The object to be scripted.
*/ */
updateLogin(contextId: string, login: ObjectManagement.Login): Thenable<void>; script(contextId: string, object: ObjectManagement.SqlObject): Thenable<string>;
/** /**
* Script a login. * Dispose a view.
* @param contextId The login view's context id.
* @param login The login information.
*/
scriptLogin(contextId: string, login: ObjectManagement.Login): Thenable<string>;
/**
* Dispose the login view.
* @param contextId The id of the view. * @param contextId The id of the view.
*/ */
disposeLoginView(contextId: string): Thenable<void>; disposeView(contextId: string): Thenable<void>;
/**
* Initialize the user view and return the information to render the view.
* @param connectionUri The original connection's URI.
* @param database Name of the database.
* @param contextId The id of the view, generated by the extension and will be used in subsequent create/update/dispose operations.
* @param isNewObject Whether the view is for creating a new user object.
* @param name Name of the user. Only applicable when isNewObject is false.
*/
initializeUserView(connectionUri: string, database: string, contextId: string, isNewObject: boolean, name: string | undefined): Thenable<ObjectManagement.UserViewInfo>;
/**
* Create a user.
* @param contextId Id of the view.
* @param user The user information.
*/
createUser(contextId: string, user: ObjectManagement.User): Thenable<void>;
/**
* Update a user.
* @param contextId Id of the view.
* @param user The user information.
*/
updateUser(contextId: string, user: ObjectManagement.User): Thenable<void>;
/**
* Script a user.
* @param contextId Id of the view.
* @param user The user information.
*/
scriptUser(contextId: string, user: ObjectManagement.User): Thenable<string>;
/**
* Dispose the user view.
* @param contextId The id of the view.
*/
disposeUserView(contextId: string): Thenable<void>;
/** /**
* Rename an object. * Rename an object.
* @param connectionUri The URI of the server connection. * @param connectionUri The URI of the server connection.
* @param objectType The object type.
* @param objectUrn SMO Urn of the object to be renamed. More information: https://learn.microsoft.com/sql/relational-databases/server-management-objects-smo/overview-smo * @param objectUrn SMO Urn of the object to be renamed. More information: https://learn.microsoft.com/sql/relational-databases/server-management-objects-smo/overview-smo
* @param newName The new name of the object. * @param newName The new name of the object.
*/ */
rename(connectionUri: string, objectUrn: string, newName: string): Thenable<void>; rename(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string, newName: string): Thenable<void>;
/** /**
* Drop an object. * Drop an object.
* @param connectionUri The URI of the server connection. * @param connectionUri The URI of the server connection.
* @param objectType The object type.
* @param objectUrn SMO Urn of the object to be dropped. More information: https://learn.microsoft.com/sql/relational-databases/server-management-objects-smo/overview-smo * @param objectUrn SMO Urn of the object to be dropped. More information: https://learn.microsoft.com/sql/relational-databases/server-management-objects-smo/overview-smo
*/ */
drop(connectionUri: string, objectUrn: string): Thenable<void>; drop(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string): Thenable<void>;
} }
// Object Management - End. // Object Management - End.
} }

View File

@@ -9,22 +9,20 @@ import * as vscode from 'vscode';
import { LoginDialog } from './ui/loginDialog'; import { LoginDialog } from './ui/loginDialog';
import { TestObjectManagementService } from './objectManagementService'; import { TestObjectManagementService } from './objectManagementService';
import { getErrorMessage } from '../utils'; import { getErrorMessage } from '../utils';
import { NodeType, TelemetryActions, TelemetryViews } from './constants'; import { FolderType, TelemetryActions, ObjectManagementViewName } from './constants';
import * as localizedConstants from './localizedConstants'; import * as localizedConstants from './localizedConstants';
import { UserDialog } from './ui/userDialog'; import { UserDialog } from './ui/userDialog';
import { IObjectManagementService } from 'mssql'; import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as constants from '../constants'; import * as constants from '../constants';
import { getNodeTypeDisplayName, refreshParentNode } from './utils'; import { getNodeTypeDisplayName, refreshParentNode } from './utils';
import { TelemetryReporter } from '../telemetry'; import { TelemetryReporter } from '../telemetry';
import { ObjectManagementDialogBase, ObjectManagementDialogOptions } from './ui/objectManagementDialogBase';
export function registerObjectManagementCommands(appContext: AppContext) { export function registerObjectManagementCommands(appContext: AppContext) {
// Notes: Change the second parameter to false to use the actual object management service. // Notes: Change the second parameter to false to use the actual object management service.
const service = getObjectManagementService(appContext, false); const service = getObjectManagementService(appContext, false);
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newLogin', async (context: azdata.ObjectExplorerContext) => { appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newObject', async (context: azdata.ObjectExplorerContext) => {
await handleNewLoginDialogCommand(context, service); await handleNewObjectDialogCommand(context, service);
}));
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.newUser', async (context: azdata.ObjectExplorerContext) => {
await handleNewUserDialogCommand(context, service);
})); }));
appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.objectProperties', async (context: azdata.ObjectExplorerContext) => { appContext.extensionContext.subscriptions.push(vscode.commands.registerCommand('mssql.objectProperties', async (context: azdata.ObjectExplorerContext) => {
await handleObjectPropertiesDialogCommand(context, service); await handleObjectPropertiesDialogCommand(context, service);
@@ -45,41 +43,45 @@ function getObjectManagementService(appContext: AppContext, useTestService: bool
} }
} }
async function handleNewLoginDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> { async function handleNewObjectDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context); const connectionUri = await getConnectionUri(context);
if (!connectionUri) { if (!connectionUri) {
return; return;
} }
let newObjectType: ObjectManagement.NodeType;
switch (context.nodeInfo!.objectType) {
case FolderType.ServerLevelLogins:
newObjectType = ObjectManagement.NodeType.ServerLevelLogin;
break;
case FolderType.Users:
newObjectType = ObjectManagement.NodeType.User;
break;
default:
throw new Error(`Unsupported folder type: ${context.nodeInfo!.objectType}`);
}
try { try {
const dialog = new LoginDialog(service, connectionUri, true, undefined, context); const parentUrn = await getParentUrn(context);
const options: ObjectManagementDialogOptions = {
connectionUri: connectionUri,
isNewObject: true,
database: context.connectionProfile!.databaseName!,
objectType: newObjectType,
objectName: '',
parentUrn: parentUrn,
objectExplorerContext: context
};
const dialog = getDialog(service, options);
await dialog.open(); await dialog.open();
} }
catch (err) { catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({
objectType: NodeType.Login objectType: context.nodeInfo!.nodeType
}).send(); }).send();
await vscode.window.showErrorMessage(localizedConstants.OpenNewObjectDialogError(localizedConstants.LoginTypeDisplayName, getErrorMessage(err))); await vscode.window.showErrorMessage(localizedConstants.OpenNewObjectDialogError(localizedConstants.LoginTypeDisplayName, getErrorMessage(err)));
} }
} }
async function handleNewUserDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context);
if (!connectionUri) {
return;
}
try {
const dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, true, undefined, context);
await dialog.open();
}
catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenNewObjectDialog, err).withAdditionalProperties({
objectType: NodeType.User
}).send();
await vscode.window.showErrorMessage(localizedConstants.OpenNewObjectDialogError(localizedConstants.UserTypeDisplayName, getErrorMessage(err)));
}
}
async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> { async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplorerContext, service: IObjectManagementService): Promise<void> {
const connectionUri = await getConnectionUri(context); const connectionUri = await getConnectionUri(context);
if (!connectionUri) { if (!connectionUri) {
@@ -87,23 +89,22 @@ async function handleObjectPropertiesDialogCommand(context: azdata.ObjectExplore
} }
const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType); const nodeTypeDisplayName = getNodeTypeDisplayName(context.nodeInfo!.nodeType);
try { try {
let dialog; const parentUrn = await getParentUrn(context);
switch (context.nodeInfo!.nodeType) { const options: ObjectManagementDialogOptions = {
case NodeType.Login: connectionUri: connectionUri,
dialog = new LoginDialog(service, connectionUri, false, context.nodeInfo!.label); isNewObject: false,
break; database: context.connectionProfile!.databaseName!,
case NodeType.User: objectType: context.nodeInfo.nodeType as ObjectManagement.NodeType,
dialog = new UserDialog(service, connectionUri, context.connectionProfile!.databaseName!, false, context.nodeInfo!.label); objectName: context.nodeInfo.label,
break; parentUrn: parentUrn,
default: objectUrn: context.nodeInfo!.metadata!.urn,
break; objectExplorerContext: context
} };
if (dialog) { const dialog = getDialog(service, options);
await dialog.open(); await dialog.open();
}
} }
catch (err) { catch (err) {
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.OpenPropertiesDialog, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.OpenPropertiesDialog, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType objectType: context.nodeInfo!.nodeType
}).send(); }).send();
await vscode.window.showErrorMessage(localizedConstants.OpenObjectPropertiesDialogError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err))); await vscode.window.showErrorMessage(localizedConstants.OpenObjectPropertiesDialogError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err)));
@@ -117,7 +118,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
} }
let additionalConfirmationMessage: string | undefined = undefined; let additionalConfirmationMessage: string | undefined = undefined;
switch (context.nodeInfo!.nodeType) { switch (context.nodeInfo!.nodeType) {
case NodeType.Login: case ObjectManagement.NodeType.ServerLevelLogin:
additionalConfirmationMessage = localizedConstants.DeleteLoginConfirmationText; additionalConfirmationMessage = localizedConstants.DeleteLoginConfirmationText;
break; break;
default: default:
@@ -139,7 +140,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
operation: async (operation) => { operation: async (operation) => {
try { try {
const startTime = Date.now(); const startTime = Date.now();
await service.drop(connectionUri, context.nodeInfo!.metadata!.urn); await service.drop(connectionUri, context.nodeInfo.nodeType as ObjectManagement.NodeType, context.nodeInfo!.metadata!.urn);
TelemetryReporter.sendTelemetryEvent(TelemetryActions.DeleteObject, { TelemetryReporter.sendTelemetryEvent(TelemetryActions.DeleteObject, {
objectType: context.nodeInfo!.nodeType objectType: context.nodeInfo!.nodeType
}, { }, {
@@ -148,7 +149,7 @@ async function handleDeleteObjectCommand(context: azdata.ObjectExplorerContext,
} }
catch (err) { catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.DeleteObjectError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err))); operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.DeleteObjectError(nodeTypeDisplayName, context.nodeInfo!.label, getErrorMessage(err)));
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.DeleteObject, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.DeleteObject, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType objectType: context.nodeInfo!.nodeType
}).send(); }).send();
return; return;
@@ -191,7 +192,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
operation: async (operation) => { operation: async (operation) => {
try { try {
const startTime = Date.now(); const startTime = Date.now();
await service.rename(connectionUri, context.nodeInfo!.metadata!.urn, newName); await service.rename(connectionUri, context.nodeInfo.nodeType as ObjectManagement.NodeType, context.nodeInfo!.metadata!.urn, newName);
TelemetryReporter.sendTelemetryEvent(TelemetryActions.RenameObject, { TelemetryReporter.sendTelemetryEvent(TelemetryActions.RenameObject, {
objectType: context.nodeInfo!.nodeType objectType: context.nodeInfo!.nodeType
}, { }, {
@@ -200,7 +201,7 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
} }
catch (err) { catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.RenameObjectError(nodeTypeDisplayName, originalName, newName, getErrorMessage(err))); operation.updateStatus(azdata.TaskStatus.Failed, localizedConstants.RenameObjectError(nodeTypeDisplayName, originalName, newName, getErrorMessage(err)));
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, TelemetryActions.RenameObject, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, TelemetryActions.RenameObject, err).withAdditionalProperties({
objectType: context.nodeInfo!.nodeType objectType: context.nodeInfo!.nodeType
}).send(); }).send();
return; return;
@@ -211,6 +212,17 @@ async function handleRenameObjectCommand(context: azdata.ObjectExplorerContext,
}); });
} }
function getDialog(service: IObjectManagementService, dialogOptions: ObjectManagementDialogOptions): ObjectManagementDialogBase<ObjectManagement.SqlObject, ObjectManagement.ObjectViewInfo<ObjectManagement.SqlObject>> {
switch (dialogOptions.objectType) {
case ObjectManagement.NodeType.ServerLevelLogin:
return new LoginDialog(service, dialogOptions);
case ObjectManagement.NodeType.User:
return new UserDialog(service, dialogOptions);
default:
throw new Error(`Unsupported object type: ${dialogOptions.objectType}`);
}
}
async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise<string> { async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise<string> {
const connectionUri = await azdata.connection.getUriForConnection(context.connectionProfile!.id); const connectionUri = await azdata.connection.getUriForConnection(context.connectionProfile!.id);
if (!connectionUri) { if (!connectionUri) {
@@ -218,3 +230,13 @@ async function getConnectionUri(context: azdata.ObjectExplorerContext): Promise<
} }
return connectionUri; return connectionUri;
} }
async function getParentUrn(context: azdata.ObjectExplorerContext): Promise<string> {
let node = undefined;
let currentNodePath = context.nodeInfo!.parentNodePath;
do {
node = await azdata.objectexplorer.getNode(context.connectionProfile!.id, currentNodePath);
currentNodePath = node?.parentNodePath;
} while (node && currentNodePath && !node.metadata?.urn);
return node?.metadata?.urn;
}

View File

@@ -4,56 +4,21 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
/** /**
* The object types in object explorer's node context. * The folder types in object explorer.
*/ */
export enum NodeType { export const enum FolderType {
Column = 'Column', ServerLevelLogins = 'ServerLevelLogins',
Database = 'Database', Users = 'Users'
Login = 'ServerLevelLogin',
Table = 'Table',
User = 'User',
View = 'View'
} }
export const PublicServerRoleName = 'public'; export const PublicServerRoleName = 'public';
/**
* User types.
*/
export enum UserType {
/**
* User with a server level login.
*/
WithLogin = 'WithLogin',
/**
* User based on a Windows user/group that has no login, but can connect to the Database Engine through membership in a Windows group.
*/
WithWindowsGroupLogin = 'WithWindowsGroupLogin',
/**
* Contained user, authentication is done within the database.
*/
Contained = 'Contained',
/**
* User that cannot authenticate.
*/
NoConnectAccess = 'NoConnectAccess'
}
/**
* The authentication types.
*/
export enum AuthenticationType {
Windows = 'Windows',
Sql = 'Sql',
AzureActiveDirectory = 'AAD'
}
export const CreateUserDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql'; export const CreateUserDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql';
export const AlterUserDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-user-transact-sql'; export const AlterUserDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-user-transact-sql';
export const CreateLoginDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/create-login-transact-sql'; export const CreateLoginDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/create-login-transact-sql';
export const AlterLoginDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-login-transact-sql'; export const AlterLoginDocUrl = 'https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-login-transact-sql';
export enum TelemetryActions { export const enum TelemetryActions {
CreateObject = 'CreateObject', CreateObject = 'CreateObject',
DeleteObject = 'DeleteObject', DeleteObject = 'DeleteObject',
OpenNewObjectDialog = 'OpenNewObjectDialog', OpenNewObjectDialog = 'OpenNewObjectDialog',
@@ -62,6 +27,4 @@ export enum TelemetryActions {
UpdateObject = 'UpdateObject' UpdateObject = 'UpdateObject'
} }
export enum TelemetryViews { export const ObjectManagementViewName = 'ObjectManagement';
ObjectManagement = 'ObjectManagement'
}

View File

@@ -24,6 +24,7 @@ export const LoadingDialogText: string = localize('objectManagement.loadingDialo
export const FailedToRetrieveConnectionInfoErrorMessage: string = localize('objectManagement.noConnectionUriError', "Failed to retrieve the connection information, please reconnect and try again.") export const FailedToRetrieveConnectionInfoErrorMessage: string = localize('objectManagement.noConnectionUriError', "Failed to retrieve the connection information, please reconnect and try again.")
export const RenameObjectDialogTitle: string = localize('objectManagement.renameObjectDialogTitle', "Enter new name"); export const RenameObjectDialogTitle: string = localize('objectManagement.renameObjectDialogTitle', "Enter new name");
export const ScriptText: string = localize('objectManagement.scriptText', "Script"); export const ScriptText: string = localize('objectManagement.scriptText', "Script");
export const NoActionScriptedMessage: string = localize('objectManagement.noActionScriptedMessage', "There is no action to be scripted.");
export const ScriptGeneratedText: string = localize('objectManagement.scriptGenerated', "Script has been generated successfully. You can close the dialog to view it in the newly opened editor.") export const ScriptGeneratedText: string = localize('objectManagement.scriptGenerated', "Script has been generated successfully. You can close the dialog to view it in the newly opened editor.")

View File

@@ -11,7 +11,6 @@ import { BaseService, ISqlOpsFeature, SqlOpsDataClient } from 'dataprotocol-clie
import { ObjectManagement, IObjectManagementService } from 'mssql'; import { ObjectManagement, IObjectManagementService } from 'mssql';
import { ClientCapabilities } from 'vscode-languageclient'; import { ClientCapabilities } from 'vscode-languageclient';
import { AppContext } from '../appContext'; import { AppContext } from '../appContext';
import { AuthenticationType, UserType } from './constants';
export class ObjectManagementService extends BaseService implements IObjectManagementService { export class ObjectManagementService extends BaseService implements IObjectManagementService {
public static asFeature(context: AppContext): ISqlOpsFeature { public static asFeature(context: AppContext): ISqlOpsFeature {
@@ -29,235 +28,190 @@ export class ObjectManagementService extends BaseService implements IObjectManag
}; };
} }
async initializeView(contextId: string, objectType: ObjectManagement.NodeType, connectionUri: string, database: string, isNewObject: boolean, parentUrn: string, objectUrn: string): Promise<ObjectManagement.ObjectViewInfo<ObjectManagement.SqlObject>> {
const params: contracts.InitializeViewRequestParams = { connectionUri, contextId, isNewObject, objectType, database, parentUrn, objectUrn };
return this.runWithErrorHandling(contracts.InitializeViewRequest.type, params);
}
private constructor(context: AppContext, client: SqlOpsDataClient) { private constructor(context: AppContext, client: SqlOpsDataClient) {
super(client); super(client);
context.registerService(constants.ObjectManagementService, this); context.registerService(constants.ObjectManagementService, this);
} }
async initializeLoginView(connectionUri: string, contextId: string, isNewObject: boolean, name: string | undefined): Promise<ObjectManagement.LoginViewInfo> { async save(contextId: string, object: ObjectManagement.SqlObject): Promise<void> {
const params: contracts.InitializeLoginViewRequestParams = { connectionUri, contextId, isNewObject, name }; const params: contracts.SaveObjectRequestParams = { contextId, object };
return this.runWithErrorHandling(contracts.InitializeLoginViewRequest.type, params); return this.runWithErrorHandling(contracts.SaveObjectRequest.type, params);
} }
async createLogin(contextId: string, login: ObjectManagement.Login): Promise<void> { async script(contextId: string, object: ObjectManagement.SqlObject): Promise<string> {
const params: contracts.CreateLoginRequestParams = { contextId, login }; const params: contracts.ScriptObjectRequestParams = { contextId, object };
return this.runWithErrorHandling(contracts.CreateLoginRequest.type, params); return this.runWithErrorHandling(contracts.ScriptObjectRequest.type, params);
} }
async updateLogin(contextId: string, login: ObjectManagement.Login): Promise<void> { async disposeView(contextId: string): Promise<void> {
const params: contracts.UpdateLoginRequestParams = { contextId, login }; const params: contracts.DisposeViewRequestParams = { contextId };
return this.runWithErrorHandling(contracts.UpdateLoginRequest.type, params); return this.runWithErrorHandling(contracts.DisposeViewRequest.type, params);
} }
async scriptLogin(contextId: string, login: ObjectManagement.Login): Promise<string> { async rename(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string, newName: string): Promise<void> {
const params: contracts.ScriptLoginRequestParams = { contextId, login }; const params: contracts.RenameObjectRequestParams = { connectionUri, objectUrn, newName, objectType };
return this.runWithErrorHandling(contracts.ScriptLoginRequest.type, params);
}
async disposeLoginView(contextId: string): Promise<void> {
const params: contracts.DisposeLoginViewRequestParams = { contextId };
return this.runWithErrorHandling(contracts.DisposeLoginViewRequest.type, params);
}
async initializeUserView(connectionUri: string, database: string, contextId: string, isNewObject: boolean, name: string | undefined): Promise<ObjectManagement.UserViewInfo> {
const params: contracts.InitializeUserViewRequestParams = { connectionUri, database, contextId, isNewObject, name };
return this.runWithErrorHandling(contracts.InitializeUserViewRequest.type, params);
}
async createUser(contextId: string, user: ObjectManagement.User): Promise<void> {
const params: contracts.CreateUserRequestParams = { contextId, user };
return this.runWithErrorHandling(contracts.CreateUserRequest.type, params);
}
async updateUser(contextId: string, user: ObjectManagement.User): Promise<void> {
const params: contracts.UpdateUserRequestParams = { contextId, user };
return this.runWithErrorHandling(contracts.UpdateUserRequest.type, params);
}
async scriptUser(contextId: string, user: ObjectManagement.User): Promise<string> {
const params: contracts.ScriptUserRequestParams = { contextId, user };
return this.runWithErrorHandling(contracts.ScriptUserRequest.type, params);
}
async disposeUserView(contextId: string): Promise<void> {
const params: contracts.DisposeUserViewRequestParams = { contextId };
return this.runWithErrorHandling(contracts.DisposeUserViewRequest.type, params);
}
async rename(connectionUri: string, objectUrn: string, newName: string): Promise<void> {
const params: contracts.RenameObjectRequestParams = { connectionUri, objectUrn, newName };
return this.runWithErrorHandling(contracts.RenameObjectRequest.type, params); return this.runWithErrorHandling(contracts.RenameObjectRequest.type, params);
} }
async drop(connectionUri: string, objectUrn: string): Promise<void> { async drop(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string): Promise<void> {
const params: contracts.DropObjectRequestParams = { connectionUri, objectUrn }; const params: contracts.DropObjectRequestParams = { connectionUri, objectUrn, objectType };
return this.runWithErrorHandling(contracts.DropObjectRequest.type, params); return this.runWithErrorHandling(contracts.DropObjectRequest.type, params);
} }
} }
export class TestObjectManagementService implements IObjectManagementService { export class TestObjectManagementService implements IObjectManagementService {
initializeLoginView(connectionUri: string, contextId: string, isNewObject: boolean, name: string | undefined): Promise<ObjectManagement.LoginViewInfo> { initializeView(contextId: string, objectType: ObjectManagement.NodeType, connectionUri: string, database: string, isNewObject: boolean, parentUrn: string, objectUrn: string): Thenable<ObjectManagement.ObjectViewInfo<ObjectManagement.SqlObject>> {
return new Promise((resolve, reject) => { if (objectType === ObjectManagement.NodeType.ServerLevelLogin) {
setTimeout(() => { return Promise.resolve(this.getLoginView(isNewObject, objectUrn));
const serverRoles = ['sysadmin', 'public', 'bulkadmin', 'dbcreator', 'diskadmin', 'processadmin', 'securityadmin', 'serveradmin']; } else if (objectType === ObjectManagement.NodeType.User) {
const languages = ['<default>', 'English']; return Promise.resolve(this.getUserView(isNewObject, objectUrn));
const databases = ['master', 'db1', 'db2']; }
let login: ObjectManagement.LoginViewInfo; else {
if (isNewObject) { throw Error('Not implemented');
login = <ObjectManagement.LoginViewInfo>{ }
objectInfo: {
name: '',
authenticationType: AuthenticationType.Sql,
enforcePasswordPolicy: true,
enforcePasswordExpiration: true,
mustChangePassword: true,
defaultDatabase: 'master',
defaultLanguage: '<default>',
serverRoles: ['public', 'bulkadmin'],
connectPermission: true,
isEnabled: true,
isLockedOut: false
},
supportAADAuthentication: true,
supportSQLAuthentication: true,
supportWindowsAuthentication: true,
supportAdvancedOptions: true,
supportAdvancedPasswordOptions: true,
canEditLockedOutState: false,
languages: languages,
databases: databases,
serverRoles: serverRoles
};
} else {
login = <ObjectManagement.LoginViewInfo>{
objectInfo: {
name: name,
authenticationType: AuthenticationType.Sql,
enforcePasswordPolicy: true,
enforcePasswordExpiration: true,
mustChangePassword: true,
defaultDatabase: 'master',
defaultLanguage: '<default>',
serverRoles: ['public'],
connectPermission: true,
isEnabled: true,
isLockedOut: false,
password: '******************'
},
supportAADAuthentication: true,
supportSQLAuthentication: true,
supportWindowsAuthentication: true,
supportAdvancedOptions: true,
supportAdvancedPasswordOptions: true,
canEditLockedOutState: false,
languages: languages,
databases: databases,
serverRoles: serverRoles
};
}
resolve(login);
}, 3000);
});
} }
async createLogin(contextId: string, login: ObjectManagement.Login): Promise<void> { save(contextId: string, object: ObjectManagement.SqlObject): Thenable<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, 3000); }, 3000);
}); });
} }
async updateLogin(contextId: string, login: ObjectManagement.Login): Promise<void> { script(contextId: string, object: ObjectManagement.SqlObject): Thenable<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 3000);
});
}
async scriptLogin(contextId: string, login: ObjectManagement.Login): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve('test script'); resolve('test script');
}, 1000); }, 1000);
}); });
} }
async disposeLoginView(contextId: string): Promise<void> { disposeView(contextId: string): Thenable<void> {
}
async initializeUserView(connectionUri: string, database: string, contextId: string, isNewObject: boolean, name: string): Promise<ObjectManagement.UserViewInfo> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
let viewInfo: ObjectManagement.UserViewInfo; resolve();
const languages = ['<default>', 'English']; }, 100);
const schemas = ['dbo', 'sys', 'alanren']; });
const logins = ['sa', 'alanren', 'alanren@microsoft.com']; }
const databaseRoles = ['dbmanager', 'loginmanager', 'bulkadmin', 'sysadmin', 'tablemanager', 'viewmanager']; async rename(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string, newName: string): Promise<void> {
return this.delayAndResolve();
}
async drop(connectionUri: string, objectType: ObjectManagement.NodeType, objectUrn: string): Promise<void> {
return this.delayAndResolve();
}
private getLoginView(isNewObject: boolean, name: string): ObjectManagement.LoginViewInfo {
const serverRoles = ['sysadmin', 'public', 'bulkadmin', 'dbcreator', 'diskadmin', 'processadmin', 'securityadmin', 'serveradmin'];
const languages = ['<default>', 'English'];
const databases = ['master', 'db1', 'db2'];
let login: ObjectManagement.LoginViewInfo;
if (isNewObject) {
login = <ObjectManagement.LoginViewInfo>{
objectInfo: {
name: '',
authenticationType: ObjectManagement.AuthenticationType.Sql,
enforcePasswordPolicy: true,
enforcePasswordExpiration: true,
mustChangePassword: true,
defaultDatabase: 'master',
defaultLanguage: '<default>',
serverRoles: ['public', 'bulkadmin'],
connectPermission: true,
isEnabled: true,
isLockedOut: false
},
supportAADAuthentication: true,
supportSQLAuthentication: true,
supportWindowsAuthentication: true,
supportAdvancedOptions: true,
supportAdvancedPasswordOptions: true,
canEditLockedOutState: false,
languages: languages,
databases: databases,
serverRoles: serverRoles
};
} else {
login = <ObjectManagement.LoginViewInfo>{
objectInfo: {
name: name,
authenticationType: ObjectManagement.AuthenticationType.Sql,
enforcePasswordPolicy: true,
enforcePasswordExpiration: true,
mustChangePassword: true,
defaultDatabase: 'master',
defaultLanguage: '<default>',
serverRoles: ['public'],
connectPermission: true,
isEnabled: true,
isLockedOut: false,
password: '******************'
},
supportAADAuthentication: true,
supportSQLAuthentication: true,
supportWindowsAuthentication: true,
supportAdvancedOptions: true,
supportAdvancedPasswordOptions: true,
canEditLockedOutState: false,
languages: languages,
databases: databases,
serverRoles: serverRoles
};
}
return login;
}
private getUserView(isNewObject: boolean, name: string): ObjectManagement.UserViewInfo {
let viewInfo: ObjectManagement.UserViewInfo;
const languages = ['<default>', 'English'];
const schemas = ['dbo', 'sys', 'alanren'];
const logins = ['sa', 'alanren', 'alanren@microsoft.com'];
const databaseRoles = ['dbmanager', 'loginmanager', 'bulkadmin', 'sysadmin', 'tablemanager', 'viewmanager'];
if (isNewObject) { if (isNewObject) {
viewInfo = { viewInfo = {
objectInfo: <ObjectManagement.User>{ objectInfo: <ObjectManagement.User>{
name: '', name: '',
type: UserType.WithLogin, type: ObjectManagement.UserType.WithLogin,
defaultSchema: 'dbo', defaultSchema: 'dbo',
defaultLanguage: '<default>', defaultLanguage: '<default>',
authenticationType: AuthenticationType.Sql, authenticationType: ObjectManagement.AuthenticationType.Sql,
loginName: 'sa', loginName: 'sa',
ownedSchemas: [], ownedSchemas: [],
databaseRoles: [], databaseRoles: [],
password: '' password: ''
}, },
languages: languages, languages: languages,
schemas: schemas, schemas: schemas,
logins: logins, logins: logins,
databaseRoles: databaseRoles, databaseRoles: databaseRoles,
supportContainedUser: true, supportContainedUser: true,
supportAADAuthentication: true, supportAADAuthentication: true,
supportSQLAuthentication: true, supportSQLAuthentication: true,
supportWindowsAuthentication: true supportWindowsAuthentication: true
}; };
} else { } else {
viewInfo = { viewInfo = {
objectInfo: <ObjectManagement.User>{ objectInfo: <ObjectManagement.User>{
name: name, name: name,
type: UserType.WithLogin, type: ObjectManagement.UserType.WithLogin,
defaultSchema: 'dbo', defaultSchema: 'dbo',
defaultLanguage: '<default>', defaultLanguage: '<default>',
loginName: 'sa', loginName: 'sa',
authenticationType: AuthenticationType.Sql, authenticationType: ObjectManagement.AuthenticationType.Sql,
ownedSchemas: ['dbo'], ownedSchemas: ['dbo'],
databaseRoles: ['dbmanager', 'bulkadmin'] databaseRoles: ['dbmanager', 'bulkadmin']
}, },
languages: languages, languages: languages,
schemas: schemas, schemas: schemas,
logins: logins, logins: logins,
databaseRoles: databaseRoles, databaseRoles: databaseRoles,
supportContainedUser: true, supportContainedUser: true,
supportAADAuthentication: true, supportAADAuthentication: true,
supportSQLAuthentication: true, supportSQLAuthentication: true,
supportWindowsAuthentication: true supportWindowsAuthentication: true
}; };
} }
resolve(viewInfo); return viewInfo;
}, 3000);
});
}
async createUser(contextId: string, user: ObjectManagement.User): Promise<void> {
return this.delayAndResolve();
}
async updateUser(contextId: string, login: ObjectManagement.User): Promise<void> {
return this.delayAndResolve();
}
async scriptUser(contextId: string, login: ObjectManagement.User): Promise<string> {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('generate script for user not supported');
}, 1000);
});
}
async disposeUserView(contextId: string): Promise<void> {
}
async rename(connectionUri: string, objectUrn: string, newName: string): Promise<void> {
return this.delayAndResolve();
}
async drop(connectionUri: string, objectUrn: string): Promise<void> {
return this.delayAndResolve();
} }
private delayAndResolve(): Promise<void> { private delayAndResolve(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@@ -4,14 +4,13 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata'; import * as azdata from 'azdata';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { DefaultInputWidth, ObjectManagementDialogBase } from './objectManagementDialogBase'; import { DefaultInputWidth, ObjectManagementDialogBase, ObjectManagementDialogOptions } from './objectManagementDialogBase';
import { IObjectManagementService, ObjectManagement } from 'mssql'; import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as localizedConstants from '../localizedConstants'; import * as localizedConstants from '../localizedConstants';
import { AlterLoginDocUrl, AuthenticationType, CreateLoginDocUrl, NodeType, PublicServerRoleName } from '../constants'; import { AlterLoginDocUrl, CreateLoginDocUrl, PublicServerRoleName } from '../constants';
import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, isValidSQLPassword } from '../utils'; import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, isValidSQLPassword } from '../utils';
export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Login, ObjectManagement.LoginViewInfo> { export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Login, ObjectManagement.LoginViewInfo> {
private generalSection: azdata.GroupContainer; private generalSection: azdata.GroupContainer;
private sqlAuthSection: azdata.GroupContainer; private sqlAuthSection: azdata.GroupContainer;
private serverRoleSection: azdata.GroupContainer; private serverRoleSection: azdata.GroupContainer;
@@ -32,15 +31,19 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
private enabledCheckbox: azdata.CheckBoxComponent; private enabledCheckbox: azdata.CheckBoxComponent;
private lockedOutCheckbox: azdata.CheckBoxComponent; private lockedOutCheckbox: azdata.CheckBoxComponent;
constructor(objectManagementService: IObjectManagementService, connectionUri: string, isNewObject: boolean, name?: string, objectExplorerContext?: azdata.ObjectExplorerContext) { constructor(objectManagementService: IObjectManagementService, options: ObjectManagementDialogOptions) {
super(NodeType.Login, isNewObject ? CreateLoginDocUrl : AlterLoginDocUrl, objectManagementService, connectionUri, isNewObject, name, objectExplorerContext); super(objectManagementService, options);
}
protected override get docUrl(): string {
return this.options.isNewObject ? CreateLoginDocUrl : AlterLoginDocUrl
} }
protected override async onConfirmation(): Promise<boolean> { protected override async onConfirmation(): Promise<boolean> {
// Empty password is only allowed when advanced password options are supported and the password policy check is off. // 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. // To match the SSMS behavior, a warning is shown to the user.
if (this.viewInfo.supportAdvancedPasswordOptions if (this.viewInfo.supportAdvancedPasswordOptions
&& this.objectInfo.authenticationType === AuthenticationType.Sql && this.objectInfo.authenticationType === ObjectManagement.AuthenticationType.Sql
&& !this.objectInfo.password && !this.objectInfo.password
&& !this.objectInfo.enforcePasswordPolicy) { && !this.objectInfo.enforcePasswordPolicy) {
const result = await vscode.window.showWarningMessage(localizedConstants.BlankPasswordConfirmationText, { modal: true }, localizedConstants.YesText); const result = await vscode.window.showWarningMessage(localizedConstants.BlankPasswordConfirmationText, { modal: true }, localizedConstants.YesText);
@@ -54,14 +57,14 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
if (!this.objectInfo.name) { if (!this.objectInfo.name) {
errors.push(localizedConstants.NameCannotBeEmptyError); errors.push(localizedConstants.NameCannotBeEmptyError);
} }
if (this.objectInfo.authenticationType === AuthenticationType.Sql) { if (this.objectInfo.authenticationType === ObjectManagement.AuthenticationType.Sql) {
if (!this.objectInfo.password && !(this.viewInfo.supportAdvancedPasswordOptions && !this.objectInfo.enforcePasswordPolicy)) { if (!this.objectInfo.password && !(this.viewInfo.supportAdvancedPasswordOptions && !this.objectInfo.enforcePasswordPolicy)) {
errors.push(localizedConstants.PasswordCannotBeEmptyError); errors.push(localizedConstants.PasswordCannotBeEmptyError);
} }
if (this.objectInfo.password && (this.objectInfo.enforcePasswordPolicy || !this.viewInfo.supportAdvancedPasswordOptions) if (this.objectInfo.password && (this.objectInfo.enforcePasswordPolicy || !this.viewInfo.supportAdvancedPasswordOptions)
&& !isValidSQLPassword(this.objectInfo.password, this.objectInfo.name) && !isValidSQLPassword(this.objectInfo.password, this.objectInfo.name)
&& (this.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) { && (this.options.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) {
errors.push(localizedConstants.InvalidPasswordError); errors.push(localizedConstants.InvalidPasswordError);
} }
@@ -76,22 +79,8 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
return errors; return errors;
} }
protected async onComplete(): Promise<void> { protected override postInitializeData(): void {
if (this.isNewObject) { this.objectInfo.password = this.objectInfo.password ?? '';
await this.objectManagementService.createLogin(this.contextId, this.objectInfo);
} else {
await this.objectManagementService.updateLogin(this.contextId, this.objectInfo);
}
}
protected async disposeView(): 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> { protected async initializeUI(): Promise<void> {
@@ -99,7 +88,7 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
this.initializeGeneralSection(); this.initializeGeneralSection();
sections.push(this.generalSection); sections.push(this.generalSection);
if (this.isNewObject || this.objectInfo.authenticationType === 'Sql') { if (this.options.isNewObject || this.objectInfo.authenticationType === 'Sql') {
this.initializeSqlAuthSection(); this.initializeSqlAuthSection();
sections.push(this.sqlAuthSection); sections.push(this.sqlAuthSection);
} }
@@ -114,14 +103,10 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
this.formContainer.addItems(sections); this.formContainer.addItems(sections);
} }
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptLogin(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void { private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({ this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText, ariaLabel: localizedConstants.NameText,
enabled: this.isNewObject, enabled: this.options.isNewObject,
value: this.objectInfo.name, value: this.objectInfo.name,
width: DefaultInputWidth width: DefaultInputWidth
}).component(); }).component();
@@ -142,7 +127,7 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
if (this.viewInfo.supportAADAuthentication) { if (this.viewInfo.supportAADAuthentication) {
authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText); authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText);
} }
this.authTypeDropdown = this.createDropdown(localizedConstants.AuthTypeText, authTypes, getAuthenticationTypeDisplayName(this.objectInfo.authenticationType), this.isNewObject); this.authTypeDropdown = this.createDropdown(localizedConstants.AuthTypeText, authTypes, getAuthenticationTypeDisplayName(this.objectInfo.authenticationType), this.options.isNewObject);
this.disposables.push(this.authTypeDropdown.onValueChanged(async () => { this.disposables.push(this.authTypeDropdown.onValueChanged(async () => {
this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value); this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value);
this.setViewByAuthenticationType(); this.setViewByAuthenticationType();
@@ -175,7 +160,7 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
const confirmPasswordRow = this.createLabelInputContainer(localizedConstants.ConfirmPasswordText, this.confirmPasswordInput); const confirmPasswordRow = this.createLabelInputContainer(localizedConstants.ConfirmPasswordText, this.confirmPasswordInput);
items.push(passwordRow, confirmPasswordRow); items.push(passwordRow, confirmPasswordRow);
if (!this.isNewObject) { if (!this.options.isNewObject) {
this.specifyOldPasswordCheckbox = this.createCheckbox(localizedConstants.SpecifyOldPasswordText); this.specifyOldPasswordCheckbox = this.createCheckbox(localizedConstants.SpecifyOldPasswordText);
this.oldPasswordInput = this.createPasswordInputBox(localizedConstants.OldPasswordText, '', false); this.oldPasswordInput = this.createPasswordInputBox(localizedConstants.OldPasswordText, '', false);
const oldPasswordRow = this.createLabelInputContainer(localizedConstants.OldPasswordText, this.oldPasswordInput); const oldPasswordRow = this.createLabelInputContainer(localizedConstants.OldPasswordText, this.oldPasswordInput);
@@ -222,7 +207,7 @@ export class LoginDialog extends ObjectManagementDialogBase<ObjectManagement.Log
this.onObjectValueChange(); this.onObjectValueChange();
})); }));
items.push(this.enforcePasswordPolicyCheckbox, this.enforcePasswordExpirationCheckbox, this.mustChangePasswordCheckbox); items.push(this.enforcePasswordPolicyCheckbox, this.enforcePasswordExpirationCheckbox, this.mustChangePasswordCheckbox);
if (!this.isNewObject) { if (!this.options.isNewObject) {
this.lockedOutCheckbox = this.createCheckbox(localizedConstants.LoginLockedOutText, this.objectInfo.isLockedOut, this.viewInfo.canEditLockedOutState); this.lockedOutCheckbox = this.createCheckbox(localizedConstants.LoginLockedOutText, this.objectInfo.isLockedOut, this.viewInfo.canEditLockedOutState);
items.push(this.lockedOutCheckbox); items.push(this.lockedOutCheckbox);
this.disposables.push(this.lockedOutCheckbox.onChanged(() => { this.disposables.push(this.lockedOutCheckbox.onChanged(() => {

View File

@@ -12,11 +12,11 @@ import * as vscode from 'vscode';
import { EOL } from 'os'; import { EOL } from 'os';
import { generateUuid } from 'vscode-languageclient/lib/utils/uuid'; import { generateUuid } from 'vscode-languageclient/lib/utils/uuid';
import { getErrorMessage } from '../../utils'; import { getErrorMessage } from '../../utils';
import { NodeType, TelemetryActions, TelemetryViews } from '../constants'; import { TelemetryActions, ObjectManagementViewName } from '../constants';
import { import {
CreateObjectOperationDisplayName, HelpText, LoadingDialogText, CreateObjectOperationDisplayName, HelpText, LoadingDialogText,
NameText, NameText,
NewObjectDialogTitle, ObjectPropertiesDialogTitle, OkText, ScriptError, ScriptGeneratedText, ScriptText, SelectedText, UpdateObjectOperationDisplayName NewObjectDialogTitle, NoActionScriptedMessage, ObjectPropertiesDialogTitle, OkText, ScriptError, ScriptGeneratedText, ScriptText, SelectedText, UpdateObjectOperationDisplayName
} from '../localizedConstants'; } from '../localizedConstants';
import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils'; import { deepClone, getNodeTypeDisplayName, refreshNode } from '../utils';
import { TelemetryReporter } from '../../telemetry'; import { TelemetryReporter } from '../../telemetry';
@@ -34,14 +34,26 @@ export function getTableHeight(rowCount: number, minRowCount: number = DefaultTa
return Math.min(Math.max(rowCount, minRowCount) * TableRowHeight + TableColumnHeaderHeight, maxHeight); return Math.min(Math.max(rowCount, minRowCount) * TableRowHeight + TableColumnHeaderHeight, maxHeight);
} }
function getDialogName(type: NodeType, isNewObject: boolean): string { function getDialogName(type: ObjectManagement.NodeType, isNewObject: boolean): string {
return isNewObject ? `New${type}` : `${type}Properties` return isNewObject ? `New${type}` : `${type}Properties`
} }
export interface ObjectManagementDialogOptions {
connectionUri: string;
database?: string;
objectType: ObjectManagement.NodeType;
isNewObject: boolean;
parentUrn: string;
objectUrn?: string;
objectExplorerContext?: azdata.ObjectExplorerContext;
width?: azdata.window.DialogWidth;
objectName?: string;
}
export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectManagement.SqlObject, ViewInfoType extends ObjectManagement.ObjectViewInfo<ObjectInfoType>> { export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectManagement.SqlObject, ViewInfoType extends ObjectManagement.ObjectViewInfo<ObjectInfoType>> {
protected readonly disposables: vscode.Disposable[] = []; protected readonly disposables: vscode.Disposable[] = [];
protected readonly dialogObject: azdata.window.Dialog; protected readonly dialogObject: azdata.window.Dialog;
protected readonly contextId: string; private _contextId: string;
private _viewInfo: ViewInfoType; private _viewInfo: ViewInfoType;
private _originalObjectInfo: ObjectInfoType; private _originalObjectInfo: ObjectInfoType;
private _modelView: azdata.ModelView; private _modelView: azdata.ModelView;
@@ -50,28 +62,22 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
private _helpButton: azdata.window.Button; private _helpButton: azdata.window.Button;
private _scriptButton: azdata.window.Button; private _scriptButton: azdata.window.Button;
constructor(private readonly objectType: NodeType, constructor(protected readonly objectManagementService: IObjectManagementService, protected readonly options: ObjectManagementDialogOptions) {
docUrl: string, this.options.width = this.options.width || 'narrow';
protected readonly objectManagementService: IObjectManagementService, const objectTypeDisplayName = getNodeTypeDisplayName(options.objectType, true);
protected readonly connectionUri: string, const dialogTitle = options.isNewObject ? NewObjectDialogTitle(objectTypeDisplayName) : ObjectPropertiesDialogTitle(objectTypeDisplayName, options.objectName);
protected isNewObject: boolean, this.dialogObject = azdata.window.createModelViewDialog(dialogTitle, getDialogName(options.objectType, options.isNewObject), options.width);
protected readonly objectName: string = '',
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.dialogObject.okButton.label = OkText;
this.disposables.push(this.dialogObject.onClosed(async (reason: azdata.window.CloseReason) => { await this.dispose(reason); })); this.disposables.push(this.dialogObject.onClosed(async (reason: azdata.window.CloseReason) => { await this.dispose(reason); }));
this._helpButton = azdata.window.createButton(HelpText, 'left'); this._helpButton = azdata.window.createButton(HelpText, 'left');
this.disposables.push(this._helpButton.onClick(async () => { this.disposables.push(this._helpButton.onClick(async () => {
await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(docUrl)); await vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(this.docUrl));
})); }));
this._scriptButton = azdata.window.createButton(ScriptText, 'left'); this._scriptButton = azdata.window.createButton(ScriptText, 'left');
this.disposables.push(this._scriptButton.onClick(async () => { await this.onScriptButtonClick(); })); this.disposables.push(this._scriptButton.onClick(async () => { await this.onScriptButtonClick(); }));
this.dialogObject.customButtons = [this._helpButton, this._scriptButton]; this.dialogObject.customButtons = [this._helpButton, this._scriptButton];
this.updateLoadingStatus(true); this.updateLoadingStatus(true);
this.contextId = generateUuid(); this._contextId = generateUuid();
this.dialogObject.registerCloseValidator(async (): Promise<boolean> => { this.dialogObject.registerCloseValidator(async (): Promise<boolean> => {
const confirmed = await this.onConfirmation(); const confirmed = await this.onConfirmation();
if (!confirmed) { if (!confirmed) {
@@ -81,18 +87,13 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
}); });
} }
protected abstract initializeData(): Promise<ViewInfoType>;
protected abstract initializeUI(): Promise<void>; protected abstract initializeUI(): Promise<void>;
protected abstract onComplete(): Promise<void>;
protected async onDispose(): Promise<void> { }
protected abstract validateInput(): Promise<string[]>; protected abstract validateInput(): Promise<string[]>;
protected abstract generateScript(): Promise<string>; protected abstract get docUrl(): string;
/** protected postInitializeData(): void {
* Dispose the information related to this view in the backend service.
*/
protected abstract disposeView(): Promise<void>;
}
protected onObjectValueChange(): void { protected onObjectValueChange(): void {
this.dialogObject.okButton.enabled = this.isDirty; this.dialogObject.okButton.enabled = this.isDirty;
} }
@@ -141,28 +142,28 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
}); });
})); }));
azdata.window.openDialog(this.dialogObject); azdata.window.openDialog(this.dialogObject);
this._viewInfo = await this.initializeData(); await this.initializeData();
await initializeViewPromise; await initializeViewPromise;
await this.initializeUI(); await this.initializeUI();
this._originalObjectInfo = deepClone(this.objectInfo); this._originalObjectInfo = deepClone(this.objectInfo);
const typeDisplayName = getNodeTypeDisplayName(this.objectType); const typeDisplayName = getNodeTypeDisplayName(this.options.objectType);
this.dialogObject.registerOperation({ this.dialogObject.registerOperation({
displayName: this.isNewObject ? CreateObjectOperationDisplayName(typeDisplayName) displayName: this.options.isNewObject ? CreateObjectOperationDisplayName(typeDisplayName)
: UpdateObjectOperationDisplayName(typeDisplayName, this.objectName), : UpdateObjectOperationDisplayName(typeDisplayName, this.options.objectName),
description: '', description: '',
isCancelable: false, isCancelable: false,
operation: async (operation: azdata.BackgroundOperation): Promise<void> => { operation: async (operation: azdata.BackgroundOperation): Promise<void> => {
const actionName = this.isNewObject ? TelemetryActions.CreateObject : TelemetryActions.UpdateObject; const actionName = this.options.isNewObject ? TelemetryActions.CreateObject : TelemetryActions.UpdateObject;
try { try {
if (JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo)) { if (JSON.stringify(this.objectInfo) !== JSON.stringify(this._originalObjectInfo)) {
const startTime = Date.now(); const startTime = Date.now();
await this.onComplete(); await this.objectManagementService.save(this._contextId, this.objectInfo);
if (this.isNewObject && this.objectExplorerContext) { if (this.options.isNewObject && this.options.objectExplorerContext) {
await refreshNode(this.objectExplorerContext); await refreshNode(this.options.objectExplorerContext);
} }
TelemetryReporter.sendTelemetryEvent(actionName, { TelemetryReporter.sendTelemetryEvent(actionName, {
objectType: this.objectType objectType: this.options.objectType
}, { }, {
elapsedTimeMs: Date.now() - startTime elapsedTimeMs: Date.now() - startTime
}); });
@@ -171,8 +172,8 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
} }
catch (err) { catch (err) {
operation.updateStatus(azdata.TaskStatus.Failed, getErrorMessage(err)); operation.updateStatus(azdata.TaskStatus.Failed, getErrorMessage(err));
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, actionName, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, actionName, err).withAdditionalProperties({
objectType: this.objectType objectType: this.options.objectType
}).send(); }).send();
} finally { } finally {
await this.disposeView(); await this.disposeView();
@@ -181,9 +182,9 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
}); });
this.updateLoadingStatus(false); this.updateLoadingStatus(false);
} catch (err) { } catch (err) {
const actionName = this.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog; const actionName = this.options.isNewObject ? TelemetryActions.OpenNewObjectDialog : TelemetryActions.OpenPropertiesDialog;
TelemetryReporter.createErrorEvent2(TelemetryViews.ObjectManagement, actionName, err).withAdditionalProperties({ TelemetryReporter.createErrorEvent2(ObjectManagementViewName, actionName, err).withAdditionalProperties({
objectType: this.objectType objectType: this.options.objectType
}).send(); }).send();
void vscode.window.showErrorMessage(getErrorMessage(err)); void vscode.window.showErrorMessage(getErrorMessage(err));
azdata.window.closeDialog(this.dialogObject); azdata.window.closeDialog(this.dialogObject);
@@ -191,13 +192,22 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
} }
private async dispose(reason: azdata.window.CloseReason): Promise<void> { private async dispose(reason: azdata.window.CloseReason): Promise<void> {
await this.onDispose();
this.disposables.forEach(disposable => disposable.dispose()); this.disposables.forEach(disposable => disposable.dispose());
if (reason !== 'ok') { if (reason !== 'ok') {
await this.disposeView(); await this.disposeView();
} }
} }
private async disposeView(): Promise<void> {
await this.objectManagementService.disposeView(this._contextId);
}
private async initializeData(): Promise<void> {
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();
}
protected async runValidation(showErrorMessage: boolean = true): Promise<boolean> { protected async runValidation(showErrorMessage: boolean = true): Promise<boolean> {
const errors = await this.validateInput(); const errors = await this.validateInput();
if (errors.length > 0 && (this.dialogObject.message?.text || showErrorMessage)) { if (errors.length > 0 && (this.dialogObject.message?.text || showErrorMessage)) {
@@ -335,10 +345,16 @@ export abstract class ObjectManagementDialogBase<ObjectInfoType extends ObjectMa
if (!isValid) { if (!isValid) {
return; return;
} }
const script = await this.generateScript(); let message: string;
await azdata.queryeditor.openQueryDocument({ content: script }, providerId); const script = await this.objectManagementService.script(this._contextId, this.objectInfo);
if (script) {
message = ScriptGeneratedText;
await azdata.queryeditor.openQueryDocument({ content: script }, providerId);
} else {
message = NoActionScriptedMessage;
}
this.dialogObject.message = { this.dialogObject.message = {
text: ScriptGeneratedText, text: message,
level: azdata.window.MessageLevel.Information level: azdata.window.MessageLevel.Information
}; };
} catch (err) { } catch (err) {

View File

@@ -3,10 +3,10 @@
* Licensed under the Source EULA. See License.txt in the project root for license information. * Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import * as azdata from 'azdata'; import * as azdata from 'azdata';
import { DefaultInputWidth, ObjectManagementDialogBase } from './objectManagementDialogBase'; import { DefaultInputWidth, ObjectManagementDialogBase, ObjectManagementDialogOptions } from './objectManagementDialogBase';
import { IObjectManagementService, ObjectManagement } from 'mssql'; import { IObjectManagementService, ObjectManagement } from 'mssql';
import * as localizedConstants from '../localizedConstants'; import * as localizedConstants from '../localizedConstants';
import { AlterUserDocUrl, AuthenticationType, CreateUserDocUrl, NodeType, UserType } from '../constants'; import { AlterUserDocUrl, CreateUserDocUrl } from '../constants';
import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, getUserTypeByDisplayName, getUserTypeDisplayName, isValidSQLPassword } from '../utils'; import { getAuthenticationTypeByDisplayName, getAuthenticationTypeDisplayName, getUserTypeByDisplayName, getUserTypeDisplayName, isValidSQLPassword } from '../utils';
export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User, ObjectManagement.UserViewInfo> { export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User, ObjectManagement.UserViewInfo> {
@@ -31,14 +31,16 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
private ownedSchemaTable: azdata.TableComponent; private ownedSchemaTable: azdata.TableComponent;
private membershipTable: azdata.TableComponent; private membershipTable: azdata.TableComponent;
constructor(objectManagementService: IObjectManagementService, connectionUri: string, private readonly database: string, isNewObject: boolean, name?: string, objectExplorerContext?: azdata.ObjectExplorerContext) { constructor(objectManagementService: IObjectManagementService, options: ObjectManagementDialogOptions) {
super(NodeType.User, isNewObject ? CreateUserDocUrl : AlterUserDocUrl, objectManagementService, connectionUri, isNewObject, name, objectExplorerContext); super(objectManagementService, options);
} }
protected async initializeData(): Promise<ObjectManagement.UserViewInfo> { protected override get docUrl(): string {
const viewInfo = await this.objectManagementService.initializeUserView(this.connectionUri, this.database, this.contextId, this.isNewObject, this.objectName); return this.options.isNewObject ? CreateUserDocUrl : AlterUserDocUrl;
viewInfo.objectInfo.password = viewInfo.objectInfo.password ?? ''; }
return viewInfo;
protected override postInitializeData(): void {
this.objectInfo.password = this.objectInfo.password ?? '';
} }
protected async validateInput(): Promise<string[]> { protected async validateInput(): Promise<string[]> {
@@ -46,7 +48,7 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
if (!this.objectInfo.name) { if (!this.objectInfo.name) {
errors.push(localizedConstants.NameCannotBeEmptyError); errors.push(localizedConstants.NameCannotBeEmptyError);
} }
if (this.objectInfo.type === UserType.Contained && this.objectInfo.authenticationType === AuthenticationType.Sql) { if (this.objectInfo.type === ObjectManagement.UserType.Contained && this.objectInfo.authenticationType === ObjectManagement.AuthenticationType.Sql) {
if (!this.objectInfo.password) { if (!this.objectInfo.password) {
errors.push(localizedConstants.PasswordCannotBeEmptyError); errors.push(localizedConstants.PasswordCannotBeEmptyError);
} }
@@ -54,10 +56,10 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
errors.push(localizedConstants.PasswordsNotMatchError); errors.push(localizedConstants.PasswordsNotMatchError);
} }
if (!isValidSQLPassword(this.objectInfo.password!, this.objectInfo.name) if (!isValidSQLPassword(this.objectInfo.password!, this.objectInfo.name)
&& (this.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) { && (this.options.isNewObject || this.objectInfo.password !== this.originalObjectInfo.password)) {
errors.push(localizedConstants.InvalidPasswordError); errors.push(localizedConstants.InvalidPasswordError);
} }
} else if (this.objectInfo.type === UserType.WithLogin) { } else if (this.objectInfo.type === ObjectManagement.UserType.WithLogin) {
if (!this.objectInfo.loginName) { if (!this.objectInfo.loginName) {
errors.push(localizedConstants.LoginNotSelectedError); errors.push(localizedConstants.LoginNotSelectedError);
} }
@@ -65,18 +67,6 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
return errors; 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 disposeView(): Promise<void> {
await this.objectManagementService.disposeUserView(this.contextId);
}
protected async initializeUI(): Promise<void> { protected async initializeUI(): Promise<void> {
this.initializeGeneralSection(); this.initializeGeneralSection();
this.initializeOwnedSchemaSection(); this.initializeOwnedSchemaSection();
@@ -88,14 +78,10 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
}, 100); }, 100);
} }
protected async generateScript(): Promise<string> {
return this.objectManagementService.scriptUser(this.contextId, this.objectInfo);
}
private initializeGeneralSection(): void { private initializeGeneralSection(): void {
this.nameInput = this.modelView.modelBuilder.inputBox().withProps({ this.nameInput = this.modelView.modelBuilder.inputBox().withProps({
ariaLabel: localizedConstants.NameText, ariaLabel: localizedConstants.NameText,
enabled: this.isNewObject, enabled: this.options.isNewObject,
value: this.objectInfo.name, value: this.objectInfo.name,
width: DefaultInputWidth width: DefaultInputWidth
}).component(); }).component();
@@ -114,7 +100,7 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
// only supporting user with login for initial preview // only supporting user with login for initial preview
const userTypes = [localizedConstants.UserWithLoginText, localizedConstants.UserWithWindowsGroupLoginText, localizedConstants.ContainedUserText, localizedConstants.UserWithNoConnectAccess]; const userTypes = [localizedConstants.UserWithLoginText, localizedConstants.UserWithWindowsGroupLoginText, localizedConstants.ContainedUserText, localizedConstants.UserWithNoConnectAccess];
this.typeDropdown = this.createDropdown(localizedConstants.UserTypeText, userTypes, getUserTypeDisplayName(this.objectInfo.type), this.isNewObject); this.typeDropdown = this.createDropdown(localizedConstants.UserTypeText, userTypes, getUserTypeDisplayName(this.objectInfo.type), this.options.isNewObject);
this.disposables.push(this.typeDropdown.onValueChanged(async () => { this.disposables.push(this.typeDropdown.onValueChanged(async () => {
this.objectInfo.type = getUserTypeByDisplayName(<string>this.typeDropdown.value); this.objectInfo.type = getUserTypeByDisplayName(<string>this.typeDropdown.value);
this.onObjectValueChange(); this.onObjectValueChange();
@@ -122,7 +108,7 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
await this.runValidation(false); await this.runValidation(false);
})); }));
this.typeContainer = this.createLabelInputContainer(localizedConstants.UserTypeText, this.typeDropdown); this.typeContainer = this.createLabelInputContainer(localizedConstants.UserTypeText, this.typeDropdown);
this.loginDropdown = this.createDropdown(localizedConstants.LoginText, this.viewInfo.logins, this.objectInfo.loginName, this.isNewObject); this.loginDropdown = this.createDropdown(localizedConstants.LoginText, this.viewInfo.logins, this.objectInfo.loginName, this.options.isNewObject);
this.disposables.push(this.loginDropdown.onValueChanged(async () => { this.disposables.push(this.loginDropdown.onValueChanged(async () => {
this.objectInfo.loginName = <string>this.loginDropdown.value; this.objectInfo.loginName = <string>this.loginDropdown.value;
this.onObjectValueChange(); this.onObjectValueChange();
@@ -140,7 +126,7 @@ export class UserDialog extends ObjectManagementDialogBase<ObjectManagement.User
if (this.viewInfo.supportAADAuthentication) { if (this.viewInfo.supportAADAuthentication) {
authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText); authTypes.push(localizedConstants.AADAuthenticationTypeDisplayText);
} }
this.authTypeDropdown = this.createDropdown(localizedConstants.AuthTypeText, authTypes, getAuthenticationTypeDisplayName(this.objectInfo.authenticationType), this.isNewObject); this.authTypeDropdown = this.createDropdown(localizedConstants.AuthTypeText, authTypes, getAuthenticationTypeDisplayName(this.objectInfo.authenticationType), this.options.isNewObject);
this.authTypeContainer = this.createLabelInputContainer(localizedConstants.AuthTypeText, this.authTypeDropdown); this.authTypeContainer = this.createLabelInputContainer(localizedConstants.AuthTypeText, this.authTypeDropdown);
this.disposables.push(this.authTypeDropdown.onValueChanged(async () => { this.disposables.push(this.authTypeDropdown.onValueChanged(async () => {
this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value); this.objectInfo.authenticationType = getAuthenticationTypeByDisplayName(<string>this.authTypeDropdown.value);

View File

@@ -6,7 +6,7 @@
import * as azdata from 'azdata'; import * as azdata from 'azdata';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { getErrorMessage } from '../utils'; import { getErrorMessage } from '../utils';
import { AuthenticationType, NodeType, UserType } from './constants'; import { ObjectManagement } from 'mssql';
import { AADAuthenticationTypeDisplayText, ColumnTypeDisplayName, ContainedUserText, DatabaseTypeDisplayName, LoginTypeDisplayName, LoginTypeDisplayNameInTitle, RefreshObjectExplorerError, SQLAuthenticationTypeDisplayText, TableTypeDisplayName, UserTypeDisplayName, UserTypeDisplayNameInTitle, UserWithLoginText, UserWithNoConnectAccess, UserWithWindowsGroupLoginText, ViewTypeDisplayName, WindowsAuthenticationTypeDisplayText } from './localizedConstants'; import { AADAuthenticationTypeDisplayText, ColumnTypeDisplayName, ContainedUserText, DatabaseTypeDisplayName, LoginTypeDisplayName, LoginTypeDisplayNameInTitle, RefreshObjectExplorerError, SQLAuthenticationTypeDisplayText, TableTypeDisplayName, UserTypeDisplayName, UserTypeDisplayNameInTitle, UserWithLoginText, UserWithNoConnectAccess, UserWithWindowsGroupLoginText, ViewTypeDisplayName, WindowsAuthenticationTypeDisplayText } from './localizedConstants';
export function deepClone<T>(obj: T): T { export function deepClone<T>(obj: T): T {
@@ -55,70 +55,70 @@ export async function refreshNode(context: azdata.ObjectExplorerContext): Promis
export function getNodeTypeDisplayName(type: string, inTitle: boolean = false): string { export function getNodeTypeDisplayName(type: string, inTitle: boolean = false): string {
switch (type) { switch (type) {
case NodeType.Login: case ObjectManagement.NodeType.ServerLevelLogin:
return inTitle ? LoginTypeDisplayNameInTitle : LoginTypeDisplayName; return inTitle ? LoginTypeDisplayNameInTitle : LoginTypeDisplayName;
case NodeType.User: case ObjectManagement.NodeType.User:
return inTitle ? UserTypeDisplayNameInTitle : UserTypeDisplayName; return inTitle ? UserTypeDisplayNameInTitle : UserTypeDisplayName;
case NodeType.Table: case ObjectManagement.NodeType.Table:
return TableTypeDisplayName; return TableTypeDisplayName;
case NodeType.View: case ObjectManagement.NodeType.View:
return ViewTypeDisplayName; return ViewTypeDisplayName;
case NodeType.Column: case ObjectManagement.NodeType.Column:
return ColumnTypeDisplayName; return ColumnTypeDisplayName;
case NodeType.Database: case ObjectManagement.NodeType.Database:
return DatabaseTypeDisplayName; return DatabaseTypeDisplayName;
default: default:
throw new Error(`Unkown node type: ${type}`); throw new Error(`Unkown node type: ${type}`);
} }
} }
export function getAuthenticationTypeDisplayName(authType: AuthenticationType | undefined): string | undefined { export function getAuthenticationTypeDisplayName(authType: ObjectManagement.AuthenticationType | undefined): string | undefined {
if (authType === undefined) { return undefined; } if (authType === undefined) { return undefined; }
switch (authType) { switch (authType) {
case AuthenticationType.Windows: case ObjectManagement.AuthenticationType.Windows:
return WindowsAuthenticationTypeDisplayText; return WindowsAuthenticationTypeDisplayText;
case AuthenticationType.AzureActiveDirectory: case ObjectManagement.AuthenticationType.AzureActiveDirectory:
return AADAuthenticationTypeDisplayText; return AADAuthenticationTypeDisplayText;
default: default:
return SQLAuthenticationTypeDisplayText; return SQLAuthenticationTypeDisplayText;
} }
} }
export function getAuthenticationTypeByDisplayName(displayValue: string): AuthenticationType { export function getAuthenticationTypeByDisplayName(displayValue: string): ObjectManagement.AuthenticationType {
switch (displayValue) { switch (displayValue) {
case WindowsAuthenticationTypeDisplayText: case WindowsAuthenticationTypeDisplayText:
return AuthenticationType.Windows; return ObjectManagement.AuthenticationType.Windows;
case AADAuthenticationTypeDisplayText: case AADAuthenticationTypeDisplayText:
return AuthenticationType.AzureActiveDirectory; return ObjectManagement.AuthenticationType.AzureActiveDirectory;
default: default:
return AuthenticationType.Sql; return ObjectManagement.AuthenticationType.Sql;
} }
} }
export function getUserTypeDisplayName(userType: UserType): string { export function getUserTypeDisplayName(userType: ObjectManagement.UserType): string {
switch (userType) { switch (userType) {
case UserType.WithLogin: case ObjectManagement.UserType.WithLogin:
return UserWithLoginText; return UserWithLoginText;
case UserType.WithWindowsGroupLogin: case ObjectManagement.UserType.WithWindowsGroupLogin:
return UserWithWindowsGroupLoginText; return UserWithWindowsGroupLoginText;
case UserType.Contained: case ObjectManagement.UserType.Contained:
return ContainedUserText; return ContainedUserText;
default: default:
return UserWithNoConnectAccess; return UserWithNoConnectAccess;
} }
} }
export function getUserTypeByDisplayName(userTypeDisplayName: string): UserType { export function getUserTypeByDisplayName(userTypeDisplayName: string): ObjectManagement.UserType {
switch (userTypeDisplayName) { switch (userTypeDisplayName) {
case UserWithLoginText: case UserWithLoginText:
return UserType.WithLogin; return ObjectManagement.UserType.WithLogin;
case UserWithWindowsGroupLoginText: case UserWithWindowsGroupLoginText:
return UserType.WithWindowsGroupLogin; return ObjectManagement.UserType.WithWindowsGroupLogin;
case ContainedUserText: case ContainedUserText:
return UserType.Contained; return ObjectManagement.UserType.Contained;
default: default:
return UserType.NoConnectAccess; return ObjectManagement.UserType.NoConnectAccess;
} }
} }

View File

@@ -171,7 +171,8 @@ export class TreeNode {
label: this.label, label: this.label,
isLeaf: this.isAlwaysLeaf, isLeaf: this.isAlwaysLeaf,
metadata: this.metadata, metadata: this.metadata,
errorMessage: this.errorStateMessage errorMessage: this.errorStateMessage,
objectType: this.objectType
}; };
} }