bpromas
bpromas

Reputation: 694

Datasnap: Session destroy event

In my application, whenever a user logs in, he is added to a table that stores data about the logged users, but I have nothing implemented to take those users off my table. I need to remove that user from my table when he logs out or loses his session in any other way. Is there a "SessionDestroy" event or something like that, that allows me do implement something upon a destroyed session?

Upvotes: 0

Views: 880

Answers (1)

Marco Cantù
Marco Cantù

Reputation: 1195

You can add "session events" (using anonymous methods) to the Session Manager. Since 2009, I guess (but tested on XE). Works for REST sessions and more. Sample code:

 TDSSessionManager.Instance.AddSessionEvent(
procedure (Sender: TObject; const EventType: TDSSessionEventType;
  const session: TDSSession)
begin
  case EventType of
    SessionCreate :
    begin
      session.UserRoles.Add('guest'); // guest role is fixed
      ...
    end;
    SessionClose:
    begin
      FreeAndNil(some_more_data);
    end;
  end;
end);

Upvotes: 2

Related Questions