#1 2018-01-26 15:05:09

Kixemi
Member
Registered: 2017-02-10
Posts: 10

How to get the authenticated user when using interface based approach?

Hello, we are using sessions and INTERFACE based endpoints. We need to know at the server side who the logged user is and this users GroupRights.
We have our own inherited TSQLAuthGroup with custom new fields.

In method based system we know you can get the user on serverside from the Ctxt variable. But with interface based system we dont have this variable at hands.

Now we think we found a solution, that is:
sqlauthuser := CurrentServiceContext.Request.Server.SessionGetUser(CurrentServiceContext.Request.Session);

And then we can get data from the user table for exampe like this:
MyCustomVariable := TSQLRestAuthUser(sqlauthuser).MyCustomField;

Is there a easier, and perhaps faster way to indentfy who is calling the interface endpoint?
We want to reply with user specific data.
Still we have not found a way to link and read from the custom fields of the TSQLMyAuthGroup external MSSQL table.

Still learning this amazing framework wink !

Last edited by Kixemi (2018-01-26 15:10:14)

Offline

#2 2018-01-26 16:43:54

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: How to get the authenticated user when using interface based approach?

Yes, this is the right way to do that, but note that CurrentServiceContext.Request may be nil if the service is executed in-place (i.e. directly from the server side), which may happen in the future of your project.

Thanks for the feedback!

Offline

#3 2018-01-27 09:32:13

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: How to get the authenticated user when using interface based approach?

Since ab has said that:
>>"but note that CurrentServiceContext.Request may be nil if the service is executed in-place (i.e. directly from the server side)",

I guess the more reliable way is to inherit your implementation class from TInjectableObjectRest, which has a Server property auto-assigned?


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2018-01-27 10:33:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: How to get the authenticated user when using interface based approach?

Yes, TInjectableObjectRest is perhaps a cleaner way. wink
As documented:

// - note that in case of direct server side execution of the service, this
  // information won't be filled, so the safest (and slightly faster) access
  // to the TSQLRestServer instance associated with a service is to inherit your
  // implementation class from TInjectableObjectRest, and not use this threadvar

I should RTFM myself. big_smile

Offline

Board footer

Powered by FluxBB