Jump to content

Main Form ist instantiated twice


ThomasVoelker

Recommended Posts

After I authenticated in a login form, a new instance of the mainform seems to be created. Interesting though that the Mainform created before the loginform is shown is displayed topmost. Verified this by casting TObject to Pointer to Cardinal and showing on the form together with a database session token (if available).

I'll work around this by removing the login form, but I want to ask whether this behaviour is intended.

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm,Generics.Collections,
  uniGUIBaseClasses, uniLabel;

const
  cSQLArrayMax=0;
type
  TFieldNamesID = (fnAuthenticated=0,fnSessionToken=1);
  TFieldNamesArray = array[0..1]of String;
  TSQLArray = array[0..cSQLArrayMax]of String;

  TMainForm = class(TUniForm)
    UniLabel1: TUniLabel;
    procedure UniFormShow(Sender: TObject);
  private

    const
      cFieldNames:TFieldNamesArray=(
                                    'Authenticated'
                                    ,'SessionToken'
                                   );

      cSQLIdents:TSQLArray=(
                            'Authenticate'
                           );
      cSQLDefaults:TSQLArray=(
                              'EXEC EXT.[Authenticate-User] @UserName=''%0:s'',@PasswordSHA256Base64=''%1:s'''
                             );

    class var
    FLock:TmultiReadExclusiveWriteSynchronizer;
    FSQLDict:TDictionary<string,string>;

    var
    FSessionToken:String;

    class Constructor Create;
    class Destructor Destroy;

    class function getSQL(Ident:String):String;
    class function getFieldName(Ident:TFieldNamesID):String;
  public
    { Public declarations }
    function Authenticate(UserName,Password:String):Boolean;
  end;

function MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication,DB
  ,DebugHelper
  ,DatabaseHelper
  ,ResourceHelper
  ,ConfigFile
  ;

function MainForm: TMainForm;
begin
  Result := TMainForm(UniMainModule.GetFormInstance(TMainForm));
end;

{ TMainForm }

function TMainForm.Authenticate(UserName, Password: String): Boolean;
var sPWBase64:String;
    aPWSha256:TBytes;
    sSQL:String;
    ds:TDataset;
    fST:TField;
    iCallID:UInt64;
begin
  iCallID:=Log.MethodEnter(Self.ClassType,'Authenticate',[UserName,Password]);
  aPWSha256:=Resources.SHA256(Password);
  sPWBAse64:=Resources.EncodeBase64(@aPWSha256[0],32);
  sSQL:=GetSQL('Authenticate');
  try
    try
      sSQL:=Format(sSQL,[UserName,sPWBAse64]);
      ds:=DBHElper.MakeDataset(sSQL,'',Self);
      ds.Active:=True;
      Log.Event(iCallID,88);
      fST:=ds.FindField(getFieldName(fnSessionToken));
      if not fST.IsNull then Begin
        FSessionToken:=fST.AsString;
        Result:=True;
      End Else Begin
        FSessionToken:='';
        Result:=False;
      End;
    finally
      FreeAndNil(ds);
    end;
  except
    on e:exception do begin
      Log.Error(iCallID,e);
      Result:=False;
    end;
  end;
  Log.MethodExit(iCallID);
end;


class Constructor TMainForm.Create;
var sKey,sVal:String;
    i:LongInt;
    iCallID:UInt64;
Begin
  Log.MethodEnter(TMainForm,'Create(class)',[]);
  FLock:=TMultiReadExclusiveWriteSynchronizer.Create;
  FLock.BeginWrite;
  try
    FSQLDict:=TDictionary<string,string>.Create;
    for i := 0 to cSQLArrayMax do BEgin
      sKey:=cSQLIDents[i];
      sVal:=cSQLDefaults[i];
      sVal:=Config.GetValue('BASESQL',sKEy,sVal);
      FSQLDict.Add(UpperCase(sKey),sVal);
    End;
  finally
    FLock.EndWrite;
  end;
  Log.MethodExit(iCallID);
End;

class destructor TMainForm.Destroy;
var iCallID:UInt64;
begin
  Log.MethodEnter(TMainForm,'Destroy(class)',[]);
  FreeAndNil(FLock);
  FreeAndNil(FSQLDict);
  Log.MethodExit(iCallID);
end;

class function TMainForm.getFieldName(Ident: TFieldNamesID): String;
begin
  Result:=cFieldNames[Ord(Ident)];
end;

class function TMainForm.GetSQL(Ident: string):String;
var iCallID:UInt64;
begin
  iCallID:=Log.MethodEnter(TMainForm,'GetSQL',[Ident]);
  FLock.BeginRead;
  if not FSQLDict.TryGetValue(UpperCase(Ident),Result) then Result:='';
  FLock.EndRead;
  Log.MethodExit(iCallID);
end;

procedure TMainForm.UniFormShow(Sender: TObject);
begin
  UniLabel1.Caption:=IntToStr(Cardinal(Pointer(Self)))+#13#10+FSessionToken;
end;

initialization
  RegisterAppFormClass(TMainForm);

end.
unit dLogin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniFieldSet, uniPanel, uniButton,
  uniLabel, uniGUIBaseClasses, uniEdit;

type
  TfrmLogin = class(TUniLoginForm)
    edtUserName: TUniEdit;
    edtPassword: TUniEdit;
    btnLogin: TUniButton;
    btnCancel: TUniButton;
    UniFieldSet1: TUniFieldSet;
    UniContainerPanel1: TUniContainerPanel;
    UniFieldContainer1: TUniFieldContainer;
    procedure btnLoginClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function frmLogin: TfrmLogin;

implementation

{$R *.dfm}

uses
  uniGUIVars, MainModule, uniGUIApplication,Main;

function frmLogin: TfrmLogin;
begin
  Result := TfrmLogin(UniMainModule.GetFormInstance(TfrmLogin));
end;

procedure TfrmLogin.btnLoginClick(Sender: TObject);
begin
  if MainForm.Authenticate(edtUserName.Text,edtPassword.Text) then ModalResult:=mrOK
  Else ShowMessage('Ungültige Anmeldung');
end;

initialization
  RegisterAppFormClass(TfrmLogin);

end.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...