Skip to content

Commit 8778d40

Browse files
committed
fix: delete CLI-created session settings from registry after disconnect
Refs #2162
1 parent 24f3e36 commit 8778d40

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

source/apphelpers.pas

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,9 +2703,13 @@ procedure ParseCommandLine(CommandLine: String; var ConnectionParams: TConnectio
27032703
ConnectionParams.SSHTimeout := StrToIntDef(SshTimeout, ConnectionParams.SSHTimeout);
27042704
end;
27052705

2706-
// Ensure we have a session name to pass to InitConnection
2707-
if (ConnectionParams.SessionPath = '') and (ConnectionParams.Hostname <> '') then
2708-
ConnectionParams.SessionPath := ConnectionParams.Hostname;
2706+
if ConnectionParams.SessionPath.IsEmpty then begin
2707+
// Ensure we have a (random) session name to pass to InitConnection
2708+
ConnectionParams.SessionPath := IfEmpty(ConnectionParams.Hostname, 'temp')+'-'+GeneratePassword(4);
2709+
end;
2710+
2711+
// Delete stored session in Destroy:
2712+
ConnectionParams.DeleteAfterUse := True;
27092713
end;
27102714

27112715
// Check for valid filename(s) in parameters.

source/dbconnection.pas

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ TSQLFunctionList = class(TObjectList<TSQLFunction>)
310310

311311
TConnectionParameters = class(TObject)
312312
strict private
313+
FDeleteAfterUse: Boolean;
314+
FLoadedFromSettings: Boolean;
313315
FNetType: TNetType;
314316
FHostname, FUsername, FPassword, FAllDatabases, FLibraryOrProvider, FComment, FStartupScriptFilename,
315317
FSessionPath, FSSLPrivateKey, FSSLCertificate, FSSLCACertificate, FSSLCipher, FServerVersion,
@@ -330,7 +332,9 @@ TConnectionParameters = class(TObject)
330332
public
331333
constructor Create; overload;
332334
constructor Create(SessionRegPath: String); overload;
335+
destructor Destroy; override;
333336
procedure SaveToRegistry;
337+
property DeleteAfterUse: Boolean read FDeleteAfterUse write FDeleteAfterUse;
334338
function CreateConnection(AOwner: TComponent): TDBConnection;
335339
function CreateQuery(Connection: TDbConnection): TDBQuery;
336340
function NetTypeName(LongFormat: Boolean): String;
@@ -1281,6 +1285,8 @@ constructor TConnectionParameters.Create;
12811285
begin
12821286
inherited Create;
12831287
FIsFolder := False;
1288+
FDeleteAfterUse := False;
1289+
FLoadedFromSettings := False;
12841290

12851291
FNetType := TNetType(AppSettings.GetDefaultInt(asNetType));
12861292
FHostname := DefaultHost;
@@ -1354,6 +1360,7 @@ constructor TConnectionParameters.Create(SessionRegPath: String);
13541360
);
13551361
FNetType := ntMySQL_TCPIP;
13561362
end;
1363+
FLoadedFromSettings := True;
13571364
FHostname := AppSettings.ReadString(asHost);
13581365
FUsername := AppSettings.ReadString(asUser);
13591366
FPassword := decrypt(AppSettings.ReadString(asPassword));
@@ -1410,6 +1417,16 @@ constructor TConnectionParameters.Create(SessionRegPath: String);
14101417
end;
14111418
end;
14121419

1420+
destructor TConnectionParameters.Destroy;
1421+
begin
1422+
if FDeleteAfterUse and (not FLoadedFromSettings) and (not FSessionPath.IsEmpty) then begin
1423+
if AppSettings.SessionPathExists(FSessionPath) then begin
1424+
AppSettings.SessionPath := FSessionPath;
1425+
AppSettings.DeleteCurrentKey;
1426+
end;
1427+
end;
1428+
end;
1429+
14131430

14141431
procedure TConnectionParameters.SaveToRegistry;
14151432
var
@@ -2130,6 +2147,7 @@ destructor TDBConnection.Destroy;
21302147
FKeepAliveTimer.Free;
21312148
FFavorites.Free;
21322149
FInformationSchemaObjects.Free;
2150+
FParameters.Free;
21332151
inherited;
21342152
end;
21352153

0 commit comments

Comments
 (0)