mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-17 17:23:48 -05:00
simplify drop object request handler (#1953)
* simplify drop object request handler * fix test cases * fix issues * update strings * fix error * fix error
This commit is contained in:
@@ -32,29 +32,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Security.Contracts
|
||||
RequestType<CreateLoginParams, object>.Create("objectManagement/createLogin");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete Login params
|
||||
/// </summary>
|
||||
public class DeleteLoginParams
|
||||
{
|
||||
public string ConnectionUri { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete Login request type
|
||||
/// </summary>
|
||||
public class DeleteLoginRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Request definition
|
||||
/// </summary>
|
||||
public static readonly
|
||||
RequestType<DeleteLoginParams, object> Type =
|
||||
RequestType<DeleteLoginParams, object>.Create("objectManagement/deleteLogin");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Login params
|
||||
/// </summary>
|
||||
|
||||
@@ -89,31 +89,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Security.Contracts
|
||||
RequestType<UpdateUserParams, ResultStatus>.Create("objectManagement/updateUser");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete User params
|
||||
/// </summary>
|
||||
public class DeleteUserParams
|
||||
{
|
||||
public string? ConnectionUri { get; set; }
|
||||
|
||||
public string? Database { get; set; }
|
||||
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delete User request type
|
||||
/// </summary>
|
||||
public class DeleteUserRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Request definition
|
||||
/// </summary>
|
||||
public static readonly
|
||||
RequestType<DeleteUserParams, ResultStatus> Type =
|
||||
RequestType<DeleteUserParams, ResultStatus>.Create("objectManagement/deleteUser");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update User params
|
||||
/// </summary>
|
||||
|
||||
@@ -88,13 +88,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
// Credential request handlers
|
||||
this.ServiceHost.SetRequestHandler(CreateCredentialRequest.Type, HandleCreateCredentialRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(UpdateCredentialRequest.Type, HandleUpdateCredentialRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(DeleteCredentialRequest.Type, HandleDeleteCredentialRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(GetCredentialsRequest.Type, HandleGetCredentialsRequest, true);
|
||||
|
||||
// Login request handlers
|
||||
this.ServiceHost.SetRequestHandler(CreateLoginRequest.Type, HandleCreateLoginRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(UpdateLoginRequest.Type, HandleUpdateLoginRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(DeleteLoginRequest.Type, HandleDeleteLoginRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(InitializeLoginViewRequest.Type, HandleInitializeLoginViewRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(DisposeLoginViewRequest.Type, HandleDisposeLoginViewRequest, true);
|
||||
|
||||
@@ -102,12 +100,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
this.ServiceHost.SetRequestHandler(InitializeUserViewRequest.Type, this.userServiceHandler.HandleInitializeUserViewRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(CreateUserRequest.Type, this.userServiceHandler.HandleCreateUserRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(UpdateUserRequest.Type, this.userServiceHandler.HandleUpdateUserRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(DeleteUserRequest.Type, this.userServiceHandler.HandleDeleteUserRequest, true);
|
||||
this.ServiceHost.SetRequestHandler(DisposeUserViewRequest.Type, this.userServiceHandler.HandleDisposeUserViewRequest, true);
|
||||
}
|
||||
|
||||
|
||||
#region "Login Handlers"
|
||||
#region "Login Handlers"
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to create a login
|
||||
@@ -161,31 +158,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
await requestContext.SendResult(new object());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to delete a credential
|
||||
/// </summary>
|
||||
internal async Task HandleDeleteLoginRequest(DeleteLoginParams parameters, RequestContext<object> requestContext)
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.ConnectionUri, out connInfo);
|
||||
if (connInfo == null)
|
||||
{
|
||||
throw new ArgumentException("Invalid ConnectionUri");
|
||||
}
|
||||
|
||||
CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
|
||||
Login login = dataContainer.Server?.Logins[parameters.Name];
|
||||
|
||||
dataContainer.SqlDialogSubject = login;
|
||||
DatabaseUtils.DoDropObject(dataContainer);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
internal async Task HandleUpdateLoginRequest(UpdateLoginParams parameters, RequestContext<object> requestContext)
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
@@ -418,24 +390,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to delete a credential
|
||||
/// </summary>
|
||||
internal async Task HandleDeleteCredentialRequest(DeleteCredentialParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
var result = await ConfigureCredential(parameters.OwnerUri,
|
||||
parameters.Credential,
|
||||
ConfigAction.Drop,
|
||||
RunType.RunNow);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = result.Item1,
|
||||
ErrorMessage = result.Item2
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to get all credentials
|
||||
/// </summary>
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
private ConnectionService? connectionService;
|
||||
|
||||
private Dictionary<string, UserViewState> contextIdToViewState = new Dictionary<string, UserViewState>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Internal for testing purposes only
|
||||
/// </summary>
|
||||
@@ -122,8 +122,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
}
|
||||
|
||||
UserPrototypeFactory userPrototypeFactory = UserPrototypeFactory.GetInstance(dataContainer, userInfo, originalData: null);
|
||||
UserPrototype currentUserPrototype = userPrototypeFactory.GetUserPrototype(ExhaustiveUserTypes.LoginMappedUser);
|
||||
|
||||
UserPrototype currentUserPrototype = userPrototypeFactory.GetUserPrototype(ExhaustiveUserTypes.LoginMappedUser);
|
||||
|
||||
IUserPrototypeWithDefaultLanguage defaultLanguagePrototype = currentUserPrototype as IUserPrototypeWithDefaultLanguage;
|
||||
string? defaultLanguageAlias = null;
|
||||
if (defaultLanguagePrototype != null && defaultLanguagePrototype.IsDefaultLanguageSupported)
|
||||
@@ -209,7 +209,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
};
|
||||
|
||||
this.contextIdToViewState.Add(
|
||||
parameters.ContextId,
|
||||
parameters.ContextId,
|
||||
new UserViewState(parameters.Database, currentUserPrototype.CurrentState));
|
||||
|
||||
await requestContext.SendResult(userViewInfo);
|
||||
@@ -282,42 +282,10 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to delete a user
|
||||
/// </summary>
|
||||
internal async Task HandleDeleteUserRequest(DeleteUserParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
ConnectionInfo connInfo;
|
||||
ConnectionServiceInstance.TryFindConnection(parameters.ConnectionUri, out connInfo);
|
||||
if (connInfo == null)
|
||||
{
|
||||
throw new ArgumentException("Invalid ConnectionUri");
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(parameters.Name) || string.IsNullOrWhiteSpace(parameters.Database))
|
||||
{
|
||||
throw new ArgumentException("Invalid null parameter");
|
||||
}
|
||||
|
||||
CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
|
||||
string dbUrn = "Server/Database[@Name='" + Urn.EscapeString(parameters.Database) + "']";
|
||||
Database? parent = dataContainer.Server.GetSmoObject(new Urn(dbUrn)) as Database;
|
||||
User user = parent.Users[parameters.Name];
|
||||
dataContainer.SqlDialogSubject = user;
|
||||
|
||||
CheckForSchemaOwnerships(parent, user);
|
||||
DatabaseUtils.DoDropObject(dataContainer);
|
||||
|
||||
await requestContext.SendResult(new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = string.Empty
|
||||
});
|
||||
}
|
||||
|
||||
internal async Task HandleDisposeUserViewRequest(DisposeUserViewRequestParams parameters, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
this.ConnectionServiceInstance.Disconnect(new DisconnectParams(){
|
||||
this.ConnectionServiceInstance.Disconnect(new DisconnectParams()
|
||||
{
|
||||
OwnerUri = parameters.ContextId,
|
||||
Type = null
|
||||
});
|
||||
@@ -335,8 +303,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
}
|
||||
|
||||
internal CDataContainer CreateUserDataContainer(
|
||||
ConnectionInfo connInfo,
|
||||
UserInfo? user,
|
||||
ConnectionInfo connInfo,
|
||||
UserInfo? user,
|
||||
ConfigAction configAction,
|
||||
string databaseName)
|
||||
{
|
||||
@@ -344,7 +312,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
var connectionInfoWithConnection = new SqlConnectionInfoWithConnection();
|
||||
connectionInfoWithConnection.ServerConnection = serverConnection;
|
||||
|
||||
string urn = (configAction == ConfigAction.Update && user != null)
|
||||
string urn = (configAction == ConfigAction.Update && user != null)
|
||||
? string.Format(System.Globalization.CultureInfo.InvariantCulture,
|
||||
"Server/Database[@Name='{0}']/User[@Name='{1}']",
|
||||
Urn.EscapeString(databaseName),
|
||||
@@ -361,7 +329,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
containerXml.AddProperty("itemtype", "User");
|
||||
}
|
||||
|
||||
XmlDocument xmlDoc = containerXml.GenerateXmlDocument();
|
||||
XmlDocument xmlDoc = containerXml.GenerateXmlDocument();
|
||||
return CDataContainer.CreateDataContainer(connectionInfoWithConnection, xmlDoc);
|
||||
}
|
||||
|
||||
@@ -393,30 +361,18 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
|
||||
return new Tuple<bool, string>(true, string.Empty);
|
||||
}
|
||||
|
||||
private void CheckForSchemaOwnerships(Database parentDb, User existingUser)
|
||||
{
|
||||
foreach (Schema sch in parentDb.Schemas)
|
||||
{
|
||||
var comparer = parentDb.GetStringComparer();
|
||||
if (comparer.Compare(sch.Owner, existingUser.Name) == 0)
|
||||
{
|
||||
throw new ApplicationException("Cannot drop user since it owns a schema");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class UserActions : ManagementActionBase
|
||||
{
|
||||
#region Variables
|
||||
#region Variables
|
||||
//private UserPrototypeData userData;
|
||||
private UserPrototype userPrototype;
|
||||
private UserInfo? user;
|
||||
private ConfigAction configAction;
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Constructors / Dispose
|
||||
#region Constructors / Dispose
|
||||
/// <summary>
|
||||
/// required when loading from Object Explorer context
|
||||
/// </summary>
|
||||
@@ -442,7 +398,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
// base.Dispose(disposing);
|
||||
// }
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// called on background thread by the framework to execute the action
|
||||
@@ -462,7 +418,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
this.userPrototype.ApplyChanges();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private UserPrototype InitUserPrototype(CDataContainer dataContainer, UserInfo user, UserPrototypeData? originalData)
|
||||
{
|
||||
ExhaustiveUserTypes currentUserType;
|
||||
@@ -485,8 +441,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
dataContainer.Server.GetSmoObject(dataContainer.ObjectUrn) as User);
|
||||
}
|
||||
|
||||
UserPrototype currentUserPrototype = userPrototypeFactory.GetUserPrototype(currentUserType);
|
||||
return currentUserPrototype;
|
||||
UserPrototype currentUserPrototype = userPrototypeFactory.GetUserPrototype(currentUserType);
|
||||
return currentUserPrototype;
|
||||
}
|
||||
|
||||
private ExhaustiveUserTypes GetCurrentUserTypeForExistingUser(User? user)
|
||||
@@ -503,25 +459,20 @@ namespace Microsoft.SqlTools.ServiceLayer.Security
|
||||
{
|
||||
if (user.AuthenticationType == AuthenticationType.Windows)
|
||||
{
|
||||
return ExhaustiveUserTypes.WindowsUser;
|
||||
return ExhaustiveUserTypes.WindowsUser;
|
||||
}
|
||||
else if (user.AuthenticationType == AuthenticationType.Database)
|
||||
{
|
||||
return ExhaustiveUserTypes.SqlUserWithPassword;
|
||||
}
|
||||
}
|
||||
|
||||
return ExhaustiveUserTypes.LoginMappedUser;
|
||||
|
||||
case UserType.NoLogin:
|
||||
return ExhaustiveUserTypes.SqlUserWithoutLogin;
|
||||
|
||||
case UserType.Certificate:
|
||||
return ExhaustiveUserTypes.CertificateMappedUser;
|
||||
|
||||
case UserType.AsymmetricKey:
|
||||
return ExhaustiveUserTypes.AsymmetricKeyMappedUser;
|
||||
|
||||
default:
|
||||
return ExhaustiveUserTypes.Unknown;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user