Jump to content

Close old Session - if the same username starts a new Session


erich.wanker

Recommended Posts

Hello,

 

i want to close the old Session - if the same username starts a new Session

Any ideas how i can do this? - one user should not be able to start the Software unlimited in his company - every user should buy a licence ;-)

 

Any Ideas?

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

  • Administrators
var
  ASessionList: TList;
  I : Integer;
  M : TUniMainModule;
  USession : TUniGUISession;
begin
  ASessionList := UniServerModule.SessionManager.Sessions.SessionList.LockList;
  try
    for I := 0 to ASessionList.Count -1 do
    begin
      USession := TUniGUISession(ASessionList[i]);
      M := USession.UniMainModule as TUniMainModule;
      if M.username = 'ThisUserName' then
      begin
        USession.TerminateAfterSecs(0);
        Break;
      end;
    end;
  finally
    UniServerModule.SessionManager.Sessions.SessionList.UnlockList;
  end;
end;

This may help.

Link to comment
Share on other sites

  • 3 weeks later...

i hope - i understand you right:

 

In Main / Script:

function bunload(){
dontleave="Softwarename";
return dontleave;
};
function ounload(){
ajaxRequest(MainForm.window, 'SessionClosed', [] );
};
onbeforeunload = bunload;
onunload=ounload;

 

 

procedure TMainForm.UniFormAjaxEvent(Sender: TComponent; EventName: string;
  Params: TStrings);
begin
if EventName ='SessionClosed' then
begin
UniServerModule.Logger.AddLog('Session Closed..',mainform.aktuell_session+mainform.aktuell_ip+'Softwarename');
uniapplication.terminate;
end;
aktuell_session:=UniApplication.UniSession.SessionID;
aktuell_ip:=UniApplication.RemoteAddress;
aktuell_session:='[sessionID:'+aktuell_session+'] ';
aktuell_ip:='[DeviceIP:'+aktuell_ip+'] ';

 

 

Hope that helps ;-)

 

nice greetings

erich

Link to comment
Share on other sites

...
    if M.username = 'ThisUserName' then
      begin
        USession.TerminateAfterSecs(0);
        Break;
      end;
    end;
  ....

....

This Code is Work fine but  this code Will terminate Other USession without show any message to  "Other USession" user

 

Required to show a Message to the  "Other USession".....

 

Like :

 

if M.username = 'ThisUserName' then

      begin

          ....USession....(ShowMessage(' The System will terminate after 10 Sec').....

          USession.TerminateAfterSecs(10);

 

 

Thanx

       

Link to comment
Share on other sites

I Ahmad,

 

The callback procedure is not the one that shows a message, but the one called as parameter from the procedure that shows a message.

Did you try something like this?

//uniFormConfirm is an application form with an uniLabel ("Do you really want to close your session?") and an uniButton "OK" (with ModalResult set to mrOK).

uniFormConfirm.ShowModal(
  procedure(Sender:TObject;Result:Integer);
  begin
    if Result=mrOK then TerminateMySession;
  end
);

procedure TerminateMySession;
begin
  //do whatever you want to close properly a session
end;
Link to comment
Share on other sites

Dear ZigZig

 

The code that Farshad give  WORK fine  :

 

When  I login with username "XXXX"   the  application search the old Session with the same username "XXXX" and Terminate it

 

I mean the application Terminate the "other Session" not MySession....

 

The system must sent to the "other Session" Message "The System will terminate after.....

 

The Message Must sent to "USession := TUniGUISession(ASessionList)"

 

 

Thanx
 

 

 

var
  ASessionList: TList;
  I : Integer;
  M : TUniMainModule;
  USession : TUniGUISession;
begin
  ASessionList := UniServerModule.SessionManager.Sessions.SessionList.LockList;
  try
    for I := 0 to ASessionList.Count -1 do
    begin
      USession := TUniGUISession(ASessionList[i]);
      M := USession.UniMainModule as TUniMainModule;
      if M.username = 'ThisUserName' then
      begin
        USession.TerminateAfterSecs(0);
        Break;
      end;
    end;
  finally
    UniServerModule.SessionManager.Sessions.SessionList.UnlockList;
  end;
end;

This may help.

 

Link to comment
Share on other sites

Thank to    Oliver Morsch  &   ZigZig

 

 

I want to send message used unigui only .  

 

I solved the problem in a primitive method, but it's work fine :D 

 

If there is a better solution please share

begin USession := TUniGUISession(ASessionList[i]);
  M := USession.UniMainModule as TUniMainModule;
  if M.username = 'ThisUserName' then
  begin
   M.LMessageStr := 'Your Session will Terminate after 10 Sec !!!';////// <---
   USession.TerminateAfterSecs(10); Break;
   end;
end;

// And in the MainForm. a Timer With this code:
procedure TMainFrm.UniTimer1Timer(Sender: TObject);
begin
 if UniMainModule.LMessageStr <> '' then
 begin
  ShowMessage(UniMainModule.LMessageStr);
  UniMainModule.LMessageStr := '';
 end;
end;

  • Upvote 1
Link to comment
Share on other sites

  • 1 year later...

To User XUYB19870303  -  i think "personal conversation" is not the best way to ask ;-)

 

 

I made a automatic logoff for old sessions like this:

 

i use a uniTimer ... i didn´t find a solution without timer :-(

 

 

 

 
    in unimainmodule:
 
    public
    username:String;      
    LMessageStr:String;     
 
 
 
    in main:
 
        procedure TMainForm.UniTimer2Timer(Sender: TObject);
        begin
 
         if UniMainModule.LMessageStr <> '' then
         begin
          ShowMessage(UniMainModule.LMessageStr);
          UniMainModule.LMessageStr := '';
         end;
 
 
     in my login-form:
 
    // Andere Session finden und beenden....
    uniMainModule.username:=  uniedit1.text   // the username
 
        ASessionList := UniServerModule.SessionManager.Sessions.SessionList;
          try
            for I := 0 to ASessionList.Count -1 do
            begin
              USession := TUniGUISession(ASessionList);
              M := USession.UniMainModule as TUniMainModule;
 
              if UniApplication.UniSession.SessionID <> USession.SessionId  then   // Do not close my OWN Session
               begin
 
                    if M.username = uniedit1.text then
                    begin
                      M.LMessageStr := 'Sie haben sich an einem anderen System angemeldet..'+#13+'Sie werden automatisch in 10 Sekunden abgemeldet!';
                      USession.TerminateAfterSecs(10);
                      Break;
                    end;
               end;
            end;
          finally
             // UniServerModule.SessionManager.Sessions.SessionList.UnlockList;
          end;

 

 

 

hth and nice greetings

Erich

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...