📄 jvbdelogindialog.pas
字号:
LoginName := GetUserName;
if Assigned(AppStorage) then
begin
AppStorage.WriteString(AppStorage.ConcatPaths([AppStoragePath, RsLastLoginUserName]), GetUserName);
AppStorage.WriteString(AppStorage.ConcatPaths([AppStoragePath, RsLastAliasName]), Database.AliasName);
end;
end;
end;
function TJvDBLoginDialog.ExecuteDbLogin(LoginParams: TStrings): Boolean;
var
CurrSession: TSession;
begin
Result := False;
if (Database = nil) or not Assigned(LoginParams) then
Exit;
if ShowDBName then
FDialog.AppTitleLabel.Caption := Format(RsDatabaseName, [Database.DatabaseName]);
FDialog.UserNameEdit.Text := LoginParams.Values[szUSERNAME];
CurrSession := Sessions.CurrentSession;
try
Result := FDialog.ShowModal = mrOk;
if Result then
FillParams(LoginParams)
else
SysUtils.Abort;
finally
Sessions.CurrentSession := CurrSession;
end;
end;
function TJvDBLoginDialog.ExecuteUnlock: Boolean;
begin
with FDialog.UserNameEdit do
begin
Text := LoginName;
ReadOnly := True;
Font.Color := clGrayText;
end;
Result := (FDialog.ShowModal = mrOk);
end;
function TJvDBLoginDialog.Execute(LoginParams: TStrings): Boolean;
var
SaveCursor: TCursor;
begin
SaveCursor := Screen.Cursor;
Screen.Cursor := crDefault;
try
if Assigned(FIconDblClick) then
begin
with FDialog.AppIcon do
begin
OnDblClick := OnIconDblClick;
Cursor := crHand;
end;
with FDialog.KeyImage do
begin
OnDblClick := OnIconDblClick;
Cursor := crHand;
end;
end;
FDialog.PasswordEdit.MaxLength := MaxPwdLen;
FDialog.AttemptNumber := AttemptNumber;
case FMode of
dmAppLogin:
Result := ExecuteAppLogin;
dmDBLogin:
Result := ExecuteDbLogin(LoginParams);
dmUnlock:
Result := ExecuteUnlock;
else
Result := False;
end;
if Result then
LoginName := GetUserName;
finally
Screen.Cursor := SaveCursor;
end;
end;
function TJvDBLoginDialog.GetUserName: string;
begin
if CheckDatabaseChange then
Result := Copy(FDialog.UserNameEdit.Text, 1,
Pos('@', FDialog.UserNameEdit.Text) - 1)
else
Result := FDialog.UserNameEdit.Text;
end;
function TJvDBLoginDialog.CheckDatabaseChange: Boolean;
begin
Result := (FMode in [dmAppLogin, dmDBLogin]) and
(Pos('@', FDialog.UserNameEdit.Text) > 0) and
((Database <> nil) and (Database.DriverName <> '') and
(CompareText(Database.DriverName, szCFGDBSTANDARD) <> 0));
end;
procedure TJvDBLoginDialog.FillParams(LoginParams: TStrings);
begin
LoginParams.BeginUpdate;
try
LoginParams.Values[szUSERNAME] := GetUserName;
LoginParams.Values[szPASSWORD] := FDialog.PasswordEdit.Text;
if CheckDatabaseChange then
begin
LoginParams.Values[szSERVERNAME] := Copy(FDialog.UserNameEdit.Text,
Pos('@', FDialog.UserNameEdit.Text) + 1, MaxInt)
end;
finally
LoginParams.EndUpdate;
end;
end;
procedure TJvDBLoginDialog.Login(Database: TDatabase; LoginParams: TStrings);
begin
FillParams(LoginParams);
end;
function TJvDBLoginDialog.GetUserInfo: Boolean;
var
Table: TTable;
begin
if UsersTableName = '' then
Result := CheckUser(nil)
else
begin
Result := False;
// Table := TTable.Create(Database);
Table := TTable.Create(Application); // Polaris (?)
try
try
Table.DatabaseName := Database.DatabaseName;
Table.SessionName := Database.SessionName;
Table.TableName := UsersTableName;
Table.IndexFieldNames := UserNameField;
Table.Open;
if Table.FindKey([GetUserName]) then
begin
Result := CheckUser(Table);
if not Result then
begin
if Assigned(FOnLoginFailure) then
FOnLoginFailure(Self, GetUserName, FDialog.PasswordEdit.Text)
else
raise EDatabaseError.CreateRes(@RsEInvalidUserName);
end;
end
else
begin
if Assigned(FOnLoginFailure) then
FOnLoginFailure(Self, GetUserName, FDialog.PasswordEdit.Text)
else
raise EDatabaseError.CreateRes(@RsEInvalidUserName);
end;
except
Application.HandleException(Self);
end;
finally
Table.Free;
end;
end;
end;
function TJvDBLoginDialog.CheckUser(Table: TTable): Boolean;
begin
if Assigned(FCheckUserEvent) then
Result := FCheckUserEvent(Table, GetUserName, FDialog.PasswordEdit.Text)
else
Result := True;
end;
function TJvDBLoginDialog.CheckUnlock: Boolean;
begin
if Assigned(FCheckUnlock) then
Result := FCheckUnlock(FDialog.PasswordEdit.Text)
else
Result := True;
end;
//=== Utility routines =======================================================
procedure OnLoginDialog(Database: TDatabase; LoginParams: TStrings;
AttemptNumber: Integer; ShowDBName: Boolean);
var
Dlg: TJvDBLoginDialog;
begin
Dlg := TJvDBLoginDialog.Create(dmDBLogin, False);
try
Dlg.Database := Database;
Dlg.ShowDBName := ShowDBName;
Dlg.AttemptNumber := AttemptNumber;
Dlg.Execute(LoginParams);
finally
Dlg.Free;
end;
end;
function UnlockDialogEx(const UserName: string; OnUnlock: TCheckUnlockEvent;
IconDblClick: TNotifyEvent; MaxPwdLen, AttemptNumber: Integer): Boolean;
var
Dlg: TJvDBLoginDialog;
begin
Dlg := TJvDBLoginDialog.Create(dmUnlock, False);
try
Dlg.LoginName := UserName;
Dlg.OnIconDblClick := IconDblClick;
Dlg.OnCheckUnlock := OnUnlock;
Dlg.MaxPwdLen := MaxPwdLen;
Dlg.AttemptNumber := AttemptNumber;
Result := Dlg.Execute(nil);
finally
Dlg.Free;
end;
end;
function UnlockDialog(const UserName: string; OnUnlock: TCheckUnlockEvent;
IconDblClick: TNotifyEvent): Boolean;
begin
Result := UnlockDialogEx(UserName, OnUnlock, IconDblClick, 0, 1);
end;
function LoginDialog(Database: TDatabase; AttemptNumber: Integer;
const UsersTableName, UserNameField: string; MaxPwdLen: Integer;
CheckUserEvent: TCheckUserNameEvent; IconDblClick: TNotifyEvent;
var LoginName: string; AppStorage: TJvCustomAppStorage;
AppStoragePath: string; SelectDatabase: Boolean;
LoginFailure: TJvDBLoginEvent): Boolean;
var
Dlg: TJvDBLoginDialog;
begin
Dlg := TJvDBLoginDialog.Create(dmAppLogin, SelectDatabase);
try
Dlg.LoginName := LoginName;
Dlg.OnIconDblClick := IconDblClick;
Dlg.OnCheckUserEvent := CheckUserEvent;
Dlg.OnLoginFailure := LoginFailure;
Dlg.MaxPwdLen := MaxPwdLen;
Dlg.Database := Database;
Dlg.AttemptNumber := AttemptNumber;
Dlg.UsersTableName := UsersTableName;
Dlg.UserNameField := UserNameField;
Dlg.AppStorage := AppStorage;
Dlg.AppStoragePath := AppStoragePath;
Result := Dlg.Execute(nil);
if Result then
LoginName := Dlg.LoginName;
finally
Dlg.Free;
end;
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -