protected override bool Unbind(ServiceCredentials credentials) { if (credentials == null) { return false; } bool success = true; Logger.Debug(Strings.SqlNodeUnbindServiceDebugMessage, credentials.SerializeToJson()); string name = credentials.Name; string user = credentials.User; ProvisionedService serviceInstance = ProvisionedService.GetService(name); // Remove the binding form the local db var binding = serviceInstance.Bindings.FirstOrDefault(p => p.User == user); serviceInstance.Bindings.Remove(binding); if (!ProvisionedService.Save()) { Logger.Error(Strings.SqlNodeCannotSaveProvisionedServicesErrorMessage, credentials.SerializeToJson()); throw new FileServiceErrorException(FileServiceErrorException.FileServiceLocalDBError); } try { DeleteInstanceUser(user); } catch (Exception ex) { Logger.Error("Unable to delete bound user {1} for instance {0}. Exception: {2}", name, user, ex.ToString()); success = false; } return success; }
protected override bool Unbind(ServiceCredentials credentials) { if (credentials == null) { return false; } Logger.Debug(Strings.SqlNodeUnbindServiceDebugMessage, credentials.SerializeToJson()); string user = credentials.User; string databaseName = credentials.Name; using (SqlConnection databaseConnection = new SqlConnection(this.ConnectionString)) { databaseConnection.Open(); databaseConnection.ChangeDatabase(databaseName); using (SqlCommand cmdUserExists = new SqlCommand(string.Format(CultureInfo.InvariantCulture, "select count(*) from sys.sysusers where name=N'{0}'", user), databaseConnection)) { int userCount = (int)cmdUserExists.ExecuteScalar(); if (userCount != 1) { throw new MSSqlErrorException(MSSqlErrorException.MSSqlCredentialsNotFound, user); } } } this.DeleteDatabaseUser(user); return true; }
/// <summary> /// Binds a shared directory to an app. /// </summary> /// <param name="name">The name of the service.</param> /// <param name="bindOptions">Binding options.</param> /// <param name="credentials">Already existing credentials.</param> /// <returns> /// A new set of credentials used for binding. /// </returns> protected override ServiceCredentials Bind(string name, Dictionary<string, object> bindOptions, ServiceCredentials credentials) { Logger.Debug(Strings.SqlNodeBindServiceDebugMessage, name, JsonConvertibleObject.SerializeToJson(bindOptions)); ProvisionedService service = ProvisionedService.GetService(name); if (service == null) { throw new FileServiceErrorException(FileServiceErrorException.FileServiceConfigNotFound, name); } string user = null; string password = null; if (credentials != null) { user = credentials.User; password = credentials.Password; } else { user = "******" + Credentials.GenerateCredential(); password = "******" + Credentials.GenerateCredential(); } var binding = new ServiceBinding { User = user, Password = password }; Bind(service, binding); service.Bindings.Add(binding); if (!ProvisionedService.Save()) { Logger.Error(Strings.SqlNodeCannotSaveProvisionedServicesErrorMessage, credentials == null ? string.Empty : credentials.SerializeToJson()); throw new FileServiceErrorException(FileServiceErrorException.FileServiceLocalDBError); } ServiceCredentials response = this.GenerateCredential(name, user, password, service.Port.Value); Logger.Debug(Strings.SqlNodeBindResponseDebugMessage, response.SerializeToJson()); this.bindingServed += 1; return response; }