Browse Source

* Add Session.States to track new/expired etc.

git-svn-id: trunk@39531 -
michael 7 years ago
parent
commit
99a83cd392

+ 42 - 0
packages/fcl-web/src/base/httpdefs.pp

@@ -519,13 +519,20 @@ type
 
 
 
 
   { TCustomSession }
   { TCustomSession }
+  TSessionState = (ssNew,ssExpired,ssActive,ssResponseInitialized);
+  TSessionStates = set of TSessionState;
 
 
   TCustomSession = Class(TComponent)
   TCustomSession = Class(TComponent)
   Private
   Private
+    FOnSessionStateChange: TNotifyEvent;
     FSessionCookie: String;
     FSessionCookie: String;
     FSessionCookiePath: String;
     FSessionCookiePath: String;
+    FStates: TSessionStates;
     FTimeOut: Integer;
     FTimeOut: Integer;
+    Procedure SetSessionState(aValue : TSessionStates);
   Protected
   Protected
+    Procedure AddToSessionState(aValue : TSessionState);
+    Procedure RemoveFromSessionState(aValue : TSessionState);
     // Can be overridden to provide custom behaviour.
     // Can be overridden to provide custom behaviour.
     procedure SetSessionCookie(const AValue: String); virtual;
     procedure SetSessionCookie(const AValue: String); virtual;
     procedure SetSessionCookiePath(const AValue: String); virtual;
     procedure SetSessionCookiePath(const AValue: String); virtual;
@@ -556,6 +563,10 @@ type
     Property SessionCookiePath : String Read FSessionCookiePath write SetSessionCookiePath;
     Property SessionCookiePath : String Read FSessionCookiePath write SetSessionCookiePath;
     // Variables, tracked in session.
     // Variables, tracked in session.
     Property Variables[VarName : String] : String Read GetSessionVariable Write SetSessionVariable;
     Property Variables[VarName : String] : String Read GetSessionVariable Write SetSessionVariable;
+    // Session state
+    Property SessionState : TSessionStates Read FStates;
+    // Called when state changes
+    Property OnSessionStateChange : TNotifyEvent Read FOnSessionStateChange Write FOnSessionStateChange;
   end;
   end;
 
 
   TRequestEvent = Procedure (Sender: TObject; ARequest : TRequest) of object;
   TRequestEvent = Procedure (Sender: TObject; ARequest : TRequest) of object;
@@ -2261,6 +2272,36 @@ end;
   TCustomSession
   TCustomSession
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
+procedure TCustomSession.SetSessionState(aValue: TSessionStates);
+
+begin
+  if FStates=aValue then exit;
+  If Assigned(OnSessionStateChange) then
+    OnSessionStateChange(Self);
+  FStates:=AValue;
+end;
+
+procedure TCustomSession.AddToSessionState(aValue: TSessionState);
+
+Var
+  S: TSessionStates;
+
+begin
+  S:=SessionState;
+  Include(S,AValue);
+  SetSessionState(S);
+end;
+
+procedure TCustomSession.RemoveFromSessionState(aValue: TSessionState);
+Var
+  S: TSessionStates;
+
+begin
+  S:=SessionState;
+  Exclude(S,AValue);
+  SetSessionState(S);
+end;
+
 procedure TCustomSession.SetSessionCookie(const AValue: String);
 procedure TCustomSession.SetSessionCookie(const AValue: String);
 begin
 begin
   FSessionCookie:=AValue;
   FSessionCookie:=AValue;
@@ -2286,6 +2327,7 @@ constructor TCustomSession.Create(AOwner: TComponent);
 begin
 begin
   FTimeOut:=DefaultTimeOut;
   FTimeOut:=DefaultTimeOut;
   inherited Create(AOwner);
   inherited Create(AOwner);
+  FStates:=[];
 end;
 end;
 
 
 procedure TCustomSession.InitResponse(AResponse: TResponse);
 procedure TCustomSession.InitResponse(AResponse: TResponse);

+ 7 - 0
packages/fcl-web/src/base/iniwebsession.pp

@@ -354,6 +354,9 @@ begin
     DeleteFile(Finifile.FileName);
     DeleteFile(Finifile.FileName);
     FreeAndNil(FIniFile);
     FreeAndNil(FIniFile);
     end;
     end;
+  RemoveFromSessionState(ssActive);
+  RemoveFromSessionState(ssNew);
+  RemoveFromSessionState(ssExpired);
 end;
 end;
 
 
 procedure TIniWebSession.UpdateResponse(AResponse: TResponse);
 procedure TIniWebSession.UpdateResponse(AResponse: TResponse);
@@ -389,6 +392,7 @@ begin
     FIniFile:=CreateIniFile(FN);
     FIniFile:=CreateIniFile(FN);
     if SF.SessionExpired(FIniFile) then
     if SF.SessionExpired(FIniFile) then
       begin
       begin
+      AddToSessionState(ssExpired);
       // Expire session.
       // Expire session.
       If Assigned(OnExpired) then
       If Assigned(OnExpired) then
         OnExpired(Self);
         OnExpired(Self);
@@ -401,6 +405,7 @@ begin
     end;
     end;
   If (S='') then
   If (S='') then
     begin
     begin
+    AddToSessionState(ssNew);
     If Assigned(OnNewSession) then
     If Assigned(OnNewSession) then
       OnNewSession(Self);
       OnNewSession(Self);
     GetSessionID;
     GetSessionID;
@@ -414,6 +419,7 @@ begin
   FIniFile.WriteDateTime(SSession,KeyLast,Now);
   FIniFile.WriteDateTime(SSession,KeyLast,Now);
   If not FCached then
   If not FCached then
     UpdateIniFile;
     UpdateIniFile;
+  AddToSessionState(ssActive);
 {$ifdef cgidebug}SendMethodExit('TIniWebSession.InitSession');{$endif}
 {$ifdef cgidebug}SendMethodExit('TIniWebSession.InitSession');{$endif}
 end;
 end;
 
 
@@ -442,6 +448,7 @@ begin
     C.Path:=SessionCookiePath;
     C.Path:=SessionCookiePath;
     end;
     end;
 {$ifdef cgidebug}SendMethodExit('TIniWebSession.InitResponse');{$endif}
 {$ifdef cgidebug}SendMethodExit('TIniWebSession.InitResponse');{$endif}
+  AddToSessionState(ssResponseInitialized);
 end;
 end;
 
 
 procedure TIniWebSession.RemoveVariable(VariableName: String);
 procedure TIniWebSession.RemoveVariable(VariableName: String);