From b5db1643875eac7731e5ba5b73cbf7e963cabd2f Mon Sep 17 00:00:00 2001 From: Hai Cao Date: Fri, 7 Apr 2023 16:18:13 -0700 Subject: [PATCH] Port code for server role smo operations (#1996) * port code for server role smo operations * styling --- .../ServerRoleManageTaskFormComponent.cs | 327 ++++++++++++++++++ 1 file changed, 327 insertions(+) create mode 100644 src/Microsoft.SqlTools.ServiceLayer/Security/ServerRoleManageTaskFormComponent.cs diff --git a/src/Microsoft.SqlTools.ServiceLayer/Security/ServerRoleManageTaskFormComponent.cs b/src/Microsoft.SqlTools.ServiceLayer/Security/ServerRoleManageTaskFormComponent.cs new file mode 100644 index 00000000..143990d1 --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/Security/ServerRoleManageTaskFormComponent.cs @@ -0,0 +1,327 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +#nullable disable + +using System; +using System.Collections.Generic; +using Microsoft.SqlServer.Management.Sdk.Sfc; +using Microsoft.SqlServer.Management.Smo; +using System.ComponentModel; +using System.Collections.Specialized; +using Microsoft.SqlServer.Management.Common; +using Microsoft.SqlTools.ServiceLayer.Management; + +namespace Microsoft.SqlTools.ServiceLayer.Security +{ + public class ServerRoleManageTaskFormComponent + { + protected ServerRole serverRole; + ServerRoleExtender extender; + + ServerRole instance; + Server server; + + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + public ServerRoleManageTaskFormComponent() + { + InitializeComponent(); + } + + public ServerRoleManageTaskFormComponent(IContainer container) + { + // container.Add(this); + InitializeComponent(); + } + + protected string MethodName + { + get + { + return "Alter"; + } + } + + protected ServerRole Instance + { + get + { +#pragma warning disable IDE0074 // Use compound assignment + if (this.instance == null) + { + this.instance = CreateSmoObjectInstance(); + } +#pragma warning restore IDE0074 // Use compound assignment + return this.instance; + } + } + + protected Server Server + { + get + { + return this.server; + } + } + + protected Microsoft.SqlServer.Management.Sdk.Sfc.ISfcPropertyProvider CreatePropertyProvider() + { +#pragma warning disable IDE0074 // Use compound assignment + if (extender == null) + { + extender = new ServerRoleExtender(this.Instance); + } +#pragma warning restore IDE0074 // Use compound assignment + return extender; + } + + protected ServerRole CreateSmoObjectInstance() + { + if (this.serverRole == null) + { + Urn urn; + // if (!SmoTaskHelper.TryGetUrn(this.TaskManager.Context, out urn)) + if (true) // TODO new server role + { + this.serverRole = new ServerRole(this.Server, GetServerRoleName()); + } + else + { + this.serverRole = this.Server.GetSmoObject(urn) as ServerRole; + } + } + return this.serverRole; + } + + protected void PerformTask() + { + this.CreateOrManage(); + + //General Page Permissions Related actions + EventHandler tempEventHandler = (EventHandler)this.extender.GeneralPageOnRunNow; + if (tempEventHandler != null) + { + tempEventHandler(this, new EventArgs()); + } + + //Disposing DataContainer object as it has a dedicated server connection. + if (this.extender.GeneralPageDataContainer != null) + { + ((CDataContainer)this.extender.GeneralPageDataContainer).Dispose(); + } + } + + private void CreateOrManage() + { + //General Page Actions + if (Utils.IsSql11OrLater(this.Instance.ServerVersion.Major)) + { + if (this.Instance.State == SqlSmoState.Creating) + { + if (this.extender.OwnerForUI == string.Empty) //In order to avoid scripting Authorization part of ddl. + { + this.extender.OwnerForUI = null; + } + this.Instance.Create(); + } + else + { + this.Instance.Alter(); + } + } + + //Members Page Related actions + this.extender.RefreshRoleMembersHash(); + Dictionary memberNameIsMemberHash = this.extender.MemberNameIsMemberHash; + + StringCollection membersToBeDropped = new StringCollection(); + StringCollection membersToBeAdded = new StringCollection(); + + StringCollection membershipsToBeDropped = new StringCollection(); + StringCollection membershipsToBeAdded = new StringCollection(); + + foreach (string memberName in memberNameIsMemberHash.Keys) + { + if (memberNameIsMemberHash[memberName]) //if added as member + { + membersToBeAdded.Add(memberName); + } + else //if dropped from members + { + membersToBeDropped.Add(memberName); + } + } + + //Membership page Related actions + this.extender.RefreshServerRoleNameHasMembershipHash(); + Dictionary membershipInfoHash = this.extender.ServerRoleNameHasMembershipHash; + + foreach (string serverRoleName in membershipInfoHash.Keys) + { + if (membershipInfoHash[serverRoleName]) //If new membership added + { + membershipsToBeAdded.Add(serverRoleName); + } + else //If now not a member of + { + membershipsToBeDropped.Add(serverRoleName); + } + } + + //First dropping members and memberships + foreach (string member in membersToBeDropped) + { + this.serverRole.DropMember(member); + } + + foreach (string containingRole in membershipsToBeDropped) + { + this.serverRole.DropMembershipFromRole(containingRole); + } + + //Now adding members and memberships. + foreach (string member in membersToBeAdded) + { + this.serverRole.AddMember(member); + } + + foreach (string containingRole in membershipsToBeAdded) + { + this.serverRole.AddMembershipToRole(containingRole); + } + } + + // protected System.Collections.Specialized.StringCollection GetScriptStrings(ITaskExecutionContext context) + // { + // StringCollection script = this.GetScriptForCreateOrManage(); + + // StringCollection permissionScript = this.GetPermissionRelatedScripts(); + + // foreach (string str in permissionScript) + // { + // script.Add(str); + // } + + // if (script.Count == 0) + // { + // //When the user tries to script and no changes have been made. + // // throw new SsmsException(SR.NoActionToBeScripted); + // } + // return script; + // } + + private StringCollection GetPermissionRelatedScripts() + { + StringCollection script = new StringCollection(); + + if (this.extender.GeneralPageDataContainer != null) //Permission controls have been initialized. + { + //For General Page permissions + ServerConnection permControlConn = ((CDataContainer)this.extender.GeneralPageDataContainer).ServerConnection; + SqlExecutionModes em = permControlConn.SqlExecutionModes; + + //PermissionUI has a cloned connection. + permControlConn.CapturedSql.Clear(); + permControlConn.SqlExecutionModes = SqlExecutionModes.CaptureSql; + + //This will run General page's permission related actions. + EventHandler tempEventHandler = (EventHandler)this.extender.GeneralPageOnRunNow; + if (tempEventHandler != null) + { + tempEventHandler(this, new EventArgs()); + } + + script = permControlConn.CapturedSql.Text; + permControlConn.SqlExecutionModes = em; + } + + return script; + } + + /// + /// Generates script for Create and Properties. + /// + /// + private StringCollection GetScriptForCreateOrManage() + { + StringCollection script = new StringCollection(); + + Server svr = this.Instance.Parent; + svr.ConnectionContext.CapturedSql.Clear(); + SqlExecutionModes em = svr.ConnectionContext.SqlExecutionModes; + + svr.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql; + + //T-SQL Capturing starts. + + this.CreateOrManage(); + + //T-SQL capturing ends + script = svr.ConnectionContext.CapturedSql.Text; + svr.ConnectionContext.SqlExecutionModes = em; + + return script; + } + + protected string GetServerRoleName() + { + return "ServerRole-" + DateTime.Now.ToString("yyyyMMdd-HHmmss",SmoApplication.DefaultCulture); + } + + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected void Dispose(bool disposing) + { + // if (disposing && (components != null)) + // { + // components.Dispose(); + // } + // base.Dispose(disposing); + } + } + + public class ServerRoleCreateTaskFormComponent : ServerRoleManageTaskFormComponent + { + public ServerRoleCreateTaskFormComponent() + : base() + { + } + + public ServerRoleCreateTaskFormComponent(IContainer container) + : base(container) + { + } + + protected ServerRole CreateSmoObjectInstance() + { +#pragma warning disable IDE0074 // Use compound assignment + if (this.serverRole == null) + { + this.serverRole = new ServerRole(this.Server, GetServerRoleName()); + } +#pragma warning restore IDE0074 // Use compound assignment + return this.serverRole; + } + + protected string MethodName + { + get + { + return "Create"; + } + } + } +}