2017-05-12 12:48:44 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
|
|
|
// Copyright (c) 2010-2017 Daniele Teti and the DMVCFramework Team
|
|
|
|
//
|
2017-05-18 07:02:07 +02:00
|
|
|
// Contributer on this file: Janidan - https://github.com/janidan
|
|
|
|
//
|
2017-05-12 12:48:44 +02:00
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
|
|
unit MainClientFormU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-05-16 18:34:39 +02:00
|
|
|
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
|
|
|
|
System.Classes, Vcl.Graphics,
|
|
|
|
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.AppEvnts,
|
2020-09-24 21:30:45 +02:00
|
|
|
MVCFramework.RESTClient.Intf, MVCFramework.RESTClient,
|
2017-05-12 12:48:44 +02:00
|
|
|
Vcl.ExtCtrls;
|
|
|
|
|
|
|
|
type
|
|
|
|
TForm7 = class(TForm)
|
|
|
|
GroupBox1: TGroupBox;
|
|
|
|
edtUsername: TEdit;
|
|
|
|
edtPassword: TEdit;
|
|
|
|
btnLogInLogOut: TButton;
|
|
|
|
ApplicationEvents1: TApplicationEvents;
|
|
|
|
Panel1: TPanel;
|
|
|
|
Button1: TButton;
|
|
|
|
Memo1: TMemo;
|
|
|
|
Button2: TButton;
|
|
|
|
Button3: TButton;
|
|
|
|
Label1: TLabel;
|
|
|
|
Label2: TLabel;
|
|
|
|
ListBox1: TListBox;
|
|
|
|
Label3: TLabel;
|
|
|
|
Label4: TLabel;
|
|
|
|
Label5: TLabel;
|
|
|
|
Label6: TLabel;
|
|
|
|
Label7: TLabel;
|
|
|
|
Button4: TButton;
|
2017-05-15 14:58:13 +02:00
|
|
|
Button5: TButton;
|
2017-05-16 07:18:06 +02:00
|
|
|
Button6: TButton;
|
2017-05-16 18:34:39 +02:00
|
|
|
Button7: TButton;
|
2017-05-12 12:48:44 +02:00
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
|
|
|
|
procedure btnLogInLogOutClick(Sender: TObject);
|
|
|
|
procedure Button1Click(Sender: TObject);
|
|
|
|
procedure Button2Click(Sender: TObject);
|
|
|
|
procedure Button3Click(Sender: TObject);
|
|
|
|
procedure ListBox1Click(Sender: TObject);
|
|
|
|
procedure Button4Click(Sender: TObject);
|
2017-05-15 14:58:13 +02:00
|
|
|
procedure Button5Click(Sender: TObject);
|
2017-05-16 07:18:06 +02:00
|
|
|
procedure Button6Click(Sender: TObject);
|
2017-05-16 18:34:39 +02:00
|
|
|
procedure Button7Click(Sender: TObject);
|
2017-05-12 12:48:44 +02:00
|
|
|
private
|
2020-09-24 21:30:45 +02:00
|
|
|
FRESTClient: IMVCRESTClient;
|
2017-05-12 12:48:44 +02:00
|
|
|
FLogoutUrl: string;
|
|
|
|
FLogoutMethod: string;
|
2020-09-24 21:30:45 +02:00
|
|
|
procedure FillMemo(Response: IMVCRESTResponse);
|
2017-05-12 12:48:44 +02:00
|
|
|
{ Private declarations }
|
|
|
|
public
|
|
|
|
{ Public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form7: TForm7;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.JSON,
|
2020-09-03 19:57:00 +02:00
|
|
|
MVCFramework.SystemJSONUtils;
|
2017-05-12 12:48:44 +02:00
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
procedure TForm7.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
|
|
|
|
begin
|
2020-09-03 19:57:00 +02:00
|
|
|
if FRESTClient.SessionID.IsEmpty then
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
|
|
|
btnLogInLogOut.Caption := 'LOGIN';
|
|
|
|
Panel1.Caption := 'Not Logged';
|
|
|
|
edtUsername.Enabled := True;
|
|
|
|
edtPassword.Enabled := True;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
btnLogInLogOut.Caption := 'LOGOUT';
|
2020-09-03 19:57:00 +02:00
|
|
|
Panel1.Caption := 'SessionID = ' + FRESTClient.SessionID;
|
2017-05-12 12:48:44 +02:00
|
|
|
edtUsername.Enabled := False;
|
|
|
|
edtPassword.Enabled := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.btnLogInLogOutClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
lJObj: TJSONObject;
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
|
|
|
if btnLogInLogOut.Caption = 'LOGIN' then
|
|
|
|
begin
|
|
|
|
lJObj := TJSONObject.Create;
|
|
|
|
try
|
|
|
|
lJObj.AddPair('username', edtUsername.Text);
|
|
|
|
lJObj.AddPair('password', edtPassword.Text);
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Post('/system/users/logged', TSystemJSON.JSONValueToString(lJObj, False));
|
|
|
|
if not lRes.Success then
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
ShowMessage(lRes.Content);
|
2017-05-12 12:48:44 +02:00
|
|
|
end;
|
|
|
|
FLogoutUrl := lRes.HeaderValue('X-LOGOUT-URL');
|
|
|
|
FLogoutMethod := lRes.HeaderValue('X-LOGOUT-METHOD');
|
|
|
|
finally
|
|
|
|
lJObj.Free;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Assert(FLogoutMethod = 'DELETE');
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Delete(FLogoutUrl);
|
|
|
|
if not lRes.Success then
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
ShowMessage(lRes.Content);
|
2017-05-12 12:48:44 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.Button1Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/public/action');
|
2017-05-12 12:48:44 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.Button2Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/role1');
|
2017-05-12 12:48:44 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.Button3Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/role2');
|
2017-05-12 12:48:44 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.Button4Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/role1and2');
|
2017-05-12 12:48:44 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
2017-05-15 14:58:13 +02:00
|
|
|
procedure TForm7.Button5Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-15 14:58:13 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/role/admin');
|
2017-05-15 14:58:13 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
2017-05-16 07:18:06 +02:00
|
|
|
procedure TForm7.Button6Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-16 07:18:06 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/authenticatedOnly');
|
2017-05-16 07:18:06 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
2017-05-16 18:34:39 +02:00
|
|
|
procedure TForm7.Button7Click(Sender: TObject);
|
|
|
|
var
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes: IMVCRESTResponse;
|
2017-05-16 18:34:39 +02:00
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
lRes := FRESTClient.Get('/private/role1or2');
|
2017-05-16 18:34:39 +02:00
|
|
|
FillMemo(lRes);
|
|
|
|
end;
|
|
|
|
|
2020-09-24 21:30:45 +02:00
|
|
|
procedure TForm7.FillMemo(Response: IMVCRESTResponse);
|
2017-05-12 12:48:44 +02:00
|
|
|
begin
|
2017-05-16 18:34:39 +02:00
|
|
|
Memo1.Lines.Add(Format('[%s] [%s] %s', [TimeToStr(Time),
|
2020-09-24 21:30:45 +02:00
|
|
|
Response.StatusText, Response.Content]));
|
2017-05-12 12:48:44 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.FormCreate(Sender: TObject);
|
|
|
|
begin
|
2020-09-24 21:30:45 +02:00
|
|
|
FRESTClient := TMVCRESTClient.New.BaseURL('localhost', 8080);
|
2017-05-12 12:48:44 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm7.ListBox1Click(Sender: TObject);
|
|
|
|
var
|
|
|
|
lText: string;
|
|
|
|
lPieces: TArray<string>;
|
|
|
|
begin
|
|
|
|
if ListBox1.ItemIndex > -1 then
|
|
|
|
begin
|
|
|
|
lText := ListBox1.Items[ListBox1.ItemIndex];
|
|
|
|
lPieces := lText.Split([':']);
|
|
|
|
edtUsername.Text := lPieces[0];
|
|
|
|
edtPassword.Text := lPieces[1];
|
|
|
|
ShowMessage('Now you can log in using the login/logout button');
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|